internal/diagmatrix.h

00001 //-*- c++ -*-
00002 //
00003 // Copyright (C) 2009 Tom Drummond (twd20@cam.ac.uk),
00004 // Ed Rosten (er258@cam.ac.uk)
00005 //
00006 // This file is part of the TooN Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 
00032 
00033 namespace TooN {
00034 
00035 
00055 template<int Size=Dynamic, typename Precision=DefaultPrecision, typename Base=Internal::VBase>
00056 struct DiagonalMatrix {
00057 public:
00058     inline DiagonalMatrix() {}
00059     inline DiagonalMatrix(int size_in) : my_vector(size_in) {}
00060     inline DiagonalMatrix(Precision* data) : my_vector(data) {}
00061     inline DiagonalMatrix(Precision* data, int size_in) : my_vector(data,size_in) {}
00062     inline DiagonalMatrix(Precision* data_in, int size_in, int stride_in, Internal::Slicing)
00063         : my_vector(data_in, size_in, stride_in, Internal::Slicing() ) {}
00064 
00065 
00066     // constructors to allow return value optimisations
00067     // construction from 0-ary operator
00068     template <class Op>
00069     inline DiagonalMatrix(const Operator<Op>& op)
00070         : my_vector (op)
00071     {
00072         op.eval(my_vector);
00073     }
00074 
00075     // constructor from arbitrary vector
00076     template<int Size2, typename Precision2, typename Base2>
00077     inline DiagonalMatrix(const Vector<Size2,Precision2,Base2>& from)
00078         : my_vector(from.size())
00079     {
00080         my_vector=from;
00081     }
00082 
00083 
00084     Precision& operator[](int i){return my_vector[i];}
00085     const Precision& operator[](int i) const {return my_vector[i];}
00086 
00087     typename Vector<Size, Precision, Base>::as_slice_type diagonal_slice() {
00088         return my_vector.as_slice();
00089     }
00090 
00091 
00092     Vector<Size,Precision,Base> my_vector;
00093 };
00094 
00095 
00096 template<int S1, typename P1, typename B1, int S2, typename P2, typename B2>
00097 inline Vector<Internal::Sizer<S1,S2>::size, typename Internal::MultiplyType<P1,P2>::type>
00098 operator*(const DiagonalMatrix<S1,P1,B1>& d, const Vector<S2,P2,B2>& v){
00099     return diagmult(d.my_vector,v);
00100 }
00101 
00102 template<int S1, typename P1, typename B1, int S2, typename P2, typename B2>
00103 inline Vector<Internal::Sizer<S1,S2>::size, typename Internal::MultiplyType<P1,P2>::type>
00104 operator*( const Vector<S1,P1,B1>& v, const DiagonalMatrix<S2,P2,B2>& d){
00105     return diagmult(v,d.my_vector);
00106 }
00107 
00108 // perhaps not the safest way to do this as we're returning the same operator used to normally make vectors
00109 template<int S1, typename P1, typename B1, int S2, typename P2, typename B2>
00110 inline DiagonalMatrix<Internal::Sizer<S1,S2>::size, typename Internal::MultiplyType<P1,P2>::type>
00111 operator*( const DiagonalMatrix<S1,P1,B1>& d1, const DiagonalMatrix<S2,P2,B2>& d2){
00112     SizeMismatch<S1,S2>::test(d1.my_vector.size(),d2.my_vector.size());
00113     return Operator<Internal::VPairwise<Internal::Multiply,S1,P1,B1,S2,P2,B2> >(d1.my_vector,d2.my_vector);
00114 }
00115 
00116 template<int R, int C, int Size, typename P1, typename P2, typename B1, typename B2>
00117 Matrix<R, C, typename Internal::MultiplyType<P1,P2>::type>
00118 operator* (const Matrix<R, C, P1, B1>& m, const DiagonalMatrix<Size, P2, B2>& d){
00119     return diagmult(m,d.my_vector);
00120 }
00121 
00122 template<int R, int C, typename P1, typename B1, int Size, typename P2, typename B2> 
00123 Matrix<R, C, typename Internal::MultiplyType<P1,P2>::type>
00124 operator* (const DiagonalMatrix<Size,P1,B1>& d, const Matrix<R,C,P2,B2>& m)
00125 {
00126     return diagmult(d.my_vector, m);
00127 }
00128 
00129 }

Generated on Thu May 7 20:28:40 2009 for TooN by  doxygen 1.5.3