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