当前位置:网站首页>2. C language matrix multiplication

2. C language matrix multiplication

2022-07-06 13:26:00 It's Wang Jiujiu

Catalog

1. principle

Multiply left and multiply right

Multiplication principle

2.C Programming matrix multiplication function with language

Write function ( Array form and pointer form )

test

3. Complete code


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: \begin{bmatrix} 1 1 \\ 0 1 \end{bmatrix}, matrix B:\begin{bmatrix} 0 1 \\ 1 0 \end{bmatrix}.

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;
}

原网站

版权声明
本文为[It's Wang Jiujiu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060916176490.html