#include"stdlib.h"
#include"stdint.h"
#include"string.h"
+#include"time.h"
#include"assert.h"
typedef struct buf_s buf_t;
}
}
-void mat_mul(mat_t* m, mat_t* m0, mat_t* m1)
+void mat_add(mat_t* m, mat_t* m0, mat_t* m1)
+{
+ assert(m0->n == m1->n);
+ assert(m->n == m0->n);
+
+ int i;
+ int j;
+ int n = m->n;
+
+ for (i = 0; i < n; i++) {
+ for (j = 0; j < n; j++) {
+
+ int ij0 = (i + m0->i) * m0->b->n + (j + m0->j);
+ int ij1 = (i + m1->i) * m1->b->n + (j + m1->j);
+ int ij = (i + m->i) * m->b->n + (j + m->j);
+
+ m->b->data[ij] = m0->b->data[ij0] + m1->b->data[ij1];
+ }
+ }
+}
+
+void mat_sub(mat_t* m, mat_t* m0, mat_t* m1)
{
assert(m0->n == m1->n);
assert(m->n == m0->n);
- assert(m0->b);
- assert(m1->b);
- assert(m->b);
+ int i;
+ int j;
+ int n = m->n;
+
+ for (i = 0; i < n; i++) {
+ for (j = 0; j < n; j++) {
+
+ int ij0 = (i + m0->i) * m0->b->n + (j + m0->j);
+ int ij1 = (i + m1->i) * m1->b->n + (j + m1->j);
+ int ij = (i + m->i) * m->b->n + (j + m->j);
+
+ m->b->data[ij] = m0->b->data[ij0] - m1->b->data[ij1];
+ }
+ }
+}
+
+void mat_mul(mat_t* m, mat_t* m0, mat_t* m1)
+{
+ assert(m0->n == m1->n);
+ assert(m->n == m0->n);
int i;
int j;
}
}
+void mat_fill(mat_t* m)
+{
+ assert(m && m->b);
+
+ int i;
+ int j;
+
+ for (i = 0; i < m->n; i++) {
+ for (j = 0; j < m->n; j++) {
+
+ int ij = (i + m->i) * m->b->n + (j + m->j);
+
+ m->b->data[ij] = rand() % 10;
+ }
+ }
+}
+
void mat_print(mat_t* m)
{
assert(m && m->b);
printf("\n");
}
-int main()
+int main(int argc, char* argv[])
{
- int a[4] = {1, 2, 3, 4};
+ if (argc < 2) {
+ printf("argc: %d, < 2\n", argc);
+ return -1;
+ }
- mat_t* m0 = mat_alloc(0, 0, 2, NULL);
- mat_t* m1 = mat_alloc(0, 0, 2, NULL);
- mat_t* m = mat_alloc(0, 0, 2, NULL);
+ int n = atoi(argv[1]);
- memcpy(m0->b->data, a, sizeof(int) * 4);
- memcpy(m1->b->data, a, sizeof(int) * 4);
+ srand(time(NULL));
+
+ mat_t* m0 = mat_alloc(0, 0, n, NULL);
+ mat_t* m1 = mat_alloc(0, 0, n, NULL);
+ mat_t* m = mat_alloc(0, 0, n, NULL);
+
+ mat_fill(m0);
+ mat_fill(m1);
mat_mul(m, m0, m1);
return 0;
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-