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: Thu, 30 Apr 2009 12:43:03 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/04/30 12:43:03

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

Log message:
        Fixed precision bug in operator/ of scalable operator
        
        Identity now works as a scalable operator.

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

Patches:
Index: internal/objects.h
===================================================================
RCS file: /cvsroot/toon/TooN/internal/objects.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- internal/objects.h  30 Apr 2009 12:24:28 -0000      1.5
+++ internal/objects.h  30 Apr 2009 12:43:03 -0000      1.6
@@ -40,6 +40,7 @@
 
        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<int R, int C, class P, class B, class Ps> class AddIdentity;   
        template<class P> class Scalars;        
        template<class P> class SizedScalars;   
        template<class P> class RCScalars;
@@ -110,6 +111,32 @@
 // Identity
 //////////////
 
+//Operator to construct a new matrix with idendity added 
+template<int R, int C, class P, class B, class Precision> struct 
Operator<Internal::AddIdentity<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] = m[r][c];
+               for(int i=0; i < m.num_rows(); i++)
+                               mm[i][i] += s;
+       }
+
+       int num_rows() const
+       {
+               return m.num_rows();
+       }
+       int num_cols() const
+       {
+               return m.num_cols();
+       }
+};
+
 
 template<class Precision> struct Operator<Internal::Identity<Precision> >;
 
@@ -132,8 +159,9 @@
        }
 };
 
-template<class Precision> struct Operator<Internal::Identity<Precision> > {
+template<class Pr> struct Operator<Internal::Identity<Pr> > {
 
+       typedef Pr Precision;
        const Precision val;
        Operator(const Precision& v=1)
                :val(v)
@@ -154,6 +182,20 @@
                }
        }
 
+       template <int Rows, int Cols, typename P1, typename B1> 
+       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);
+       }
+
+       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
+       {
+               SizeMismatch<Rows, Cols>::test(m.num_rows(), m.num_cols());
+               return 
Operator<Internal::AddIdentity<Rows,Cols,P1,B1,Precision> >(-val, m);
+       }
+
        Operator<Internal::SizedIdentity<Precision> > operator()(int s){
                return Operator<Internal::SizedIdentity<Precision> >(s);
        }
@@ -247,6 +289,11 @@
                return Operator<Internal::ScalarsVector<Size,P1,B1,Precision> 
>(s, v);
        }
 
+       template <int Size, typename P1, typename B1> 
+       Operator<Internal::ScalarsVector<Size,P1,B1,Precision> > subtract(const 
Vector<Size, P1, B1>& v) const
+       {
+               return Operator<Internal::ScalarsVector<Size,P1,B1,Precision> 
>(-s, v);
+       }
 
        ////////////////////////////////////////
        //
@@ -275,6 +322,12 @@
                return 
Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> >(s, v);
        }
 
+
+       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
+       {
+               return 
Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> >(-s, v);
+       }
        ////////////////////////////////////////
        //
        // Create sized versions for initialization
@@ -365,10 +418,10 @@
 }
 
 template<template<class> class Op, class Pl, class Pr> 
-Operator<Op<typename Internal::MultiplyType<Pl, Pr>::type > >
+Operator<Op<typename Internal::DivideType<Pl, Pr>::type > >
 operator/(const Operator<Op<Pl> >& l, const Pr&  r)
 {
-       return l.template scale_me<typename Internal::MultiplyType<Pl, 
Pr>::type, Pl>(1/r); 
+       return l.template scale_me<typename Internal::MultiplyType<Pl, 
Pr>::type, Pl>(static_cast<typename Internal::DivideType<Pl,Pr>::type>(1)/r); 
 }
 /**This function us used to add a scalar to every element of a vector or
    matrix. For example:

Index: internal/operators.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/operators.hh,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -b -r1.39 -r1.40
--- internal/operators.hh       29 Apr 2009 22:03:31 -0000      1.39
+++ internal/operators.hh       30 Apr 2009 12:43:03 -0000      1.40
@@ -611,6 +611,29 @@
 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);
 }
+
+
+
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
 
////////////////////////////////////////////////////////////////////////////////
 //
 // Stream I/O operators

Index: test/identity_test.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/identity_test.cc,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- test/identity_test.cc       16 Apr 2009 17:33:55 -0000      1.1
+++ test/identity_test.cc       30 Apr 2009 12:43:03 -0000      1.2
@@ -21,4 +21,5 @@
        Matrix<> q = 5.5 * Identity(6) * 2;
 
        cout << q << endl;
+       cout << q - 3*Identity/4 << endl;
 }

Index: test/scalars.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/scalars.cc,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- test/scalars.cc     26 Apr 2009 17:21:52 -0000      1.3
+++ test/scalars.cc     30 Apr 2009 12:43:03 -0000      1.4
@@ -19,7 +19,7 @@
 
        Matrix<> m = 3*Ones(4,4) * 2;
        cout << m << endl;
-       cout << m.slice<0,0,2,3>() + 2*Ones << endl;
+       cout << m.slice<0,0,2,3>() - 2*Ones << endl;
 
        m+= Ones;
        cout << m << endl;




reply via email to

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