/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ #include "Matrix3.h" #include #include #include /*--------------------*/ Matrix newMatrix (int h, int w) { Matrix res = malloc (sizeof (*res)); if (res) { res->rows = malloc (h * sizeof (Row)); res->elems = malloc (h * w * sizeof (Element)); res->width = w; res->height = h; { Rows rs = res->rows; Row r = res->elems; if (rs && r) { while (h--) { *rs++ = r; r += w; } return res; } } } /* heap overflow */ perror ("newMatrix: can't allocate matix"); exit (1); } /*--------------------*/ void freeMatrix (Matrix m) { free (m->elems); free (m->rows); free (m); } /*--------------------*/ Matrix zeroMatrix (int h, int w) { Matrix res = newMatrix (h, w); Element *p = res->elems; int len = w * h; while (len--) { *p++ = 0.0; } return res; } /*--------------------*/ Matrix unitMatrix (int h, int w) { Matrix res = zeroMatrix (h, w); Rows r = res->rows; int i; for (i = 0; i < w && i < h; ++i) { r[i][i] = 1.0; } return res; } /*--------------------*/ Matrix addMatrix (Matrix m1, Matrix m2) { int w = m1->width; int h = m1->height; assert (w == m2->width && h == m2->height); { Matrix res = newMatrix (h, w); Rows r = res->rows; Rows r1 = m1->rows; Rows r2 = m2->rows; int i; for (i = 0; i < h; ++i) { int j; for (j = 0; j < w; ++j) { r[i][j] = r1[i][j] + r2[i][j]; } } return res; } } /*--------------------*/ Matrix transposeMatrix (Matrix m) { int w = m->width; int h = m->height; Matrix res = newMatrix (w, h); Rows r = res->rows; Rows r1 = m->rows; int i; for (i = 0; i < h; ++i) { int j; for (j = 0; j < w; ++j) { r[j][i] = r1[i][j]; } } return res; } /*--------------------*/ Element at (Matrix m, int i, int j) { /* index check */ assert (0 <= i && i < m->width); assert (0 <= j && j < m->height); return m->rows[i][j]; } Matrix setAt (Matrix m, int i, int j, Element v) { /* index check */ assert (0 <= i && i < m->width); assert (0 <= j && j < m->height); m->rows[i][j] = v; return m; } /*--------------------*/