]> www.ginac.de Git - ginac.git/blob - ginac/matrix.cpp
a63275f54338e7688065a79176091e36902503a1
[ginac.git] / ginac / matrix.cpp
1 /** @file matrix.cpp
2  *
3  *  Implementation of symbolic matrices */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <algorithm>
24 #include <map>
25 #include <stdexcept>
26
27 #include "matrix.h"
28 #include "archive.h"
29 #include "numeric.h"
30 #include "lst.h"
31 #include "idx.h"
32 #include "indexed.h"
33 #include "utils.h"
34 #include "debugmsg.h"
35 #include "power.h"
36 #include "symbol.h"
37 #include "normal.h"
38
39 namespace GiNaC {
40
41 GINAC_IMPLEMENT_REGISTERED_CLASS(matrix, basic)
42
43 //////////
44 // default ctor, dtor, copy ctor, assignment operator and helpers:
45 //////////
46
47 // public
48
49 /** Default ctor.  Initializes to 1 x 1-dimensional zero-matrix. */
50 matrix::matrix() : inherited(TINFO_matrix), row(1), col(1)
51 {
52         debugmsg("matrix default ctor",LOGLEVEL_CONSTRUCT);
53         m.push_back(_ex0());
54 }
55
56 // protected
57
58 /** For use by copy ctor and assignment operator. */
59 void matrix::copy(const matrix & other)
60 {
61         inherited::copy(other);
62         row = other.row;
63         col = other.col;
64         m = other.m;  // STL's vector copying invoked here
65 }
66
67 void matrix::destroy(bool call_parent)
68 {
69         if (call_parent) inherited::destroy(call_parent);
70 }
71
72 //////////
73 // other ctors
74 //////////
75
76 // public
77
78 /** Very common ctor.  Initializes to r x c-dimensional zero-matrix.
79  *
80  *  @param r number of rows
81  *  @param c number of cols */
82 matrix::matrix(unsigned r, unsigned c)
83   : inherited(TINFO_matrix), row(r), col(c)
84 {
85         debugmsg("matrix ctor from unsigned,unsigned",LOGLEVEL_CONSTRUCT);
86         m.resize(r*c, _ex0());
87 }
88
89 // protected
90
91 /** Ctor from representation, for internal use only. */
92 matrix::matrix(unsigned r, unsigned c, const exvector & m2)
93   : inherited(TINFO_matrix), row(r), col(c), m(m2)
94 {
95         debugmsg("matrix ctor from unsigned,unsigned,exvector",LOGLEVEL_CONSTRUCT);
96 }
97
98 //////////
99 // archiving
100 //////////
101
102 /** Construct object from archive_node. */
103 matrix::matrix(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
104 {
105         debugmsg("matrix ctor from archive_node", LOGLEVEL_CONSTRUCT);
106         if (!(n.find_unsigned("row", row)) || !(n.find_unsigned("col", col)))
107                 throw (std::runtime_error("unknown matrix dimensions in archive"));
108         m.reserve(row * col);
109         for (unsigned int i=0; true; i++) {
110                 ex e;
111                 if (n.find_ex("m", e, sym_lst, i))
112                         m.push_back(e);
113                 else
114                         break;
115         }
116 }
117
118 /** Unarchive the object. */
119 ex matrix::unarchive(const archive_node &n, const lst &sym_lst)
120 {
121         return (new matrix(n, sym_lst))->setflag(status_flags::dynallocated);
122 }
123
124 /** Archive the object. */
125 void matrix::archive(archive_node &n) const
126 {
127         inherited::archive(n);
128         n.add_unsigned("row", row);
129         n.add_unsigned("col", col);
130         exvector::const_iterator i = m.begin(), iend = m.end();
131         while (i != iend) {
132                 n.add_ex("m", *i);
133                 ++i;
134         }
135 }
136
137 //////////
138 // functions overriding virtual functions from bases classes
139 //////////
140
141 // public
142
143 void matrix::print(std::ostream & os, unsigned upper_precedence) const
144 {
145         debugmsg("matrix print",LOGLEVEL_PRINT);
146         os << "[[ ";
147         for (unsigned r=0; r<row-1; ++r) {
148                 os << "[[";
149                 for (unsigned c=0; c<col-1; ++c)
150                         os << m[r*col+c] << ",";
151                 os << m[col*(r+1)-1] << "]], ";
152         }
153         os << "[[";
154         for (unsigned c=0; c<col-1; ++c)
155                 os << m[(row-1)*col+c] << ",";
156         os << m[row*col-1] << "]] ]]";
157 }
158
159 void matrix::printraw(std::ostream & os) const
160 {
161         debugmsg("matrix printraw",LOGLEVEL_PRINT);
162         os << class_name() << "(" << row << "," << col <<",";
163         for (unsigned r=0; r<row-1; ++r) {
164                 os << "(";
165                 for (unsigned c=0; c<col-1; ++c)
166                         os << m[r*col+c] << ",";
167                 os << m[col*(r-1)-1] << "),";
168         }
169         os << "(";
170         for (unsigned c=0; c<col-1; ++c)
171                 os << m[(row-1)*col+c] << ",";
172         os << m[row*col-1] << "))";
173 }
174
175 /** nops is defined to be rows x columns. */
176 unsigned matrix::nops() const
177 {
178         return row*col;
179 }
180
181 /** returns matrix entry at position (i/col, i%col). */
182 ex matrix::op(int i) const
183 {
184         return m[i];
185 }
186
187 /** returns matrix entry at position (i/col, i%col). */
188 ex & matrix::let_op(int i)
189 {
190         GINAC_ASSERT(i>=0);
191         GINAC_ASSERT(i<nops());
192         
193         return m[i];
194 }
195
196 /** expands the elements of a matrix entry by entry. */
197 ex matrix::expand(unsigned options) const
198 {
199         exvector tmp(row*col);
200         for (unsigned i=0; i<row*col; ++i)
201                 tmp[i] = m[i].expand(options);
202         
203         return matrix(row, col, tmp);
204 }
205
206 /** Search ocurrences.  A matrix 'has' an expression if it is the expression
207  *  itself or one of the elements 'has' it. */
208 bool matrix::has(const ex & other) const
209 {
210         GINAC_ASSERT(other.bp!=0);
211         
212         // tautology: it is the expression itself
213         if (is_equal(*other.bp)) return true;
214         
215         // search all the elements
216         for (exvector::const_iterator r=m.begin(); r!=m.end(); ++r)
217                 if ((*r).has(other)) return true;
218         
219         return false;
220 }
221
222 /** Evaluate matrix entry by entry. */
223 ex matrix::eval(int level) const
224 {
225         debugmsg("matrix eval",LOGLEVEL_MEMBER_FUNCTION);
226         
227         // check if we have to do anything at all
228         if ((level==1)&&(flags & status_flags::evaluated))
229                 return *this;
230         
231         // emergency break
232         if (level == -max_recursion_level)
233                 throw (std::runtime_error("matrix::eval(): recursion limit exceeded"));
234         
235         // eval() entry by entry
236         exvector m2(row*col);
237         --level;
238         for (unsigned r=0; r<row; ++r)
239                 for (unsigned c=0; c<col; ++c)
240                         m2[r*col+c] = m[r*col+c].eval(level);
241         
242         return (new matrix(row, col, m2))->setflag(status_flags::dynallocated |
243                                                                                            status_flags::evaluated );
244 }
245
246 /** Evaluate matrix numerically entry by entry. */
247 ex matrix::evalf(int level) const
248 {
249         debugmsg("matrix evalf",LOGLEVEL_MEMBER_FUNCTION);
250                 
251         // check if we have to do anything at all
252         if (level==1)
253                 return *this;
254         
255         // emergency break
256         if (level == -max_recursion_level) {
257                 throw (std::runtime_error("matrix::evalf(): recursion limit exceeded"));
258         }
259         
260         // evalf() entry by entry
261         exvector m2(row*col);
262         --level;
263         for (unsigned r=0; r<row; ++r)
264                 for (unsigned c=0; c<col; ++c)
265                         m2[r*col+c] = m[r*col+c].evalf(level);
266         
267         return matrix(row, col, m2);
268 }
269
270 ex matrix::subs(const lst & ls, const lst & lr) const
271 {
272         exvector m2(row * col);
273         for (unsigned r=0; r<row; ++r)
274                 for (unsigned c=0; c<col; ++c)
275                         m2[r*col+c] = m[r*col+c].subs(ls, lr);
276
277         return matrix(row, col, m2);
278 }
279
280 // protected
281
282 int matrix::compare_same_type(const basic & other) const
283 {
284         GINAC_ASSERT(is_exactly_of_type(other, matrix));
285         const matrix & o = static_cast<matrix &>(const_cast<basic &>(other));
286         
287         // compare number of rows
288         if (row != o.rows())
289                 return row < o.rows() ? -1 : 1;
290         
291         // compare number of columns
292         if (col != o.cols())
293                 return col < o.cols() ? -1 : 1;
294         
295         // equal number of rows and columns, compare individual elements
296         int cmpval;
297         for (unsigned r=0; r<row; ++r) {
298                 for (unsigned c=0; c<col; ++c) {
299                         cmpval = ((*this)(r,c)).compare(o(r,c));
300                         if (cmpval!=0) return cmpval;
301                 }
302         }
303         // all elements are equal => matrices are equal;
304         return 0;
305 }
306
307 /** Automatic symbolic evaluation of an indexed matrix. */
308 ex matrix::eval_indexed(const basic & i) const
309 {
310         GINAC_ASSERT(is_of_type(i, indexed));
311         GINAC_ASSERT(is_ex_of_type(i.op(0), matrix));
312
313         bool all_indices_unsigned = static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint);
314
315         // Check indices
316         if (i.nops() == 2) {
317
318                 // One index, must be one-dimensional vector
319                 if (row != 1 && col != 1)
320                         throw (std::runtime_error("matrix::eval_indexed(): vector must have exactly 1 index"));
321
322                 const idx & i1 = ex_to_idx(i.op(1));
323
324                 if (col == 1) {
325
326                         // Column vector
327                         if (!i1.get_dim().is_equal(row))
328                                 throw (std::runtime_error("matrix::eval_indexed(): dimension of index must match number of vector elements"));
329
330                         // Index numeric -> return vector element
331                         if (all_indices_unsigned) {
332                                 unsigned n1 = ex_to_numeric(i1.get_value()).to_int();
333                                 if (n1 >= row)
334                                         throw (std::runtime_error("matrix::eval_indexed(): value of index exceeds number of vector elements"));
335                                 return (*this)(n1, 0);
336                         }
337
338                 } else {
339
340                         // Row vector
341                         if (!i1.get_dim().is_equal(col))
342                                 throw (std::runtime_error("matrix::eval_indexed(): dimension of index must match number of vector elements"));
343
344                         // Index numeric -> return vector element
345                         if (all_indices_unsigned) {
346                                 unsigned n1 = ex_to_numeric(i1.get_value()).to_int();
347                                 if (n1 >= col)
348                                         throw (std::runtime_error("matrix::eval_indexed(): value of index exceeds number of vector elements"));
349                                 return (*this)(0, n1);
350                         }
351                 }
352
353         } else if (i.nops() == 3) {
354
355                 // Two indices
356                 const idx & i1 = ex_to_idx(i.op(1));
357                 const idx & i2 = ex_to_idx(i.op(2));
358
359                 if (!i1.get_dim().is_equal(row))
360                         throw (std::runtime_error("matrix::eval_indexed(): dimension of first index must match number of rows"));
361                 if (!i2.get_dim().is_equal(col))
362                         throw (std::runtime_error("matrix::eval_indexed(): dimension of second index must match number of columns"));
363
364                 // Pair of dummy indices -> compute trace
365                 if (is_dummy_pair(i1, i2))
366                         return trace();
367
368                 // Both indices numeric -> return matrix element
369                 if (all_indices_unsigned) {
370                         unsigned n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
371                         if (n1 >= row)
372                                 throw (std::runtime_error("matrix::eval_indexed(): value of first index exceeds number of rows"));
373                         if (n2 >= col)
374                                 throw (std::runtime_error("matrix::eval_indexed(): value of second index exceeds number of columns"));
375                         return (*this)(n1, n2);
376                 }
377
378         } else
379                 throw (std::runtime_error("matrix::eval_indexed(): matrix must have exactly 2 indices"));
380
381         return i.hold();
382 }
383
384 /** Contraction of an indexed matrix with something else. */
385 bool matrix::contract_with(ex & self, ex & other) const
386 {
387         GINAC_ASSERT(is_ex_of_type(self, indexed));
388         GINAC_ASSERT(is_ex_of_type(other, indexed));
389         GINAC_ASSERT(self.nops() == 2 || self.nops() == 3);
390         GINAC_ASSERT(is_ex_of_type(self.op(0), matrix));
391
392         // Only contract with other matrices
393         if (!is_ex_of_type(other.op(0), matrix))
394                 return false;
395
396         GINAC_ASSERT(other.nops() == 2 || other.nops() == 3);
397
398         const matrix &self_matrix = ex_to_matrix(self.op(0));
399         const matrix &other_matrix = ex_to_matrix(other.op(0));
400
401         if (self.nops() == 2) {
402                 unsigned self_dim = (self_matrix.col == 1) ? self_matrix.row : self_matrix.col;
403
404                 if (other.nops() == 2) { // vector * vector (scalar product)
405                         unsigned other_dim = (other_matrix.col == 1) ? other_matrix.row : other_matrix.col;
406
407                         if (self_matrix.col == 1) {
408                                 if (other_matrix.col == 1) {
409                                         // Column vector * column vector, transpose first vector
410                                         self = self_matrix.transpose().mul(other_matrix)(0, 0);
411                                 } else {
412                                         // Column vector * row vector, swap factors
413                                         self = other_matrix.mul(self_matrix)(0, 0);
414                                 }
415                         } else {
416                                 if (other_matrix.col == 1) {
417                                         // Row vector * column vector, perfect
418                                         self = self_matrix.mul(other_matrix)(0, 0);
419                                 } else {
420                                         // Row vector * row vector, transpose second vector
421                                         self = self_matrix.mul(other_matrix.transpose())(0, 0);
422                                 }
423                         }
424                         other = _ex1();
425                         return true;
426
427                 } else { // vector * matrix
428
429                         GINAC_ASSERT(other.nops() == 3);
430
431                         // B_i * A_ij = (B*A)_j (B is row vector)
432                         if (is_dummy_pair(self.op(1), other.op(1))) {
433                                 if (self_matrix.row == 1)
434                                         self = indexed(self_matrix.mul(other_matrix), other.op(2));
435                                 else
436                                         self = indexed(self_matrix.transpose().mul(other_matrix), other.op(2));
437                                 other = _ex1();
438                                 return true;
439                         }
440
441                         // B_j * A_ij = (A*B)_i (B is column vector)
442                         if (is_dummy_pair(self.op(1), other.op(2))) {
443                                 if (self_matrix.col == 1)
444                                         self = indexed(other_matrix.mul(self_matrix), other.op(1));
445                                 else
446                                         self = indexed(other_matrix.mul(self_matrix.transpose()), other.op(1));
447                                 other = _ex1();
448                                 return true;
449                         }
450                 }
451
452         } else if (other.nops() == 3) { // matrix * matrix
453
454                 GINAC_ASSERT(self.nops() == 3);
455                 GINAC_ASSERT(other.nops() == 3);
456
457                 // A_ij * B_jk = (A*B)_ik
458                 if (is_dummy_pair(self.op(2), other.op(1))) {
459                         self = indexed(self_matrix.mul(other_matrix), self.op(1), other.op(2));
460                         other = _ex1();
461                         return true;
462                 }
463
464                 // A_ij * B_kj = (A*Btrans)_ik
465                 if (is_dummy_pair(self.op(2), other.op(2))) {
466                         self = indexed(self_matrix.mul(other_matrix.transpose()), self.op(1), other.op(1));
467                         other = _ex1();
468                         return true;
469                 }
470
471                 // A_ji * B_jk = (Atrans*B)_ik
472                 if (is_dummy_pair(self.op(1), other.op(1))) {
473                         self = indexed(self_matrix.transpose().mul(other_matrix), self.op(2), other.op(2));
474                         other = _ex1();
475                         return true;
476                 }
477
478                 // A_ji * B_kj = (B*A)_ki
479                 if (is_dummy_pair(self.op(1), other.op(2))) {
480                         self = indexed(other_matrix.mul(self_matrix), other.op(1), self.op(2));
481                         other = _ex1();
482                         return true;
483                 }
484         }
485
486         return false;
487 }
488
489
490 //////////
491 // non-virtual functions in this class
492 //////////
493
494 // public
495
496 /** Sum of matrices.
497  *
498  *  @exception logic_error (incompatible matrices) */
499 matrix matrix::add(const matrix & other) const
500 {
501         if (col != other.col || row != other.row)
502                 throw (std::logic_error("matrix::add(): incompatible matrices"));
503         
504         exvector sum(this->m);
505         exvector::iterator i;
506         exvector::const_iterator ci;
507         for (i=sum.begin(), ci=other.m.begin(); i!=sum.end(); ++i, ++ci)
508                 (*i) += (*ci);
509         
510         return matrix(row,col,sum);
511 }
512
513
514 /** Difference of matrices.
515  *
516  *  @exception logic_error (incompatible matrices) */
517 matrix matrix::sub(const matrix & other) const
518 {
519         if (col != other.col || row != other.row)
520                 throw (std::logic_error("matrix::sub(): incompatible matrices"));
521         
522         exvector dif(this->m);
523         exvector::iterator i;
524         exvector::const_iterator ci;
525         for (i=dif.begin(), ci=other.m.begin(); i!=dif.end(); ++i, ++ci)
526                 (*i) -= (*ci);
527         
528         return matrix(row,col,dif);
529 }
530
531
532 /** Product of matrices.
533  *
534  *  @exception logic_error (incompatible matrices) */
535 matrix matrix::mul(const matrix & other) const
536 {
537         if (this->cols() != other.rows())
538                 throw (std::logic_error("matrix::mul(): incompatible matrices"));
539         
540         exvector prod(this->rows()*other.cols());
541         
542         for (unsigned r1=0; r1<this->rows(); ++r1) {
543                 for (unsigned c=0; c<this->cols(); ++c) {
544                         if (m[r1*col+c].is_zero())
545                                 continue;
546                         for (unsigned r2=0; r2<other.cols(); ++r2)
547                                 prod[r1*other.col+r2] += (m[r1*col+c] * other.m[c*other.col+r2]).expand();
548                 }
549         }
550         return matrix(row, other.col, prod);
551 }
552
553
554 /** operator() to access elements.
555  *
556  *  @param ro row of element
557  *  @param co column of element
558  *  @exception range_error (index out of range) */
559 const ex & matrix::operator() (unsigned ro, unsigned co) const
560 {
561         if (ro>=row || co>=col)
562                 throw (std::range_error("matrix::operator(): index out of range"));
563
564         return m[ro*col+co];
565 }
566
567
568 /** Set individual elements manually.
569  *
570  *  @exception range_error (index out of range) */
571 matrix & matrix::set(unsigned ro, unsigned co, ex value)
572 {
573         if (ro>=row || co>=col)
574                 throw (std::range_error("matrix::set(): index out of range"));
575     
576         ensure_if_modifiable();
577         m[ro*col+co] = value;
578         return *this;
579 }
580
581
582 /** Transposed of an m x n matrix, producing a new n x m matrix object that
583  *  represents the transposed. */
584 matrix matrix::transpose(void) const
585 {
586         exvector trans(this->cols()*this->rows());
587         
588         for (unsigned r=0; r<this->cols(); ++r)
589                 for (unsigned c=0; c<this->rows(); ++c)
590                         trans[r*this->rows()+c] = m[c*this->cols()+r];
591         
592         return matrix(this->cols(),this->rows(),trans);
593 }
594
595
596 /** Determinant of square matrix.  This routine doesn't actually calculate the
597  *  determinant, it only implements some heuristics about which algorithm to
598  *  run.  If all the elements of the matrix are elements of an integral domain
599  *  the determinant is also in that integral domain and the result is expanded
600  *  only.  If one or more elements are from a quotient field the determinant is
601  *  usually also in that quotient field and the result is normalized before it
602  *  is returned.  This implies that the determinant of the symbolic 2x2 matrix
603  *  [[a/(a-b),1],[b/(a-b),1]] is returned as unity.  (In this respect, it
604  *  behaves like MapleV and unlike Mathematica.)
605  *
606  *  @param     algo allows to chose an algorithm
607  *  @return    the determinant as a new expression
608  *  @exception logic_error (matrix not square)
609  *  @see       determinant_algo */
610 ex matrix::determinant(unsigned algo) const
611 {
612         if (row!=col)
613                 throw (std::logic_error("matrix::determinant(): matrix not square"));
614         GINAC_ASSERT(row*col==m.capacity());
615         
616         // Gather some statistical information about this matrix:
617         bool numeric_flag = true;
618         bool normal_flag = false;
619         unsigned sparse_count = 0;  // counts non-zero elements
620         for (exvector::const_iterator r=m.begin(); r!=m.end(); ++r) {
621                 lst srl;  // symbol replacement list
622                 ex rtest = (*r).to_rational(srl);
623                 if (!rtest.is_zero())
624                         ++sparse_count;
625                 if (!rtest.info(info_flags::numeric))
626                         numeric_flag = false;
627                 if (!rtest.info(info_flags::crational_polynomial) &&
628                          rtest.info(info_flags::rational_function))
629                         normal_flag = true;
630         }
631         
632         // Here is the heuristics in case this routine has to decide:
633         if (algo == determinant_algo::automatic) {
634                 // Minor expansion is generally a good guess:
635                 algo = determinant_algo::laplace;
636                 // Does anybody know when a matrix is really sparse?
637                 // Maybe <~row/2.236 nonzero elements average in a row?
638                 if (row>3 && 5*sparse_count<=row*col)
639                         algo = determinant_algo::bareiss;
640                 // Purely numeric matrix can be handled by Gauss elimination.
641                 // This overrides any prior decisions.
642                 if (numeric_flag)
643                         algo = determinant_algo::gauss;
644         }
645         
646         // Trap the trivial case here, since some algorithms don't like it
647         if (this->row==1) {
648                 // for consistency with non-trivial determinants...
649                 if (normal_flag)
650                         return m[0].normal();
651                 else
652                         return m[0].expand();
653         }
654         
655         // Compute the determinant
656         switch(algo) {
657                 case determinant_algo::gauss: {
658                         ex det = 1;
659                         matrix tmp(*this);
660                         int sign = tmp.gauss_elimination(true);
661                         for (unsigned d=0; d<row; ++d)
662                                 det *= tmp.m[d*col+d];
663                         if (normal_flag)
664                                 return (sign*det).normal();
665                         else
666                                 return (sign*det).normal().expand();
667                 }
668                 case determinant_algo::bareiss: {
669                         matrix tmp(*this);
670                         int sign;
671                         sign = tmp.fraction_free_elimination(true);
672                         if (normal_flag)
673                                 return (sign*tmp.m[row*col-1]).normal();
674                         else
675                                 return (sign*tmp.m[row*col-1]).expand();
676                 }
677                 case determinant_algo::divfree: {
678                         matrix tmp(*this);
679                         int sign;
680                         sign = tmp.division_free_elimination(true);
681                         if (sign==0)
682                                 return _ex0();
683                         ex det = tmp.m[row*col-1];
684                         // factor out accumulated bogus slag
685                         for (unsigned d=0; d<row-2; ++d)
686                                 for (unsigned j=0; j<row-d-2; ++j)
687                                         det = (det/tmp.m[d*col+d]).normal();
688                         return (sign*det);
689                 }
690                 case determinant_algo::laplace:
691                 default: {
692                         // This is the minor expansion scheme.  We always develop such
693                         // that the smallest minors (i.e, the trivial 1x1 ones) are on the
694                         // rightmost column.  For this to be efficient it turns out that
695                         // the emptiest columns (i.e. the ones with most zeros) should be
696                         // the ones on the right hand side.  Therefore we presort the
697                         // columns of the matrix:
698                         typedef std::pair<unsigned,unsigned> uintpair;
699                         std::vector<uintpair> c_zeros;  // number of zeros in column
700                         for (unsigned c=0; c<col; ++c) {
701                                 unsigned acc = 0;
702                                 for (unsigned r=0; r<row; ++r)
703                                         if (m[r*col+c].is_zero())
704                                                 ++acc;
705                                 c_zeros.push_back(uintpair(acc,c));
706                         }
707                         sort(c_zeros.begin(),c_zeros.end());
708                         std::vector<unsigned> pre_sort;
709                         for (std::vector<uintpair>::iterator i=c_zeros.begin(); i!=c_zeros.end(); ++i)
710                                 pre_sort.push_back(i->second);
711                         int sign = permutation_sign(pre_sort);
712                         exvector result(row*col);  // represents sorted matrix
713                         unsigned c = 0;
714                         for (std::vector<unsigned>::iterator i=pre_sort.begin();
715                                  i!=pre_sort.end();
716                                  ++i,++c) {
717                                 for (unsigned r=0; r<row; ++r)
718                                         result[r*col+c] = m[r*col+(*i)];
719                         }
720                         
721                         if (normal_flag)
722                                 return (sign*matrix(row,col,result).determinant_minor()).normal();
723                         else
724                                 return sign*matrix(row,col,result).determinant_minor();
725                 }
726         }
727 }
728
729
730 /** Trace of a matrix.  The result is normalized if it is in some quotient
731  *  field and expanded only otherwise.  This implies that the trace of the
732  *  symbolic 2x2 matrix [[a/(a-b),x],[y,b/(b-a)]] is recognized to be unity.
733  *
734  *  @return    the sum of diagonal elements
735  *  @exception logic_error (matrix not square) */
736 ex matrix::trace(void) const
737 {
738         if (row != col)
739                 throw (std::logic_error("matrix::trace(): matrix not square"));
740         
741         ex tr;
742         for (unsigned r=0; r<col; ++r)
743                 tr += m[r*col+r];
744         
745         if (tr.info(info_flags::rational_function) &&
746                 !tr.info(info_flags::crational_polynomial))
747                 return tr.normal();
748         else
749                 return tr.expand();
750 }
751
752
753 /** Characteristic Polynomial.  Following mathematica notation the
754  *  characteristic polynomial of a matrix M is defined as the determiant of
755  *  (M - lambda * 1) where 1 stands for the unit matrix of the same dimension
756  *  as M.  Note that some CASs define it with a sign inside the determinant
757  *  which gives rise to an overall sign if the dimension is odd.  This method
758  *  returns the characteristic polynomial collected in powers of lambda as a
759  *  new expression.
760  *
761  *  @return    characteristic polynomial as new expression
762  *  @exception logic_error (matrix not square)
763  *  @see       matrix::determinant() */
764 ex matrix::charpoly(const symbol & lambda) const
765 {
766         if (row != col)
767                 throw (std::logic_error("matrix::charpoly(): matrix not square"));
768         
769         bool numeric_flag = true;
770         for (exvector::const_iterator r=m.begin(); r!=m.end(); ++r) {
771                 if (!(*r).info(info_flags::numeric)) {
772                         numeric_flag = false;
773                 }
774         }
775         
776         // The pure numeric case is traditionally rather common.  Hence, it is
777         // trapped and we use Leverrier's algorithm which goes as row^3 for
778         // every coefficient.  The expensive part is the matrix multiplication.
779         if (numeric_flag) {
780                 matrix B(*this);
781                 ex c = B.trace();
782                 ex poly = power(lambda,row)-c*power(lambda,row-1);
783                 for (unsigned i=1; i<row; ++i) {
784                         for (unsigned j=0; j<row; ++j)
785                                 B.m[j*col+j] -= c;
786                         B = this->mul(B);
787                         c = B.trace()/ex(i+1);
788                         poly -= c*power(lambda,row-i-1);
789                 }
790                 if (row%2)
791                         return -poly;
792                 else
793                         return poly;
794         }
795         
796         matrix M(*this);
797         for (unsigned r=0; r<col; ++r)
798                 M.m[r*col+r] -= lambda;
799         
800         return M.determinant().collect(lambda);
801 }
802
803
804 /** Inverse of this matrix.
805  *
806  *  @return    the inverted matrix
807  *  @exception logic_error (matrix not square)
808  *  @exception runtime_error (singular matrix) */
809 matrix matrix::inverse(void) const
810 {
811         if (row != col)
812                 throw (std::logic_error("matrix::inverse(): matrix not square"));
813         
814         // NOTE: the Gauss-Jordan elimination used here can in principle be
815         // replaced by two clever calls to gauss_elimination() and some to
816         // transpose().  Wouldn't be more efficient (maybe less?), just more
817         // orthogonal.
818         matrix tmp(row,col);
819         // set tmp to the unit matrix
820         for (unsigned i=0; i<col; ++i)
821                 tmp.m[i*col+i] = _ex1();
822         
823         // create a copy of this matrix
824         matrix cpy(*this);
825         for (unsigned r1=0; r1<row; ++r1) {
826                 int indx = cpy.pivot(r1, r1);
827                 if (indx == -1) {
828                         throw (std::runtime_error("matrix::inverse(): singular matrix"));
829                 }
830                 if (indx != 0) {  // swap rows r and indx of matrix tmp
831                         for (unsigned i=0; i<col; ++i)
832                                 tmp.m[r1*col+i].swap(tmp.m[indx*col+i]);
833                 }
834                 ex a1 = cpy.m[r1*col+r1];
835                 for (unsigned c=0; c<col; ++c) {
836                         cpy.m[r1*col+c] /= a1;
837                         tmp.m[r1*col+c] /= a1;
838                 }
839                 for (unsigned r2=0; r2<row; ++r2) {
840                         if (r2 != r1) {
841                                 if (!cpy.m[r2*col+r1].is_zero()) {
842                                         ex a2 = cpy.m[r2*col+r1];
843                                         // yes, there is something to do in this column
844                                         for (unsigned c=0; c<col; ++c) {
845                                                 cpy.m[r2*col+c] -= a2 * cpy.m[r1*col+c];
846                                                 if (!cpy.m[r2*col+c].info(info_flags::numeric))
847                                                         cpy.m[r2*col+c] = cpy.m[r2*col+c].normal();
848                                                 tmp.m[r2*col+c] -= a2 * tmp.m[r1*col+c];
849                                                 if (!tmp.m[r2*col+c].info(info_flags::numeric))
850                                                         tmp.m[r2*col+c] = tmp.m[r2*col+c].normal();
851                                         }
852                                 }
853                         }
854                 }
855         }
856         
857         return tmp;
858 }
859
860
861 /** Solve a linear system consisting of a m x n matrix and a m x p right hand
862  *  side by applying an elimination scheme to the augmented matrix.
863  *
864  *  @param vars n x p matrix, all elements must be symbols 
865  *  @param rhs m x p matrix
866  *  @return n x p solution matrix
867  *  @exception logic_error (incompatible matrices)
868  *  @exception invalid_argument (1st argument must be matrix of symbols)
869  *  @exception runtime_error (inconsistent linear system)
870  *  @see       solve_algo */
871 matrix matrix::solve(const matrix & vars,
872                                          const matrix & rhs,
873                                          unsigned algo) const
874 {
875         const unsigned m = this->rows();
876         const unsigned n = this->cols();
877         const unsigned p = rhs.cols();
878         
879         // syntax checks    
880         if ((rhs.rows() != m) || (vars.rows() != n) || (vars.col != p))
881                 throw (std::logic_error("matrix::solve(): incompatible matrices"));
882         for (unsigned ro=0; ro<n; ++ro)
883                 for (unsigned co=0; co<p; ++co)
884                         if (!vars(ro,co).info(info_flags::symbol))
885                                 throw (std::invalid_argument("matrix::solve(): 1st argument must be matrix of symbols"));
886         
887         // build the augmented matrix of *this with rhs attached to the right
888         matrix aug(m,n+p);
889         for (unsigned r=0; r<m; ++r) {
890                 for (unsigned c=0; c<n; ++c)
891                         aug.m[r*(n+p)+c] = this->m[r*n+c];
892                 for (unsigned c=0; c<p; ++c)
893                         aug.m[r*(n+p)+c+n] = rhs.m[r*p+c];
894         }
895         
896         // Gather some statistical information about the augmented matrix:
897         bool numeric_flag = true;
898         for (exvector::const_iterator r=aug.m.begin(); r!=aug.m.end(); ++r) {
899                 if (!(*r).info(info_flags::numeric))
900                         numeric_flag = false;
901         }
902         
903         // Here is the heuristics in case this routine has to decide:
904         if (algo == solve_algo::automatic) {
905                 // Bareiss (fraction-free) elimination is generally a good guess:
906                 algo = solve_algo::bareiss;
907                 // For m<3, Bareiss elimination is equivalent to division free
908                 // elimination but has more logistic overhead
909                 if (m<3)
910                         algo = solve_algo::divfree;
911                 // This overrides any prior decisions.
912                 if (numeric_flag)
913                         algo = solve_algo::gauss;
914         }
915         
916         // Eliminate the augmented matrix:
917         switch(algo) {
918                 case solve_algo::gauss:
919                         aug.gauss_elimination();
920                 case solve_algo::divfree:
921                         aug.division_free_elimination();
922                 case solve_algo::bareiss:
923                 default:
924                         aug.fraction_free_elimination();
925         }
926         
927         // assemble the solution matrix:
928         matrix sol(n,p);
929         for (unsigned co=0; co<p; ++co) {
930                 unsigned last_assigned_sol = n+1;
931                 for (int r=m-1; r>=0; --r) {
932                         unsigned fnz = 1;    // first non-zero in row
933                         while ((fnz<=n) && (aug.m[r*(n+p)+(fnz-1)].is_zero()))
934                                 ++fnz;
935                         if (fnz>n) {
936                                 // row consists only of zeros, corresponding rhs must be 0, too
937                                 if (!aug.m[r*(n+p)+n+co].is_zero()) {
938                                         throw (std::runtime_error("matrix::solve(): inconsistent linear system"));
939                                 }
940                         } else {
941                                 // assign solutions for vars between fnz+1 and
942                                 // last_assigned_sol-1: free parameters
943                                 for (unsigned c=fnz; c<last_assigned_sol-1; ++c)
944                                         sol.set(c,co,vars.m[c*p+co]);
945                                 ex e = aug.m[r*(n+p)+n+co];
946                                 for (unsigned c=fnz; c<n; ++c)
947                                         e -= aug.m[r*(n+p)+c]*sol.m[c*p+co];
948                                 sol.set(fnz-1,co,
949                                                 (e/(aug.m[r*(n+p)+(fnz-1)])).normal());
950                                 last_assigned_sol = fnz;
951                         }
952                 }
953                 // assign solutions for vars between 1 and
954                 // last_assigned_sol-1: free parameters
955                 for (unsigned ro=0; ro<last_assigned_sol-1; ++ro)
956                         sol.set(ro,co,vars(ro,co));
957         }
958         
959         return sol;
960 }
961
962
963 // protected
964
965 /** Recursive determinant for small matrices having at least one symbolic
966  *  entry.  The basic algorithm, known as Laplace-expansion, is enhanced by
967  *  some bookkeeping to avoid calculation of the same submatrices ("minors")
968  *  more than once.  According to W.M.Gentleman and S.C.Johnson this algorithm
969  *  is better than elimination schemes for matrices of sparse multivariate
970  *  polynomials and also for matrices of dense univariate polynomials if the
971  *  matrix' dimesion is larger than 7.
972  *
973  *  @return the determinant as a new expression (in expanded form)
974  *  @see matrix::determinant() */
975 ex matrix::determinant_minor(void) const
976 {
977         // for small matrices the algorithm does not make any sense:
978         const unsigned n = this->cols();
979         if (n==1)
980                 return m[0].expand();
981         if (n==2)
982                 return (m[0]*m[3]-m[2]*m[1]).expand();
983         if (n==3)
984                 return (m[0]*m[4]*m[8]-m[0]*m[5]*m[7]-
985                         m[1]*m[3]*m[8]+m[2]*m[3]*m[7]+
986                         m[1]*m[5]*m[6]-m[2]*m[4]*m[6]).expand();
987         
988         // This algorithm can best be understood by looking at a naive
989         // implementation of Laplace-expansion, like this one:
990         // ex det;
991         // matrix minorM(this->rows()-1,this->cols()-1);
992         // for (unsigned r1=0; r1<this->rows(); ++r1) {
993         //     // shortcut if element(r1,0) vanishes
994         //     if (m[r1*col].is_zero())
995         //         continue;
996         //     // assemble the minor matrix
997         //     for (unsigned r=0; r<minorM.rows(); ++r) {
998         //         for (unsigned c=0; c<minorM.cols(); ++c) {
999         //             if (r<r1)
1000         //                 minorM.set(r,c,m[r*col+c+1]);
1001         //             else
1002         //                 minorM.set(r,c,m[(r+1)*col+c+1]);
1003         //         }
1004         //     }
1005         //     // recurse down and care for sign:
1006         //     if (r1%2)
1007         //         det -= m[r1*col] * minorM.determinant_minor();
1008         //     else
1009         //         det += m[r1*col] * minorM.determinant_minor();
1010         // }
1011         // return det.expand();
1012         // What happens is that while proceeding down many of the minors are
1013         // computed more than once.  In particular, there are binomial(n,k)
1014         // kxk minors and each one is computed factorial(n-k) times.  Therefore
1015         // it is reasonable to store the results of the minors.  We proceed from
1016         // right to left.  At each column c we only need to retrieve the minors
1017         // calculated in step c-1.  We therefore only have to store at most 
1018         // 2*binomial(n,n/2) minors.
1019         
1020         // Unique flipper counter for partitioning into minors
1021         std::vector<unsigned> Pkey;
1022         Pkey.reserve(n);
1023         // key for minor determinant (a subpartition of Pkey)
1024         std::vector<unsigned> Mkey;
1025         Mkey.reserve(n-1);
1026         // we store our subminors in maps, keys being the rows they arise from
1027         typedef std::map<std::vector<unsigned>,class ex> Rmap;
1028         typedef std::map<std::vector<unsigned>,class ex>::value_type Rmap_value;
1029         Rmap A;
1030         Rmap B;
1031         ex det;
1032         // initialize A with last column:
1033         for (unsigned r=0; r<n; ++r) {
1034                 Pkey.erase(Pkey.begin(),Pkey.end());
1035                 Pkey.push_back(r);
1036                 A.insert(Rmap_value(Pkey,m[n*(r+1)-1]));
1037         }
1038         // proceed from right to left through matrix
1039         for (int c=n-2; c>=0; --c) {
1040                 Pkey.erase(Pkey.begin(),Pkey.end());  // don't change capacity
1041                 Mkey.erase(Mkey.begin(),Mkey.end());
1042                 for (unsigned i=0; i<n-c; ++i)
1043                         Pkey.push_back(i);
1044                 unsigned fc = 0;  // controls logic for our strange flipper counter
1045                 do {
1046                         det = _ex0();
1047                         for (unsigned r=0; r<n-c; ++r) {
1048                                 // maybe there is nothing to do?
1049                                 if (m[Pkey[r]*n+c].is_zero())
1050                                         continue;
1051                                 // create the sorted key for all possible minors
1052                                 Mkey.erase(Mkey.begin(),Mkey.end());
1053                                 for (unsigned i=0; i<n-c; ++i)
1054                                         if (i!=r)
1055                                                 Mkey.push_back(Pkey[i]);
1056                                 // Fetch the minors and compute the new determinant
1057                                 if (r%2)
1058                                         det -= m[Pkey[r]*n+c]*A[Mkey];
1059                                 else
1060                                         det += m[Pkey[r]*n+c]*A[Mkey];
1061                         }
1062                         // prevent build-up of deep nesting of expressions saves time:
1063                         det = det.expand();
1064                         // store the new determinant at its place in B:
1065                         if (!det.is_zero())
1066                                 B.insert(Rmap_value(Pkey,det));
1067                         // increment our strange flipper counter
1068                         for (fc=n-c; fc>0; --fc) {
1069                                 ++Pkey[fc-1];
1070                                 if (Pkey[fc-1]<fc+c)
1071                                         break;
1072                         }
1073                         if (fc<n-c && fc>0)
1074                                 for (unsigned j=fc; j<n-c; ++j)
1075                                         Pkey[j] = Pkey[j-1]+1;
1076                 } while(fc);
1077                 // next column, so change the role of A and B:
1078                 A = B;
1079                 B.clear();
1080         }
1081         
1082         return det;
1083 }
1084
1085
1086 /** Perform the steps of an ordinary Gaussian elimination to bring the m x n
1087  *  matrix into an upper echelon form.  The algorithm is ok for matrices
1088  *  with numeric coefficients but quite unsuited for symbolic matrices.
1089  *
1090  *  @param det may be set to true to save a lot of space if one is only
1091  *  interested in the diagonal elements (i.e. for calculating determinants).
1092  *  The others are set to zero in this case.
1093  *  @return sign is 1 if an even number of rows was swapped, -1 if an odd
1094  *  number of rows was swapped and 0 if the matrix is singular. */
1095 int matrix::gauss_elimination(const bool det)
1096 {
1097         ensure_if_modifiable();
1098         const unsigned m = this->rows();
1099         const unsigned n = this->cols();
1100         GINAC_ASSERT(!det || n==m);
1101         int sign = 1;
1102         
1103         unsigned r0 = 0;
1104         for (unsigned r1=0; (r1<n-1)&&(r0<m-1); ++r1) {
1105                 int indx = pivot(r0, r1, true);
1106                 if (indx == -1) {
1107                         sign = 0;
1108                         if (det)
1109                                 return 0;  // leaves *this in a messy state
1110                 }
1111                 if (indx>=0) {
1112                         if (indx > 0)
1113                                 sign = -sign;
1114                         for (unsigned r2=r0+1; r2<m; ++r2) {
1115                                 if (!this->m[r2*n+r1].is_zero()) {
1116                                         // yes, there is something to do in this row
1117                                         ex piv = this->m[r2*n+r1] / this->m[r0*n+r1];
1118                                         for (unsigned c=r1+1; c<n; ++c) {
1119                                                 this->m[r2*n+c] -= piv * this->m[r0*n+c];
1120                                                 if (!this->m[r2*n+c].info(info_flags::numeric))
1121                                                         this->m[r2*n+c] = this->m[r2*n+c].normal();
1122                                         }
1123                                 }
1124                                 // fill up left hand side with zeros
1125                                 for (unsigned c=0; c<=r1; ++c)
1126                                         this->m[r2*n+c] = _ex0();
1127                         }
1128                         if (det) {
1129                                 // save space by deleting no longer needed elements
1130                                 for (unsigned c=r0+1; c<n; ++c)
1131                                         this->m[r0*n+c] = _ex0();
1132                         }
1133                         ++r0;
1134                 }
1135         }
1136         
1137         return sign;
1138 }
1139
1140
1141 /** Perform the steps of division free elimination to bring the m x n matrix
1142  *  into an upper echelon form.
1143  *
1144  *  @param det may be set to true to save a lot of space if one is only
1145  *  interested in the diagonal elements (i.e. for calculating determinants).
1146  *  The others are set to zero in this case.
1147  *  @return sign is 1 if an even number of rows was swapped, -1 if an odd
1148  *  number of rows was swapped and 0 if the matrix is singular. */
1149 int matrix::division_free_elimination(const bool det)
1150 {
1151         ensure_if_modifiable();
1152         const unsigned m = this->rows();
1153         const unsigned n = this->cols();
1154         GINAC_ASSERT(!det || n==m);
1155         int sign = 1;
1156         
1157         unsigned r0 = 0;
1158         for (unsigned r1=0; (r1<n-1)&&(r0<m-1); ++r1) {
1159                 int indx = pivot(r0, r1, true);
1160                 if (indx==-1) {
1161                         sign = 0;
1162                         if (det)
1163                                 return 0;  // leaves *this in a messy state
1164                 }
1165                 if (indx>=0) {
1166                         if (indx>0)
1167                                 sign = -sign;
1168                         for (unsigned r2=r0+1; r2<m; ++r2) {
1169                                 for (unsigned c=r1+1; c<n; ++c)
1170                                         this->m[r2*n+c] = (this->m[r0*n+r1]*this->m[r2*n+c] - this->m[r2*n+r1]*this->m[r0*n+c]).expand();
1171                                 // fill up left hand side with zeros
1172                                 for (unsigned c=0; c<=r1; ++c)
1173                                         this->m[r2*n+c] = _ex0();
1174                         }
1175                         if (det) {
1176                                 // save space by deleting no longer needed elements
1177                                 for (unsigned c=r0+1; c<n; ++c)
1178                                         this->m[r0*n+c] = _ex0();
1179                         }
1180                         ++r0;
1181                 }
1182         }
1183         
1184         return sign;
1185 }
1186
1187
1188 /** Perform the steps of Bareiss' one-step fraction free elimination to bring
1189  *  the matrix into an upper echelon form.  Fraction free elimination means
1190  *  that divide is used straightforwardly, without computing GCDs first.  This
1191  *  is possible, since we know the divisor at each step.
1192  *  
1193  *  @param det may be set to true to save a lot of space if one is only
1194  *  interested in the last element (i.e. for calculating determinants). The
1195  *  others are set to zero in this case.
1196  *  @return sign is 1 if an even number of rows was swapped, -1 if an odd
1197  *  number of rows was swapped and 0 if the matrix is singular. */
1198 int matrix::fraction_free_elimination(const bool det)
1199 {
1200         // Method:
1201         // (single-step fraction free elimination scheme, already known to Jordan)
1202         //
1203         // Usual division-free elimination sets m[0](r,c) = m(r,c) and then sets
1204         //     m[k+1](r,c) = m[k](k,k) * m[k](r,c) - m[k](r,k) * m[k](k,c).
1205         //
1206         // Bareiss (fraction-free) elimination in addition divides that element
1207         // by m[k-1](k-1,k-1) for k>1, where it can be shown by means of the
1208         // Sylvester determinant that this really divides m[k+1](r,c).
1209         //
1210         // We also allow rational functions where the original prove still holds.
1211         // However, we must care for numerator and denominator separately and
1212         // "manually" work in the integral domains because of subtle cancellations
1213         // (see below).  This blows up the bookkeeping a bit and the formula has
1214         // to be modified to expand like this (N{x} stands for numerator of x,
1215         // D{x} for denominator of x):
1216         //     N{m[k+1](r,c)} = N{m[k](k,k)}*N{m[k](r,c)}*D{m[k](r,k)}*D{m[k](k,c)}
1217         //                     -N{m[k](r,k)}*N{m[k](k,c)}*D{m[k](k,k)}*D{m[k](r,c)}
1218         //     D{m[k+1](r,c)} = D{m[k](k,k)}*D{m[k](r,c)}*D{m[k](r,k)}*D{m[k](k,c)}
1219         // where for k>1 we now divide N{m[k+1](r,c)} by
1220         //     N{m[k-1](k-1,k-1)}
1221         // and D{m[k+1](r,c)} by
1222         //     D{m[k-1](k-1,k-1)}.
1223         
1224         ensure_if_modifiable();
1225         const unsigned m = this->rows();
1226         const unsigned n = this->cols();
1227         GINAC_ASSERT(!det || n==m);
1228         int sign = 1;
1229         if (m==1)
1230                 return 1;
1231         ex divisor_n = 1;
1232         ex divisor_d = 1;
1233         ex dividend_n;
1234         ex dividend_d;
1235         
1236         // We populate temporary matrices to subsequently operate on.  There is
1237         // one holding numerators and another holding denominators of entries.
1238         // This is a must since the evaluator (or even earlier mul's constructor)
1239         // might cancel some trivial element which causes divide() to fail.  The
1240         // elements are normalized first (yes, even though this algorithm doesn't
1241         // need GCDs) since the elements of *this might be unnormalized, which
1242         // makes things more complicated than they need to be.
1243         matrix tmp_n(*this);
1244         matrix tmp_d(m,n);  // for denominators, if needed
1245         lst srl;  // symbol replacement list
1246         exvector::iterator it = this->m.begin();
1247         exvector::iterator tmp_n_it = tmp_n.m.begin();
1248         exvector::iterator tmp_d_it = tmp_d.m.begin();
1249         for (; it!= this->m.end(); ++it, ++tmp_n_it, ++tmp_d_it) {
1250                 (*tmp_n_it) = (*it).normal().to_rational(srl);
1251                 (*tmp_d_it) = (*tmp_n_it).denom();
1252                 (*tmp_n_it) = (*tmp_n_it).numer();
1253         }
1254         
1255         unsigned r0 = 0;
1256         for (unsigned r1=0; (r1<n-1)&&(r0<m-1); ++r1) {
1257                 int indx = tmp_n.pivot(r0, r1, true);
1258                 if (indx==-1) {
1259                         sign = 0;
1260                         if (det)
1261                                 return 0;
1262                 }
1263                 if (indx>=0) {
1264                         if (indx>0) {
1265                                 sign = -sign;
1266                                 // tmp_n's rows r0 and indx were swapped, do the same in tmp_d:
1267                                 for (unsigned c=r1; c<n; ++c)
1268                                         tmp_d.m[n*indx+c].swap(tmp_d.m[n*r0+c]);
1269                         }
1270                         for (unsigned r2=r0+1; r2<m; ++r2) {
1271                                 for (unsigned c=r1+1; c<n; ++c) {
1272                                         dividend_n = (tmp_n.m[r0*n+r1]*tmp_n.m[r2*n+c]*
1273                                                       tmp_d.m[r2*n+r1]*tmp_d.m[r0*n+c]
1274                                                      -tmp_n.m[r2*n+r1]*tmp_n.m[r0*n+c]*
1275                                                       tmp_d.m[r0*n+r1]*tmp_d.m[r2*n+c]).expand();
1276                                         dividend_d = (tmp_d.m[r2*n+r1]*tmp_d.m[r0*n+c]*
1277                                                       tmp_d.m[r0*n+r1]*tmp_d.m[r2*n+c]).expand();
1278                                         bool check = divide(dividend_n, divisor_n,
1279                                                             tmp_n.m[r2*n+c], true);
1280                                         check &= divide(dividend_d, divisor_d,
1281                                                         tmp_d.m[r2*n+c], true);
1282                                         GINAC_ASSERT(check);
1283                                 }
1284                                 // fill up left hand side with zeros
1285                                 for (unsigned c=0; c<=r1; ++c)
1286                                         tmp_n.m[r2*n+c] = _ex0();
1287                         }
1288                         if ((r1<n-1)&&(r0<m-1)) {
1289                                 // compute next iteration's divisor
1290                                 divisor_n = tmp_n.m[r0*n+r1].expand();
1291                                 divisor_d = tmp_d.m[r0*n+r1].expand();
1292                                 if (det) {
1293                                         // save space by deleting no longer needed elements
1294                                         for (unsigned c=0; c<n; ++c) {
1295                                                 tmp_n.m[r0*n+c] = _ex0();
1296                                                 tmp_d.m[r0*n+c] = _ex1();
1297                                         }
1298                                 }
1299                         }
1300                         ++r0;
1301                 }
1302         }
1303         // repopulate *this matrix:
1304         it = this->m.begin();
1305         tmp_n_it = tmp_n.m.begin();
1306         tmp_d_it = tmp_d.m.begin();
1307         for (; it!= this->m.end(); ++it, ++tmp_n_it, ++tmp_d_it)
1308                 (*it) = ((*tmp_n_it)/(*tmp_d_it)).subs(srl);
1309         
1310         return sign;
1311 }
1312
1313
1314 /** Partial pivoting method for matrix elimination schemes.
1315  *  Usual pivoting (symbolic==false) returns the index to the element with the
1316  *  largest absolute value in column ro and swaps the current row with the one
1317  *  where the element was found.  With (symbolic==true) it does the same thing
1318  *  with the first non-zero element.
1319  *
1320  *  @param ro is the row from where to begin
1321  *  @param co is the column to be inspected
1322  *  @param symbolic signal if we want the first non-zero element to be pivoted
1323  *  (true) or the one with the largest absolute value (false).
1324  *  @return 0 if no interchange occured, -1 if all are zero (usually signaling
1325  *  a degeneracy) and positive integer k means that rows ro and k were swapped.
1326  */
1327 int matrix::pivot(unsigned ro, unsigned co, bool symbolic)
1328 {
1329         unsigned k = ro;
1330         if (symbolic) {
1331                 // search first non-zero element in column co beginning at row ro
1332                 while ((k<row) && (this->m[k*col+co].expand().is_zero()))
1333                         ++k;
1334         } else {
1335                 // search largest element in column co beginning at row ro
1336                 GINAC_ASSERT(is_ex_of_type(this->m[k*col+co],numeric));
1337                 unsigned kmax = k+1;
1338                 numeric mmax = abs(ex_to_numeric(m[kmax*col+co]));
1339                 while (kmax<row) {
1340                         GINAC_ASSERT(is_ex_of_type(this->m[kmax*col+co],numeric));
1341                         numeric tmp = ex_to_numeric(this->m[kmax*col+co]);
1342                         if (abs(tmp) > mmax) {
1343                                 mmax = tmp;
1344                                 k = kmax;
1345                         }
1346                         ++kmax;
1347                 }
1348                 if (!mmax.is_zero())
1349                         k = kmax;
1350         }
1351         if (k==row)
1352                 // all elements in column co below row ro vanish
1353                 return -1;
1354         if (k==ro)
1355                 // matrix needs no pivoting
1356                 return 0;
1357         // matrix needs pivoting, so swap rows k and ro
1358         ensure_if_modifiable();
1359         for (unsigned c=0; c<col; ++c)
1360                 this->m[k*col+c].swap(this->m[ro*col+c]);
1361         
1362         return k;
1363 }
1364
1365 /** Convert list of lists to matrix. */
1366 ex lst_to_matrix(const ex &l)
1367 {
1368         if (!is_ex_of_type(l, lst))
1369                 throw(std::invalid_argument("argument to lst_to_matrix() must be a lst"));
1370         
1371         // Find number of rows and columns
1372         unsigned rows = l.nops(), cols = 0, i, j;
1373         for (i=0; i<rows; i++)
1374                 if (l.op(i).nops() > cols)
1375                         cols = l.op(i).nops();
1376         
1377         // Allocate and fill matrix
1378         matrix &m = *new matrix(rows, cols);
1379         for (i=0; i<rows; i++)
1380                 for (j=0; j<cols; j++)
1381                         if (l.op(i).nops() > j)
1382                                 m.set(i, j, l.op(i).op(j));
1383                         else
1384                                 m.set(i, j, ex(0));
1385         return m;
1386 }
1387
1388 } // namespace GiNaC