当前位置:网站首页>2. C language matrix multiplication
2. C language matrix multiplication
2022-07-06 13:26:00 【It's Wang Jiujiu】
Catalog
Multiply left and multiply right
2.C Programming matrix multiplication function with language
Write function ( Array form and pointer form )
1. principle
Multiply left and multiply right
In linear algebra , Matrix left multiplication is different from matrix right multiplication .
for example : The existing matrix A: , matrix B:
.
AB For matrix B Left multiplication matrix A:
BA For matrix B Right multiply matrix A:
It is found by calculation that : The result of left multiplication and right multiplication is completely different , So in matrix multiplication , It is necessary to distinguish between left and right multiplication .
Multiplication principle
The conditions that matrix multiplication needs to meet : The number of columns of the matrix on the left = The number of rows of the matrix on the right .
Rows and columns of the result matrix : Row number = The number of rows of the matrix on the left , Number of columns = The number of columns in the right matrix .
Matrix multiplication process :
With AB For example :
- A The order of the matrix One The elements in the row and B Matrix No One Elements in columns Corresponding Multiply again Add up , The results are placed in the first row and the first column ;
- A The order of the matrix One The elements in the row and B Matrix No Two Elements in columns Corresponding Multiply again Add up , The results are placed in the first row and the second column ;
- A The order of the matrix One The elements in the row and B Matrix No 3、 ... and Elements in columns Corresponding Multiply again Add up , The results are placed in the first row and the third column ;
- ......
- A The order of the matrix 3、 ... and The elements in the row and B Matrix No 3、 ... and Elements in columns Corresponding Multiply again Add up , The results are placed in the third row and the third column ;
The final result is :
2.C Programming matrix multiplication function with language
Write function ( Array form and pointer form )
Although matrix can be divided into left multiplication and right multiplication , But just change the position of the incoming parameter , You can realize left multiplication and right multiplication .
The core idea :
- Use a two-dimensional array to store the numbers in the matrix , use define Define row and column values , Facilitate code changes .
- Define a matrix left multiplication function Matrix_left_mul, Pass the matrix and its row and column length to the function .
- utilize 3 individual for Loop traversal completes the calculation .
Parameters can be passed in the form of an array , It can also be passed in the form of a pointer , Both are essentially the same . Both methods will be introduced .
1、 Let's create two arrays arr1 and arr2 To store two matrices
( When creating a two-dimensional array , You can't omit Columns , You can omit lines ; But in matrix multiplication , Both row and column information should be )
// Rows and columns of the left matrix
#define COL1 4
#define ROW1 3
// Rows and columns of the right matrix
#define ROW2 4
#define COL2 3
double arr1[ROW1][COL1] = { 1,2,3,4,5,6,7,8,9,10,11,12};
double arr2[ROW2][COL2] = { 12,11,10,9,8,7,6,5,4,3,2,1};
2、 Pass parameters and judge whether the condition of matrix matching is true
utilize assert Assert whether the condition holds , If not, the system will prompt an error ,assert Need to include header file <assert.h>.
Because the array parameter passes the address , So the type of the function is set to void that will do .
If there is no judgment , After passing in the wrong parameter , The access of two-dimensional array will overflow , Returns a random number .
Array form :
void Matrix_left_mul(double arr1[][COL1], double arr2[][COL2], double arr3[][COL2],int row1,int row2,int col1,int col2)
{
assert(col1 == row2);// Determine whether the left column is equal to the right row , Need to include header file
}
Pointer form :
void Matrix_left_mul(double(*arr1)[COL1], double (*arr2)[COL2], double(*arr3)[COL2], int row1, int row2, int col1, int col2)
{
assert(col1 == row2);// Determine whether the left column is equal to the right row ,assert Need to include header file
}
3、for Loop traversal calculation
Create three for loop :
- first for loop (i): Circulant left matrix i That's ok
- the second for loop (j): Circular right matrix j Column
- Third for loop (k): Cycle left rows one by one k Elements × Right column k Elements , Add up the results
Array form :
int i = 0;// That's ok
int j = 0;// Column
int k = 0;// In the ranks , The first k Multiply elements
for (i = 0; i < row1; i++)// From i OK, let's start
{
for (j = 0; j < col2; j++)// From j Column start
{
for (k = 0; k < col1; k++)//i Line elements and j Multiply column elements , The results add up
{
arr3[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
Pointer form :
void Matrix_left_mul(double(*arr1)[COL1], double (*arr2)[COL2], double(*arr3)[COL2], int row1, int row2, int col1, int col2)
{
assert(col1 == row2);// Determine whether the left column is equal to the right row ,assert Need to include header file
int i = 0;// That's ok
int j = 0;// Column
int k = 0;// In the ranks , The first k Multiply elements
for (i = 0; i < row1; i++)// From i OK, let's start
{
for (j = 0; j < col2; j++)// From j Column start
{
for (k = 0; k < col1; k++)//i Line elements and j Multiply column elements , The results add up
{
*(* (arr3 + i)+j) += *(*(arr1 + i) + k) * *(*(arr2 +k) + j);
}
}
}
}
use define The advantage of defining constants is flexibility , Easy to use , Subsequently, you only need to change the relevant parameters of the input matrix to perform the operation .
test
stay VS in ,F10 Enter debugging mode , monitor arr1,arr2,arr3 Array . For the convenience of monitoring , We temporarily change the array to integer , The parameters in the array are integers .
The result is absolutely right , If you don't trust, you can also test several groups of data .
3. Complete code
Array form :
#include<stdio.h>
#include<assert.h>
// Rows and columns of the left matrix
#define COL1 4
#define ROW1 3
// Rows and columns of the right matrix
#define ROW2 4
#define COL2 3
void Matrix_left_mul(double arr1[][COL1], double arr2[][COL2], double arr3[][COL2],int row1,int row2,int col1,int col2)
{
assert(col1 == row2);// Determine whether the left column is equal to the right row ,assert Need to include header file
int i = 0;// That's ok
int j = 0;// Column
int k = 0;// In the ranks , The first k Multiply elements
for (i = 0; i < row1; i++)// From i OK, let's start
{
for (j = 0; j < col2; j++)// From j Column start
{
for (k = 0; k < col1; k++)//i Line elements and j Multiply column elements , The results add up
{
arr3[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
}
int main()
{
double arr1[ROW1][COL1] = { 1,2,3,4,5,6,7,8,9,10,11,12};
double arr2[ROW2][COL2] = { 12,11,10,9,8,7,6,5,4,3,2,1 };
double arr3[ROW1][COL2]={0};// The row of the resulting matrix is equal to the row of the left matrix , The column is equal to the column of the right matrix
Matrix_left_mul(arr1, arr2, arr3,ROW1,ROW2,COL1,COL2);
return 0;
}
Pointer form
#include<stdio.h>
#include<assert.h>
// Rows and columns of the left matrix
#define COL1 4
#define ROW1 3
// Rows and columns of the right matrix
#define ROW2 4
#define COL2 3
void Matrix_left_mul(double(*arr1)[COL1], double (*arr2)[COL2], double(*arr3)[COL2], int row1, int row2, int col1, int col2)
{
assert(col1 == row2);// Determine whether the left column is equal to the right row ,assert Need to include header file
int i = 0;// That's ok
int j = 0;// Column
int k = 0;// In the ranks , The first k Multiply elements
for (i = 0; i < row1; i++)// From i OK, let's start
{
for (j = 0; j < col2; j++)// From j Column start
{
for (k = 0; k < col1; k++)//i Line elements and j Multiply column elements , The results add up
{
*(* (arr3 + i)+j) += *(*(arr1 + i) + k) * *(*(arr2 +k) + j);
}
}
}
}
int main()
{
double arr1[ROW1][COL1] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
double arr2[ROW2][COL2] = { 12,11,10,9,8,7,6,5,4,3,2,1 };
double arr3[ROW1][COL2] = { 0 };// The row of the resulting matrix is equal to the row of the left matrix , The column is equal to the column of the right matrix
Matrix_left_mul(arr1, arr2, arr3, ROW1, ROW2, COL1, COL2);
return 0;
}
边栏推荐
- Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
- View UI plus releases version 1.1.0, supports SSR, supports nuxt, and adds TS declaration files
- Answer to "software testing" exercise: Chapter 1
- Record: solution of 404 error of servlet accessing database in dynamic web project
- 162. Find peak - binary search
- TYUT太原理工大学2022数据库题库选择题总结
- Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
- 国企秋招经验总结
- View UI plus released version 1.3.1 to enhance the experience of typescript
- 4.30动态内存分配笔记
猜你喜欢
How do architects draw system architecture blueprints?
Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
Design a key value cache to save the results of the most recent Web server queries
TYUT太原理工大学2022数据库之关系代数小题
Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
Cloud native trend in 2022
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology
System design learning (III) design Amazon's sales rank by category feature
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
随机推荐
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
阿里云微服务(四) Service Mesh综述以及实例Istio
Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
Inheritance and polymorphism (Part 2)
Abstract classes and interfaces
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
2.初识C语言(2)
Small exercise of library management system
[Topic terminator]
TYUT太原理工大学2022软工导论考试题型大纲
Smart classroom solution and mobile teaching concept description
3.输入和输出函数(printf、scanf、getchar和putchar)
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
8.C语言——位操作符与位移操作符
[中国近代史] 第六章测验
Rich Shenzhen people and renting Shenzhen people
vector
凡人修仙学指针-2