toon-members
[Top][All Lists]
Advanced

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

[Toon-members] TooN internal/objects.h internal/operators.hh t...


From: Edward Rosten
Subject: [Toon-members] TooN internal/objects.h internal/operators.hh t...
Date: Tue, 05 May 2009 16:26:38 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/05/05 16:26:38

Modified files:
        internal       : objects.h operators.hh 
        test           : identity_test.cc scalars.cc 

Log message:
        Fix silly bug where Object - Matrix gives the wrong answer.
        
        The code had one subtract function, implicitly assuming A-B == B-A.
        
        Operator has to provide rsubtract and lsubtract debending on the side
        (left or right) that the operator is on.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/objects.h?cvsroot=toon&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/operators.hh?cvsroot=toon&r1=1.40&r2=1.41
http://cvs.savannah.gnu.org/viewcvs/TooN/test/identity_test.cc?cvsroot=toon&r1=1.2&r2=1.3
http://cvs.savannah.gnu.org/viewcvs/TooN/test/scalars.cc?cvsroot=toon&r1=1.4&r2=1.5

Patches:
Index: internal/objects.h
===================================================================
RCS file: /cvsroot/toon/TooN/internal/objects.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- internal/objects.h  1 May 2009 09:47:16 -0000       1.7
+++ internal/objects.h  5 May 2009 16:26:37 -0000       1.8
@@ -116,13 +116,19 @@
 {
        const Precision s;
        const Matrix<R,C,P,B>& m;
-       Operator(Precision s_, const Matrix<R,C,P,B>& m_)
-               :s(s_),m(m_){}
+       bool invert_m;
+
+       Operator(Precision s_, const Matrix<R,C,P,B>& m_, bool b)
+               :s(s_),m(m_),invert_m(b){}
        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++)
+                               if(invert_m)
+                                       mm[r][c] = -m[r][c];
+                               else
                                mm[r][c] = m[r][c];
+
                for(int i=0; i < m.num_rows(); i++)
                                mm[i][i] += s;
        }
@@ -186,16 +192,24 @@
        Operator<Internal::AddIdentity<Rows,Cols,P1,B1,Precision> > add(const 
Matrix<Rows,Cols, P1, B1>& m) const
        {
                SizeMismatch<Rows, Cols>::test(m.num_rows(), m.num_cols());
-               return 
Operator<Internal::AddIdentity<Rows,Cols,P1,B1,Precision> >(val, m);
+               return 
Operator<Internal::AddIdentity<Rows,Cols,P1,B1,Precision> >(val, m, 0);
+       }
+
+       template <int Rows, int Cols, typename P1, typename B1> 
+       Operator<Internal::AddIdentity<Rows,Cols,P1,B1,Precision> > 
rsubtract(const Matrix<Rows,Cols, P1, B1>& m) const
+       {
+               SizeMismatch<Rows, Cols>::test(m.num_rows(), m.num_cols());
+               return 
Operator<Internal::AddIdentity<Rows,Cols,P1,B1,Precision> >(-val, m, 0);
        }
 
        template <int Rows, int Cols, typename P1, typename B1> 
-       Operator<Internal::AddIdentity<Rows,Cols,P1,B1,Precision> > 
subtract(const Matrix<Rows,Cols, P1, B1>& m) const
+       Operator<Internal::AddIdentity<Rows,Cols,P1,B1,Precision> > 
lsubtract(const Matrix<Rows,Cols, P1, B1>& m) const
        {
                SizeMismatch<Rows, Cols>::test(m.num_rows(), m.num_cols());
-               return 
Operator<Internal::AddIdentity<Rows,Cols,P1,B1,Precision> >(-val, m);
+               return 
Operator<Internal::AddIdentity<Rows,Cols,P1,B1,Precision> >(val, m, 1);
        }
 
+
        Operator<Internal::SizedIdentity<Precision> > operator()(int s){
                return Operator<Internal::SizedIdentity<Precision> >(s);
        }
@@ -219,12 +233,16 @@
 {
        const Precision s;
        const Vector<S,P,B>& v;
-       Operator(Precision s_, const Vector<S,P,B>& v_)
-               :s(s_),v(v_){}
+       const bool invert_v;
+       Operator(Precision s_, const Vector<S,P,B>& v_, bool inv)
+               :s(s_),v(v_),invert_v(inv){}
 
        template<int S1, class P1, class B1>
        void eval(Vector<S1,P1,B1>& vv) const{
                for(int i=0; i < v.size(); i++)
+                       if(invert_v)
+                               vv[i] = s - v[i];
+                       else
                        vv[i] = s + v[i];
        }
 
@@ -239,12 +257,16 @@
 {
        const Precision s;
        const Matrix<R,C,P,B>& m;
-       Operator(Precision s_, const Matrix<R,C,P,B>& m_)
-               :s(s_),m(m_){}
+       const bool invert_m;
+       Operator(Precision s_, const Matrix<R,C,P,B>& m_, bool inv)
+               :s(s_),m(m_),invert_m(inv){}
        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++)
+                               if(invert_m)
+                                       mm[r][c] = s - m[r][c];
+                               else
                                mm[r][c] = s + m[r][c];
        }
 
@@ -288,13 +310,19 @@
        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);
+               return Operator<Internal::ScalarsVector<Size,P1,B1,Precision> 
>(s, v, 0);
        }
 
        template <int Size, typename P1, typename B1> 
-       Operator<Internal::ScalarsVector<Size,P1,B1,Precision> > subtract(const 
Vector<Size, P1, B1>& v) const
+       Operator<Internal::ScalarsVector<Size,P1,B1,Precision> > 
rsubtract(const Vector<Size, P1, B1>& v) const
        {
-               return Operator<Internal::ScalarsVector<Size,P1,B1,Precision> 
>(-s, v);
+               return Operator<Internal::ScalarsVector<Size,P1,B1,Precision> 
>(-s, v, 0);
+       }
+
+       template <int Size, typename P1, typename B1> 
+       Operator<Internal::ScalarsVector<Size,P1,B1,Precision> > 
lsubtract(const Vector<Size, P1, B1>& v) const
+       {
+               return Operator<Internal::ScalarsVector<Size,P1,B1,Precision> 
>(s, v, 1);
        }
 
        ////////////////////////////////////////
@@ -329,14 +357,20 @@
        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);
+               return 
Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> >(s, v, 0);
        }
 
 
        template <int Rows, int Cols, typename P1, typename B1> 
-       Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> > 
subtract(const Matrix<Rows,Cols, P1, B1>& v) const
+       Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> > 
rsubtract(const Matrix<Rows,Cols, P1, B1>& v) const
+       {
+               return 
Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> >(-s, v, 0);
+       }
+
+       template <int Rows, int Cols, typename P1, typename B1> 
+       Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> > 
lsubtract(const Matrix<Rows,Cols, P1, B1>& v) const
        {
-               return 
Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> >(-s, v);
+               return 
Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> >(s, v, 1);
        }
        ////////////////////////////////////////
        //

Index: internal/operators.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/operators.hh,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -b -r1.40 -r1.41
--- internal/operators.hh       30 Apr 2009 12:43:03 -0000      1.40
+++ internal/operators.hh       5 May 2009 16:26:38 -0000       1.41
@@ -617,22 +617,22 @@
 
 template <int Size, typename P1, typename B1, typename Op>
 Vector<Size, typename Internal::Subtract::Return<P1,typename 
Operator<Op>::Precision>::Type> operator-(const Vector<Size, P1, B1>& v, const 
Operator<Op>& op){
-       return op.subtract(v);
+       return op.rsubtract(v);
 }
 
 template <int Size, typename P1, typename B1, typename Op>
 Vector<Size, typename Internal::Subtract::Return<typename 
Operator<Op>::Precision, P1>::Type> operator-(const Operator<Op>& op, const 
Vector<Size, P1, B1>& v){
-       return op.subtract(v);
+       return op.lsubtract(v);
 }
 
 template <int Rows, int Cols, typename P1, typename B1, typename Op>
 Matrix<Rows, Cols, typename Internal::Subtract::Return<P1,typename 
Operator<Op>::Precision>::Type> operator-(const Matrix<Rows, Cols, P1, B1>& m, 
const Operator<Op>& op){
-       return op.subtract(m);
+       return op.rsubtract(m);
 }
 
 template <int Rows, int Cols, typename P1, typename B1, typename Op>
 Matrix<Rows, Cols, typename Internal::Subtract::Return<typename 
Operator<Op>::Precision,P1>::Type> operator-(const Operator<Op>& op, const 
Matrix<Rows, Cols, P1, B1>& m){
-       return op.subtract(m);
+       return op.lsubtract(m);
 }
 
////////////////////////////////////////////////////////////////////////////////
 //

Index: test/identity_test.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/identity_test.cc,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- test/identity_test.cc       30 Apr 2009 12:43:03 -0000      1.2
+++ test/identity_test.cc       5 May 2009 16:26:38 -0000       1.3
@@ -22,4 +22,10 @@
 
        cout << q << endl;
        cout << q - 3*Identity/4 << endl;
+
+       Matrix<3> p = Ones * 2;
+
+       cout <<  Identity - p << endl;
+
+
 }

Index: test/scalars.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/scalars.cc,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- test/scalars.cc     30 Apr 2009 12:43:03 -0000      1.4
+++ test/scalars.cc     5 May 2009 16:26:38 -0000       1.5
@@ -25,4 +25,9 @@
        cout << m << endl;
        m.slice<0,0,3,2>() += Ones*2;
        cout << m << endl;
+
+
+       Matrix<3> p = Identity;
+       cout << p - Ones << endl;
+       cout << Ones - p << endl;
 }




reply via email to

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