当前位置:网站首页>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;
}
边栏推荐
- 8.C语言——位操作符与位移操作符
- Introduction and use of redis
- 系统设计学习(一)Design Pastebin.com (or Bit.ly)
- (super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
- 13 power map
- JS interview questions (I)
- 2-year experience summary, tell you how to do a good job in project management
- 更改VS主题及设置背景图片
- 学编程的八大电脑操作,总有一款你不会
- 系统设计学习(三)Design Amazon‘s sales rank by category feature
猜你喜欢
学编程的八大电脑操作,总有一款你不会
5.MSDN的下载和使用
Introduction and use of redis
3.C语言用代数余子式计算行列式
抽象类和接口
arduino+水位传感器+led显示+蜂鸣器报警
Interview Essentials: talk about the various implementations of distributed locks!
5.函数递归练习
(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
Design a key value cache to save the results of the most recent Web server queries
随机推荐
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
Common method signatures and meanings of Iterable, collection and list
System design learning (I) design pastebin com (or Bit.ly)
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
MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
TYUT太原理工大学2022软工导论简答题
Experience summary of autumn recruitment of state-owned enterprises
2年经验总结,告诉你如何做好项目管理
Cloud native trend in 2022
arduino+水位传感器+led显示+蜂鸣器报警
阿里云一面:并发场景下的底层细节 - 伪共享问题
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
Solution: warning:tensorflow:gradients do not exist for variables ['deny_1/kernel:0', 'deny_1/bias:0',
MPLS experiment
8.C语言——位操作符与位移操作符
系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
Inheritance and polymorphism (Part 2)
十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
FileInputStream和BufferedInputStream的比较