When I saw a video of the linear algebra open course at MIT, I learned several ways to multiply matrices:
# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
# Define M 2
# Define N 3
# Define P 4
/*
* A * B = C
* (M, n) * (n, p) = (m, p)
*/
Float a [m] [N] = {5, 2, 4 },
{6, 3, 9 }};
Float B [N] [p] = {7, 8, 9, 10 },
{1, 4, 22,171 },
{13, 14, 2, 21 }};
Float C [m] [p];
Void clear_c ()
{// Used to clear the C array,
// When multiple multiplication methods are used at the same time, some methods need to clear the C array; otherwise, the calculation will be repeated.
Int I, J;
For (I = 0; I <m; I ++)
For (j = 0; j <p; j ++)
C [I] [J] = 0;
}
Void print_matrix ()
{// This function prints only the C matrix
Int I, J;
For (I = 0; I <m; I ++)
{
For (j = 0; j <p; j ++)
{
Printf ("%. 3g \ t", C [I] [J]);
}
Printf ("\ n ");
}
}
Void mul_1 ()
{// General Matrix Product
Printf ("\ N1. general method of matrix multiplication: \ n ");
Int I, J, K;
Float c_key, c_sumkey;
// Pay attention to the order of three-layer loops.
For (I = 0; I <m; I ++ ){
For (j = 0; j <p; j ++ ){
C_sumkey = 0; // calculate the value of the next element after clearing
For (k = 0; k <n; k ++ ){
C_key = A [I] [k] * B [k] [J];
C_sumkey + = c_key;
Printf ("A [% d, % d] x B [% d, % d] = %. 3G x %. 3G = %. 3G \ n ", I, K, K, J, a [I] [K], B [k] [J], c_key );
}
C [I] [J] = c_sumkey;
Printf ("add = C [% d, % d] = %. 3g \ n", I, j, c_sumkey );
}
}
}
Float get_key_val (int I, Int J)
{// Evaluate the inner product of a vertex in the result matrix
Int N;
Float sumkeyval = 0;
Printf ("C [% d, % d] =", I, j );
For (n = 0; n <n; n ++ ){
If (n! = 0) printf ("+ ");
// Printf ("A [% d, % d] * B [% d, % d]", I, N, N, J );
Printf ("(%. 3g * %. 3g)", a [I] [N], B [N] [J]);
Sumkeyval + = A [I] [N] * B [N] [J];
}
Printf ("= %. 3g \ n", sumkeyval );
Return sumkeyval;
}
Void mul_2 ()
{// Calculate the product of the matrix based on Definitions
Printf ("\ N2. calculate directly as defined: \ n ");
Int I, J;
For (I = 0; I <m; I ++ ){
For (j = 0; j <p; j ++ ){
C [I] [J] = get_key_val (I, j );
}
Printf ("\ n ");
}
}
Void show_multi_proc (float X, Int J)
{// This function only shows a more detailed multiplication process.
Int K;
If (J! = 0) {printf ("+");} else {printf ("= ");}
Printf ("[");
For (k = 0; k <p; k ++)
{
Printf ("%. 3g", x * B [J] [k]);
}
Printf ("]");
}
Void splite_matrix_row (float X, int I, Int J)
{// Divide matrix B into n one-dimensional arrays with a length of P.
// Multiply each value in the array with a vector.
// Then add the matrix, that is, add the corresponding positions of multiple one-dimensional arrays. Write the value to the C matrix.
Int K;
If (J! = 0) {printf ("\ n + ");}
Printf ("%. 3g * [", X );
For (k = 0; k <p; k ++)
{
Printf ("%. 3g", B [J] [k]);
C [I] [k] + = x * B [J] [k]; // multiply a vector by a matrix
}
Printf ("]");
}
Void mul_3 ()
{// Coefficient-Vector Method
// Only the row vector method is used here. The column vector is also feasible.
// Or divide matrix A into multiple row (column) vectors, and then divide matrix B into coefficients.
Printf ("\ N3. coefficient-vector method: \ n ");
Int I, J;
Float Xs; // Coefficient
For (I = 0; I <m; I ++ ){
For (j = 0; j <n; j ++)
{
Xs = A [I] [J];
Splite_matrix_row (XS, I, j );
}
For (j = 0; j <n; j ++)
{
Xs = A [I] [J];
Show_multi_proc (XS, J );
}
Printf ("\ n ");
}
}
Void mul_4 ()
{// Multiply the row vector and column vector. The calculation result is the value of an element in the final matrix.
Printf ("\ n4. Method for multiplying row vectors by column vectors: \ n ");
Int I, J, K;
Float c_key, c_sumkey;
// Pay attention to the relationship between three layers of loops.
For (I = 0; I <m; I ++ ){
For (j = 0; j <p; j ++ ){
C_sumkey = 0; // calculate the value of the next element after clearing
Printf ("[");
For (k = 0; k <n; k ++ ){
Printf ("%. 3g", a [I] [k]);
}
Printf ("].");
Printf ("[");
For (k = 0; k <n; k ++ ){
Printf ("%. 3g", B [k] [J]);
}
Printf ("] = ");
For (k = 0; k <n; k ++ ){
If (K! = 0) {printf ("+ ");}
C_key = A [I] [k] * B [k] [J];
C_sumkey + = c_key;
Printf ("%. 3g x %. 3g", a [I] [K], B [k] [J]);
}
C [I] [J] = c_sumkey;
Printf ("= C [% d, % d] = %. 3g \ n", I, j, c_sumkey );
}
}
}
Int main ()
{
Mul_1 ();
Print_matrix ();
Mul_2 ();
Print_matrix ();
Clear_c ();
Mul_3 ();
Print_matrix ();
Mul_4 ();
Printf ("\ n ");
Print_matrix ();
}