toon-members
[Top][All Lists]
Advanced

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

[Toon-members] TooN helpers.h doc/documentation.h test/scalars.cc


From: Edward Rosten
Subject: [Toon-members] TooN helpers.h doc/documentation.h test/scalars.cc
Date: Sun, 26 Apr 2009 17:21:52 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/04/26 17:21:52

Modified files:
        .              : helpers.h 
        doc            : documentation.h 
        test           : scalars.cc 

Log message:
        Replaced Scalrs with Ones. Added eval() interface.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/helpers.h?cvsroot=toon&r1=1.55&r2=1.56
http://cvs.savannah.gnu.org/viewcvs/TooN/doc/documentation.h?cvsroot=toon&r1=1.19&r2=1.20
http://cvs.savannah.gnu.org/viewcvs/TooN/test/scalars.cc?cvsroot=toon&r1=1.2&r2=1.3

Patches:
Index: helpers.h
===================================================================
RCS file: /cvsroot/toon/TooN/helpers.h,v
retrieving revision 1.55
retrieving revision 1.56
diff -u -b -r1.55 -r1.56
--- helpers.h   26 Apr 2009 12:07:32 -0000      1.55
+++ helpers.h   26 Apr 2009 17:21:51 -0000      1.56
@@ -391,6 +391,8 @@
                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 P> class SizedScalars;   
+               template<class P> class RCScalars;      
        }
        
        //Operator to construct a new vector a a vector with a scalar added to 
every element
@@ -445,6 +447,18 @@
                Operator(Precision s_)
                :s(s_){}
 
+               ////////////////////////////////////////
+               //
+               // All applications for vector
+               //
+
+               template <int Size, typename P1, typename B1> 
+               void eval(Vector<Size, P1, B1>& v) const
+               {
+                       for(int i=0; i < v.size(); i++)
+                               v[i] = s;
+               }
+
                template <int Size, typename P1, typename B1> 
                void plusequals(Vector<Size, P1, B1>& v) const
                {
@@ -458,6 +472,20 @@
                        return 
Operator<Internal::ScalarsVector<Size,P1,B1,Precision> >(s, v);
                }
 
+
+               ////////////////////////////////////////
+               //
+               // All applications for matrix
+               //
+
+               template <int Rows, int Cols, typename P1, typename B1> 
+               void eval(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> 
                void plusequals(Matrix<Rows,Cols, P1, B1>& m) const
                {
@@ -471,22 +499,109 @@
                {
                        return 
Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> >(s, v);
                }
+
+               ////////////////////////////////////////
+               //
+               // Create sized versions for initialization
+               //
+
+               Operator<Internal::SizedScalars<Precision> > operator()(int 
size) const
+               {
+                       return Operator<Internal::SizedScalars<Precision> > 
(s,size);
+               }
+
+               Operator<Internal::RCScalars<Precision> > operator()(int r, int 
c) const
+               {
+                       return Operator<Internal::RCScalars<Precision> > 
(s,r,c);
+               }
+       };
+
+       template<class P> struct Operator<Internal::SizedScalars<P> >: public 
Operator<Internal::Scalars<P> >
+       {
+               const int my_size;
+               int size() const {
+                       return my_size;
+               }
+               
+               Operator(P s, int sz)
+               :Operator<Internal::Scalars<P> >(s),my_size(sz){}
+               
+               private:
+                       void operator()(int);
+                       void operator()(int,int);
+       };
+
+               
+
+       template<class P> struct Operator<Internal::RCScalars<P> >: public 
Operator<Internal::Scalars<P> >
+       {
+               const int my_rows, my_cols;
+               int num_rows() const {
+                       return my_rows;
+               }
+               int num_cols() const {
+                       return my_cols;
+               }
+               
+               Operator(P s, int r, int c)
+               :Operator<Internal::Scalars<P> >(s),my_rows(r),my_cols(c)
+               {}
+               
+               private:
+                       void operator()(int);
+                       void operator()(int,int);
        };
        
+       template<class Pl, class Pr> 
+       Operator<Internal::Scalars<typename Internal::MultiplyType<Pl, 
Pr>::type > > operator*(const Pl& l, const Operator<Internal::Scalars<Pr> >& r)
+       {
+               return Operator<Internal::Scalars<typename 
Internal::MultiplyType<Pl, Pr>::type > >(l * r.s);
+       }
+
+       
+       template<class Pl, class Pr> 
+       Operator<Internal::Scalars<typename Internal::MultiplyType<Pl, 
Pr>::type > > operator*(const Operator<Internal::Scalars<Pl> >& l, const Pr& r)
+       {
+               return Operator<Internal::Scalars<typename 
Internal::MultiplyType<Pl, Pr>::type > >(l.s * r);
+       }
+
+       
+       template<class Pl, class Pr> 
+       Operator<Internal::SizedScalars<typename Internal::MultiplyType<Pl, 
Pr>::type > > operator*(const Pl& l, const Operator<Internal::SizedScalars<Pr> 
>& r)
+       {
+               return Operator<Internal::SizedScalars<typename 
Internal::MultiplyType<Pl, Pr>::type > >(l * r.s, r.my_size);
+       }
+       
+       template<class Pl, class Pr> 
+       Operator<Internal::SizedScalars<typename Internal::MultiplyType<Pl, 
Pr>::type > > operator*(const Operator<Internal::SizedScalars<Pl> >& l, const 
Pr& r)
+       {
+               return Operator<Internal::SizedScalars<typename 
Internal::MultiplyType<Pl, Pr>::type > >(l.s * r, l.my_size);
+       }
+
+       
+       template<class Pl, class Pr> 
+       Operator<Internal::RCScalars<typename Internal::MultiplyType<Pl, 
Pr>::type > > operator*(const Pl& l, const Operator<Internal::RCScalars<Pr> >& 
r)
+       {
+               return Operator<Internal::RCScalars<typename 
Internal::MultiplyType<Pl, Pr>::type > >(l * r.s, r.my_rows, r.my_cols);
+       }
+       
+       template<class Pl, class Pr> 
+       Operator<Internal::RCScalars<typename Internal::MultiplyType<Pl, 
Pr>::type > > operator*(const Operator<Internal::RCScalars<Pl> >& l, const Pr& 
r)
+       {
+               return Operator<Internal::RCScalars<typename 
Internal::MultiplyType<Pl, Pr>::type > >(l.s * r, l.my_rows, l.my_cols);
+       }
+
+
        /**This function us used to add a scalar to every element of a vector or
        matrix. For example:
        @code
                Vector<> v;
                ...
                ...
-               v += Scalars(3); //Add 3 to every element of v;
+               v += Ones * 3; //Add 3 to every element of v;
        @endcode
        Both + and += are supported on vectors,matrices and slices.
-       @param  s Scalar to add.
        */
-       template<class P> Operator<Internal::Scalars<P> > Scalars(const P& s)
-       {
-               return Operator<Internal::Scalars<P> > (s);
-       }
+       static const Operator<Internal::Scalars<DefaultPrecision> > Ones(1);
 }
 #endif

Index: doc/documentation.h
===================================================================
RCS file: /cvsroot/toon/TooN/doc/documentation.h,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -b -r1.19 -r1.20
--- doc/documentation.h 26 Apr 2009 12:07:32 -0000      1.19
+++ doc/documentation.h 26 Apr 2009 17:21:51 -0000      1.20
@@ -56,6 +56,7 @@
  - \ref sFunctionVector
  - \ref sGenericCode
  - \ref sElemOps
+ - \ref sInitialize
  - \ref sScalars
  - \ref ssExamples
  - \ref sNoResize
@@ -214,17 +215,45 @@
 
                See also \ref sSlices
 
+       \subsection sInitialize How I initialize a vector/matrix?
+
+               Vectors and matrices start off uninitialized (filled with 
random garbage).
+               They can be easily filled with zeros, or ones (see also 
TooN::Ones):
+               @code
+                       Vector<3> v = Zero;
+                       Matrix<3> m = Zero;
+                       Vector<>  v2 = Zero(2); //Note in they dynamic case, 
the size must be specified
+                       Matrix<>  m2 = Zero(2,2); //Note in they dynamic case, 
the size must be specified
+               @endcode
+
+               Vectors can be filled with makeVector:
+               @code
+                       Vector<> v = makeVector(2,3,4,5,6);
+               @endcode
+               
+               Matrices can be initialized to the identity matrix:
+               @code
+                       Matrix<2> m = Idendity;
+               @endcode
+               though note that you need to specify the size in the dynamic 
case.
+
+               They can also be initialized with data from another source. See 
also \ref  sWrap.
+
+
+
+
+
        \subsection sScalars How do I add a scalar to every element of a 
vector/matrix? 
                
                Addition to every element is not an elementary operation in the 
same way
-               as multiplication by a scalar. It is supported throught the 
::Scalars
-               function:
+               as multiplication by a scalar. It is supported throught the 
::Ones
+               object:
                
                @code
                        Vector<3> a, b;
                        ...
-                       b = a + Scalars(3);   // b_i = a_i + 3
-                       a+=Scalars(3);        // a_i <- a_i + 3
+                       b = a + Ones*3;       // b_i = a_i + 3
+                       a+= Ones * 3;         // a_i <- a_i + 3
                @endcode
 
                It is supported the same way on Matrix and slices.

Index: test/scalars.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/scalars.cc,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- test/scalars.cc     26 Apr 2009 11:55:41 -0000      1.2
+++ test/scalars.cc     26 Apr 2009 17:21:52 -0000      1.3
@@ -4,22 +4,25 @@
 using namespace std;
 int main()
 {
-       Vector<5> v = makeVector(1,2,3,4,5);
+       Vector<> v = Ones(5);
 
-       cout << v + Scalars(3) << endl;
-       cout << v.slice(2,3) + Scalars(3) << endl;
+       cout << v << endl;
+
+       cout << v + Ones*3 << endl;
 
-       v+=Scalars(1);
+       cout << v.slice(2,3) + Ones*3 << endl;
+
+       v+= Ones;
        cout << v << endl;
-       v.slice(0,3) += Scalars(3);
+       v.slice(0,3) += Ones * 3;
        cout << v << endl;
 
-       Matrix<> m = Identity(4);
-       cout << m + Scalars(1) << endl;
-       cout << m.slice<0,0,2,3>() + Scalars(2) << endl;
+       Matrix<> m = 3*Ones(4,4) * 2;
+       cout << m << endl;
+       cout << m.slice<0,0,2,3>() + 2*Ones << endl;
 
-       m+=Scalars(1);
+       m+= Ones;
        cout << m << endl;
-       m.slice<0,0,3,2>() += Scalars(2);
+       m.slice<0,0,3,2>() += Ones*2;
        cout << m << endl;
 }




reply via email to

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