Root/
| 1 | /* |
| 2 | * matrix.c - 2D matrix operations |
| 3 | * |
| 4 | * Written 2010 by Werner Almesberger |
| 5 | * Copyright 2010 by Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <assert.h> |
| 15 | |
| 16 | #include "matrix.h" |
| 17 | |
| 18 | |
| 19 | void matrix_identity(struct matrix *m) |
| 20 | { |
| 21 | m->a[0][0] = m->a[1][1] = 1; |
| 22 | m->a[0][1] = m->a[1][0] = 0; |
| 23 | m->b[0] = m->b[1] = 0; |
| 24 | } |
| 25 | |
| 26 | |
| 27 | void matrix_invert(const double m[2][2], double res[2][2]) |
| 28 | { |
| 29 | double det = m[0][0]*m[1][1]-m[0][1]*m[1][0]; |
| 30 | |
| 31 | assert(res != (void *) m); |
| 32 | res[0][0] = m[1][1]/det; |
| 33 | res[0][1] = -m[0][1]/det; |
| 34 | res[1][0] = -m[1][0]/det; |
| 35 | res[1][1] = m[0][0]/det; |
| 36 | } |
| 37 | |
| 38 | |
| 39 | void matrix_mult(double a[2][2], double b[2][2], double res[2][2]) |
| 40 | { |
| 41 | assert(res != a); |
| 42 | assert(res != b); |
| 43 | res[0][0] = a[0][0]*b[0][0]+a[0][1]*b[1][0]; |
| 44 | res[0][1] = a[0][0]*b[0][1]+a[0][1]*b[1][1]; |
| 45 | res[1][0] = a[1][0]*b[0][0]+a[1][1]*b[1][0]; |
| 46 | res[1][1] = a[1][0]*b[0][1]+a[1][1]*b[1][1]; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | void matrix_multv(const double v[2], double m[2][2], double res[2]) |
| 51 | { |
| 52 | double tmp; |
| 53 | |
| 54 | tmp = v[0]*m[0][0]+v[1]*m[0][1]; |
| 55 | res[1] = v[0]*m[1][0]+v[1]*m[1][1]; |
| 56 | res[0] = tmp; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void matrix_copy(double from[2][2], double to[2][2]) |
| 61 | { |
| 62 | to[0][0] = from[0][0]; |
| 63 | to[0][1] = from[0][1]; |
| 64 | to[1][0] = from[1][0]; |
| 65 | to[1][1] = from[1][1]; |
| 66 | } |
| 67 |
Branches:
master
