我需要构建制作矩阵乘法的程序。 当我有2个静态矩阵,并且函数返回指向新动态矩阵的指针。
我的问题是当我将矩阵移动到函数中时使乘法正确。
主要是:
int f_matrix[EX2_SIZE][EX2_SIZE] = { {1,2,3}, {1,4,7},{1,5,9} };
int s_matrix[EX2_SIZE][EX2_SIZE] = { {9,8,7},{4,5,6},{0,9,6} };
int m1_rows = EX2_SIZE, m1_cols = EX2_SIZE;
int m2_rows = EX2_SIZE, m2_cols = EX2_SIZE; //For tester can change easily
int **new_matrix;
if (m1_rows != m2_rows || m1_cols != m2_cols) return 0; //For multiplying must be equal matrix !!
new_matrix = multiplying(f_matrix, s_matrix, m1_rows, m1_cols);
print_mtrx(new_matrix, m1_rows, m1_cols);
free_mtrx(new_matrix, m1_cols);
问题在于“乘法”功能,现在就是:
int multiplying(int **A, int **B, int rows, int cols) {
int **C;
int i,j;
C = (int**)malloc(sizeof(int*)*rows);
for (i = 0; i < cols; i++) {
C[i] = (int*)malloc(sizeof(int)*cols);
for (j = 0; j < cols; j++)
C[i][j] = (A[i][j]) * (B[i][j]);
}
return C;
}
THX
答案 0 :(得分:0)
假设C99(或C11并且实现没有定义__STDC_NO_VLA__
),那么最简单的方法是使用可变长度数组(VLA)并使调用代码也为结果矩阵分配空间。要按顺序将A[M][N]
和B[P][Q]
两个矩阵相乘,N
和P
的值必须相等,结果矩阵为C[M][Q]
。
#include <stdio.h>
static
void matrix_multiply(int A_rows, int A_cols, int B_cols,
int A[A_rows][A_cols],
int B[A_cols][B_cols],
int C[A_rows][B_cols])
{
for (int i = 0; i < A_rows; i++)
{
for (int j = 0; j < B_cols; j++)
{
int sum = 0;
for (int k = 0; k < A_cols; k++)
sum += A[i][k] * B[k][j];
C[i][j] = sum;
}
}
}
static void print_matrix(const char *tag, int w, int N, int M, int matrix[N][M])
{
printf("%s (%dx%d):\n", tag, N, M);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
printf("%*d", w, matrix[i][j]);
putchar('\n');
}
}
int main(void)
{
int A[3][4] =
{
{ 41, 76, 70, 42, },
{ 70, 62, 77, 74, },
{ 49, 55, 43, 65, },
};
int B[4][5] =
{
{ 73, 33, 42, 72, 65, },
{ 69, 30, 83, 83, 64, },
{ 90, 74, 84, 51, 23, },
{ 62, 45, 84, 46, 43, },
};
int C[3][5];
print_matrix("A", 3, 3, 4, A);
print_matrix("B", 3, 4, 5, B);
matrix_multiply(3, 4, 5, A, B, C);
print_matrix("C", 6, 3, 5, C);
return 0;
}
输出:
A (3x4):
41 76 70 42
70 62 77 74
49 55 43 65
B (4x5):
73 33 42 72 65
69 30 83 83 64
90 74 84 51 23
62 45 84 46 43
C (3x5):
17141 10703 17438 14762 10945
20906 13198 20770 17517 13471
15272 9374 15695 13276 10489
如果你想进行动态内存分配,那么我认为你必须将指向VLA数组的指针传递给函数以获取返回的值。这导致像这样的代码,它与前面的代码非常相似 - 除了(未经检查的)内存分配。请注意,因为它使用static_assert
,所以它是C11程序,而不是C99程序。
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef __STDC_NO_VLA__ // C11
static_assert(0, "No VLA support");
#endif
static void matrix_multiply(int A_rows, int A_cols, int B_cols,
int A[A_rows][A_cols], int B[A_cols][B_cols],
int (**C)[B_cols])
{
(*C) = malloc(sizeof(int [A_rows][B_cols])); // XXX: Check memory allocation!
for (int i = 0; i < A_rows; i++)
{
for (int j = 0; j < B_cols; j++)
{
int sum = 0;
for (int k = 0; k < A_cols; k++)
sum += A[i][k] * B[k][j];
(*C)[i][j] = sum;
}
}
}
static void print_matrix(const char *tag, int w, int N, int M, int matrix[N][M])
{
printf("%s (%dx%d):\n", tag, N, M);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
printf("%*d", w, matrix[i][j]);
putchar('\n');
}
}
int main(void)
{
enum { N = 3, M = 4, P = 4, Q = 5 };
int A[N][M] =
{
{ 41, 76, 70, 42, },
{ 70, 62, 77, 74, },
{ 49, 55, 43, 65, },
};
int B[P][Q] =
{
{ 73, 33, 42, 72, 65, },
{ 69, 30, 83, 83, 64, },
{ 90, 74, 84, 51, 23, },
{ 62, 45, 84, 46, 43, },
};
static_assert(M == P, "Matrix dimensions are mismatched");
int (*C)[Q];
print_matrix("A", 3, N, M, A);
print_matrix("B", 3, P, Q, B);
matrix_multiply(N, M, Q, A, B, &C);
print_matrix("C", 6, N, Q, C);
free(C);
return 0;
}
当然,这会产生与以前相同的输出。
这是原始代码的固定变体。
#include <stdio.h>
#include <stdlib.h>
static void free_mtrx(int **mtrx, int rows)
{
for (int i = 0; i < rows; i++)
free(mtrx[i]);
free(mtrx);
}
static int **multiplying_1(int **A, int **B, int rows, int cols)
{
int **C;
C = (int **)malloc(sizeof(int *) * rows);
for (int i = 0; i < cols; i++)
{
C[i] = (int *)malloc(sizeof(int) * cols);
for (int j = 0; j < cols; j++)
C[i][j] = (A[i][j]) * (B[i][j]);
}
return C;
}
static void print_mtrx(const char *tag, int w, int N, int M, int **matrix)
{
printf("%s (%dx%d):\n", tag, N, M);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
printf("%*d", w, matrix[i][j]);
putchar('\n');
}
}
int main(void)
{
enum { EX2_SIZE = 3, EX3_SIZE = 3 };
int f_matrix[EX2_SIZE][EX2_SIZE] = { {1, 2, 3}, {1, 4, 7}, {1, 5, 9} };
int s_matrix[EX2_SIZE][EX2_SIZE] = { {9, 8, 7}, {4, 5, 6}, {0, 9, 6} };
int m1_rows = EX2_SIZE, m1_cols = EX2_SIZE;
int m2_rows = EX2_SIZE, m2_cols = EX2_SIZE; // For tester can change easily
int *f[] = { &f_matrix[0][0], &f_matrix[1][0], &f_matrix[2][0] };
int *s[] = { &s_matrix[0][0], &s_matrix[1][0], &s_matrix[2][0] };
int **new_matrix;
if (m1_rows != m2_rows || m1_cols != m2_cols)
return 0;
print_mtrx("F", 3, EX2_SIZE, EX2_SIZE, f);
print_mtrx("S", 3, EX3_SIZE, EX3_SIZE, s);
new_matrix = multiplying(f, s, m1_rows, m1_cols);
print_mtrx("R", 3, m1_rows, m1_cols, new_matrix);
free_mtrx(new_matrix, m1_cols);
return 0;
}
当然,&#39;矩阵乘法&#39;算法不是传统的数学算法;这不是一个重大问题。关键点是int **matrix
与int matrix[N][M]
不同。后者仅由整数值组成;前者是一个指针数组,每个指针指向一系列整数值。对于范围内multiplying()
的原型,编译器抱怨您的原始调用。在从f
和s
创建矩阵f_matrix
和s_matrix
时,我从整数数组中制作了指针数组。请注意,print_mtrx()
函数的主体与前两个程序中print_matrix()
函数的主体相同。但是,生成的代码不一样,因为参数列表对matrix参数使用两种不同的,不兼容的类型。 Valgrind为这个计划提供了一个清洁的健康状况。
输出:
F (3x3):
1 2 3
1 4 7
1 5 9
S (3x3):
9 8 7
4 5 6
0 9 6
R (3x3):
9 16 21
4 20 42
0 45 54