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