3 * Implementation of symbolic matrices
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 // default constructor, destructor, copy constructor, assignment operator
34 /** Default ctor. Initializes to 1 x 1-dimensional zero-matrix. */
36 : basic(TINFO_matrix), row(1), col(1)
38 debugmsg("matrix default constructor",LOGLEVEL_CONSTRUCT);
39 m.push_back(exZERO());
44 debugmsg("matrix destructor",LOGLEVEL_DESTRUCT);
47 matrix::matrix(matrix const & other)
49 debugmsg("matrix copy constructor",LOGLEVEL_CONSTRUCT);
53 matrix const & matrix::operator=(matrix const & other)
55 debugmsg("matrix operator=",LOGLEVEL_ASSIGNMENT);
65 void matrix::copy(matrix const & other)
70 m=other.m; // use STL's vector copying
73 void matrix::destroy(bool call_parent)
75 if (call_parent) basic::destroy(call_parent);
84 /** Very common ctor. Initializes to r x c-dimensional zero-matrix.
86 * @param r number of rows
87 * @param c number of cols */
88 matrix::matrix(int r, int c)
89 : basic(TINFO_matrix), row(r), col(c)
91 debugmsg("matrix constructor from int,int",LOGLEVEL_CONSTRUCT);
92 m.resize(r*c, exZERO());
97 /** Ctor from representation, for internal use only. */
98 matrix::matrix(int r, int c, vector<ex> const & m2)
99 : basic(TINFO_matrix), row(r), col(c), m(m2)
101 debugmsg("matrix constructor from int,int,vector<ex>",LOGLEVEL_CONSTRUCT);
105 // functions overriding virtual functions from bases classes
110 basic * matrix::duplicate() const
112 debugmsg("matrix duplicate",LOGLEVEL_DUPLICATE);
113 return new matrix(*this);
116 /** nops is defined to be rows x columns. */
117 int matrix::nops() const
122 /** returns matrix entry at position (i/col, i%col). */
123 ex & matrix::let_op(int const i)
128 /** expands the elements of a matrix entry by entry. */
129 ex matrix::expand(unsigned options) const
131 vector<ex> tmp(row*col);
132 for (int i=0; i<row*col; ++i) {
133 tmp[i]=m[i].expand(options);
135 return matrix(row, col, tmp);
138 /** Search ocurrences. A matrix 'has' an expression if it is the expression
139 * itself or one of the elements 'has' it. */
140 bool matrix::has(ex const & other) const
144 // tautology: it is the expression itself
145 if (is_equal(*other.bp)) return true;
147 // search all the elements
148 for (vector<ex>::const_iterator r=m.begin(); r!=m.end(); ++r) {
149 if ((*r).has(other)) return true;
154 /** evaluate matrix entry by entry. */
155 ex matrix::eval(int level) const
157 debugmsg("matrix eval",LOGLEVEL_MEMBER_FUNCTION);
159 // check if we have to do anything at all
160 if ((level==1)&&(flags & status_flags::evaluated)) {
165 if (level == -max_recursion_level) {
166 throw (std::runtime_error("matrix::eval(): recursion limit exceeded"));
169 // eval() entry by entry
170 vector<ex> m2(row*col);
172 for (int r=0; r<row; ++r) {
173 for (int c=0; c<col; ++c) {
174 m2[r*col+c] = m[r*col+c].eval(level);
178 return (new matrix(row, col, m2))->setflag(status_flags::dynallocated |
179 status_flags::evaluated );
182 /** evaluate matrix numerically entry by entry. */
183 ex matrix::evalf(int level) const
185 debugmsg("matrix evalf",LOGLEVEL_MEMBER_FUNCTION);
187 // check if we have to do anything at all
193 if (level == -max_recursion_level) {
194 throw (std::runtime_error("matrix::evalf(): recursion limit exceeded"));
197 // evalf() entry by entry
198 vector<ex> m2(row*col);
200 for (int r=0; r<row; ++r) {
201 for (int c=0; c<col; ++c) {
202 m2[r*col+c] = m[r*col+c].evalf(level);
205 return matrix(row, col, m2);
210 int matrix::compare_same_type(basic const & other) const
212 ASSERT(is_exactly_of_type(other, matrix));
213 matrix const & o=static_cast<matrix &>(const_cast<basic &>(other));
215 // compare number of rows
216 if (row != o.rows()) {
217 return row < o.rows() ? -1 : 1;
220 // compare number of columns
221 if (col != o.cols()) {
222 return col < o.cols() ? -1 : 1;
225 // equal number of rows and columns, compare individual elements
227 for (int r=0; r<row; ++r) {
228 for (int c=0; c<col; ++c) {
229 cmpval=((*this)(r,c)).compare(o(r,c));
230 if (cmpval!=0) return cmpval;
233 // all elements are equal => matrices are equal;
238 // non-virtual functions in this class
245 * @exception logic_error (incompatible matrices) */
246 matrix matrix::add(matrix const & other) const
248 if (col != other.col || row != other.row) {
249 throw (std::logic_error("matrix::add(): incompatible matrices"));
252 vector<ex> sum(this->m);
253 vector<ex>::iterator i;
254 vector<ex>::const_iterator ci;
255 for (i=sum.begin(), ci=other.m.begin();
260 return matrix(row,col,sum);
263 /** Difference of matrices.
265 * @exception logic_error (incompatible matrices) */
266 matrix matrix::sub(matrix const & other) const
268 if (col != other.col || row != other.row) {
269 throw (std::logic_error("matrix::sub(): incompatible matrices"));
272 vector<ex> dif(this->m);
273 vector<ex>::iterator i;
274 vector<ex>::const_iterator ci;
275 for (i=dif.begin(), ci=other.m.begin();
280 return matrix(row,col,dif);
283 /** Product of matrices.
285 * @exception logic_error (incompatible matrices) */
286 matrix matrix::mul(matrix const & other) const
288 if (col != other.row) {
289 throw (std::logic_error("matrix::mul(): incompatible matrices"));
292 vector<ex> prod(row*other.col);
293 for (int i=0; i<row; ++i) {
294 for (int j=0; j<other.col; ++j) {
295 for (int l=0; l<col; ++l) {
296 prod[i*other.col+j] += m[i*col+l] * other.m[l*other.col+j];
300 return matrix(row, other.col, prod);
303 /** operator() to access elements.
305 * @param ro row of element
306 * @param co column of element
307 * @exception range_error (index out of range) */
308 ex const & matrix::operator() (int ro, int co) const
310 if (ro<0 || ro>=row || co<0 || co>=col) {
311 throw (std::range_error("matrix::operator(): index out of range"));
317 /** Set individual elements manually.
319 * @exception range_error (index out of range) */
320 matrix & matrix::set(int ro, int co, ex value)
322 if (ro<0 || ro>=row || co<0 || co>=col) {
323 throw (std::range_error("matrix::set(): index out of range"));
326 ensure_if_modifiable();
331 /** Transposed of an m x n matrix, producing a new n x m matrix object that
332 * represents the transposed. */
333 matrix matrix::transpose(void) const
335 vector<ex> trans(col*row);
337 for (int r=0; r<col; ++r) {
338 for (int c=0; c<row; ++c) {
339 trans[r*row+c] = m[c*col+r];
342 return matrix(col,row,trans);
345 /* Determiant of purely numeric matrix, using pivoting. This routine is only
346 * called internally by matrix::determinant(). */
347 ex determinant_numeric(const matrix & M)
349 ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
354 for (int r1=0; r1<M.rows(); ++r1) {
355 int indx = tmp.pivot(r1);
362 det = det * tmp.m[r1*M.cols()+r1];
363 for (int r2=r1+1; r2<M.rows(); ++r2) {
364 piv = tmp.m[r2*M.cols()+r1] / tmp.m[r1*M.cols()+r1];
365 for (int c=r1+1; c<M.cols(); c++) {
366 tmp.m[r2*M.cols()+c] -= piv * tmp.m[r1*M.cols()+c];
373 // Compute the sign of a permutation of a vector of things, used internally
374 // by determinant_symbolic_perm() where it is instantiated for int.
376 int permutation_sign(vector<T> s)
381 for (typename vector<T>::iterator i=s.begin(); i!=s.end()-1; ++i) {
382 for (typename vector<T>::iterator j=i+1; j!=s.end(); ++j) {
394 /** Determinant built by application of the full permutation group. This
395 * routine is only called internally by matrix::determinant(). */
396 ex determinant_symbolic_perm(const matrix & M)
398 ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
400 if (M.rows()==1) { // speed things up
406 vector<int> sigma(M.cols());
407 for (int i=0; i<M.cols(); ++i) sigma[i]=i;
410 term = M(sigma[0],0);
411 for (int i=1; i<M.cols(); ++i) term *= M(sigma[i],i);
412 det += permutation_sign(sigma)*term;
413 } while (next_permutation(sigma.begin(), sigma.end()));
418 /** Recursive determiant for small matrices having at least one symbolic entry.
419 * This algorithm is also known as Laplace-expansion. This routine is only
420 * called internally by matrix::determinant(). */
421 ex determinant_symbolic_minor(const matrix & M)
423 ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
425 if (M.rows()==1) { // end of recursion
428 if (M.rows()==2) { // speed things up
429 return (M(0,0)*M(1,1)-
432 if (M.rows()==3) { // speed things up even a little more
433 return ((M(2,1)*M(0,2)-M(2,2)*M(0,1))*M(1,0)+
434 (M(1,2)*M(0,1)-M(1,1)*M(0,2))*M(2,0)+
435 (M(2,2)*M(1,1)-M(2,1)*M(1,2))*M(0,0));
439 matrix minorM(M.rows()-1,M.cols()-1);
440 for (int r1=0; r1<M.rows(); ++r1) {
441 // assemble the minor matrix
442 for (int r=0; r<minorM.rows(); ++r) {
443 for (int c=0; c<minorM.cols(); ++c) {
445 minorM.set(r,c,M(r,c+1));
447 minorM.set(r,c,M(r+1,c+1));
453 det -= M(r1,0) * determinant_symbolic_minor(minorM);
455 det += M(r1,0) * determinant_symbolic_minor(minorM);
461 /* Leverrier algorithm for large matrices having at least one symbolic entry.
462 * This routine is only called internally by matrix::determinant(). The
463 * algorithm is deemed bad for symbolic matrices since it returns expressions
464 * that are very hard to canonicalize. */
465 /*ex determinant_symbolic_leverrier(const matrix & M)
467 * ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
470 * matrix I(M.row, M.col);
472 * for (int i=1; i<M.row; ++i) {
473 * for (int j=0; j<M.row; ++j)
474 * I.m[j*M.col+j] = c;
475 * B = M.mul(B.sub(I));
476 * c = B.trace()/ex(i+1);
485 /** Determinant of square matrix. This routine doesn't actually calculate the
486 * determinant, it only implements some heuristics about which algorithm to
487 * call. When the parameter for normalization is explicitly turned off this
488 * method does not normalize its result at the end, which might imply that
489 * the symbolic 2x2 matrix [[a/(a-b),1],[b/(a-b),1]] is not immediatly
490 * recognized to be unity. (This is Mathematica's default behaviour, it
491 * should be used with care.)
493 * @param normalized may be set to false if no normalization of the
494 * result is desired (i.e. to force Mathematica behavior, Maple
495 * does normalize the result).
496 * @return the determinant as a new expression
497 * @exception logic_error (matrix not square) */
498 ex matrix::determinant(bool normalized) const
501 throw (std::logic_error("matrix::determinant(): matrix not square"));
504 // check, if there are non-numeric entries in the matrix:
505 for (vector<ex>::const_iterator r=m.begin(); r!=m.end(); ++r) {
506 if (!(*r).info(info_flags::numeric)) {
508 return determinant_symbolic_minor(*this).normal();
510 return determinant_symbolic_perm(*this);
514 // if it turns out that all elements are numeric
515 return determinant_numeric(*this);
518 /** Trace of a matrix.
520 * @return the sum of diagonal elements
521 * @exception logic_error (matrix not square) */
522 ex matrix::trace(void) const
525 throw (std::logic_error("matrix::trace(): matrix not square"));
529 for (int r=0; r<col; ++r) {
535 /** Characteristic Polynomial. The characteristic polynomial of a matrix M is
536 * defined as the determiant of (M - lambda * 1) where 1 stands for the unit
537 * matrix of the same dimension as M. This method returns the characteristic
538 * polynomial as a new expression.
540 * @return characteristic polynomial as new expression
541 * @exception logic_error (matrix not square)
542 * @see matrix::determinant() */
543 ex matrix::charpoly(ex const & lambda) const
546 throw (std::logic_error("matrix::charpoly(): matrix not square"));
550 for (int r=0; r<col; ++r) {
551 M.m[r*col+r] -= lambda;
553 return (M.determinant());
556 /** Inverse of this matrix.
558 * @return the inverted matrix
559 * @exception logic_error (matrix not square)
560 * @exception runtime_error (singular matrix) */
561 matrix matrix::inverse(void) const
564 throw (std::logic_error("matrix::inverse(): matrix not square"));
568 // set tmp to the unit matrix
569 for (int i=0; i<col; ++i) {
570 tmp.m[i*col+i] = exONE();
572 // create a copy of this matrix
574 for (int r1=0; r1<row; ++r1) {
575 int indx = cpy.pivot(r1);
577 throw (std::runtime_error("matrix::inverse(): singular matrix"));
579 if (indx != 0) { // swap rows r and indx of matrix tmp
580 for (int i=0; i<col; ++i) {
581 tmp.m[r1*col+i].swap(tmp.m[indx*col+i]);
584 ex a1 = cpy.m[r1*col+r1];
585 for (int c=0; c<col; ++c) {
586 cpy.m[r1*col+c] /= a1;
587 tmp.m[r1*col+c] /= a1;
589 for (int r2=0; r2<row; ++r2) {
591 ex a2 = cpy.m[r2*col+r1];
592 for (int c=0; c<col; ++c) {
593 cpy.m[r2*col+c] -= a2 * cpy.m[r1*col+c];
594 tmp.m[r2*col+c] -= a2 * tmp.m[r1*col+c];
602 void matrix::ffe_swap(int r1, int c1, int r2 ,int c2)
604 ensure_if_modifiable();
606 ex tmp=ffe_get(r1,c1);
607 ffe_set(r1,c1,ffe_get(r2,c2));
611 void matrix::ffe_set(int r, int c, ex e)
616 ex matrix::ffe_get(int r, int c) const
618 return operator()(r-1,c-1);
621 /** Solve a set of equations for an m x n matrix by fraction-free Gaussian
622 * elimination. Based on algorithm 9.1 from 'Algorithms for Computer Algebra'
623 * by Keith O. Geddes et al.
625 * @param vars n x p matrix
626 * @param rhs m x p matrix
627 * @exception logic_error (incompatible matrices)
628 * @exception runtime_error (singular matrix) */
629 matrix matrix::fraction_free_elim(matrix const & vars,
630 matrix const & rhs) const
632 if ((row != rhs.row) || (col != vars.row) || (rhs.col != vars.col)) {
633 throw (std::logic_error("matrix::solve(): incompatible matrices"));
636 matrix a(*this); // make a copy of the matrix
637 matrix b(rhs); // make a copy of the rhs vector
639 // given an m x n matrix a, reduce it to upper echelon form
646 // eliminate below row r, with pivot in column k
647 for (int k=1; (k<=n)&&(r<=m); ++k) {
648 // find a nonzero pivot
650 for (p=r; (p<=m)&&(a.ffe_get(p,k).is_equal(exZERO())); ++p) {}
654 // switch rows p and r
655 for (int j=k; j<=n; ++j) {
659 // keep track of sign changes due to row exchange
662 for (int i=r+1; i<=m; ++i) {
663 for (int j=k+1; j<=n; ++j) {
664 a.ffe_set(i,j,(a.ffe_get(r,k)*a.ffe_get(i,j)
665 -a.ffe_get(r,j)*a.ffe_get(i,k))/divisor);
666 a.ffe_set(i,j,a.ffe_get(i,j).normal() /*.normal() */ );
668 b.ffe_set(i,1,(a.ffe_get(r,k)*b.ffe_get(i,1)
669 -b.ffe_get(r,1)*a.ffe_get(i,k))/divisor);
670 b.ffe_set(i,1,b.ffe_get(i,1).normal() /*.normal() */ );
673 divisor=a.ffe_get(r,k);
677 // optionally compute the determinant for square or augmented matrices
678 // if (r==m+1) { det=sign*divisor; } else { det=0; }
681 for (int r=1; r<=m; ++r) {
682 for (int c=1; c<=n; ++c) {
683 cout << a.ffe_get(r,c) << "\t";
685 cout << " | " << b.ffe_get(r,1) << endl;
690 // test if we really have an upper echelon matrix
691 int zero_in_last_row=-1;
692 for (int r=1; r<=m; ++r) {
693 int zero_in_this_row=0;
694 for (int c=1; c<=n; ++c) {
695 if (a.ffe_get(r,c).is_equal(exZERO())) {
701 ASSERT((zero_in_this_row>zero_in_last_row)||(zero_in_this_row=n));
702 zero_in_last_row=zero_in_this_row;
704 #endif // def DOASSERT
708 int last_assigned_sol=n+1;
709 for (int r=m; r>0; --r) {
710 int first_non_zero=1;
711 while ((first_non_zero<=n)&&(a.ffe_get(r,first_non_zero).is_zero())) {
714 if (first_non_zero>n) {
715 // row consists only of zeroes, corresponding rhs must be 0 as well
716 if (!b.ffe_get(r,1).is_zero()) {
717 throw (std::runtime_error("matrix::fraction_free_elim(): singular matrix"));
720 // assign solutions for vars between first_non_zero+1 and
721 // last_assigned_sol-1: free parameters
722 for (int c=first_non_zero+1; c<=last_assigned_sol-1; ++c) {
723 sol.ffe_set(c,1,vars.ffe_get(c,1));
726 for (int c=first_non_zero+1; c<=n; ++c) {
727 e=e-a.ffe_get(r,c)*sol.ffe_get(c,1);
729 sol.ffe_set(first_non_zero,1,
730 (e/a.ffe_get(r,first_non_zero)).normal());
731 last_assigned_sol=first_non_zero;
734 // assign solutions for vars between 1 and
735 // last_assigned_sol-1: free parameters
736 for (int c=1; c<=last_assigned_sol-1; ++c) {
737 sol.ffe_set(c,1,vars.ffe_get(c,1));
741 for (int c=1; c<=n; ++c) {
742 cout << vars.ffe_get(c,1) << "->" << sol.ffe_get(c,1) << endl;
747 // test solution with echelon matrix
748 for (int r=1; r<=m; ++r) {
750 for (int c=1; c<=n; ++c) {
751 e=e+a.ffe_get(r,c)*sol.ffe_get(c,1);
753 if (!(e-b.ffe_get(r,1)).normal().is_zero()) {
755 cout << "b.ffe_get(" << r<<",1)=" << b.ffe_get(r,1) << endl;
756 cout << "diff=" << (e-b.ffe_get(r,1)).normal() << endl;
758 ASSERT((e-b.ffe_get(r,1)).normal().is_zero());
761 // test solution with original matrix
762 for (int r=1; r<=m; ++r) {
764 for (int c=1; c<=n; ++c) {
765 e=e+ffe_get(r,c)*sol.ffe_get(c,1);
768 if (!(e-rhs.ffe_get(r,1)).normal().is_zero()) {
769 cout << "e=" << e << endl;
772 cout << "e.normal()=" << en << endl;
774 cout << "rhs.ffe_get(" << r<<",1)=" << rhs.ffe_get(r,1) << endl;
775 cout << "diff=" << (e-rhs.ffe_get(r,1)).normal() << endl;
778 ex xxx=e-rhs.ffe_get(r,1);
779 cerr << "xxx=" << xxx << endl << endl;
781 ASSERT((e-rhs.ffe_get(r,1)).normal().is_zero());
783 #endif // def DOASSERT
788 /** Solve simultaneous set of equations. */
789 matrix matrix::solve(matrix const & v) const
791 if (!(row == col && col == v.row)) {
792 throw (std::logic_error("matrix::solve(): incompatible matrices"));
795 // build the extended matrix of *this with v attached to the right
796 matrix tmp(row,col+v.col);
797 for (int r=0; r<row; ++r) {
798 for (int c=0; c<col; ++c) {
799 tmp.m[r*tmp.col+c] = m[r*col+c];
801 for (int c=0; c<v.col; ++c) {
802 tmp.m[r*tmp.col+c+col] = v.m[r*v.col+c];
805 for (int r1=0; r1<row; ++r1) {
806 int indx = tmp.pivot(r1);
808 throw (std::runtime_error("matrix::solve(): singular matrix"));
810 for (int c=r1; c<tmp.col; ++c) {
811 tmp.m[r1*tmp.col+c] /= tmp.m[r1*tmp.col+r1];
813 for (int r2=r1+1; r2<row; ++r2) {
814 for (int c=r1; c<tmp.col; ++c) {
816 -= tmp.m[r2*tmp.col+r1] * tmp.m[r1*tmp.col+c];
821 // assemble the solution matrix
822 vector<ex> sol(v.row*v.col);
823 for (int c=0; c<v.col; ++c) {
824 for (int r=col-1; r>=0; --r) {
825 sol[r*v.col+c] = tmp[r*tmp.col+c];
826 for (int i=r+1; i<col; ++i) {
828 -= tmp[r*tmp.col+i] * sol[i*v.col+c];
832 return matrix(v.row, v.col, sol);
837 /** Partial pivoting method.
838 * Usual pivoting returns the index to the element with the largest absolute
839 * value and swaps the current row with the one where the element was found.
840 * Here it does the same with the first non-zero element. (This works fine,
841 * but may be far from optimal for numerics.) */
842 int matrix::pivot(int ro)
846 for (int r=ro; r<row; ++r) {
847 if (!m[r*col+ro].is_zero()) {
852 if (m[k*col+ro].is_zero()) {
855 if (k!=ro) { // swap rows
856 for (int c=0; c<col; ++c) {
857 m[k*col+c].swap(m[ro*col+c]);
868 const matrix some_matrix;
869 type_info const & typeid_matrix=typeid(some_matrix);