algebramix_doc 0.3
|
00001 00002 /****************************************************************************** 00003 * MODULE : structured_naive.hpp 00004 * DESCRIPTION: naive algorithms for structured matrices 00005 * COPYRIGHT : (C) 2009 Joris van der Hoeven 00006 ******************************************************************************* 00007 * This software falls under the GNU general public license and comes WITHOUT 00008 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details. 00009 * If you don't have this file, write to the Free Software Foundation, Inc., 00010 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00011 ******************************************************************************/ 00012 00013 #ifndef __MMX__STRUCTURED_NAIVE__HPP 00014 #define __MMX__STRUCTURED_NAIVE__HPP 00015 #include <algebramix/matrix_naive.hpp> 00016 00017 namespace mmx { 00018 00019 /****************************************************************************** 00020 * Supported matrix types 00021 ******************************************************************************/ 00022 00023 struct full_matrix { 00024 typedef full_matrix ul; 00025 typedef full_matrix ur; 00026 typedef full_matrix ll; 00027 typedef full_matrix lr; 00028 static inline nat first_row (nat c, nat rs) { 00029 (void) c; (void) rs; return 0; } 00030 static inline nat first_col (nat r, nat cs) { 00031 (void) r; (void) cs; return 0; } 00032 static inline nat last_row (nat c, nat rs) { 00033 (void) c; return rs; } 00034 static inline nat last_col (nat r, nat cs) { 00035 (void) r; return cs; } 00036 }; 00037 00038 struct zero_matrix { 00039 typedef zero_matrix ul; 00040 typedef zero_matrix ur; 00041 typedef zero_matrix ll; 00042 typedef zero_matrix lr; 00043 static inline nat first_row (nat c, nat rs) { 00044 (void) c; return rs; } 00045 static inline nat first_col (nat r, nat cs) { 00046 (void) r; return cs; } 00047 static inline nat last_row (nat c, nat rs) { 00048 (void) c; return rs; } 00049 static inline nat last_col (nat r, nat cs) { 00050 (void) r; return cs; } 00051 }; 00052 00053 struct diagonal_matrix { 00054 typedef diagonal_matrix ul; 00055 typedef zero_matrix ur; 00056 typedef zero_matrix ll; 00057 typedef diagonal_matrix lr; 00058 static inline nat first_row (nat c, nat rs) { 00059 (void) rs; return c; } 00060 static inline nat first_col (nat r, nat cs) { 00061 (void) cs; return r; } 00062 static inline nat last_row (nat c, nat rs) { 00063 (void) rs; return c+1; } 00064 static inline nat last_col (nat r, nat cs) { 00065 (void) cs; return r+1; } 00066 }; 00067 00068 struct upper_triangular_matrix { 00069 typedef upper_triangular_matrix ul; 00070 typedef full_matrix ur; 00071 typedef zero_matrix ll; 00072 typedef upper_triangular_matrix lr; 00073 static inline nat first_row (nat c, nat rs) { 00074 (void) c; (void) rs; return 0; } 00075 static inline nat first_col (nat r, nat cs) { 00076 (void) cs; return r; } 00077 static inline nat last_row (nat c, nat rs) { 00078 (void) rs; return c+1; } 00079 static inline nat last_col (nat r, nat cs) { 00080 (void) r; return cs; } 00081 }; 00082 00083 struct lower_triangular { 00084 typedef lower_triangular_matrix ul; 00085 typedef zero_matrix ur; 00086 typedef full_matrix ll; 00087 typedef lower_triangular_matrix lr; 00088 static inline nat first_row (nat c, nat rs) { 00089 (void) rs; return c; } 00090 static inline nat first_col (nat r, nat cs) { 00091 (void) r; (void) cs; return 0; } 00092 static inline nat last_row (nat c, nat rs) { 00093 (void) c; return rs; } 00094 static inline nat last_col (nat r, nat cs) { 00095 (void) cs; return r+1; } 00096 }; 00097 00098 /****************************************************************************** 00099 * Multiplication of structured matrices 00100 ******************************************************************************/ 00101 00102 struct matrix_structured_multiply {}; 00103 00104 template<typename V> 00105 struct implementation<matrix_structured_multiply,V,matrix_naive>: 00106 public implementation<matrix_multiply,V> 00107 { 00108 typedef implementation<matrix_multiply,V> Mat; 00109 typedef implementation<vector_linear,V> Vec; 00110 00111 template<typename Op, 00112 typename TD, typename T1, typename T2, 00113 typename D , typename S1, typename S2> static inline void 00114 smul (D* d, const S1* s1, const S2* s2, 00115 nat rs, nat rr, nat ls, nat ll, nat cs, nat cc) { 00116 typedef typename Op::acc_op Acc; 00117 typedef typename Op::clr_op Clr; 00118 const S1 *s1r, *s1c; 00119 const S2 *s2r, *s2c; 00120 nat drp = Mat::index (1, 0, rr, cc), dcp = Mat::index (0, 1, rr, cc); 00121 nat s1p= Mat::index (0, 1, rr, ll); 00122 nat s2p= Mat::index (1, 0, ll, cc); 00123 for (nat ri=0; ri<rs; ri++) { 00124 nat c1= TD::first_col (ri, cs); 00125 nat c2= TD::last_col (ri, cs); 00126 for (nat ci=0; ci<cs; ci++) { 00127 nat l1= max (T1::first_col (ri, ls), T2::first_row (ci, ls)); 00128 nat l2= min (T1::last_col (ri, ls), T2::last_row (ci, ls)); 00129 D tmp= dest [Mat::index (ri, ci, rr, cc)]; 00130 Clr::set_op (tmp); 00131 S1* s1c= s1 + Mat::index (ri, 0, rr, ll); 00132 S1* s2c= s2 + Mat::index (0, ci, ll, cc); 00133 for (nat li=0; li<ls; li++, s1c += s1p, s2c += s2p) 00134 Acc::set_op (tmp, *s1c, *s2c); 00135 dest [Mat::index (ri, ci, rr, cc)]= tmp; 00136 } 00137 } 00138 } 00139 00140 }; // implementation<matrix_structured,V,matrix_naive> 00141 00142 } // namespace mmx 00143 #endif //__MMX__STRUCTURED_NAIVE__HPP