00001 00002 #include <basix/int.hpp> 00003 #include <basix/vector.hpp> 00004 #include <algebramix/vector_unrolled.hpp> 00005 #include <algebramix/vector_simd.hpp> 00006 #include <algebramix/permutation.hpp> 00007 #include <basix/port.hpp> 00008 #include <basix/literal.hpp> 00009 #include <numerix/integer.hpp> 00010 #include <numerix/modular.hpp> 00011 #include <numerix/modular_integer.hpp> 00012 #include <numerix/rational.hpp> 00013 #include <basix/row_tuple.hpp> 00014 #include <algebramix/matrix.hpp> 00015 #include <algebramix/matrix_ring_naive.hpp> 00016 #include <algebramix/matrix_integer.hpp> 00017 #include <algebramix/matrix_modular_integer.hpp> 00018 #include <algebramix/matrix_quotient.hpp> 00019 #include <basix/alias.hpp> 00020 #include <basix/tuple.hpp> 00021 #include <basix/glue.hpp> 00022 00023 #define int_literal(x) as_int (as_string (x)) 00024 #define is_generic_literal is<literal> 00025 #define gen_literal_apply(f,v) gen (as<generic> (f), v) 00026 #define gen_literal_access(f,v) access (as<generic> (f), v) 00027 #define identity_matrix_integer identity_matrix<integer> 00028 #define hilbert_matrix_rational hilbert_matrix<rational> 00029 00030 namespace mmx { 00031 template<typename C> matrix<C> 00032 matrix_new (const vector<row_tuple<C> >& t) { 00033 if (N(t) == 0) return matrix<C> (); 00034 nat i, j, rows= N(t), cols= N(t[0]); 00035 C dummy= zero_cst<C> (); 00036 matrix<C> r (dummy, rows, cols); 00037 for (i=0; i<rows; i++) { 00038 ASSERT (N(t[i]) == cols, "unequal row lengths"); 00039 for (j=0; j<cols; j++) 00040 r(i,j)= t[i][j]; 00041 } 00042 return r; 00043 } 00044 00045 template<typename C> matrix<C> 00046 matrix_new (const vector<C>& t) { 00047 nat j, rows= 1, cols= N(t); 00048 C dummy= zero_cst<C> (); 00049 matrix<C> r (dummy, rows, cols); 00050 for (j=0; j<cols; j++) 00051 r(0,j)= t[j]; 00052 return r; 00053 } 00054 00055 template<typename C> vector<generic> 00056 wrap_column_reduced_echelon_with_permutation (const matrix<C>& m) { 00057 permutation permut; 00058 generic tp=as<generic> (column_reduced_echelon (m, permut)); 00059 return vec (tp, as<generic> (permut)); 00060 } 00061 00062 template<typename C> vector<generic> 00063 wrap_column_reduced_echelon_with_transform (const matrix<C>& m) { 00064 matrix<C> k; 00065 generic tp=as<generic> (column_reduced_echelon (m, k)); 00066 return vec (tp, as<generic> (k)); 00067 } 00068 00069 template<typename C> vector<generic> 00070 wrap_row_reduced_echelon_with_transform (const matrix<C>& m) { 00071 matrix<C> k; 00072 generic tp=as<generic> (row_reduced_echelon (m, k)); 00073 return vec (tp, as<generic> (k)); 00074 } 00075 } 00076 00077 00078 namespace mmx { 00079 static bool 00080 GLUE_1 (const generic &arg_1) { 00081 return generic_is_string (arg_1); 00082 } 00083 00084 static int 00085 GLUE_2 (const string &arg_1) { 00086 return N (arg_1); 00087 } 00088 00089 static string 00090 GLUE_3 (const string &arg_1, const int &arg_2, const int &arg_3) { 00091 return arg_1 (arg_2, arg_3); 00092 } 00093 00094 static string 00095 GLUE_4 (const string &arg_1, const string &arg_2) { 00096 return arg_1 * arg_2; 00097 } 00098 00099 static string 00100 GLUE_5 (const string &arg_1, const string &arg_2) { 00101 return arg_1 * arg_2; 00102 } 00103 00104 static alias<string> 00105 GLUE_6 (const alias<string> &arg_1, const string &arg_2) { 00106 return alias_write (arg_1, arg_2); 00107 } 00108 00109 static bool 00110 GLUE_7 (const string &arg_1, const string &arg_2) { 00111 return arg_1 < arg_2; 00112 } 00113 00114 static bool 00115 GLUE_8 (const string &arg_1, const string &arg_2) { 00116 return arg_1 <= arg_2; 00117 } 00118 00119 static bool 00120 GLUE_9 (const string &arg_1, const string &arg_2) { 00121 return arg_1 > arg_2; 00122 } 00123 00124 static bool 00125 GLUE_10 (const string &arg_1, const string &arg_2) { 00126 return arg_1 >= arg_2; 00127 } 00128 00129 static bool 00130 GLUE_11 (const string &arg_1, const string &arg_2) { 00131 return starts (arg_1, arg_2); 00132 } 00133 00134 static bool 00135 GLUE_12 (const string &arg_1, const string &arg_2) { 00136 return ends (arg_1, arg_2); 00137 } 00138 00139 static string 00140 GLUE_13 (const string &arg_1, const string &arg_2, const string &arg_3) { 00141 return replace (arg_1, arg_2, arg_3); 00142 } 00143 00144 static int 00145 GLUE_14 (const string &arg_1, const string &arg_2, const int &arg_3) { 00146 return search_forwards (arg_1, arg_2, arg_3); 00147 } 00148 00149 static int 00150 GLUE_15 (const string &arg_1, const string &arg_2, const int &arg_3) { 00151 return search_backwards (arg_1, arg_2, arg_3); 00152 } 00153 00154 static string 00155 GLUE_16 (const string &arg_1) { 00156 return upcase (arg_1); 00157 } 00158 00159 static string 00160 GLUE_17 (const string &arg_1) { 00161 return locase (arg_1); 00162 } 00163 00164 static string 00165 GLUE_18 (const string &arg_1) { 00166 return upcase_first (arg_1); 00167 } 00168 00169 static string 00170 GLUE_19 (const string &arg_1) { 00171 return locase_first (arg_1); 00172 } 00173 00174 static string 00175 GLUE_20 (const string &arg_1) { 00176 return quote (arg_1); 00177 } 00178 00179 static string 00180 GLUE_21 (const string &arg_1) { 00181 return unquote (arg_1); 00182 } 00183 00184 static string 00185 GLUE_22 (const int &arg_1) { 00186 return charcode_as_string (arg_1); 00187 } 00188 00189 static int 00190 GLUE_23 (const string &arg_1) { 00191 return string_as_charcode (arg_1); 00192 } 00193 00194 static bool 00195 GLUE_24 (const generic &arg_1) { 00196 return is_generic_literal (arg_1); 00197 } 00198 00199 static generic 00200 GLUE_25 (const literal &arg_1, const tuple<generic> &arg_2) { 00201 return gen_literal_apply (arg_1, as_vector (arg_2)); 00202 } 00203 00204 static generic 00205 GLUE_26 (const literal &arg_1, const tuple<generic> &arg_2) { 00206 return gen_literal_access (arg_1, as_vector (arg_2)); 00207 } 00208 00209 static literal 00210 GLUE_27 (const string &arg_1) { 00211 return literal (arg_1); 00212 } 00213 00214 static string 00215 GLUE_28 (const literal &arg_1) { 00216 return *arg_1; 00217 } 00218 00219 static generic 00220 GLUE_29 (const int &arg_1) { 00221 return integer_construct (arg_1); 00222 } 00223 00224 static row_tuple<generic> 00225 GLUE_30 (const tuple<generic> &arg_1) { 00226 return row_tuple<generic > (as_vector (arg_1)); 00227 } 00228 00229 static matrix<generic> 00230 GLUE_31 (const tuple<generic> &arg_1) { 00231 return matrix_new (as_vector (arg_1)); 00232 } 00233 00234 static matrix<generic> 00235 GLUE_32 (const tuple<row_tuple<generic> > &arg_1) { 00236 return matrix_new (as_vector (arg_1)); 00237 } 00238 00239 static matrix<generic> 00240 GLUE_33 (const tuple<row_tuple<generic> > &arg_1) { 00241 return matrix_new (as_vector (arg_1)); 00242 } 00243 00244 static int 00245 GLUE_34 (const matrix<generic> &arg_1) { 00246 return N (arg_1); 00247 } 00248 00249 static int 00250 GLUE_35 (const matrix<generic> &arg_1) { 00251 return rows (arg_1); 00252 } 00253 00254 static int 00255 GLUE_36 (const matrix<generic> &arg_1) { 00256 return cols (arg_1); 00257 } 00258 00259 static generic 00260 GLUE_37 (const matrix<generic> &arg_1, const int &arg_2, const int &arg_3) { 00261 return arg_1 (arg_2, arg_3); 00262 } 00263 00264 static alias<generic> 00265 GLUE_38 (const alias<matrix<generic> > &arg_1, const int &arg_2, const int &arg_3) { 00266 return alias_access<generic > (arg_1, arg_2, arg_3); 00267 } 00268 00269 static matrix<generic> 00270 GLUE_39 (const matrix<generic> &arg_1, const int &arg_2, const int &arg_3, const int &arg_4, const int &arg_5) { 00271 return range (arg_1, arg_2, arg_3, arg_4, arg_5); 00272 } 00273 00274 static vector<generic> 00275 GLUE_40 (const matrix<generic> &arg_1, const int &arg_2) { 00276 return row (arg_1, arg_2); 00277 } 00278 00279 static vector<generic> 00280 GLUE_41 (const matrix<generic> &arg_1, const int &arg_2) { 00281 return column (arg_1, arg_2); 00282 } 00283 00284 static matrix<generic> 00285 GLUE_42 (const matrix<generic> &arg_1) { 00286 return transpose (arg_1); 00287 } 00288 00289 static matrix<generic> 00290 GLUE_43 (const matrix<generic> &arg_1, const matrix<generic> &arg_2) { 00291 return horizontal_join (arg_1, arg_2); 00292 } 00293 00294 static matrix<generic> 00295 GLUE_44 (const matrix<generic> &arg_1, const matrix<generic> &arg_2) { 00296 return vertical_join (arg_1, arg_2); 00297 } 00298 00299 static matrix<generic> 00300 GLUE_45 (const matrix<generic> &arg_1, const permutation &arg_2) { 00301 return arg_1 * arg_2; 00302 } 00303 00304 static matrix<generic> 00305 GLUE_46 (const permutation &arg_1, const matrix<generic> &arg_2) { 00306 return arg_1 * arg_2; 00307 } 00308 00309 static matrix<generic> 00310 GLUE_47 (const generic &arg_1, const int &arg_2, const int &arg_3) { 00311 return fill_matrix (arg_1, arg_2, arg_3); 00312 } 00313 00314 static matrix<generic> 00315 GLUE_48 (const generic &arg_1, const int &arg_2) { 00316 return jordan_matrix (arg_1, arg_2); 00317 } 00318 00319 static matrix<generic> 00320 GLUE_49 (const vector<generic> &arg_1) { 00321 return toeplitz_matrix (arg_1); 00322 } 00323 00324 static matrix<generic> 00325 GLUE_50 (const vector<generic> &arg_1) { 00326 return hankel_matrix (arg_1); 00327 } 00328 00329 static matrix<generic> 00330 GLUE_51 (const vector<generic> &arg_1, const vector<generic> &arg_2) { 00331 return tensor_matrix (arg_1, arg_2); 00332 } 00333 00334 static matrix<generic> 00335 GLUE_52 (const vector<generic> &arg_1) { 00336 return vandermonde (arg_1); 00337 } 00338 00339 static matrix<generic> 00340 GLUE_53 (const matrix<generic> &arg_1) { 00341 return -arg_1; 00342 } 00343 00344 static matrix<generic> 00345 GLUE_54 (const matrix<generic> &arg_1) { 00346 return square (arg_1); 00347 } 00348 00349 static matrix<generic> 00350 GLUE_55 (const matrix<generic> &arg_1, const matrix<generic> &arg_2) { 00351 return arg_1 + arg_2; 00352 } 00353 00354 static matrix<generic> 00355 GLUE_56 (const matrix<generic> &arg_1, const matrix<generic> &arg_2) { 00356 return arg_1 - arg_2; 00357 } 00358 00359 static matrix<generic> 00360 GLUE_57 (const matrix<generic> &arg_1, const matrix<generic> &arg_2) { 00361 return arg_1 * arg_2; 00362 } 00363 00364 static vector<generic> 00365 GLUE_58 (const matrix<generic> &arg_1, const vector<generic> &arg_2) { 00366 return arg_1 * arg_2; 00367 } 00368 00369 static vector<generic> 00370 GLUE_59 (const vector<generic> &arg_1, const matrix<generic> &arg_2) { 00371 return arg_1 * arg_2; 00372 } 00373 00374 static bool 00375 GLUE_60 (const matrix<generic> &arg_1, const matrix<generic> &arg_2) { 00376 return arg_1 == arg_2; 00377 } 00378 00379 static bool 00380 GLUE_61 (const matrix<generic> &arg_1, const matrix<generic> &arg_2) { 00381 return arg_1 != arg_2; 00382 } 00383 00384 static matrix<generic> 00385 GLUE_62 (const matrix<generic> &arg_1, const matrix<generic> &arg_2) { 00386 return krylov (arg_1, arg_2); 00387 } 00388 00389 static generic 00390 GLUE_63 (const matrix<generic> &arg_1) { 00391 return det (arg_1); 00392 } 00393 00394 static matrix<generic> 00395 GLUE_64 (const matrix<generic> &arg_1) { 00396 return row_echelon (arg_1); 00397 } 00398 00399 static matrix<generic> 00400 GLUE_65 (const matrix<generic> &arg_1) { 00401 return column_echelon (arg_1); 00402 } 00403 00404 static matrix<generic> 00405 GLUE_66 (const matrix<generic> &arg_1) { 00406 return row_reduced_echelon (arg_1); 00407 } 00408 00409 static matrix<generic> 00410 GLUE_67 (const matrix<generic> &arg_1) { 00411 return column_reduced_echelon (arg_1); 00412 } 00413 00414 static vector<generic> 00415 GLUE_68 (const matrix<generic> &arg_1) { 00416 return wrap_row_reduced_echelon_with_transform (arg_1); 00417 } 00418 00419 static vector<generic> 00420 GLUE_69 (const matrix<generic> &arg_1) { 00421 return wrap_column_reduced_echelon_with_transform (arg_1); 00422 } 00423 00424 static vector<generic> 00425 GLUE_70 (const matrix<generic> &arg_1) { 00426 return wrap_column_reduced_echelon_with_permutation (arg_1); 00427 } 00428 00429 static matrix<generic> 00430 GLUE_71 (const matrix<generic> &arg_1) { 00431 return kernel (arg_1); 00432 } 00433 00434 static matrix<generic> 00435 GLUE_72 (const matrix<generic> &arg_1) { 00436 return image (arg_1); 00437 } 00438 00439 static int 00440 GLUE_73 (const matrix<generic> &arg_1) { 00441 return rank (arg_1); 00442 } 00443 00444 static matrix<generic> 00445 GLUE_74 (const matrix<generic> &arg_1) { 00446 return invert (arg_1); 00447 } 00448 00449 static matrix<generic> 00450 GLUE_75 (const matrix<generic> &arg_1) { 00451 return derive (arg_1); 00452 } 00453 00454 static matrix<generic> 00455 GLUE_76 (const matrix<generic> &arg_1) { 00456 return integrate (arg_1); 00457 } 00458 00459 void 00460 glue_matrix_generic () { 00461 static bool done = false; 00462 if (done) return; 00463 done = true; 00464 call_glue (string ("glue_basix_vector_generic")); 00465 call_glue (string ("glue_vector_int")); 00466 call_glue (string ("glue_permutation")); 00467 define ("string?", GLUE_1); 00468 define ("#", GLUE_2); 00469 define (".[]", GLUE_3); 00470 define ("*", GLUE_4); 00471 define ("><", GLUE_5); 00472 define ("<<", GLUE_6); 00473 define ("<", GLUE_7); 00474 define ("<=", GLUE_8); 00475 define (">", GLUE_9); 00476 define (">=", GLUE_10); 00477 define ("starts?", GLUE_11); 00478 define ("ends?", GLUE_12); 00479 define ("replace", GLUE_13); 00480 define ("search_forwards", GLUE_14); 00481 define ("search_backwards", GLUE_15); 00482 define ("upcase", GLUE_16); 00483 define ("locase", GLUE_17); 00484 define ("upcase_first", GLUE_18); 00485 define ("locase_first", GLUE_19); 00486 define ("quote", GLUE_20); 00487 define ("unquote", GLUE_21); 00488 define ("ascii", GLUE_22); 00489 define ("ascii_code", GLUE_23); 00490 define ("literal?", GLUE_24); 00491 define (".()", GLUE_25); 00492 define (".[]", GLUE_26); 00493 define ("as_literal", GLUE_27); 00494 define ("as_string", GLUE_28); 00495 define_constructor<int > (GLUE_29); 00496 define_type<row_tuple<generic> > (gen (lit ("Row"), lit ("Generic"))); 00497 define_type<matrix<generic> > (gen (lit ("Matrix"), lit ("Generic"))); 00498 define ("(.)", GLUE_30); 00499 define ("matrix", GLUE_31); 00500 define ("matrix", GLUE_32); 00501 define ("[]", GLUE_33); 00502 define ("#", GLUE_34); 00503 define ("rows", GLUE_35); 00504 define ("columns", GLUE_36); 00505 define (".[]", GLUE_37); 00506 define (".[]", GLUE_38); 00507 define (".[]", GLUE_39); 00508 define ("row", GLUE_40); 00509 define ("column", GLUE_41); 00510 define ("transpose", GLUE_42); 00511 define ("horizontal_join", GLUE_43); 00512 define ("vertical_join", GLUE_44); 00513 define ("*", GLUE_45); 00514 define ("*", GLUE_46); 00515 define ("fill_matrix", GLUE_47); 00516 define ("jordan_matrix", GLUE_48); 00517 define ("toeplitz_matrix", GLUE_49); 00518 define ("hankel_matrix", GLUE_50); 00519 define ("tensor_matrix", GLUE_51); 00520 define ("vandermonde", GLUE_52); 00521 define ("-", GLUE_53); 00522 define ("square", GLUE_54); 00523 define ("+", GLUE_55); 00524 define ("-", GLUE_56); 00525 define ("*", GLUE_57); 00526 define ("*", GLUE_58); 00527 define ("*", GLUE_59); 00528 define ("=", GLUE_60); 00529 define ("!=", GLUE_61); 00530 define ("krylov", GLUE_62); 00531 define ("det", GLUE_63); 00532 define ("row_echelon", GLUE_64); 00533 define ("column_echelon", GLUE_65); 00534 define ("row_reduced_echelon", GLUE_66); 00535 define ("column_reduced_echelon", GLUE_67); 00536 define ("row_reduced_echelon_with_transform", GLUE_68); 00537 define ("column_reduced_echelon_with_transform", GLUE_69); 00538 define ("column_reduced_echelon_with_permutation", GLUE_70); 00539 define ("ker", GLUE_71); 00540 define ("im", GLUE_72); 00541 define ("rank", GLUE_73); 00542 define ("invert", GLUE_74); 00543 define ("derive", GLUE_75); 00544 define ("integrate", GLUE_76); 00545 } 00546 }