toon-members
[Top][All Lists]
Advanced

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

[Toon-members] TooN helpers.h


From: Edward Rosten
Subject: [Toon-members] TooN helpers.h
Date: Wed, 06 May 2009 10:38:28 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/05/06 10:38:28

Modified files:
        .              : helpers.h 

Log message:
        Element-wise functions.
        
        These are of the form e_exp(), e_log().

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/helpers.h?cvsroot=toon&r1=1.62&r2=1.63

Patches:
Index: helpers.h
===================================================================
RCS file: /cvsroot/toon/TooN/helpers.h,v
retrieving revision 1.62
retrieving revision 1.63
diff -u -b -r1.62 -r1.63
--- helpers.h   29 Apr 2009 09:47:35 -0000      1.62
+++ helpers.h   6 May 2009 10:38:28 -0000       1.63
@@ -229,5 +229,78 @@
                        tr += m(i,i);
                return tr;
        }
+
+
+       namespace Internal
+       {
+               template<int S, class P, class B, class Func> class VectorUnary;
+               template<int R, int C, class P, class B, class Func> class 
MatrixUnary;
+               template<class P> struct Exp{P apply(const P& v) const { using 
std::exp; return exp(v);}};
+               template<class P> struct Log{P apply(const P& v) const { using 
std::exp; return log(v);}};
+       };
+
+       template<int S, class P, class B, class Func> struct 
Operator<Internal::VectorUnary<S, P, B, Func> >
+       {
+               const Vector<S, P, B>& v;
+               const Func& f;
+               Operator(const Vector<S, P, B>& v_, const Func& f_)
+               :v(v_),f(f_){}
+               
+
+               int size() const { return v.size(); }
+
+               template<int S_, class P_>
+               void eval(Vector<S_, P_>& r) const
+               {
+                       for(int i=0; i < r.size(); i++)
+                               r[i] = f.apply(v[i]);
+               }
+       };
+
+       template<int R, int C, class P, class B, class Func> struct 
Operator<Internal::MatrixUnary<R, C, P, B, Func> >
+       {
+               const Matrix<R, C, P, B>& m;
+               const Func& f;
+               Operator(const Matrix<R, C, P, B>& m_, const Func& f_)
+               :m(m_),f(f_){}
+               
+               int num_rows() const { return m.num_rows(); }
+               int num_cols() const { return m.num_cols(); }
+
+               template<int R_, int C_, class P_>
+               void eval(Matrix<R_, C_, P_>& ret) const
+               {
+                       for(int r=0; r < m.num_rows(); r++)
+                               for(int c=0; c < m.num_cols(); c++)
+                                       ret(r,c) = f.apply(m(r,c));
+               }
+       };
+
+       
+       template<int S, class P, class B>
+       Vector<S, P> e_exp(const Vector<S, P, B>& v)
+       {
+               return Operator<Internal::VectorUnary<S, P, B, Internal::Exp<P> 
> >(v, Internal::Exp<P> ());
+       }
+
+       template<int S, class P, class B>
+       Vector<S, P> e_log(const Vector<S, P, B>& v)
+       {
+               return Operator<Internal::VectorUnary<S, P, B, Internal::Log<P> 
> >(v, Internal::Log<P> ());
+       }
+       
+       template<int R, int C, class P, class B>
+       Matrix<R, C, P> e_exp(const Matrix<R, C, P, B>& m)
+       {
+               return Operator<Internal::MatrixUnary<R, C, P, B, 
Internal::Exp<P> > >(m, Internal::Exp<P> ());
+       }
+
+       template<int R, int C, class P, class B>
+       Matrix<R, C, P> e_log(const Matrix<R, C, P, B>& m)
+       {
+               return Operator<Internal::MatrixUnary<R, C, P, B, 
Internal::Log<P> > >(m, Internal::Log<P> ());
+       }
+
+
 }
 #endif




reply via email to

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