toon-members
[Top][All Lists]
Advanced

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

[Toon-members] TooN helpers.h test/identity_test.cc


From: Edward Rosten
Subject: [Toon-members] TooN helpers.h test/identity_test.cc
Date: Thu, 16 Apr 2009 17:33:55 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/04/16 17:33:55

Modified files:
        .              : helpers.h 
Added files:
        test           : identity_test.cc 

Log message:
        Added left and right multiplication of identity operator by a scalar. 
Eg:
        
        Matrix<3> m = 5 * Identity;

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/helpers.h?cvsroot=toon&r1=1.48&r2=1.49
http://cvs.savannah.gnu.org/viewcvs/TooN/test/identity_test.cc?cvsroot=toon&rev=1.1

Patches:
Index: helpers.h
===================================================================
RCS file: /cvsroot/toon/TooN/helpers.h,v
retrieving revision 1.48
retrieving revision 1.49
diff -u -b -r1.48 -r1.49
--- helpers.h   16 Apr 2009 15:23:23 -0000      1.48
+++ helpers.h   16 Apr 2009 17:33:55 -0000      1.49
@@ -110,8 +110,8 @@
                struct Zero;
                struct SizedZero;
                struct RCZero;
-               struct Identity;
-               struct SizedIdentity;
+               template<class P> struct Identity;
+               template<class P> struct SizedIdentity;
        }
 
        template<> struct Operator<Internal::RCZero> {
@@ -192,11 +192,15 @@
 
        };
 
-       template<> struct Operator<Internal::SizedIdentity> {
-               Operator(int s) : my_size(s) {}
+       template<class Precision> struct 
Operator<Internal::SizedIdentity<Precision> > {
                
+               const Precision val;
                const int my_size;
                
+               Operator(int s, const Precision& v=1)
+               :my_size(s),val(v)
+               {}
+
                int num_rows() const {return my_size;}
                int num_cols() const {return my_size;}
 
@@ -211,14 +215,17 @@
                        }
                                                
                        for(int r=0; r < m.num_rows(); r++) {
-                               m(r,r) = 1;
+                               m(r,r) = val;
                        }
                }
        };
                
+       template<class Precision> struct Operator<Internal::Identity<Precision> 
> {
 
-
-       template<> struct Operator<Internal::Identity> {
+               const Precision val;
+               Operator(const Precision& v=1)
+               :val(v)
+               {}
 
                template<int R, int C, class P, class B>
                void eval(Matrix<R,C,P,B>& m) const {
@@ -231,18 +238,37 @@
                        }
                                                
                        for(int r=0; r < m.num_rows(); r++) {
-                               m(r,r) = 1;
+                               m(r,r) = val;
                        }
                }
 
-               Operator<Internal::SizedIdentity> operator()(int s){
-                       return Operator<Internal::SizedIdentity>(s);
+               Operator<Internal::SizedIdentity<Precision> > operator()(int s){
+                       return Operator<Internal::SizedIdentity<Precision> >(s);
                }
        };
 
+       template<class P1, class P2> Operator<Internal::Identity<typename 
Internal::MultiplyType<P1, P2>::type> > operator*(const P1& p, const 
Operator<Internal::Identity<P2> >& i)
+       {
+               return Operator<Internal::Identity<typename 
Internal::MultiplyType<P1, P2>::type> >(p * i.val);
+       }
+
+       template<class P1, class P2> Operator<Internal::Identity<typename 
Internal::MultiplyType<P1, P2>::type> > operator*(const 
Operator<Internal::Identity<P2> >& i, const P1&p)
+       {
+               return Operator<Internal::Identity<typename 
Internal::MultiplyType<P1, P2>::type> >(p * i.val);
+       }
+
+       template<class P1, class P2> Operator<Internal::SizedIdentity<typename 
Internal::MultiplyType<P1, P2>::type> > operator*(const P1& p, const 
Operator<Internal::SizedIdentity<P2> >& i)
+       {
+               return Operator<Internal::SizedIdentity<typename 
Internal::MultiplyType<P1, P2>::type> >(i.my_size, p * i.val);
+       }
+
+       template<class P1, class P2> Operator<Internal::SizedIdentity<typename 
Internal::MultiplyType<P1, P2>::type> > operator*(const 
Operator<Internal::SizedIdentity<P2> >& i, const P1&p)
+       {
+               return Operator<Internal::SizedIdentity<typename 
Internal::MultiplyType<P1, P2>::type> >(i.my_size, p * i.val);
+       }
 
        static Operator<Internal::Zero> Zero;
-       static Operator<Internal::Identity> Identity;
+       static Operator<Internal::Identity<double> > Identity;
 
        /// row sum norm of input matrix m
        /// computes the maximum of the sums of absolute values over rows

Index: test/identity_test.cc
===================================================================
RCS file: test/identity_test.cc
diff -N test/identity_test.cc
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ test/identity_test.cc       16 Apr 2009 17:33:55 -0000      1.1
@@ -0,0 +1,24 @@
+#include <TooN/helpers.h>
+
+using namespace TooN;
+using namespace std;
+
+int main()
+{
+       Matrix<3> m = Identity;
+
+       cout << m << endl;
+
+       Matrix<3> n = 2 * Identity;
+
+       cout << n << endl;
+
+       n = Identity * 3;
+
+       cout << n << endl;
+
+
+       Matrix<> q = 5.5 * Identity(6) * 2;
+
+       cout << q << endl;
+}




reply via email to

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