summaryrefslogtreecommitdiff
path: root/matrixmul.c
blob: c2da2729b0b81787ccec00dec4c10135d0b2380c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <u.h>
#include <libc.h>

typedef double Matrix[4][4];

void
mulm(Matrix a, Matrix b)
{
	int i, j, k;
	Matrix r;

	for(i = 0; i < 4; i++)
		for(j = 0; j < 4; j++){
			r[i][j] = 0;
			for(k = 0; k < 4; k++)
				r[i][j] += a[i][k]*b[k][j];
		}
	for(i = 0; i < 4; i++)
		for(j = 0; j < 4; j++)
			a[i][j] = r[i][j];
}

void
printm(Matrix m)
{
	int i, j;

	for(i = 0; i < 4; i++){
		for(j = 0; j < 4; j++)
			print("%g\t", m[i][j]);
		print("\n");
	}
}

void
main()
{
	Matrix m1 = {
		1, 2, -1, 0,
		2, 0, 1, 0,
		0, 0, 0, 0,
		0, 0, 0, 0
	};
	Matrix m2 = {
		3, 1, 0, 0,
		0, -1, 0, 0,
		-2, 3, 0, 0,
		0, 0, 0, 0
	};
	mulm(m1, m2);
	printm(m1);
	exits(0);
}