C:/programs/SCPPNT/src/include/matalg.h

00001 /*
00002 
00003  Functions that apply function objects to rows or columns of matrices.
00004  
00005  Simple C++ Numerical Toolkit (SCPPNT)
00006  http://www.smallwaters.com/software/cpp/scppnt.html
00007  This release updates original work contributed by 
00008  Brad Hanson (http://www.b-a-h.com/) in 2001.
00009 
00010  */
00011 
00012 #ifndef SCPPNT_MATALG_H
00013 #define SCPPNT_MATALG_H
00014 #ifdef SCPPNT_NO_DIR_PREFIX
00015 #include "scppnt.h"
00016 #else
00017 #include "scppnt/scppnt.h"
00018 #endif
00019 
00020 namespace SCPPNT
00021 {
00022 
00023   // Apply a function to each row of a matrix
00024   template<class M, class FUNC> void apply_rows(M &matrix, FUNC &f)
00025   {
00026     typename M::rows_iterator irows = matrix.begin_rows();
00027 
00028     Subscript ncolumns = matrix.num_columns();
00029 
00030     for (Subscript i = matrix.num_rows(); i--; ++irows)
00031     {
00032       f(ncolumns, *irows);
00033     }
00034   }
00035 
00036   // Apply a function to each column of a matrix
00037   template<class M, class FUNC> void apply_columns(M &matrix, FUNC &f)
00038   {
00039     typename M::columns_iterator icolumns = matrix.begin_columns();
00040 
00041     Subscript nrows = matrix.num_rows();
00042 
00043     for (Subscript i = matrix.num_columns(); i--; ++icolumns)
00044     {
00045       f(nrows, *icolumns);
00046     }
00047   }
00048 
00049   /*
00050    Apply a function to each row of a matrix that returns a single value
00051    (the function is applied over columns of each row). These values are
00052    put into a vector that is returned (the number of elements in the
00053    vector is the number of rows in the matrix).
00054    */
00055   template<class M, class V, class FUNC> V over_columns(M &matrix, FUNC &f)
00056   {
00057     typename M::rows_iterator irows = matrix.begin_rows();
00058     V vec(matrix.num_rows());
00059 
00060     Subscript ncolumns = matrix.num_columns();
00061 
00062     typename V::iterator iv = vec.begin();
00063     for (Subscript i = matrix.num_rows(); i--; ++irows, ++iv)
00064     {
00065       *iv = f(ncolumns, *irows);
00066     }
00067 
00068     return vec;
00069   }
00070 
00071   /*
00072    Apply a function to each column of a matrix that returns a single
00073    value (the function is applied over rows of each column). These
00074    values are put into a vector that is returned (the number of
00075    elements in the vector is the number of columns in the matrix).
00076    */
00077   template<class M, class V, class FUNC> V over_rows(M &matrix, FUNC &f)
00078   {
00079     typename M::columns_iterator icolumns = matrix.begin_columns();
00080     V vec(matrix.num_columns());
00081 
00082     Subscript nrows = matrix.num_rows();
00083 
00084     typename V::iterator iv = vec.begin();
00085     for (Subscript i = matrix.num_columns(); i--; ++icolumns, ++iv)
00086     {
00087       *iv = f(nrows, *icolumns);
00088     }
00089 
00090     return vec;
00091   }
00092 
00093 } // namespace SCPPNT
00094 
00095 #endif // SCPPNT_MATALG_H

Generated on Tue Dec 18 23:34:05 2007 for SCPPNT by  doxygen 1.5.4