[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Toon-members] TooN internal/matrix.hh internal/mbase.hh test/...
From: |
Edward Rosten |
Subject: |
[Toon-members] TooN internal/matrix.hh internal/mbase.hh test/... |
Date: |
Tue, 27 Jan 2009 10:55:54 +0000 |
CVSROOT: /cvsroot/toon
Module name: TooN
Changes by: Edward Rosten <edrosten> 09/01/27 10:55:54
Modified files:
internal : matrix.hh mbase.hh
test : mat_test.cc
Log message:
ColMajor and transpose implemented for static matrices.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/matrix.hh?cvsroot=toon&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/mbase.hh?cvsroot=toon&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/TooN/test/mat_test.cc?cvsroot=toon&r1=1.1&r2=1.2
Patches:
Index: internal/matrix.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/matrix.hh,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- internal/matrix.hh 26 Jan 2009 18:41:42 -0000 1.1
+++ internal/matrix.hh 27 Jan 2009 10:55:53 -0000 1.2
@@ -1,6 +1,8 @@
template <int Rows=-1, int Cols=Rows, class Precision=double, template<int R,
int C, class P> class Layout = RowMajor>
class Matrix : public Layout<Rows, Cols, Precision>
{
+ private:
+ using Layout<Rows, Cols, Precision>::my_data;
public:
//Use Tom's sneaky constructor hack...
@@ -10,9 +12,11 @@
:Layout<Rows, Cols, Precision>(data){}
-
-
-
-
-
+ Precision* data() {
+ return my_data;
+ }
+
+ const Precision* data() const {
+ return my_data;
+ }
};
Index: internal/mbase.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/mbase.hh,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- internal/mbase.hh 26 Jan 2009 18:41:43 -0000 1.1
+++ internal/mbase.hh 27 Jan 2009 10:55:53 -0000 1.2
@@ -2,18 +2,22 @@
//Forward declarations
-
+//TODO?
+//maybe shift to having Layout have a layout template, so Matrix is
Matrix<int,int,class,class>
template<int Rows, int Cols, class Precision, template<int, int, class> class
Layout> class Matrix;
struct Slicing{};
-//Static stride Static slice
+//Static stride Static slice. Use closure to get Stride parameter
+//in to RowMajor and ColMajor
template<int Stride> struct SSSlice
{
+ template<int Rows, int Cols, class Precision> struct ColMajor;
+
template<int Rows, int Cols, class Precision>
struct RowMajor
{
- private:
+ protected:
Precision* my_data;
public:
@@ -54,19 +58,78 @@
}
//Transpose goes here
+ Matrix<Cols, Rows, Precision, SSSlice<Stride>::template ColMajor> T()
{
+ return Matrix<Cols, Rows, Precision, SSSlice<Stride>::template
ColMajor>(my_data, Slicing());
+ }
+ };
+
+ template<int Rows, int Cols, class Precision>
+ struct ColMajor
+ {
+ protected:
+ Precision* my_data;
+
+ public:
+ int num_rows() const { return Rows; }
+ int num_cols() const { return Cols; }
+ //Construction
+
+ ColMajor(Precision* d)
+ :my_data(d)
+ {}
+
+ //Indexing
+
+ Vector<Cols, Precision, SVBase<Cols,Stride,Precision> > operator[](int
row){
+ Internal::check_index(Rows, row);
+ return Vector<Cols, Precision, SVBase<Cols,Stride,Precision>
>(my_data+row);
+ }
+
+ Precision& operator()(int row, int col){
+ Internal::check_index(Rows, row);
+ Internal::check_index(Cols, col);
+ return my_data[row+Stride*col];
+ }
+
+ const Precision& operator()(int row, int col) const {
+ Internal::check_index(Rows, row);
+ Internal::check_index(Cols, col);
+ return my_data[row+Stride*col];
+ }
+
+ //Slices go here
+ template<int Rstart, int Cstart, int Rlength, int Clength>
+ Matrix<Rlength, Clength, Precision, SSSlice<Stride>::template ColMajor>
slice(){
+ Internal::CheckSlice<Rows, Rstart, Rlength>::check();
+ Internal::CheckSlice<Cols, Cstart, Clength>::check();
+
+ return Matrix<Rlength, Clength, Precision, SSSlice<Stride>::template
ColMajor>(my_data + Stride*Cstart + Rstart, Slicing());
+ }
+
+ //Transpose goes here
+ Matrix<Cols, Rows, Precision, SSSlice<Stride>::template RowMajor> T()
{
+ return Matrix<Cols, Rows, Precision, SSSlice<Stride>::template
RowMajor>(my_data, Slicing());
+ }
};
};
+////////////////////////////////////////////////////////////////////////////////
+//
+// Storage base classes
+//
+
template<int Rows, int Cols, class Precision>
struct RowMajor: public StaticSizedAllocator<Rows*Cols, Precision>
{
- int num_rows() const { return Rows; }
- int num_cols() const { return Cols; }
// has to handle operator[row] and operator(row,col)
-
+ protected:
using StaticSizedAllocator<Rows*Cols, Precision>::my_data;
+ public:
+ int num_rows() const { return Rows; }
+ int num_cols() const { return Cols; }
+
Vector<Cols, Precision, SVBase<Cols,1,Precision> > operator[](int row){
Internal::check_index(Rows, row);
return Vector<Cols, Precision, SVBase<Cols,1,Precision>
>(my_data+Cols*row);
@@ -94,4 +157,51 @@
}
//Transpose goes here
+ Matrix<Cols, Rows, Precision, SSSlice<Cols>::template ColMajor> T() {
+ return Matrix<Cols, Rows, Precision, SSSlice<Cols>::template
ColMajor>(my_data, Slicing());
+ }
+};
+
+template<int Rows, int Cols, class Precision>
+struct ColMajor: public StaticSizedAllocator<Rows*Cols, Precision>
+{
+
+ // has to handle operator[row] and operator(row,col)
+ protected:
+ using StaticSizedAllocator<Rows*Cols, Precision>::my_data;
+
+ public:
+ int num_rows() const { return Rows; }
+ int num_cols() const { return Cols; }
+
+ Vector<Cols, Precision, SVBase<Cols,Rows,Precision> > operator[](int
row){
+ Internal::check_index(Rows, row);
+ return Vector<Cols, Precision, SVBase<Cols,Rows,Precision>
>(my_data+row);
+ }
+
+
+ Precision& operator()(int row, int col){
+ Internal::check_index(Rows, row);
+ Internal::check_index(Cols, col);
+ return my_data[row+col*Rows];
+ }
+ const Precision& operator()(int row, int col) const {
+ Internal::check_index(Rows, row);
+ Internal::check_index(Cols, col);
+ return my_data[row+col*Rows];
+ }
+
+
+ template<int Rstart, int Cstart, int Rlength, int Clength>
+ Matrix<Rlength, Clength, Precision, SSSlice<Rows>::template ColMajor>
slice(){
+ Internal::CheckSlice<Rows, Rstart, Rlength>::check();
+ Internal::CheckSlice<Cols, Cstart, Clength>::check();
+
+ return Matrix<Rlength, Clength, Precision, SSSlice<Rows>::template
ColMajor>(my_data + Rstart + Cstart*Rows, Slicing());
+ }
+
+ //Transpose goes here
+ Matrix<Cols, Rows, Precision, SSSlice<Rows>::template RowMajor> T() {
+ return Matrix<Cols, Rows, Precision, SSSlice<Rows>::template
RowMajor>(my_data, Slicing());
+ }
};
Index: test/mat_test.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/mat_test.cc,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- test/mat_test.cc 26 Jan 2009 18:41:43 -0000 1.1
+++ test/mat_test.cc 27 Jan 2009 10:55:54 -0000 1.2
@@ -1,24 +1,52 @@
#include <TooN/TooN.h>
+#include <iterator>
+#include <iostream>
using namespace TooN;
using namespace std;
-int main()
+template<int R, int C, class P, template<int,int,class> class L> void
foo(Matrix<R,C,P,L>& m)
{
- Matrix<3> m;
+ cout << "In foo:\n";
m[0][0] = 1;
m[0][1] = 2;
m[0][2] = 3;
- m[1][0] = 4;
- m[1][1] = 5;
- m[1][2] = 6;
- m[2][0] = 7;
- m[2][1] = 8;
- m[2][2] = 9;
+ m[0][3] = 4;
+ m[1][0] = 5;
+ m[1][1] = 6;
+ m[1][2] = 7;
+ m[1][3] = 8;
+ m[2][0] = 9;
+ m[2][1] = 10;
+ m[2][2] = 11;
+ m[2][3] = 12;
+ cout << "Slices:\n";
cout << m << endl;
+ cout << m.template slice<0,0,2,3>() << endl;
+ cout << m.template slice<0,0,2,3>().template slice<0,1,2,2>() << endl;
+
+ cout << "Transposes:\n";
+ cout << m.T() << endl;
+ cout << m.T().template slice<0,0,2,3>() << endl;
+ cout << m.T().template slice<0,0,2,3>().template slice<0,1,2,2>() <<
endl;
+
+ cout << "Subscripts:\n";
+ cout << m[0] << ", " << m[1] << ", " << m[2] << endl;
- cout << m.slice<0,0,2,3>() << endl;
+ cout << m.template slice<1,1,2,2>()[0] << ", " << m.template
slice<1,1,2,2>()[1] << endl;
- cout << m.slice<0,0,2,3>().slice<0,1,2,2>() << endl;
+ cout << "Layout:\n";
+
+ copy(m.data(), m.data()+m.num_rows()*m.num_cols(),
ostream_iterator<double>(cout, " "));
+ cout << endl;
+}
+
+
+int main()
+{
+ Matrix<3,4> m;
+ foo(m);
+ Matrix<3,4,double,ColMajor> n;
+ foo(n);
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Toon-members] TooN internal/matrix.hh internal/mbase.hh test/...,
Edward Rosten <=