toon-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Toon-members] TooN TODO internal/operators.hh test/test3.cc


From: Edward Rosten
Subject: [Toon-members] TooN TODO internal/operators.hh test/test3.cc
Date: Wed, 18 Feb 2009 20:02:39 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/02/18 20:02:39

Modified files:
        .              : TODO 
        internal       : operators.hh 
        test           : test3.cc 

Log message:
        matrix * matrix
        
        Fix up AddType, etc so they don't call the default constructor of the 
type
        types. For some reason the broken ones aren't rejected by SFINAE.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/TODO?cvsroot=toon&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/operators.hh?cvsroot=toon&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/TooN/test/test3.cc?cvsroot=toon&r1=1.2&r2=1.3

Patches:
Index: TODO
===================================================================
RCS file: /cvsroot/toon/TooN/TODO,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- TODO        11 Feb 2009 18:04:25 -0000      1.7
+++ TODO        18 Feb 2009 20:02:38 -0000      1.8
@@ -1,3 +1,5 @@
 .as_row() and .as_col() for vectors
-matrix operators
+BLAS 
+helpers.h
+fix up decompositions.
 iterators

Index: internal/operators.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/operators.hh,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- internal/operators.hh       18 Feb 2009 18:48:17 -0000      1.7
+++ internal/operators.hh       18 Feb 2009 20:02:39 -0000      1.8
@@ -73,6 +73,18 @@
                }
        };
 
+       //FIXME what about BLAS?
+       template<typename Precision> struct MatrixMultiply
+       {
+               template<int R, int C, typename B, int R1, int C1, typename P1, 
typename B1, int R2, int C2, typename P2, typename B2> 
+               static void eval(Matrix<R, C, Precision, B>& res, const 
Matrix<R1, C1, P1, B1>& m1, const Matrix<R2, C2, P2, B2>& m2)
+               {
+                       for(int i=0; i < res.num_rows(); ++i)
+                               for(int j=0; j < res.num_cols(); ++j)
+                                       res[i][j] = m1[i] * (m2.T()[j]);
+               }
+       };
+
        //Mini operators for passing to Pairwise, etc
        struct Add{ template<class A, class B, class C>      static A op(const 
B& b, const C& c){return b+c;} };
        struct Subtract{ template<class A, class B, class C> static A op(const 
B& b, const C& c){return b-c;} };
@@ -80,10 +92,10 @@
        struct Divide{ template<class A, class B, class C>   static A op(const 
B& b, const C& c){return b/c;} };
        
        //Automatic type deduction of return types
-       template<class L, class R> struct AddType {      typedef 
TOON_TYPEOF((L()+R())) type; };
-       template<class L, class R> struct SubtractType { typedef 
TOON_TYPEOF((L()-R())) type; };
-       template<class L, class R> struct MultiplyType { typedef 
TOON_TYPEOF((L()*R())) type; };
-       template<class L, class R> struct DivideType {   typedef 
TOON_TYPEOF((L()*R())) type; };
+       template<class L, class R> struct AddType {      typedef TOON_TYPEOF( 
(*static_cast<L*>(0) + *static_cast<R*>(0))) type;};
+       template<class L, class R> struct SubtractType { typedef TOON_TYPEOF( 
(*static_cast<L*>(0) - *static_cast<R*>(0))) type;};
+       template<class L, class R> struct MultiplyType { typedef TOON_TYPEOF( 
(*static_cast<L*>(0) * *static_cast<R*>(0))) type;};
+       template<class L, class R> struct DivideType   { typedef TOON_TYPEOF( 
(*static_cast<L*>(0) / *static_cast<R*>(0))) type;};
        
        //Output size, given input size. Be static if possible.
        template<int i, int j> struct Sizer{static const int size=i;};
@@ -97,6 +109,7 @@
 // vector <op> vector
 //
 
+
 // Addition Vector + Vector
 template<int S1, int S2, typename P1, typename P2, typename B1, typename B2> 
 Vector<Internal::Sizer<S1,S2>::size, typename Internal::AddType<P1, P2>::type> 
operator+(const Vector<S1, P1, B1>& v1, const Vector<S2, P2, B2>& v2)
@@ -128,7 +141,6 @@
 }
 
 
-
 // Addition Matrix + Matrix
 template<int R1, int C1, int R2, int C2, typename P1, typename P2, typename 
B1, typename B2> 
 Matrix<Internal::Sizer<R1,R2>::size, Internal::Sizer<C1,C2>::size, typename 
Internal::AddType<P1, P2>::type> operator+(const Matrix<R1, C1, P1, B1>& m1, 
const Matrix<R2, C2, P2, B2>& m2)
@@ -149,6 +161,18 @@
        return Matrix<Internal::Sizer<R1,R2>::size, 
Internal::Sizer<C1,C2>::size,restype>(m1, m2, 
Operator<Internal::Pairwise<restype, Internal::Subtract> >(), m1.num_rows(), 
m1.num_cols());
 }
 
+// Matrix multiplication Matrix * Matrix
+
+template<int R1, int C1, int R2, int C2, typename P1, typename P2, typename 
B1, typename B2> 
+Matrix<Internal::Sizer<R1,R1>::size, Internal::Sizer<C2,C2>::size, typename 
Internal::MultiplyType<P1, P2>::type> operator*(const Matrix<R1, C1, P1, B1>& 
m1, const Matrix<R2, C2, P2, B2>& m2)
+{
+       typedef typename Internal::MultiplyType<P1, P2>::type restype;
+
+       SizeMismatch<R1, C2>:: test(m1.num_rows(),m2.num_cols());
+       SizeMismatch<C1, R2>:: test(m1.num_cols(),m2.num_rows());
+       return Matrix<Internal::Sizer<R1,R1>::size, 
Internal::Sizer<C2,C2>::size,restype>(m1, m2, 
Operator<Internal::MatrixMultiply<restype> >(), m1.num_rows(), m2.num_cols());
+}
+
 
 
////////////////////////////////////////////////////////////////////////////////
 //
@@ -183,10 +207,10 @@
        return Matrix<R, C,restype>(s, m, 
Operator<Internal::ApplyScalarLeft<restype, Internal::OPNAME> >(), 
m.num_rows(), m.num_cols());\
 }
 
-TOON_MAKE_SCALAR_OP_PAIR(Add, +)
-TOON_MAKE_SCALAR_OP_PAIR(Add, -)
-TOON_MAKE_SCALAR_OP_PAIR(Add, *)
-TOON_MAKE_SCALAR_OP_PAIR(Add, /)
+//TOON_MAKE_SCALAR_OP_PAIR(Add, +)
+//TOON_MAKE_SCALAR_OP_PAIR(Subtract, -)
+//TOON_MAKE_SCALAR_OP_PAIR(Multiply, *)
+//TOON_MAKE_SCALAR_OP_PAIR(Divide, /)
 
 #undef TOON_MAKE_SCALAR_OP_PAIR
 

Index: test/test3.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/test3.cc,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- test/test3.cc       18 Feb 2009 18:48:17 -0000      1.2
+++ test/test3.cc       18 Feb 2009 20:02:39 -0000      1.3
@@ -27,6 +27,18 @@
        cout << m1 << endl;
 
        cout << (m1+m2) << endl;
-       cout << 1+(m1+m2)+2 << endl;
+       cout << (m1+m2) << endl;
+
+       Matrix<2,3> m3;
+       m3[0] = makeVector(0, 1, 2);
+       m3[1] = makeVector(3, 4, 5);
+       Matrix<3,2> m4;
+       m4[0] = makeVector(6, 7);
+       m4[1] = makeVector(8, 9);
+       m4[2] = makeVector(10, 11);
+
+       cout << m3<<endl;
+       cout << m4<<endl;
+       cout << m3*m4;
 }
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]