3 * Implementation of symbolic matrices */
6 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #ifndef NO_GINAC_NAMESPACE
33 #endif // ndef NO_GINAC_NAMESPACE
35 GINAC_IMPLEMENT_REGISTERED_CLASS(matrix, basic)
38 // default constructor, destructor, copy constructor, assignment operator
44 /** Default ctor. Initializes to 1 x 1-dimensional zero-matrix. */
46 : inherited(TINFO_matrix), row(1), col(1)
48 debugmsg("matrix default constructor",LOGLEVEL_CONSTRUCT);
54 debugmsg("matrix destructor",LOGLEVEL_DESTRUCT);
57 matrix::matrix(const matrix & other)
59 debugmsg("matrix copy constructor",LOGLEVEL_CONSTRUCT);
63 const matrix & matrix::operator=(const matrix & other)
65 debugmsg("matrix operator=",LOGLEVEL_ASSIGNMENT);
75 void matrix::copy(const matrix & other)
77 inherited::copy(other);
80 m=other.m; // use STL's vector copying
83 void matrix::destroy(bool call_parent)
85 if (call_parent) inherited::destroy(call_parent);
94 /** Very common ctor. Initializes to r x c-dimensional zero-matrix.
96 * @param r number of rows
97 * @param c number of cols */
98 matrix::matrix(unsigned r, unsigned c)
99 : inherited(TINFO_matrix), row(r), col(c)
101 debugmsg("matrix constructor from unsigned,unsigned",LOGLEVEL_CONSTRUCT);
102 m.resize(r*c, _ex0());
107 /** Ctor from representation, for internal use only. */
108 matrix::matrix(unsigned r, unsigned c, const exvector & m2)
109 : inherited(TINFO_matrix), row(r), col(c), m(m2)
111 debugmsg("matrix constructor from unsigned,unsigned,exvector",LOGLEVEL_CONSTRUCT);
118 /** Construct object from archive_node. */
119 matrix::matrix(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
121 debugmsg("matrix constructor from archive_node", LOGLEVEL_CONSTRUCT);
122 if (!(n.find_unsigned("row", row)) || !(n.find_unsigned("col", col)))
123 throw (std::runtime_error("unknown matrix dimensions in archive"));
124 m.reserve(row * col);
125 for (unsigned int i=0; true; i++) {
127 if (n.find_ex("m", e, sym_lst, i))
134 /** Unarchive the object. */
135 ex matrix::unarchive(const archive_node &n, const lst &sym_lst)
137 return (new matrix(n, sym_lst))->setflag(status_flags::dynallocated);
140 /** Archive the object. */
141 void matrix::archive(archive_node &n) const
143 inherited::archive(n);
144 n.add_unsigned("row", row);
145 n.add_unsigned("col", col);
146 exvector::const_iterator i = m.begin(), iend = m.end();
154 // functions overriding virtual functions from bases classes
159 basic * matrix::duplicate() const
161 debugmsg("matrix duplicate",LOGLEVEL_DUPLICATE);
162 return new matrix(*this);
165 void matrix::print(ostream & os, unsigned upper_precedence) const
167 debugmsg("matrix print",LOGLEVEL_PRINT);
169 for (unsigned r=0; r<row-1; ++r) {
171 for (unsigned c=0; c<col-1; ++c) {
172 os << m[r*col+c] << ",";
174 os << m[col*(r+1)-1] << "]], ";
177 for (unsigned c=0; c<col-1; ++c) {
178 os << m[(row-1)*col+c] << ",";
180 os << m[row*col-1] << "]] ]]";
183 void matrix::printraw(ostream & os) const
185 debugmsg("matrix printraw",LOGLEVEL_PRINT);
186 os << "matrix(" << row << "," << col <<",";
187 for (unsigned r=0; r<row-1; ++r) {
189 for (unsigned c=0; c<col-1; ++c) {
190 os << m[r*col+c] << ",";
192 os << m[col*(r-1)-1] << "),";
195 for (unsigned c=0; c<col-1; ++c) {
196 os << m[(row-1)*col+c] << ",";
198 os << m[row*col-1] << "))";
201 /** nops is defined to be rows x columns. */
202 unsigned matrix::nops() const
207 /** returns matrix entry at position (i/col, i%col). */
208 ex & matrix::let_op(int i)
213 /** expands the elements of a matrix entry by entry. */
214 ex matrix::expand(unsigned options) const
216 exvector tmp(row*col);
217 for (unsigned i=0; i<row*col; ++i) {
218 tmp[i]=m[i].expand(options);
220 return matrix(row, col, tmp);
223 /** Search ocurrences. A matrix 'has' an expression if it is the expression
224 * itself or one of the elements 'has' it. */
225 bool matrix::has(const ex & other) const
227 GINAC_ASSERT(other.bp!=0);
229 // tautology: it is the expression itself
230 if (is_equal(*other.bp)) return true;
232 // search all the elements
233 for (exvector::const_iterator r=m.begin(); r!=m.end(); ++r) {
234 if ((*r).has(other)) return true;
239 /** evaluate matrix entry by entry. */
240 ex matrix::eval(int level) const
242 debugmsg("matrix eval",LOGLEVEL_MEMBER_FUNCTION);
244 // check if we have to do anything at all
245 if ((level==1)&&(flags & status_flags::evaluated)) {
250 if (level == -max_recursion_level) {
251 throw (std::runtime_error("matrix::eval(): recursion limit exceeded"));
254 // eval() entry by entry
255 exvector m2(row*col);
257 for (unsigned r=0; r<row; ++r) {
258 for (unsigned c=0; c<col; ++c) {
259 m2[r*col+c] = m[r*col+c].eval(level);
263 return (new matrix(row, col, m2))->setflag(status_flags::dynallocated |
264 status_flags::evaluated );
267 /** evaluate matrix numerically entry by entry. */
268 ex matrix::evalf(int level) const
270 debugmsg("matrix evalf",LOGLEVEL_MEMBER_FUNCTION);
272 // check if we have to do anything at all
278 if (level == -max_recursion_level) {
279 throw (std::runtime_error("matrix::evalf(): recursion limit exceeded"));
282 // evalf() entry by entry
283 exvector m2(row*col);
285 for (unsigned r=0; r<row; ++r) {
286 for (unsigned c=0; c<col; ++c) {
287 m2[r*col+c] = m[r*col+c].evalf(level);
290 return matrix(row, col, m2);
295 int matrix::compare_same_type(const basic & other) const
297 GINAC_ASSERT(is_exactly_of_type(other, matrix));
298 const matrix & o=static_cast<matrix &>(const_cast<basic &>(other));
300 // compare number of rows
301 if (row != o.rows()) {
302 return row < o.rows() ? -1 : 1;
305 // compare number of columns
306 if (col != o.cols()) {
307 return col < o.cols() ? -1 : 1;
310 // equal number of rows and columns, compare individual elements
312 for (unsigned r=0; r<row; ++r) {
313 for (unsigned c=0; c<col; ++c) {
314 cmpval=((*this)(r,c)).compare(o(r,c));
315 if (cmpval!=0) return cmpval;
318 // all elements are equal => matrices are equal;
323 // non-virtual functions in this class
330 * @exception logic_error (incompatible matrices) */
331 matrix matrix::add(const matrix & other) const
333 if (col != other.col || row != other.row) {
334 throw (std::logic_error("matrix::add(): incompatible matrices"));
337 exvector sum(this->m);
338 exvector::iterator i;
339 exvector::const_iterator ci;
340 for (i=sum.begin(), ci=other.m.begin();
345 return matrix(row,col,sum);
348 /** Difference of matrices.
350 * @exception logic_error (incompatible matrices) */
351 matrix matrix::sub(const matrix & other) const
353 if (col != other.col || row != other.row) {
354 throw (std::logic_error("matrix::sub(): incompatible matrices"));
357 exvector dif(this->m);
358 exvector::iterator i;
359 exvector::const_iterator ci;
360 for (i=dif.begin(), ci=other.m.begin();
365 return matrix(row,col,dif);
368 /** Product of matrices.
370 * @exception logic_error (incompatible matrices) */
371 matrix matrix::mul(const matrix & other) const
373 if (col != other.row) {
374 throw (std::logic_error("matrix::mul(): incompatible matrices"));
377 exvector prod(row*other.col);
378 for (unsigned i=0; i<row; ++i) {
379 for (unsigned j=0; j<other.col; ++j) {
380 for (unsigned l=0; l<col; ++l) {
381 prod[i*other.col+j] += m[i*col+l] * other.m[l*other.col+j];
385 return matrix(row, other.col, prod);
388 /** operator() to access elements.
390 * @param ro row of element
391 * @param co column of element
392 * @exception range_error (index out of range) */
393 const ex & matrix::operator() (unsigned ro, unsigned co) const
395 if (ro<0 || ro>=row || co<0 || co>=col) {
396 throw (std::range_error("matrix::operator(): index out of range"));
402 /** Set individual elements manually.
404 * @exception range_error (index out of range) */
405 matrix & matrix::set(unsigned ro, unsigned co, ex value)
407 if (ro<0 || ro>=row || co<0 || co>=col) {
408 throw (std::range_error("matrix::set(): index out of range"));
411 ensure_if_modifiable();
416 /** Transposed of an m x n matrix, producing a new n x m matrix object that
417 * represents the transposed. */
418 matrix matrix::transpose(void) const
420 exvector trans(col*row);
422 for (unsigned r=0; r<col; ++r) {
423 for (unsigned c=0; c<row; ++c) {
424 trans[r*row+c] = m[c*col+r];
427 return matrix(col,row,trans);
430 /* Determiant of purely numeric matrix, using pivoting. This routine is only
431 * called internally by matrix::determinant(). */
432 ex determinant_numeric(const matrix & M)
434 GINAC_ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
439 for (unsigned r1=0; r1<M.rows(); ++r1) {
440 int indx = tmp.pivot(r1);
447 det = det * tmp.m[r1*M.cols()+r1];
448 for (unsigned r2=r1+1; r2<M.rows(); ++r2) {
449 piv = tmp.m[r2*M.cols()+r1] / tmp.m[r1*M.cols()+r1];
450 for (unsigned c=r1+1; c<M.cols(); c++) {
451 tmp.m[r2*M.cols()+c] -= piv * tmp.m[r1*M.cols()+c];
458 // Compute the sign of a permutation of a vector of things, used internally
459 // by determinant_symbolic_perm() where it is instantiated for int.
461 int permutation_sign(vector<T> s)
466 for (typename vector<T>::iterator i=s.begin(); i!=s.end()-1; ++i) {
467 for (typename vector<T>::iterator j=i+1; j!=s.end(); ++j) {
479 /** Determinant built by application of the full permutation group. This
480 * routine is only called internally by matrix::determinant(). */
481 ex determinant_symbolic_perm(const matrix & M)
483 GINAC_ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
485 if (M.rows()==1) { // speed things up
491 vector<unsigned> sigma(M.cols());
492 for (unsigned i=0; i<M.cols(); ++i) sigma[i]=i;
495 term = M(sigma[0],0);
496 for (unsigned i=1; i<M.cols(); ++i) term *= M(sigma[i],i);
497 det += permutation_sign(sigma)*term;
498 } while (next_permutation(sigma.begin(), sigma.end()));
503 /** Recursive determiant for small matrices having at least one symbolic entry.
504 * This algorithm is also known as Laplace-expansion. This routine is only
505 * called internally by matrix::determinant(). */
506 ex determinant_symbolic_minor(const matrix & M)
508 GINAC_ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
510 if (M.rows()==1) { // end of recursion
513 if (M.rows()==2) { // speed things up
514 return (M(0,0)*M(1,1)-
517 if (M.rows()==3) { // speed things up even a little more
518 return ((M(2,1)*M(0,2)-M(2,2)*M(0,1))*M(1,0)+
519 (M(1,2)*M(0,1)-M(1,1)*M(0,2))*M(2,0)+
520 (M(2,2)*M(1,1)-M(2,1)*M(1,2))*M(0,0));
524 matrix minorM(M.rows()-1,M.cols()-1);
525 for (unsigned r1=0; r1<M.rows(); ++r1) {
526 // assemble the minor matrix
527 for (unsigned r=0; r<minorM.rows(); ++r) {
528 for (unsigned c=0; c<minorM.cols(); ++c) {
530 minorM.set(r,c,M(r,c+1));
532 minorM.set(r,c,M(r+1,c+1));
538 det -= M(r1,0) * determinant_symbolic_minor(minorM);
540 det += M(r1,0) * determinant_symbolic_minor(minorM);
546 /* Leverrier algorithm for large matrices having at least one symbolic entry.
547 * This routine is only called internally by matrix::determinant(). The
548 * algorithm is deemed bad for symbolic matrices since it returns expressions
549 * that are very hard to canonicalize. */
550 /*ex determinant_symbolic_leverrier(const matrix & M)
552 * GINAC_ASSERT(M.rows()==M.cols()); // cannot happen, just in case...
555 * matrix I(M.row, M.col);
557 * for (unsigned i=1; i<M.row; ++i) {
558 * for (unsigned j=0; j<M.row; ++j)
559 * I.m[j*M.col+j] = c;
560 * B = M.mul(B.sub(I));
561 * c = B.trace()/ex(i+1);
570 /** Determinant of square matrix. This routine doesn't actually calculate the
571 * determinant, it only implements some heuristics about which algorithm to
572 * call. When the parameter for normalization is explicitly turned off this
573 * method does not normalize its result at the end, which might imply that
574 * the symbolic 2x2 matrix [[a/(a-b),1],[b/(a-b),1]] is not immediatly
575 * recognized to be unity. (This is Mathematica's default behaviour, it
576 * should be used with care.)
578 * @param normalized may be set to false if no normalization of the
579 * result is desired (i.e. to force Mathematica behavior, Maple
580 * does normalize the result).
581 * @return the determinant as a new expression
582 * @exception logic_error (matrix not square) */
583 ex matrix::determinant(bool normalized) const
586 throw (std::logic_error("matrix::determinant(): matrix not square"));
589 // check, if there are non-numeric entries in the matrix:
590 for (exvector::const_iterator r=m.begin(); r!=m.end(); ++r) {
591 if (!(*r).info(info_flags::numeric)) {
593 return determinant_symbolic_minor(*this).normal();
595 return determinant_symbolic_perm(*this);
599 // if it turns out that all elements are numeric
600 return determinant_numeric(*this);
603 /** Trace of a matrix.
605 * @return the sum of diagonal elements
606 * @exception logic_error (matrix not square) */
607 ex matrix::trace(void) const
610 throw (std::logic_error("matrix::trace(): matrix not square"));
614 for (unsigned r=0; r<col; ++r) {
620 /** Characteristic Polynomial. The characteristic polynomial of a matrix M is
621 * defined as the determiant of (M - lambda * 1) where 1 stands for the unit
622 * matrix of the same dimension as M. This method returns the characteristic
623 * polynomial as a new expression.
625 * @return characteristic polynomial as new expression
626 * @exception logic_error (matrix not square)
627 * @see matrix::determinant() */
628 ex matrix::charpoly(const ex & lambda) const
631 throw (std::logic_error("matrix::charpoly(): matrix not square"));
635 for (unsigned r=0; r<col; ++r) {
636 M.m[r*col+r] -= lambda;
638 return (M.determinant());
641 /** Inverse of this matrix.
643 * @return the inverted matrix
644 * @exception logic_error (matrix not square)
645 * @exception runtime_error (singular matrix) */
646 matrix matrix::inverse(void) const
649 throw (std::logic_error("matrix::inverse(): matrix not square"));
653 // set tmp to the unit matrix
654 for (unsigned i=0; i<col; ++i) {
655 tmp.m[i*col+i] = _ex1();
657 // create a copy of this matrix
659 for (unsigned r1=0; r1<row; ++r1) {
660 int indx = cpy.pivot(r1);
662 throw (std::runtime_error("matrix::inverse(): singular matrix"));
664 if (indx != 0) { // swap rows r and indx of matrix tmp
665 for (unsigned i=0; i<col; ++i) {
666 tmp.m[r1*col+i].swap(tmp.m[indx*col+i]);
669 ex a1 = cpy.m[r1*col+r1];
670 for (unsigned c=0; c<col; ++c) {
671 cpy.m[r1*col+c] /= a1;
672 tmp.m[r1*col+c] /= a1;
674 for (unsigned r2=0; r2<row; ++r2) {
676 ex a2 = cpy.m[r2*col+r1];
677 for (unsigned c=0; c<col; ++c) {
678 cpy.m[r2*col+c] -= a2 * cpy.m[r1*col+c];
679 tmp.m[r2*col+c] -= a2 * tmp.m[r1*col+c];
687 void matrix::ffe_swap(unsigned r1, unsigned c1, unsigned r2 ,unsigned c2)
689 ensure_if_modifiable();
691 ex tmp=ffe_get(r1,c1);
692 ffe_set(r1,c1,ffe_get(r2,c2));
696 void matrix::ffe_set(unsigned r, unsigned c, ex e)
701 ex matrix::ffe_get(unsigned r, unsigned c) const
703 return operator()(r-1,c-1);
706 /** Solve a set of equations for an m x n matrix by fraction-free Gaussian
707 * elimination. Based on algorithm 9.1 from 'Algorithms for Computer Algebra'
708 * by Keith O. Geddes et al.
710 * @param vars n x p matrix
711 * @param rhs m x p matrix
712 * @exception logic_error (incompatible matrices)
713 * @exception runtime_error (singular matrix) */
714 matrix matrix::fraction_free_elim(const matrix & vars,
715 const matrix & rhs) const
717 if ((row != rhs.row) || (col != vars.row) || (rhs.col != vars.col)) {
718 throw (std::logic_error("matrix::solve(): incompatible matrices"));
721 matrix a(*this); // make a copy of the matrix
722 matrix b(rhs); // make a copy of the rhs vector
724 // given an m x n matrix a, reduce it to upper echelon form
731 // eliminate below row r, with pivot in column k
732 for (unsigned k=1; (k<=n)&&(r<=m); ++k) {
733 // find a nonzero pivot
735 for (p=r; (p<=m)&&(a.ffe_get(p,k).is_equal(_ex0())); ++p) {}
739 // switch rows p and r
740 for (unsigned j=k; j<=n; ++j) {
744 // keep track of sign changes due to row exchange
747 for (unsigned i=r+1; i<=m; ++i) {
748 for (unsigned j=k+1; j<=n; ++j) {
749 a.ffe_set(i,j,(a.ffe_get(r,k)*a.ffe_get(i,j)
750 -a.ffe_get(r,j)*a.ffe_get(i,k))/divisor);
751 a.ffe_set(i,j,a.ffe_get(i,j).normal() /*.normal() */ );
753 b.ffe_set(i,1,(a.ffe_get(r,k)*b.ffe_get(i,1)
754 -b.ffe_get(r,1)*a.ffe_get(i,k))/divisor);
755 b.ffe_set(i,1,b.ffe_get(i,1).normal() /*.normal() */ );
758 divisor=a.ffe_get(r,k);
762 // optionally compute the determinant for square or augmented matrices
763 // if (r==m+1) { det=sign*divisor; } else { det=0; }
766 for (unsigned r=1; r<=m; ++r) {
767 for (unsigned c=1; c<=n; ++c) {
768 cout << a.ffe_get(r,c) << "\t";
770 cout << " | " << b.ffe_get(r,1) << endl;
774 #ifdef DO_GINAC_ASSERT
775 // test if we really have an upper echelon matrix
776 int zero_in_last_row=-1;
777 for (unsigned r=1; r<=m; ++r) {
778 int zero_in_this_row=0;
779 for (unsigned c=1; c<=n; ++c) {
780 if (a.ffe_get(r,c).is_equal(_ex0())) {
786 GINAC_ASSERT((zero_in_this_row>zero_in_last_row)||(zero_in_this_row=n));
787 zero_in_last_row=zero_in_this_row;
789 #endif // def DO_GINAC_ASSERT
793 unsigned last_assigned_sol=n+1;
794 for (unsigned r=m; r>0; --r) {
795 unsigned first_non_zero=1;
796 while ((first_non_zero<=n)&&(a.ffe_get(r,first_non_zero).is_zero())) {
799 if (first_non_zero>n) {
800 // row consists only of zeroes, corresponding rhs must be 0 as well
801 if (!b.ffe_get(r,1).is_zero()) {
802 throw (std::runtime_error("matrix::fraction_free_elim(): singular matrix"));
805 // assign solutions for vars between first_non_zero+1 and
806 // last_assigned_sol-1: free parameters
807 for (unsigned c=first_non_zero+1; c<=last_assigned_sol-1; ++c) {
808 sol.ffe_set(c,1,vars.ffe_get(c,1));
811 for (unsigned c=first_non_zero+1; c<=n; ++c) {
812 e=e-a.ffe_get(r,c)*sol.ffe_get(c,1);
814 sol.ffe_set(first_non_zero,1,
815 (e/a.ffe_get(r,first_non_zero)).normal());
816 last_assigned_sol=first_non_zero;
819 // assign solutions for vars between 1 and
820 // last_assigned_sol-1: free parameters
821 for (unsigned c=1; c<=last_assigned_sol-1; ++c) {
822 sol.ffe_set(c,1,vars.ffe_get(c,1));
826 for (unsigned c=1; c<=n; ++c) {
827 cout << vars.ffe_get(c,1) << "->" << sol.ffe_get(c,1) << endl;
831 #ifdef DO_GINAC_ASSERT
832 // test solution with echelon matrix
833 for (unsigned r=1; r<=m; ++r) {
835 for (unsigned c=1; c<=n; ++c) {
836 e=e+a.ffe_get(r,c)*sol.ffe_get(c,1);
838 if (!(e-b.ffe_get(r,1)).normal().is_zero()) {
840 cout << "b.ffe_get(" << r<<",1)=" << b.ffe_get(r,1) << endl;
841 cout << "diff=" << (e-b.ffe_get(r,1)).normal() << endl;
843 GINAC_ASSERT((e-b.ffe_get(r,1)).normal().is_zero());
846 // test solution with original matrix
847 for (unsigned r=1; r<=m; ++r) {
849 for (unsigned c=1; c<=n; ++c) {
850 e=e+ffe_get(r,c)*sol.ffe_get(c,1);
853 if (!(e-rhs.ffe_get(r,1)).normal().is_zero()) {
854 cout << "e=" << e << endl;
857 cout << "e.normal()=" << en << endl;
859 cout << "rhs.ffe_get(" << r<<",1)=" << rhs.ffe_get(r,1) << endl;
860 cout << "diff=" << (e-rhs.ffe_get(r,1)).normal() << endl;
863 ex xxx=e-rhs.ffe_get(r,1);
864 cerr << "xxx=" << xxx << endl << endl;
866 GINAC_ASSERT((e-rhs.ffe_get(r,1)).normal().is_zero());
868 #endif // def DO_GINAC_ASSERT
873 /** Solve simultaneous set of equations. */
874 matrix matrix::solve(const matrix & v) const
876 if (!(row == col && col == v.row)) {
877 throw (std::logic_error("matrix::solve(): incompatible matrices"));
880 // build the extended matrix of *this with v attached to the right
881 matrix tmp(row,col+v.col);
882 for (unsigned r=0; r<row; ++r) {
883 for (unsigned c=0; c<col; ++c) {
884 tmp.m[r*tmp.col+c] = m[r*col+c];
886 for (unsigned c=0; c<v.col; ++c) {
887 tmp.m[r*tmp.col+c+col] = v.m[r*v.col+c];
890 for (unsigned r1=0; r1<row; ++r1) {
891 int indx = tmp.pivot(r1);
893 throw (std::runtime_error("matrix::solve(): singular matrix"));
895 for (unsigned c=r1; c<tmp.col; ++c) {
896 tmp.m[r1*tmp.col+c] /= tmp.m[r1*tmp.col+r1];
898 for (unsigned r2=r1+1; r2<row; ++r2) {
899 for (unsigned c=r1; c<tmp.col; ++c) {
901 -= tmp.m[r2*tmp.col+r1] * tmp.m[r1*tmp.col+c];
906 // assemble the solution matrix
907 exvector sol(v.row*v.col);
908 for (unsigned c=0; c<v.col; ++c) {
909 for (unsigned r=col-1; r>=0; --r) {
910 sol[r*v.col+c] = tmp[r*tmp.col+c];
911 for (unsigned i=r+1; i<col; ++i) {
913 -= tmp[r*tmp.col+i] * sol[i*v.col+c];
917 return matrix(v.row, v.col, sol);
922 /** Partial pivoting method.
923 * Usual pivoting returns the index to the element with the largest absolute
924 * value and swaps the current row with the one where the element was found.
925 * Here it does the same with the first non-zero element. (This works fine,
926 * but may be far from optimal for numerics.) */
927 int matrix::pivot(unsigned ro)
931 for (unsigned r=ro; r<row; ++r) {
932 if (!m[r*col+ro].is_zero()) {
937 if (m[k*col+ro].is_zero()) {
940 if (k!=ro) { // swap rows
941 for (unsigned c=0; c<col; ++c) {
942 m[k*col+c].swap(m[ro*col+c]);
953 const matrix some_matrix;
954 const type_info & typeid_matrix=typeid(some_matrix);
956 #ifndef NO_GINAC_NAMESPACE
958 #endif // ndef NO_GINAC_NAMESPACE