namespace ewalena  0.2.15
ewalena is not an acronym
include/ewalena/lac/vector_basis.h
Go to the documentation of this file.
00001 // -------------------------------------------------------------------
00002 // @author Toby D. Young
00003 // @version $Id: vector_basis.h 987 2012-11-26 15:16:27Z oneliefleft $
00004 //
00005 // Copyright 2012 namespace ewalena authors. All rights reserved.
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions
00009 // are met:
00010 //
00011 // 1. Redistributions of source code must retain the above copyright
00012 //    notice, this list of conditions and the following disclaimer.
00013 //
00014 // 2. Redistributions in binary form must reproduce the above
00015 //    copyright notice, this list of conditions and the following
00016 //    disclaimer in the documentation and/or other materials provided
00017 //    with the distribution.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE NAMEPSACE EWALENA AUTHORS ``AS
00020 // IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023 // NAMESPACE EWALENA AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00024 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00026 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00027 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00028 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00030 // OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // The views and conclusions contained in the software and
00033 // documentation are those of the authors and should not be
00034 // interpreted as representing official policies, either expressed or
00035 // implied, of the namespace ewalena authors.
00036 // -------------------------------------------------------------------
00037 
00038 #include <cassert>
00039 #include <cstring>
00040 #include <iostream>
00041 
00042 #ifndef __ewalena_vector_basis_h
00043 #define __ewalena_vector_basis_h
00044 
00045 #ifdef EWALENA_HAVE_CONFIG_H
00046 #include <ewalena/base/config.h>
00047 #endif
00048 
00049 
00050 namespace ewalena
00051 {
00052   
00066   template <typename ValueType = double>
00067     class VectorBasis
00068     {      
00069     public:
00070     
00074     VectorBasis ();
00075     
00079     ~VectorBasis ();
00080     
00087     explicit VectorBasis (const unsigned int n_vecs, 
00088                           const unsigned int m,
00089                           const bool         zero = true);
00090     
00095     VectorBasis (const VectorBasis &V);
00096     
00101     unsigned int n_rows () const;
00102     
00108     unsigned int size () const;
00109     
00113     void reinit (const unsigned int m,
00114                  const unsigned int n,
00115                  const bool         fast = false);
00116     
00121     void reinit ();
00122     
00127     ValueType& operator () (const unsigned int i, 
00128                             const unsigned int j);
00129     
00134     const ValueType& operator () (const unsigned int i, 
00135                                   const unsigned int j) const;
00136     
00142     void make_orthonormal_basis ();
00143     
00148     void operator += (const VectorBasis<ValueType> &v);
00149     
00154     void operator -= (const VectorBasis<ValueType> &v);
00155     
00160     bool operator == (const VectorBasis<ValueType> &v) const;
00161     
00166     inline
00167     const 
00168     ValueType* operator* () const
00169     {
00170       return (*this).data;
00171     }
00172     
00177     inline
00178     ValueType* operator* () 
00179     {
00180       return (*this).data;
00181     }
00182     
00183     private:
00184     
00189     unsigned int n_vecs;
00190     
00195     unsigned int __n_rows;
00196     
00201     ValueType *data;
00202     
00203     }; /* VectorBasis */
00204   
00205   /*-------------- Inline and Other Functions -----------------------*/
00206   
00207   template <typename ValueType>
00208     inline 
00209     const ValueType& 
00210     VectorBasis<ValueType>::operator () (const unsigned int i, 
00211                                          const unsigned int j) const
00212     {
00213       assert (i<__n_rows);
00214       assert (j<n_vecs);
00215       
00216       return data[__n_rows*i+j];
00217     }
00218   
00219   template <typename ValueType>
00220     inline 
00221     ValueType& 
00222     VectorBasis<ValueType>::operator () (const unsigned int i, 
00223                                          const unsigned int j) 
00224     {
00225       assert (i<__n_rows);
00226       assert (j<n_vecs);
00227       
00228       return data[__n_rows*i+j];
00229     }
00230   
00231   template <typename ValueType>
00232     inline 
00233     void
00234     VectorBasis<ValueType>::operator += (const VectorBasis<ValueType> &v) 
00235     {
00236       assert (v.__n_rows == __n_rows);
00237       assert (v.n_vecs == n_vecs);
00238       
00239       for (unsigned int i=0; i< __n_rows*n_vecs; ++i)
00240         data[i] += v.data[i];
00241     }
00242 
00243   template <typename ValueType>
00244     inline
00245     unsigned int
00246     VectorBasis<ValueType>::n_rows () const
00247     {
00248       return __n_rows;
00249     }
00250   
00251   template <typename ValueType>
00252     inline
00253     unsigned int
00254     VectorBasis<ValueType>::size () const
00255     {
00256       return n_vecs;
00257     }
00258 
00259   template <typename ValueType>
00260     inline 
00261     void
00262     VectorBasis<ValueType>::operator -= (const VectorBasis<ValueType> &v) 
00263     {
00264       assert (v.__n_rows == __n_rows);
00265       assert (v.n_vecs == n_vecs);
00266 
00267       for (unsigned int i=0; i< __n_rows*n_vecs; ++i)
00268         data[i] -= v.data[i];
00269     }
00270 
00271   template <typename ValueType>
00272     inline 
00273     bool
00274     VectorBasis<ValueType>::operator == (const VectorBasis<ValueType> &V) const
00275     {
00276       assert (V.__n_rows == __n_rows);
00277       assert (V.n_vecs == n_vecs);
00278 
00279       return static_cast<bool> (!std::memcmp (this->data, V.data, sizeof (ValueType)*(__n_rows*n_vecs)));
00280     }
00281 
00282   template <typename ValueType>
00283     inline 
00284     void
00285     VectorBasis<ValueType>::make_orthonormal_basis ()
00286     {
00287       /* An identity vector basis is always a square vector basis. */
00288       assert (__n_rows==n_vecs);
00289       reinit ();
00290 
00291       /* If the vector basis is zero size - there is nothing to do. */
00292       if (__n_rows==0) 
00293         return;
00294 
00295 
00296       /* This is a cyclic counter that runs one past the length of a
00297          column; ie. where 1 appears cyclicly in an identity vector
00298          basis. */
00299       unsigned int j = __n_rows+1;
00300     
00301       for (unsigned int i=0; i<__n_rows*n_vecs; ++i, ++j)
00302         {
00303           if (j==__n_rows+1) 
00304             {
00305               data[i] = 1.;
00306               j=0;
00307             }
00308         }
00309     }
00310 
00311 } /* namespace ewalena */
00312   
00313 #endif /* __ewalena_vector_basis_h */
00314   
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines