summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2014-11-01 06:12:47 +0100
committerStanislaw Halik <sthalik@misaki.pl>2014-11-01 08:56:09 +0100
commit99039d06ad001657da88018a2b85f654c33ca0fc (patch)
treea8c4c5d3fc0e28b529fbb57fce96cf0e4236e662
parent23d969124fc5ed1356dfc8f6ec0ae8de674a0fb2 (diff)
add simple matrix operations impl
-rw-r--r--opentrack/simple-mat.hpp74
1 files changed, 74 insertions, 0 deletions
diff --git a/opentrack/simple-mat.hpp b/opentrack/simple-mat.hpp
new file mode 100644
index 00000000..5fa3acf9
--- /dev/null
+++ b/opentrack/simple-mat.hpp
@@ -0,0 +1,74 @@
+#pragma once
+
+template<typename num, int h, int w>
+struct Mat
+{
+ num data[h][w];
+
+ template<int p>
+ Mat<num, w, p> operator*(const Mat<num, w, p>& other) const
+ {
+ Mat<num, w, p> ret;
+ for (int j = 0; j < w; j++)
+ for (int i = 0; i < p; i++)
+ {
+ num sum = num(0);
+
+ for (int k = 0; k < h; k++)
+ sum += data[j][k]*other.data[k][i];
+
+ ret.data[j][i] = sum;
+ }
+
+ return ret;
+ }
+
+ num operator()(int j, int i) const { return data[j][i]; }
+ num& operator()(int j, int i) { return data[j][i]; }
+
+ Mat()
+ {
+ for (int j = 0; j < h; j++)
+ for (int i = 0; i < w; i++)
+ data[j][i] = 0;
+ }
+
+ Mat(const num* mem)
+ {
+ for (int j = 0; j < h; j++)
+ for (int i = 0; i < w; i++)
+ data[j][i] = mem[i*h+j];
+ }
+
+ // XXX add more operators as needed, third-party dependencies mostly
+ // not needed merely for matrix algebra -sh 20141030
+
+ static Mat<num, h, h> eye()
+ {
+ Mat<num, h, h> ret;
+ for (int j = 0; j < h; j++)
+ for (int i = 0; i < w; i++)
+ ret.data[j][i] = 0;
+
+ for (int i = 0; i < h; i++)
+ ret.data[i][i] = 1;
+
+ return ret;
+ }
+
+ Mat<num, w, h> t()
+ {
+ Mat<num, w, h> ret;
+
+ for (int j = 0; j < h; j++)
+ for (int i = 0; i < w; i++)
+ ret.data[i][j] = data[j][i];
+
+ return ret;
+ }
+};
+
+template<int h, int w> using dmat = Mat<double, h, w>;
+
+using rmat = dmat<3, 3>;
+