www.pudn.com > Matrix-multiplication-.zip > ʵ6.c, change:2014-04-13,size:3376b


#include<stdio.h> 
#include<stdlib.h> 
#include<math.h> 
int  t, m; 
void merge (int A1[t][t], int A2[t][t], int A3[t][t], int A4[t][t], int t, int result[2 * t][2 * t]) 
{ 
	int d = 2 * t; 
	int i, j; 
	for (i = 0; i < d; i++) 
		for (j = 0; j < d; j++) 
		{ 
			if (i < t && j < t) 
				result[i][j] = A1[i][j]; 
			if (i < t && j >= t) 
				result[i][j] = A2[i][j - t]; 
			if (i >= t && j < t) 
				result[i][j] = A3[i - t][j]; 
			if (i >= t && j >= t) 
				result[i][j] = A4[i - t][j - t]; 
		} 
} 
 
void add (int m1[t][t], int  m2[t][t], int t, int  result[t][t]) 
{ 
	int i, j; 
	for (i = 0; i < t; i++) 
		for (j = 0; j < t; j++) 
			result[i][j] = m1[i][j] + m2[i][j]; 
 
} 
void sub (int m1[t][t], int m2[t][t], int t, int result[t][t]) 
{ 
	int i, j; 
	for (i = 0; i < t; i++) 
		for (j = 0; j < t; j++) 
			result[i][j] = m1[i][j] - m2[i][j]; 
} 
 
void Multi (int matrix1[t][t], int matrix2[t][t], int m, int result[t][t]) 
{ 
	int i, j, h; 
	if (m == 2) 
	{ 
		int m1 = matrix1[0][0] * (matrix2[0][1] - matrix2[1][1]); 
		int m2 = (matrix1[0][0] + matrix1[0][1]) * matrix2[1][1]; 
		int m3 = (matrix1[1][0] + matrix1[1][1]) * matrix2[0][0]; 
		int m4 = matrix1[1][1] * (matrix2[1][0] - matrix2[0][0]); 
		int m5 = (matrix1[0][0] + matrix1[1][1]) * (matrix2[0][0] + matrix2[1][1]); 
		int m6 = (matrix1[0][1] - matrix1[1][1]) * (matrix2[1][0] + matrix2[1][1]); 
		int m7 = (matrix1[0][0] - matrix1[1][0]) * (matrix2[0][0] + matrix2[0][1]); 
		result[0][0] = m5 + m4 - m2 + m6; 
		result[0][1] = m1 + m2; 
		result[1][0] = m3 + m4; 
		result[1][1] = m5 + m1 - m3 - m7; 
	} 
	else 
	{ 
		t = m / 2; 
		int A11[t][t], A12[t][t], A21[t][t], A22[t][t], B11[t][t], B12[t][t], B21[t][t], B22[t][t], C11[t][t], C12[t][t], C21[t][t], C22[t][t]; 
		int M1[t][t], M2[t][t], M3[t][t], M4[t][t], M5[t][t], M6[t][t], M7[t][t]; 
		int R1[t][t], R2[t][t]; 
		for (i = 0; i < t; i++) 
			for (j = 0; j < t; j++) 
			{ 
				A11[i][j] = matrix1[i][j]; 
				A12[i][j] = matrix1[i][j + t]; 
				A21[i][j] = matrix1[i + t][j]; 
				A22[i][j] = matrix1[i + t][j + t]; 
				B11[i][j] = matrix2[i][j]; 
				B12[i][j] = matrix2[i][j + t]; 
				B21[i][j] = matrix2[i + t][j]; 
				B22[i][j] = matrix2[i + t][j + t]; 
			} 
		sub (B12, B22, t, R1); 
		Multi (A11, R1, t, M1); 
 
		add (A11, A12, t, R1); 
		Multi (R1, B22, t, M2); 
 
		add (A21, A22, t, R1); 
		Multi (R1, B11, t, M3); 
 
		sub (B21, B11, t, R1); 
		Multi (A22, R1, t, M4); 
 
		add (A11, A22, t, R1); 
		add (B11, B22, t, R2); 
		Multi (R1, R2, t, M5); 
 
		sub (A12, A22, t, R1); 
		add (B21, B22, t, R2); 
		Multi (R1, R2, t, M6); 
 
		sub (A11, A21, t, R1); 
		add (B11, B12, t, R2); 
		Multi (R1, R2, t, M7); 
 
		add (M5, M4, t, R1); 
		sub (R1, M2, t, R2); 
		add (R2, M6, t, C11); 
 
		add (M1, M2, t, C12); 
 
		add (M3, M4, t, C21); 
 
		add (M5, M1, t, R1); 
		sub (R1, M3, t, R2); 
		sub (R2, M7, t, C22); 
		merge (C11, C12, C21, C22, t, result); 
	} 
} 
int main() 
{ 
	int n, i, j; 
	scanf ("%d", &n); 
	int matrix1[n][n]; 
	int matrix2[n][n]; 
	int result[n][n]; 
	for (i = 0; i < n; i++) 
		for (j = 0; j < n; j++) 
			scanf ("%d", &matrix1[i][j]); 
	for (i = 0; i < n; i++) 
		for (j = 0; j < n; j++) 
			scanf ("%d", &matrix2[i][j]); 
	t = n; 
	Multi (matrix1, matrix2, n, result); 
	for (i = 0; i < n; i++) 
	{ 
		for (j = 0; j < n; j++) 
			printf ("%5d  ", result[i][j]); 
		printf ("\n"); 
	} 
 
}