]> www.ginac.de Git - ginac.git/blobdiff - ginac/matrix.cpp
* mul::expand() (mul.cpp): When multiplying two sums, be careful not to
[ginac.git] / ginac / matrix.cpp
index 3d735fe46689aa57fc0d973a0b8588e07f2049f8..8daa11db0fab78332effeec157ed16fb17b4294f 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of symbolic matrices */
 
 /*
- *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -147,23 +147,44 @@ void matrix::print(const print_context & c, unsigned level) const
 
        } else {
 
-               c.s << "[";
-               for (unsigned y=0; y<row-1; ++y) {
+               if (is_a<print_python_repr>(c))
+                       c.s << class_name() << '(';
+
+               if (is_a<print_latex>(c))
+                       c.s << "\\left(\\begin{array}{" << std::string(col,'c') << "}";
+               else
                        c.s << "[";
-                       for (unsigned x=0; x<col-1; ++x) {
-                               m[y*col+x].print(c);
-                               c.s << ",";
+
+               for (unsigned ro=0; ro<row; ++ro) {
+                       if (!is_a<print_latex>(c))
+                               c.s << "[";
+                       for (unsigned co=0; co<col; ++co) {
+                               m[ro*col+co].print(c);
+                               if (co<col-1) {
+                                       if (is_a<print_latex>(c))
+                                               c.s << "&";
+                                       else
+                                               c.s << ",";
+                               } else {
+                                       if (!is_a<print_latex>(c))
+                                               c.s << "]";
+                               }
+                       }
+                       if (ro<row-1) {
+                               if (is_a<print_latex>(c))
+                                       c.s << "\\\\";
+                               else
+                                       c.s << ",";
                        }
-                       m[col*(y+1)-1].print(c);
-                       c.s << "],";
-               }
-               c.s << "[";
-               for (unsigned x=0; x<col-1; ++x) {
-                       m[(row-1)*col+x].print(c);
-                       c.s << ",";
                }
-               m[row*col-1].print(c);
-               c.s << "]]";
+
+               if (is_a<print_latex>(c))
+                       c.s << "\\end{array}\\right)";
+               else
+                       c.s << "]";
+
+               if (is_a<print_python_repr>(c))
+                       c.s << ')';
 
        }
 }
@@ -599,17 +620,19 @@ matrix matrix::pow(const ex & expn) const
                        matrix C(row,col);
                        for (unsigned r=0; r<row; ++r)
                                C(r,r) = _ex1;
+                       if (b.is_zero())
+                               return C;
                        // This loop computes the representation of b in base 2 from right
                        // to left and multiplies the factors whenever needed.  Note
                        // that this is not entirely optimal but close to optimal and
                        // "better" algorithms are much harder to implement.  (See Knuth,
                        // TAoCP2, section "Evaluation of Powers" for a good discussion.)
-                       while (b!=1) {
+                       while (b!=_num1) {
                                if (b.is_odd()) {
                                        C = C.mul(A);
-                                       b -= 1;
+                                       --b;
                                }
-                               b *= _num1_2;  // b /= 2, still integer.
+                               b /= _num2;  // still integer.
                                A = A.mul(A);
                        }
                        return A.mul(C);
@@ -1454,4 +1477,12 @@ ex diag_matrix(const lst & l)
        return m;
 }
 
+ex unit_matrix(unsigned r, unsigned c)
+{
+       matrix Id(r,c);
+       for (unsigned i=0; i<r && i<c; ++i)
+               Id(i,i) = _ex1;
+       return Id;
+}
+
 } // namespace GiNaC