Scythe-1.0.3
|
00001 /* 00002 * Scythe Statistical Library 00003 * Copyright (C) 2000-2002 Andrew D. Martin and Kevin M. Quinn; 00004 * 2002-present Andrew D. Martin, Kevin M. Quinn, and Daniel 00005 * Pemstein. All Rights Reserved. 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * under the terms of the GNU General Public License as published by 00009 * Free Software Foundation; either version 2 of the License, or (at 00010 * your option) any later version. See the text files COPYING 00011 * and LICENSE, distributed with this source code, for further 00012 * information. 00013 * -------------------------------------------------------------------- 00014 * scythestat/algorithm.h 00015 */ 00016 00031 #ifndef SCYTHE_ALGORITHM_H 00032 #define SCYTHE_ALGORITHM_H 00033 00034 #include <cmath> 00035 #include <functional> 00036 #include <algorithm> 00037 00038 #ifdef SCYTHE_COMPILE_DIRECT 00039 #include "defs.h" 00040 #include "matrix.h" 00041 #include "matrix_random_access_iterator.h" 00042 #else 00043 #include "scythestat/defs.h" 00044 #include "scythestat/matrix.h" 00045 #include "scythestat/matrix_random_access_iterator.h" 00046 #endif 00047 00048 // These are just goofy 00049 00050 #ifdef SCYTHE_RPACK 00051 #undef DO 00052 #undef DS 00053 #undef SO 00054 #undef SS 00055 #endif 00056 00057 namespace scythe { 00058 namespace { 00059 typedef unsigned int uint; 00060 } 00061 00062 /* Matrix forward declaration */ 00063 template <typename T_type, matrix_order ORDER, matrix_style STYLE> 00064 class Matrix; 00065 00071 template <typename T> 00072 struct exponentiate : std::binary_function<T, T, T> 00073 { 00074 T operator() (T base, T exp) const 00075 { 00076 return std::pow(base, exp); 00077 } 00078 }; 00079 00085 template <typename T> 00086 struct ax_plus_b : std::binary_function<T,T,T> 00087 { 00088 T a_; 00089 ax_plus_b (T a) : a_ (a) {} 00090 T operator() (T x, T b) const 00091 { 00092 return (a_ * x + b); 00093 } 00094 }; 00095 00110 template <typename T, matrix_order O, matrix_style S, class FUNCTOR> 00111 void 00112 for_each_ij_set (Matrix<T,O,S>& M, FUNCTOR func) 00113 { 00114 if (O == Col) { 00115 for (uint j = 0; j < M.cols(); ++j) 00116 for (uint i = 0; i < M.rows(); ++i) 00117 M(i, j) = func(i, j); 00118 } else { 00119 for (uint i = 0; i < M.cols(); ++i) 00120 for (uint j = 0; j < M.rows(); ++j) 00121 M(i, j) = func(i, j); 00122 } 00123 } 00124 00136 template <matrix_order ORDER1, matrix_order ORDER2, 00137 typename T, typename S, matrix_order SO, matrix_style SS, 00138 matrix_order DO, matrix_style DS> 00139 void 00140 copy(const Matrix<T,SO,SS>& source, Matrix<S,DO,DS>& dest) 00141 { 00142 std::copy(source.template begin_f<ORDER1>(), 00143 source.template end_f<ORDER1>(), 00144 dest.template begin_f<ORDER2>()); 00145 } 00146 00162 template <matrix_order ORDER1, matrix_order ORDER2, 00163 typename T, matrix_order SO, matrix_style SS, 00164 matrix_order DO, matrix_style DS> 00165 void 00166 copy_recycle (const Matrix<T,SO,SS>& source, Matrix<T,DO,DS>& dest) 00167 { 00168 if (source.size() == dest.size()) { 00169 copy<ORDER1,ORDER2> (source, dest); 00170 } else if (source.size() > dest.size()) { 00171 const_matrix_random_access_iterator<T,ORDER1,SO,SS> s_iter 00172 = source.template begin<ORDER1>(); 00173 std::copy(s_iter, s_iter + dest.size(), 00174 dest.template begin_f<ORDER2>()); 00175 } else { 00176 const_matrix_random_access_iterator<T,ORDER1,SO,SS> s_begin 00177 = source.template begin<ORDER1> (); 00178 matrix_random_access_iterator<T,ORDER2,DO,DS> d_iter 00179 = dest.template begin<ORDER2>(); 00180 matrix_random_access_iterator<T,ORDER2,DO,DS> d_end 00181 = dest.template end<ORDER2>(); 00182 while (d_iter != d_end) { 00183 unsigned int span = std::min(source.size(), 00184 (unsigned int) (d_end - d_iter)); 00185 d_iter = std::copy(s_begin, s_begin + span, d_iter); 00186 } 00187 } 00188 } 00189 00198 template <class T> 00199 inline T sgn (const T & x) 00200 { 00201 if (x > (T) 0) 00202 return (T) 1; 00203 else if (x < (T) 0) 00204 return (T) -1; 00205 else 00206 return (T) 0; 00207 } 00208 00209 } // end namespace scythe 00210 00211 #endif /* SCYTHE_ALGORITHM_H */