/* * matrix.c - 2D matrix operations * * Written 2010 by Werner Almesberger * Copyright 2010 by Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. */ #include #include "matrix.h" void matrix_identity(struct matrix *m) { m->a[0][0] = m->a[1][1] = 1; m->a[0][1] = m->a[1][0] = 0; m->b[0] = m->b[1] = 0; } void matrix_invert(const double m[2][2], double res[2][2]) { double det = m[0][0]*m[1][1]-m[0][1]*m[1][0]; assert(res != (void *) m); res[0][0] = m[1][1]/det; res[0][1] = -m[0][1]/det; res[1][0] = -m[1][0]/det; res[1][1] = m[0][0]/det; } void matrix_mult(double a[2][2], double b[2][2], double res[2][2]) { assert(res != a); assert(res != b); res[0][0] = a[0][0]*b[0][0]+a[0][1]*b[1][0]; res[0][1] = a[0][0]*b[0][1]+a[0][1]*b[1][1]; res[1][0] = a[1][0]*b[0][0]+a[1][1]*b[1][0]; res[1][1] = a[1][0]*b[0][1]+a[1][1]*b[1][1]; } void matrix_multv(const double v[2], double m[2][2], double res[2]) { double tmp; tmp = v[0]*m[0][0]+v[1]*m[0][1]; res[1] = v[0]*m[1][0]+v[1]*m[1][1]; res[0] = tmp; } void matrix_copy(double from[2][2], double to[2][2]) { to[0][0] = from[0][0]; to[0][1] = from[0][1]; to[1][0] = from[1][0]; to[1][1] = from[1][1]; }