3 * Implementation of symbolic matrices */
11 // default constructor, destructor, copy constructor, assignment operator
17 /** Default ctor. Initializes to 1 x 1-dimensional zero-matrix. */
19 : basic(TINFO_MATRIX), row(1), col(1)
21 debugmsg("matrix default constructor",LOGLEVEL_CONSTRUCT);
22 m.push_back(exZERO());
27 debugmsg("matrix destructor",LOGLEVEL_DESTRUCT);
30 matrix::matrix(matrix const & other)
32 debugmsg("matrix copy constructor",LOGLEVEL_CONSTRUCT);
36 matrix const & matrix::operator=(matrix const & other)
38 debugmsg("matrix operator=",LOGLEVEL_ASSIGNMENT);
48 void matrix::copy(matrix const & other)
53 m=other.m; // use STL's vector copying
56 void matrix::destroy(bool call_parent)
58 if (call_parent) basic::destroy(call_parent);
67 /** Very common ctor. Initializes to r x c-dimensional zero-matrix.
69 * @param r number of rows
70 * @param c number of cols */
71 matrix::matrix(int r, int c)
72 : basic(TINFO_MATRIX), row(r), col(c)
74 debugmsg("matrix constructor from int,int",LOGLEVEL_CONSTRUCT);
75 m.resize(r*c, exZERO());
80 /** Ctor from representation, for internal use only. */
81 matrix::matrix(int r, int c, vector<ex> const & m2)
82 : basic(TINFO_MATRIX), row(r), col(c), m(m2)
84 debugmsg("matrix constructor from int,int,vector<ex>",LOGLEVEL_CONSTRUCT);
88 // functions overriding virtual functions from bases classes
93 basic * matrix::duplicate() const
95 debugmsg("matrix duplicate",LOGLEVEL_DUPLICATE);
96 return new matrix(*this);
99 /** nops is defined to be rows x columns. */
100 int matrix::nops() const
105 /** returns matrix entry at position (i/col, i%col). */
106 ex & matrix::let_op(int const i)
111 /** expands the elements of a matrix entry by entry. */
112 ex matrix::expand(unsigned options) const
114 vector<ex> tmp(row*col);
115 for (int i=0; i<row*col; ++i) {
116 tmp[i]=m[i].expand(options);
118 return matrix(row, col, tmp);
121 /** Search ocurrences. A matrix 'has' an expression if it is the expression
122 * itself or one of the elements 'has' it. */
123 bool matrix::has(ex const & other) const
127 // tautology: it is the expression itself
128 if (is_equal(*other.bp)) return true;
130 // search all the elements
131 for (vector<ex>::const_iterator r=m.begin(); r!=m.end(); ++r) {
132 if ((*r).has(other)) return true;
137 /** evaluate matrix entry by entry. */
138 ex matrix::eval(int level) const
140 debugmsg("matrix eval",LOGLEVEL_MEMBER_FUNCTION);
142 // check if we have to do anything at all
143 if ((level==1)&&(flags & status_flags::evaluated)) {
148 if (level == -max_recursion_level) {
149 throw (std::runtime_error("matrix::eval(): recursion limit exceeded"));
152 // eval() entry by entry
153 vector<ex> m2(row*col);
155 for (int r=0; r<row; ++r) {
156 for (int c=0; c<col; ++c) {
157 m2[r*col+c] = m[r*col+c].eval(level);
161 return (new matrix(row, col, m2))->setflag(status_flags::dynallocated |
162 status_flags::evaluated );
165 /** evaluate matrix numerically entry by entry. */
166 ex matrix::evalf(int level) const
168 debugmsg("matrix evalf",LOGLEVEL_MEMBER_FUNCTION);
170 // check if we have to do anything at all
176 if (level == -max_recursion_level) {
177 throw (std::runtime_error("matrix::evalf(): recursion limit exceeded"));
180 // evalf() entry by entry
181 vector<ex> m2(row*col);
183 for (int r=0; r<row; ++r) {
184 for (int c=0; c<col; ++c) {
185 m2[r*col+c] = m[r*col+c].evalf(level);
188 return matrix(row, col, m2);
193 int matrix::compare_same_type(basic const & other) const
195 ASSERT(is_exactly_of_type(other, matrix));
196 matrix const & o=static_cast<matrix &>(const_cast<basic &>(other));
198 // compare number of rows
199 if (row != o.rows()) {
200 return row < o.rows() ? -1 : 1;
203 // compare number of columns
204 if (col != o.cols()) {
205 return col < o.cols() ? -1 : 1;
208 // equal number of rows and columns, compare individual elements
210 for (int r=0; r<row; ++r) {
211 for (int c=0; c<col; ++c) {
212 cmpval=((*this)(r,c)).compare(o(r,c));
213 if (cmpval!=0) return cmpval;
216 // all elements are equal => matrices are equal;
221 // non-virtual functions in this class
228 * @exception logic_error (incompatible matrices) */
229 matrix matrix::add(matrix const & other) const
231 if (col != other.col || row != other.row) {
232 throw (std::logic_error("matrix::add(): incompatible matrices"));
235 vector<ex> sum(this->m);
236 vector<ex>::iterator i;
237 vector<ex>::const_iterator ci;
238 for (i=sum.begin(), ci=other.m.begin();
243 return matrix(row,col,sum);
246 /** Difference of matrices.
248 * @exception logic_error (incompatible matrices) */
249 matrix matrix::sub(matrix const & other) const
251 if (col != other.col || row != other.row) {
252 throw (std::logic_error("matrix::sub(): incompatible matrices"));
255 vector<ex> dif(this->m);
256 vector<ex>::iterator i;
257 vector<ex>::const_iterator ci;
258 for (i=dif.begin(), ci=other.m.begin();
263 return matrix(row,col,dif);
266 /** Product of matrices.
268 * @exception logic_error (incompatible matrices) */
269 matrix matrix::mul(matrix const & other) const
271 if (col != other.row) {
272 throw (std::logic_error("matrix::mul(): incompatible matrices"));
275 vector<ex> prod(row*other.col);
276 for (int i=0; i<row; ++i) {
277 for (int j=0; j<other.col; ++j) {
278 for (int l=0; l<col; ++l) {
279 prod[i*other.col+j] += m[i*col+l] * other.m[l*other.col+j];
283 return matrix(row, other.col, prod);
286 /** operator() to access elements.
288 * @param ro row of element
289 * @param co column of element
290 * @exception range_error (index out of range) */
291 ex const & matrix::operator() (int ro, int co) const
293 if (ro<0 || ro>=row || co<0 || co>=col) {
294 throw (std::range_error("matrix::operator(): index out of range"));
300 /** Set individual elements manually.
302 * @exception range_error (index out of range) */
303 matrix & matrix::set(int ro, int co, ex value)
305 if (ro<0 || ro>=row || co<0 || co>=col) {
306 throw (std::range_error("matrix::set(): index out of range"));
309 ensure_if_modifiable();
314 /** Transposed of an m x n matrix, producing a new n x m matrix object that
315 * represents the transposed. */
316 matrix matrix::transpose(void) const
318 vector<ex> trans(col*row);
320 for (int r=0; r<col; ++r) {
321 for (int c=0; c<row; ++c) {
322 trans[r*row+c] = m[c*col+r];
325 return matrix(col,row,trans);
328 /* Determiant of purely numeric matrix, using pivoting. This routine is only
329 * called internally by matrix::determinant(). */
330 ex determinant_numeric(const matrix & M)
332 ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
337 for (int r1=0; r1<M.rows(); ++r1) {
338 int indx = tmp.pivot(r1);
345 det = det * tmp.m[r1*M.cols()+r1];
346 for (int r2=r1+1; r2<M.rows(); ++r2) {
347 piv = tmp.m[r2*M.cols()+r1] / tmp.m[r1*M.cols()+r1];
348 for (int c=r1+1; c<M.cols(); c++) {
349 tmp.m[r2*M.cols()+c] -= piv * tmp.m[r1*M.cols()+c];
356 // Compute the sign of a permutation of a vector of things, used internally
357 // by determinant_symbolic_perm() where it is instantiated for int.
359 int permutation_sign(vector<T> s)
364 for (typename vector<T>::iterator i=s.begin(); i!=s.end()-1; ++i) {
365 for (typename vector<T>::iterator j=i+1; j!=s.end(); ++j) {
377 /** Determinant built by application of the full permutation group. This
378 * routine is only called internally by matrix::determinant(). */
379 ex determinant_symbolic_perm(const matrix & M)
381 ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
383 if (M.rows()==1) { // speed things up
389 vector<int> sigma(M.cols());
390 for (int i=0; i<M.cols(); ++i) sigma[i]=i;
393 term = M(sigma[0],0);
394 for (int i=1; i<M.cols(); ++i) term *= M(sigma[i],i);
395 det += permutation_sign(sigma)*term;
396 } while (next_permutation(sigma.begin(), sigma.end()));
401 /** Recursive determiant for small matrices having at least one symbolic entry.
402 * This algorithm is also known as Laplace-expansion. This routine is only
403 * called internally by matrix::determinant(). */
404 ex determinant_symbolic_minor(const matrix & M)
406 ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
408 if (M.rows()==1) { // end of recursion
411 if (M.rows()==2) { // speed things up
412 return (M(0,0)*M(1,1)-
415 if (M.rows()==3) { // speed things up even a little more
416 return ((M(2,1)*M(0,2)-M(2,2)*M(0,1))*M(1,0)+
417 (M(1,2)*M(0,1)-M(1,1)*M(0,2))*M(2,0)+
418 (M(2,2)*M(1,1)-M(2,1)*M(1,2))*M(0,0));
422 matrix minorM(M.rows()-1,M.cols()-1);
423 for (int r1=0; r1<M.rows(); ++r1) {
424 // assemble the minor matrix
425 for (int r=0; r<minorM.rows(); ++r) {
426 for (int c=0; c<minorM.cols(); ++c) {
428 minorM.set(r,c,M(r,c+1));
430 minorM.set(r,c,M(r+1,c+1));
436 det -= M(r1,0) * determinant_symbolic_minor(minorM);
438 det += M(r1,0) * determinant_symbolic_minor(minorM);
444 /* Leverrier algorithm for large matrices having at least one symbolic entry.
445 * This routine is only called internally by matrix::determinant(). The
446 * algorithm is deemed bad for symbolic matrices since it returns expressions
447 * that are very hard to canonicalize. */
448 /*ex determinant_symbolic_leverrier(const matrix & M)
450 * ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
453 * matrix I(M.row, M.col);
455 * for (int i=1; i<M.row; ++i) {
456 * for (int j=0; j<M.row; ++j)
457 * I.m[j*M.col+j] = c;
458 * B = M.mul(B.sub(I));
459 * c = B.trace()/ex(i+1);
468 /** Determinant of square matrix. This routine doesn't actually calculate the
469 * determinant, it only implements some heuristics about which algorithm to
470 * call. When the parameter for normalization is explicitly turned off this
471 * method does not normalize its result at the end, which might imply that
472 * the symbolic 2x2 matrix [[a/(a-b),1],[b/(a-b),1]] is not immediatly
473 * recognized to be unity. (This is Mathematica's default behaviour, it
474 * should be used with care.)
476 * @param normalized may be set to false if no normalization of the
477 * result is desired (i.e. to force Mathematica behavior, Maple
478 * does normalize the result).
479 * @return the determinant as a new expression
480 * @exception logic_error (matrix not square) */
481 ex matrix::determinant(bool normalized) const
484 throw (std::logic_error("matrix::determinant(): matrix not square"));
487 // check, if there are non-numeric entries in the matrix:
488 for (vector<ex>::const_iterator r=m.begin(); r!=m.end(); ++r) {
489 if (!(*r).info(info_flags::numeric)) {
491 return determinant_symbolic_minor(*this).normal();
493 return determinant_symbolic_perm(*this);
497 // if it turns out that all elements are numeric
498 return determinant_numeric(*this);
501 /** Trace of a matrix.
503 * @return the sum of diagonal elements
504 * @exception logic_error (matrix not square) */
505 ex matrix::trace(void) const
508 throw (std::logic_error("matrix::trace(): matrix not square"));
512 for (int r=0; r<col; ++r) {
518 /** Characteristic Polynomial. The characteristic polynomial of a matrix M is
519 * defined as the determiant of (M - lambda * 1) where 1 stands for the unit
520 * matrix of the same dimension as M. This method returns the characteristic
521 * polynomial as a new expression.
523 * @return characteristic polynomial as new expression
524 * @exception logic_error (matrix not square)
525 * @see matrix::determinant() */
526 ex matrix::charpoly(ex const & lambda) const
529 throw (std::logic_error("matrix::charpoly(): matrix not square"));
533 for (int r=0; r<col; ++r) {
534 M.m[r*col+r] -= lambda;
536 return (M.determinant());
539 /** Inverse of this matrix.
541 * @return the inverted matrix
542 * @exception logic_error (matrix not square)
543 * @exception runtime_error (singular matrix) */
544 matrix matrix::inverse(void) const
547 throw (std::logic_error("matrix::inverse(): matrix not square"));
551 // set tmp to the unit matrix
552 for (int i=0; i<col; ++i) {
553 tmp.m[i*col+i] = exONE();
555 // create a copy of this matrix
557 for (int r1=0; r1<row; ++r1) {
558 int indx = cpy.pivot(r1);
560 throw (std::runtime_error("matrix::inverse(): singular matrix"));
562 if (indx != 0) { // swap rows r and indx of matrix tmp
563 for (int i=0; i<col; ++i) {
564 tmp.m[r1*col+i].swap(tmp.m[indx*col+i]);
567 ex a1 = cpy.m[r1*col+r1];
568 for (int c=0; c<col; ++c) {
569 cpy.m[r1*col+c] /= a1;
570 tmp.m[r1*col+c] /= a1;
572 for (int r2=0; r2<row; ++r2) {
574 ex a2 = cpy.m[r2*col+r1];
575 for (int c=0; c<col; ++c) {
576 cpy.m[r2*col+c] -= a2 * cpy.m[r1*col+c];
577 tmp.m[r2*col+c] -= a2 * tmp.m[r1*col+c];
585 void matrix::ffe_swap(int r1, int c1, int r2 ,int c2)
587 ensure_if_modifiable();
589 ex tmp=ffe_get(r1,c1);
590 ffe_set(r1,c1,ffe_get(r2,c2));
594 void matrix::ffe_set(int r, int c, ex e)
599 ex matrix::ffe_get(int r, int c) const
601 return operator()(r-1,c-1);
604 /** Solve a set of equations for an m x n matrix by fraction-free Gaussian
605 * elimination. Based on algorithm 9.1 from 'Algorithms for Computer Algebra'
606 * by Keith O. Geddes et al.
608 * @param vars n x p matrix
609 * @param rhs m x p matrix
610 * @exception logic_error (incompatible matrices)
611 * @exception runtime_error (singular matrix) */
612 matrix matrix::fraction_free_elim(matrix const & vars,
613 matrix const & rhs) const
615 if ((row != rhs.row) || (col != vars.row) || (rhs.col != vars.col)) {
616 throw (std::logic_error("matrix::solve(): incompatible matrices"));
619 matrix a(*this); // make a copy of the matrix
620 matrix b(rhs); // make a copy of the rhs vector
622 // given an m x n matrix a, reduce it to upper echelon form
629 // eliminate below row r, with pivot in column k
630 for (int k=1; (k<=n)&&(r<=m); ++k) {
631 // find a nonzero pivot
633 for (p=r; (p<=m)&&(a.ffe_get(p,k).is_equal(exZERO())); ++p) {}
637 // switch rows p and r
638 for (int j=k; j<=n; ++j) {
642 // keep track of sign changes due to row exchange
645 for (int i=r+1; i<=m; ++i) {
646 for (int j=k+1; j<=n; ++j) {
647 a.ffe_set(i,j,(a.ffe_get(r,k)*a.ffe_get(i,j)
648 -a.ffe_get(r,j)*a.ffe_get(i,k))/divisor);
649 a.ffe_set(i,j,a.ffe_get(i,j).normal() /*.normal() */ );
651 b.ffe_set(i,1,(a.ffe_get(r,k)*b.ffe_get(i,1)
652 -b.ffe_get(r,1)*a.ffe_get(i,k))/divisor);
653 b.ffe_set(i,1,b.ffe_get(i,1).normal() /*.normal() */ );
656 divisor=a.ffe_get(r,k);
660 // optionally compute the determinant for square or augmented matrices
661 // if (r==m+1) { det=sign*divisor; } else { det=0; }
664 for (int r=1; r<=m; ++r) {
665 for (int c=1; c<=n; ++c) {
666 cout << a.ffe_get(r,c) << "\t";
668 cout << " | " << b.ffe_get(r,1) << endl;
673 // test if we really have an upper echelon matrix
674 int zero_in_last_row=-1;
675 for (int r=1; r<=m; ++r) {
676 int zero_in_this_row=0;
677 for (int c=1; c<=n; ++c) {
678 if (a.ffe_get(r,c).is_equal(exZERO())) {
684 ASSERT((zero_in_this_row>zero_in_last_row)||(zero_in_this_row=n));
685 zero_in_last_row=zero_in_this_row;
687 #endif // def DOASSERT
691 int last_assigned_sol=n+1;
692 for (int r=m; r>0; --r) {
693 int first_non_zero=1;
694 while ((first_non_zero<=n)&&(a.ffe_get(r,first_non_zero).is_zero())) {
697 if (first_non_zero>n) {
698 // row consists only of zeroes, corresponding rhs must be 0 as well
699 if (!b.ffe_get(r,1).is_zero()) {
700 throw (std::runtime_error("matrix::fraction_free_elim(): singular matrix"));
703 // assign solutions for vars between first_non_zero+1 and
704 // last_assigned_sol-1: free parameters
705 for (int c=first_non_zero+1; c<=last_assigned_sol-1; ++c) {
706 sol.ffe_set(c,1,vars.ffe_get(c,1));
709 for (int c=first_non_zero+1; c<=n; ++c) {
710 e=e-a.ffe_get(r,c)*sol.ffe_get(c,1);
712 sol.ffe_set(first_non_zero,1,
713 (e/a.ffe_get(r,first_non_zero)).normal());
714 last_assigned_sol=first_non_zero;
717 // assign solutions for vars between 1 and
718 // last_assigned_sol-1: free parameters
719 for (int c=1; c<=last_assigned_sol-1; ++c) {
720 sol.ffe_set(c,1,vars.ffe_get(c,1));
724 for (int c=1; c<=n; ++c) {
725 cout << vars.ffe_get(c,1) << "->" << sol.ffe_get(c,1) << endl;
730 // test solution with echelon matrix
731 for (int r=1; r<=m; ++r) {
733 for (int c=1; c<=n; ++c) {
734 e=e+a.ffe_get(r,c)*sol.ffe_get(c,1);
736 if (!(e-b.ffe_get(r,1)).normal().is_zero()) {
738 cout << "b.ffe_get(" << r<<",1)=" << b.ffe_get(r,1) << endl;
739 cout << "diff=" << (e-b.ffe_get(r,1)).normal() << endl;
741 ASSERT((e-b.ffe_get(r,1)).normal().is_zero());
744 // test solution with original matrix
745 for (int r=1; r<=m; ++r) {
747 for (int c=1; c<=n; ++c) {
748 e=e+ffe_get(r,c)*sol.ffe_get(c,1);
751 if (!(e-rhs.ffe_get(r,1)).normal().is_zero()) {
752 cout << "e=" << e << endl;
755 cout << "e.normal()=" << en << endl;
757 cout << "rhs.ffe_get(" << r<<",1)=" << rhs.ffe_get(r,1) << endl;
758 cout << "diff=" << (e-rhs.ffe_get(r,1)).normal() << endl;
761 ex xxx=e-rhs.ffe_get(r,1);
762 cerr << "xxx=" << xxx << endl << endl;
764 ASSERT((e-rhs.ffe_get(r,1)).normal().is_zero());
766 #endif // def DOASSERT
771 /** Solve simultaneous set of equations. */
772 matrix matrix::solve(matrix const & v) const
774 if (!(row == col && col == v.row)) {
775 throw (std::logic_error("matrix::solve(): incompatible matrices"));
778 // build the extended matrix of *this with v attached to the right
779 matrix tmp(row,col+v.col);
780 for (int r=0; r<row; ++r) {
781 for (int c=0; c<col; ++c) {
782 tmp.m[r*tmp.col+c] = m[r*col+c];
784 for (int c=0; c<v.col; ++c) {
785 tmp.m[r*tmp.col+c+col] = v.m[r*v.col+c];
788 for (int r1=0; r1<row; ++r1) {
789 int indx = tmp.pivot(r1);
791 throw (std::runtime_error("matrix::solve(): singular matrix"));
793 for (int c=r1; c<tmp.col; ++c) {
794 tmp.m[r1*tmp.col+c] /= tmp.m[r1*tmp.col+r1];
796 for (int r2=r1+1; r2<row; ++r2) {
797 for (int c=r1; c<tmp.col; ++c) {
799 -= tmp.m[r2*tmp.col+r1] * tmp.m[r1*tmp.col+c];
804 // assemble the solution matrix
805 vector<ex> sol(v.row*v.col);
806 for (int c=0; c<v.col; ++c) {
807 for (int r=col-1; r>=0; --r) {
808 sol[r*v.col+c] = tmp[r*tmp.col+c];
809 for (int i=r+1; i<col; ++i) {
811 -= tmp[r*tmp.col+i] * sol[i*v.col+c];
815 return matrix(v.row, v.col, sol);
820 /** Partial pivoting method.
821 * Usual pivoting returns the index to the element with the largest absolute
822 * value and swaps the current row with the one where the element was found.
823 * Here it does the same with the first non-zero element. (This works fine,
824 * but may be far from optimal for numerics.) */
825 int matrix::pivot(int ro)
829 for (int r=ro; r<row; ++r) {
830 if (!m[r*col+ro].is_zero()) {
835 if (m[k*col+ro].is_zero()) {
838 if (k!=ro) { // swap rows
839 for (int c=0; c<col; ++c) {
840 m[k*col+c].swap(m[ro*col+c]);
851 const matrix some_matrix;
852 type_info const & typeid_matrix=typeid(some_matrix);