#include <stdio.h>
#include <matrix.h>
main()
{
int i, j, n;
vector u, v, bj;
matrix a, b, c;
printf("n="); scanf("%d", &n);
/* n 次元のベクトル u, v を準備 */
if ((u = new_vector(n)) == NULL) {
fprintf(stderr, "cannot allocate u\n"); exit(1);
}
if ((v = new_vector(n)) == NULL) {
fprintf(stderr, "cannot allocate v\n"); exit(1);
}
/* u, v の全成分を 1 にする */
for (i = 0; i < n; i++) u[i] = v[i] = 1.0;
/* u, v の内積を計算して表示する */
printf("dotprod(n, u, v)=%g\n", dotprod(n, u, v));
/* n 次正方行列 a, b, c を準備 */
if ((a = new_matrix(n, n)) == NULL) {
fprintf(stderr, "cannot allocate a\n"); exit(1);
}
if ((b = new_matrix(n, n)) == NULL) {
fprintf(stderr, "cannot allocate b\n"); exit(1);
}
if ((c = new_matrix(n, n)) == NULL) {
fprintf(stderr, "cannot allocate c\n"); exit(1);
}
/* n 次元のベクトル bj を準備 */
if ((bj = new_vector(n)) == NULL) {
fprintf(stderr, "cannot allocate v\n"); exit(1);
}
/* a, b の全成分を 1 にする */
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
a[i][j] = b[i][j] = 1.0;
for (j = 0; j < n; j++) {
/* 行列 b の第 j 列ベクトルを bj にコピーする */
for (i = 0; i < n; i++) bj[i] = b[i][j];
/* c の (i,j) 成分を a の第 i 行と b の第 j 列の内積とする
* i.e. 行列 c を行列 a, b の積とする */
for (i = 0; i < n; i++) c[i][j] = dotprod(n, a[i], bj);
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf("%g ", c[i][j]);
printf("\n");
}
return 0;
}
|
6701 号室の環境でコンパイルするには -lmatrix というリンカー・ オプションをつければよい2。
gcc -o testmat testmat.c -lmatrix -lm
|
vector は、実際には double へのポインターなので、 double 型の配列 double [数] や、 double 型へのポインター *double と互換性がある。