toon-members
[Top][All Lists]
Advanced

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

[Toon-members] TooN helpers.h internal/matrix.hh internal/oper...


From: Edward Rosten
Subject: [Toon-members] TooN helpers.h internal/matrix.hh internal/oper...
Date: Sun, 26 Apr 2009 11:55:41 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/04/26 11:55:41

Modified files:
        .              : helpers.h 
        internal       : matrix.hh operators.hh vector.hh 
        test           : scalars.cc 

Log message:
        Operator mechanism now supports generic +, +=.
        
        Scalars() exercises this mechanism. 

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/helpers.h?cvsroot=toon&r1=1.53&r2=1.54
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/matrix.hh?cvsroot=toon&r1=1.30&r2=1.31
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/operators.hh?cvsroot=toon&r1=1.37&r2=1.38
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/vector.hh?cvsroot=toon&r1=1.40&r2=1.41
http://cvs.savannah.gnu.org/viewcvs/TooN/test/scalars.cc?cvsroot=toon&r1=1.1&r2=1.2

Patches:
Index: helpers.h
===================================================================
RCS file: /cvsroot/toon/TooN/helpers.h,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -b -r1.53 -r1.54
--- helpers.h   24 Apr 2009 20:24:01 -0000      1.53
+++ helpers.h   26 Apr 2009 11:55:40 -0000      1.54
@@ -382,6 +382,97 @@
                }
        }
 
+       
////////////////////////////////////////////////////////////////////////////////
+       //
+       // Addition of scalars ro vectors and matrices
+       //
+
+       namespace Internal{
+               template<int S, class P, class B, class Ps> class 
ScalarsVector;        
+               template<int R, int C, class P, class B, class Ps> class 
ScalarsMatrix; 
+               template<class P> class Scalars;        
+       }
+       
+       //Operator to construct a new vector a a vector with a scalar added to 
every element
+       template<int S, class P, class B, class Precision> struct 
Operator<Internal::ScalarsVector<S,P,B,Precision> >
+       {
+               const Precision s;
+               const Vector<S,P,B>& v;
+               Operator(Precision s_, const Vector<S,P,B>& v_)
+               :s(s_),v(v_){}
+
+               template<int S1, class P1, class B1>
+               void eval(Vector<S1,P1,B1>& vv) const{
+                       for(int i=0; i < v.size(); i++)
+                               vv[i] = s + v[i];
+               }
+
+               int size() const
+               {
+                       return v.size();
+               }
+       };
+
+       //Operator to construct a new matrix a a matrix with a scalar added to 
every element
+       template<int R, int C, class P, class B, class Precision> struct 
Operator<Internal::ScalarsMatrix<R,C,P,B,Precision> >
+       {
+               const Precision s;
+               const Matrix<R,C,P,B>& m;
+               Operator(Precision s_, const Matrix<R,C,P,B>& m_)
+               :s(s_),m(m_){}
+               template<int R1, int C1, class P1, class B1>
+               void eval(Matrix<R1,C1,P1,B1>& mm) const{
+                       for(int r=0; r < m.num_rows(); r++)
+                               for(int c=0; c < m.num_cols(); c++)
+                                       mm[r][c] = s + m[r][c];
+               }
+
+               int num_rows() const
+               {
+                       return m.num_rows();
+               }
+               int num_cols() const
+               {
+                       return m.num_cols();
+               }
+       };
+
+       //Generic scalars object. Knows how to be added, knows how to deal with 
+=
+       template<class P> struct Operator<Internal::Scalars<P> >
+       {
+               typedef P Precision;
+               const Precision s;
+               Operator(Precision s_)
+               :s(s_){}
+
+               template <int Size, typename P1, typename B1> 
+               void plusequals(Vector<Size, P1, B1>& v) const
+               {
+                       for(int i=0; i < v.size(); i++)
+                               v[i] += s;
+               }
+
+               template <int Size, typename P1, typename B1> 
+               Operator<Internal::ScalarsVector<Size,P1,B1,Precision> > 
add(const Vector<Size, P1, B1>& v) const
+               {
+                       return 
Operator<Internal::ScalarsVector<Size,P1,B1,Precision> >(s, v);
+               }
+
+               template <int Rows, int Cols, typename P1, typename B1> 
+               void plusequals(Matrix<Rows,Cols, P1, B1>& m) const
+               {
+                       for(int r=0; r < m.num_rows(); r++)
+                               for(int c=0; c < m.num_cols(); c++)
+                                       m[r][c] += s;
+               }
+
+               template <int Rows, int Cols, typename P1, typename B1> 
+               Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> > 
add(const Matrix<Rows,Cols, P1, B1>& v) const
+               {
+                       return 
Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> >(s, v);
+               }
+       };
+
        template<class P> Operator<Internal::Scalars<P> > Scalars(const P& s)
        {
                return Operator<Internal::Scalars<P> > (s);

Index: internal/matrix.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/matrix.hh,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -b -r1.30 -r1.31
--- internal/matrix.hh  24 Apr 2009 20:24:02 -0000      1.30
+++ internal/matrix.hh  26 Apr 2009 11:55:40 -0000      1.31
@@ -155,6 +155,13 @@
            return *this;
        }
 
+       template<class Op>
+       Matrix& operator+=(const Operator<Op>& op)
+       {
+               op.plusequals(*this);
+               return *this;
+       }
+
        template<int Rows2, int Cols2, typename Precision2, typename Base2>
        Matrix& operator-= (const Matrix<Rows2, Cols2, Precision2, Base2>& from)
        {

Index: internal/operators.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/operators.hh,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -b -r1.37 -r1.38
--- internal/operators.hh       26 Apr 2009 11:21:19 -0000      1.37
+++ internal/operators.hh       26 Apr 2009 11:55:40 -0000      1.38
@@ -588,83 +588,26 @@
 
 
////////////////////////////////////////////////////////////////////////////////
 //
-// Addition of scalars
+// Addition of operators
 //
-namespace Internal{
-       template<int S, class P, class B, class Ps> class ScalarsVector;        
-       template<int R, int C, class P, class B, class Ps> class ScalarsMatrix; 
-       template<class P> class Scalars;        
-}
-
-template<class Precision> struct Operator<Internal::Scalars<Precision> >
-{
-       const Precision s;
-       Operator(Precision s_)
-       :s(s_){}
-};
-
-template<int S, class P, class B, class Precision> struct 
Operator<Internal::ScalarsVector<S,P,B,Precision> >
-{
-       const Precision s;
-       const Vector<S,P,B>& v;
-       Operator(Precision s_, const Vector<S,P,B>& v_)
-       :s(s_),v(v_){}
-
-       template<int S1, class P1, class B1>
-       void eval(Vector<S1,P1,B1>& vv) const{
-               for(int i=0; i < v.size(); i++)
-                       vv[i] = s + v[i];
-       }
-
-       int size() const
-       {
-               return v.size();
-       }
-};
-
-template<int R, int C, class P, class B, class Precision> struct 
Operator<Internal::ScalarsMatrix<R,C,P,B,Precision> >
-{
-       const Precision s;
-       const Matrix<R,C,P,B>& m;
-       Operator(Precision s_, const Matrix<R,C,P,B>& m_)
-       :s(s_),m(m_){}
-       template<int R1, int C1, class P1, class B1>
-       void eval(Matrix<R1,C1,P1,B1>& mm) const{
-               for(int r=0; r < m.num_rows(); r++)
-                       for(int c=0; c < m.num_cols(); c++)
-                               mm[r][c] = s + m[r][c];
-       }
-
-       int num_rows() const
-       {
-               return m.num_rows();
-       }
-       int num_cols() const
-       {
-               return m.num_cols();
-       }
-};
-
-// Vectors
-template <int Size, typename P1, typename B1, typename P2>
-Vector<Size, typename Internal::Add::Return<P1,P2>::Type> operator+(const 
Vector<Size, P1, B1>& v, const Operator<Internal::Scalars<P2> >& s){
-       return Operator<Internal::ScalarsVector<Size,P1,B1,P2> >(s.s, v);
+template <int Size, typename P1, typename B1, typename Op>
+Vector<Size, typename Internal::Add::Return<P1,typename 
Operator<Op>::Precision>::Type> operator+(const Vector<Size, P1, B1>& v, const 
Operator<Op>& op){
+       return op.add(v);
 }
 
-template <int Size, typename P1, typename B1, typename P2>
-Vector<Size, typename Internal::Add::Return<P1,P2>::Type> operator+(const 
Operator<Internal::Scalars<P2> >& s, const Vector<Size, P1, B1>& v){
-       return Operator<Internal::ScalarsVector<Size,P1,B1,P2> >(s.s, v);
+template <int Size, typename P1, typename B1, typename Op>
+Vector<Size, typename Internal::Add::Return<typename Operator<Op>::Precision, 
P1>::Type> operator+(const Operator<Op>& op, const Vector<Size, P1, B1>& v){
+       return op.add(v);
 }
 
-//Matrices
-template <int Rows, int Cols, typename P1, typename B1, typename P2>
-Matrix<Rows, Cols, typename Internal::Add::Return<P1,P2>::Type> 
operator+(const Matrix<Rows, Cols, P1, B1>& m, const 
Operator<Internal::Scalars<P2> >& s){
-       return Operator<Internal::ScalarsMatrix<Rows, Cols,P1,B1,P2> >(s.s, m);
+template <int Rows, int Cols, typename P1, typename B1, typename Op>
+Matrix<Rows, Cols, typename Internal::Add::Return<P1,typename 
Operator<Op>::Precision>::Type> operator+(const Matrix<Rows, Cols, P1, B1>& m, 
const Operator<Op>& op){
+       return op.add(m);
 }
 
-template <int Rows, int Cols, typename P1, typename B1, typename P2>
-Matrix<Rows, Cols, typename Internal::Add::Return<P1,P2>::Type> 
operator+(const Operator<Internal::Scalars<P2> >& s, const Matrix<Rows, Cols, 
P1, B1>& m){
-       return Operator<Internal::ScalarsMatrix<Rows, Cols,P1,B1,P2> >(s.s, m);
+template <int Rows, int Cols, typename P1, typename B1, typename Op>
+Matrix<Rows, Cols, typename Internal::Add::Return<typename 
Operator<Op>::Precision,P1>::Type> operator+(const Operator<Op>& op, const 
Matrix<Rows, Cols, P1, B1>& m){
+       return op.add(m);
 }
 
////////////////////////////////////////////////////////////////////////////////
 //

Index: internal/vector.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/vector.hh,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -b -r1.40 -r1.41
--- internal/vector.hh  24 Apr 2009 20:24:02 -0000      1.40
+++ internal/vector.hh  26 Apr 2009 11:55:41 -0000      1.41
@@ -119,6 +119,13 @@
                return *this;
        }
 
+       template<class Op>
+       Vector& operator+=(const Operator<Op>& op)
+       {
+               op.plusequals(*this);
+               return *this;
+       }               
+
        template<int Size2, class Precision2, class Base2>
        Vector& operator-=(const Vector<Size2, Precision2, Base2>& rhs) {
                SizeMismatch<Size,Size2>::test(size(),rhs.size());

Index: test/scalars.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/scalars.cc,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- test/scalars.cc     26 Apr 2009 11:21:22 -0000      1.1
+++ test/scalars.cc     26 Apr 2009 11:55:41 -0000      1.2
@@ -9,8 +9,17 @@
        cout << v + Scalars(3) << endl;
        cout << v.slice(2,3) + Scalars(3) << endl;
 
-       Matrix<> m = Identity(4);
+       v+=Scalars(1);
+       cout << v << endl;
+       v.slice(0,3) += Scalars(3);
+       cout << v << endl;
 
+       Matrix<> m = Identity(4);
        cout << m + Scalars(1) << endl;
        cout << m.slice<0,0,2,3>() + Scalars(2) << endl;
+
+       m+=Scalars(1);
+       cout << m << endl;
+       m.slice<0,0,3,2>() += Scalars(2);
+       cout << m << endl;
 }




reply via email to

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