toon-members
[Top][All Lists]
Advanced

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

[Toon-members] TooN/internal objects.h


From: Edward Rosten
Subject: [Toon-members] TooN/internal objects.h
Date: Mon, 15 Jun 2009 19:37:05 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/06/15 19:37:05

Modified files:
        internal       : objects.h 

Log message:
        Special new "One" type which represents the value 1 with no data. This 
is
        used for global objects, so that their constructors can be trivial.
        
        Special hacking of Field required. One is not a field by itself, so 
        IsField<One>::value is 0, but it can be combined with a field 
        arithmetically and the result is a field, so Field<One,T> and 
Field<T,One>
        is defined.
        
        One can cast itself to any type, and be added, subtracted or multiplied.
        
        Also, special cases for negation are required since -One is not 
representable
        using One.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/objects.h?cvsroot=toon&r1=1.18&r2=1.19

Patches:
Index: objects.h
===================================================================
RCS file: /cvsroot/toon/TooN/internal/objects.h,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -b -r1.18 -r1.19
--- objects.h   15 Jun 2009 16:25:33 -0000      1.18
+++ objects.h   15 Jun 2009 19:37:05 -0000      1.19
@@ -44,6 +44,47 @@
        template<class P> class Scalars;        
        template<class P> class SizedScalars;   
        template<class P> class RCScalars;
+
+       struct One{
+               //Generic cast to anything
+               template<class C> operator C() const
+               {
+                       return 1;
+               }
+       };
+       template<class Rhs> Rhs operator*(One, const Rhs& v){return v;}
+       template<class Lhs> Lhs operator*(const Lhs& v, One){return v;}
+       template<class Rhs> Rhs operator+(One, const Rhs& v){return 1+v;}
+       template<class Lhs> Lhs operator+(const Lhs& v, One){return v+1;}
+       template<class Rhs> Rhs operator-(One, const Rhs& v){return 1-v;}
+       template<class Lhs> Lhs operator-(const Lhs& v, One){return v-1;}
+       int operator-(const One&)
+       {
+               return -1;
+       }
+
+       template<class C> struct NegType
+       {
+               typedef C Type;
+       };
+
+       template<> struct NegType<One>
+       {
+               typedef int Type;
+       };
+
+       //One can be converted in to anything, so the resulting type is
+       //a field if the other type is a field.
+       template<class Rhs> struct Field<One, Rhs>
+       {
+               static const int is = IsField<Rhs>::value;
+       };
+
+       template<class Lhs> struct Field<Lhs, One>
+       {
+               static const int is = IsField<Lhs>::value;
+       };
+
 }
 
 ////////////////////
@@ -153,7 +194,7 @@
                                        mm[r][c] = m[r][c];
 
                for(int i=0; i < m.num_rows(); i++)
-                               mm[i][i] += s;
+                               mm[i][i] += (P)s;
        }
 
        int num_rows() const
@@ -167,13 +208,13 @@
        ///@}
 };
 
-///Object whioch behaves like an Identity matrix. See TooN::Identity.
+///Object which behaves like an Identity matrix. See TooN::Identity.
 ///@ingroup gInternal
 template<class Pr> struct Operator<Internal::Identity<Pr> > {
        
        typedef Pr Precision;
        const Precision val;
-       Operator(const Precision& v=1)
+       Operator(const Precision& v)
                :val(v)
        {}
        ///@name Operator members
@@ -190,7 +231,7 @@
                }
                
                for(int r=0; r < m.num_rows(); r++) {
-                       m(r,r) = val;
+                       m(r,r) = (P)val;
                }
        }
        
@@ -199,7 +240,7 @@
        {
                SizeMismatch<Rows, Cols>::test(m.num_rows(), m.num_cols());
                for(int i=0; i < m.num_rows(); i++)
-                       m[i][i] += val;
+                       m[i][i] += (P)val;
        }
 
        template <int Rows, int Cols, typename P1, typename B1> 
@@ -231,7 +272,7 @@
        ///@}
 
        Operator<Internal::SizedIdentity<Precision> > operator()(int s){
-               return Operator<Internal::SizedIdentity<Precision> >(s);
+               return Operator<Internal::SizedIdentity<Precision> >(s, val);
        }
 };
        
@@ -244,7 +285,7 @@
        using Operator<Internal::Identity<Precision> >::val;
        const int my_size;
 
-       Operator(int s, const Precision& v=1)
+       Operator(int s, const Precision& v)
                :Operator<Internal::Identity<Precision> > (v), my_size(s)
        {}
 
@@ -334,7 +375,7 @@
        const Precision s;
        //Default argument in constructor, otherwise Doxygen mis-parses
        //a static object with a constructor as a function.
-       Operator(Precision s_=1)
+       Operator(Precision s_)
                :s(s_){}
 
        ////////////////////////////////////////
@@ -348,21 +389,21 @@
        void eval(Vector<Size, P1, B1>& v) const
        {
                for(int i=0; i < v.size(); i++)
-                       v[i] = s;
+                       v[i] = (P1)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;
+                       v[i] += (P1)s;
        }
 
        template <int Size, typename P1, typename B1>
        void minusequals(Vector<Size, P1, B1>& v) const
        {
                for(int i=0; i < v.size(); ++i)
-                       v[i] -= s;
+                       v[i] -= (P1)s;
        }
 
        template <int Size, typename P1, typename B1> 
@@ -401,7 +442,7 @@
        {
                for(int r=0; r < m.num_rows(); r++)
                        for(int c=0; c < m.num_cols(); c++)
-                               m[r][c] += s;
+                               m[r][c] += (P1)s;
        }
 
        template <int Rows, int Cols, typename P1, typename B1> 
@@ -409,7 +450,7 @@
        {
                for(int r=0; r < m.num_rows(); r++)
                        for(int c=0; c < m.num_cols(); c++)
-                               m[r][c] -= s;
+                               m[r][c] -= (P1)s;
        }
 
        template <int Rows, int Cols, typename P1, typename B1> 
@@ -420,9 +461,9 @@
 
 
        template <int Rows, int Cols, typename P1, typename B1> 
-       Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> > 
rsubtract(const Matrix<Rows,Cols, P1, B1>& v) const
+       Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,typename 
Internal::NegType<P>::Type> > rsubtract(const Matrix<Rows,Cols, P1, B1>& v) 
const
        {
-               return 
Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,Precision> >(-s, v, 0);
+               return 
Operator<Internal::ScalarsMatrix<Rows,Cols,P1,B1,typename 
Internal::NegType<P>::Type > >(-s, v, 0);
        }
 
        template <int Rows, int Cols, typename P1, typename B1> 
@@ -529,7 +570,7 @@
 Operator<Op<typename Internal::MultiplyType<Pl, Pr>::type > >
 operator*(const Operator<Op<Pl> >& l, const Pr&  r)
 {
-       return l.template scale_me<typename Internal::MultiplyType<Pl, 
Pr>::type, Pl>(r); 
+       return l.template scale_me<typename Internal::MultiplyType<Pl, 
Pr>::type>(r); 
 }
 
 template<template<class> class Op, class Pl, class Pr> 
@@ -546,6 +587,12 @@
        return o.template scale_me<typename Operator<Op>::Precision>(-1);
 }
 
+//Special case for negating One
+template<template<class>class Op>
+Operator<Op<DefaultPrecision> > operator-(const Operator<Op<Internal::One> >& 
o)
+{
+       return o.template scale_me<DefaultPrecision>(-1);
+}
 
 /**This function is used to add a scalar to every element of a vector or
    matrix. For example:
@@ -566,7 +613,7 @@
    @endcode
    @ingroup gLinAlg
 */
-static const Operator<Internal::Scalars<DefaultPrecision> > Ones;
+static const Operator<Internal::Scalars<Internal::One> > Ones = 
Internal::One();
 
 
 /**This function is used to initialize vectors and matrices to zero.
@@ -601,6 +648,6 @@
    @ingroup gLinAlg
 */
 
-static Operator<Internal::Identity<DefaultPrecision> > Identity;
+static Operator<Internal::Identity<Internal::One> > Identity = Internal::One();
 
 }




reply via email to

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