]> www.ginac.de Git - ginac.git/blob - ginac/matrix.h
Fix chinese_remainder() so modular GCD actually works.
[ginac.git] / ginac / matrix.h
1 /** @file matrix.h
2  *
3  *  Interface to symbolic matrices */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef __GINAC_MATRIX_H__
24 #define __GINAC_MATRIX_H__
25
26 #include <vector>
27 #include <string>
28 #include "basic.h"
29 #include "ex.h"
30 #include "archive.h"
31
32 namespace GiNaC {
33
34
35 /** Helper template to allow initialization of matrices via an overloaded
36  *  comma operator (idea stolen from Blitz++). */
37 template <typename T, typename It>
38 class matrix_init {
39 public:
40         matrix_init(It i) : iter(i) {}
41
42         matrix_init<T, It> operator,(const T & x)
43         {
44                 *iter = x;
45                 return matrix_init<T, It>(++iter);
46         }
47
48         // The following specializations produce much tighter code than the
49         // general case above
50
51         matrix_init<T, It> operator,(int x)
52         {
53                 *iter = T(x);
54                 return matrix_init<T, It>(++iter);
55         }
56
57         matrix_init<T, It> operator,(unsigned int x)
58         {
59                 *iter = T(x);
60                 return matrix_init<T, It>(++iter);
61         }
62
63         matrix_init<T, It> operator,(long x)
64         {
65                 *iter = T(x);
66                 return matrix_init<T, It>(++iter);
67         }
68
69         matrix_init<T, It> operator,(unsigned long x)
70         {
71                 *iter = T(x);
72                 return matrix_init<T, It>(++iter);
73         }
74
75         matrix_init<T, It> operator,(double x)
76         {
77                 *iter = T(x);
78                 return matrix_init<T, It>(++iter);
79         }
80
81         matrix_init<T, It> operator,(const symbol & x)
82         {
83                 *iter = T(x);
84                 return matrix_init<T, It>(++iter);
85         }
86
87 private:
88         matrix_init();
89         It iter;
90 };
91
92
93 /** Symbolic matrices. */
94 class matrix : public basic
95 {
96         GINAC_DECLARE_REGISTERED_CLASS(matrix, basic)
97         
98         // other constructors
99 public:
100         matrix(unsigned r, unsigned c);
101         matrix(unsigned r, unsigned c, const exvector & m2);
102         matrix(unsigned r, unsigned c, const lst & l);
103
104         // First step of initialization of matrix with a comma-separated seqeuence
105         // of expressions. Subsequent steps are handled by matrix_init<>::operator,().
106         matrix_init<ex, exvector::iterator> operator=(const ex & x)
107         {
108                 m[0] = x;
109                 return matrix_init<ex, exvector::iterator>(++m.begin());
110         }
111         
112         // functions overriding virtual functions from base classes
113 public:
114         size_t nops() const;
115         ex op(size_t i) const;
116         ex & let_op(size_t i);
117         ex eval(int level=0) const;
118         ex evalm() const {return *this;}
119         ex subs(const exmap & m, unsigned options = 0) const;
120         ex eval_indexed(const basic & i) const;
121         ex add_indexed(const ex & self, const ex & other) const;
122         ex scalar_mul_indexed(const ex & self, const numeric & other) const;
123         bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
124         ex conjugate() const;
125         ex real_part() const;
126         ex imag_part() const;
127
128         /** Save (a.k.a. serialize) object into archive. */
129         void archive(archive_node& n) const;
130         /** Read (a.k.a. deserialize) object from archive. */
131         void read_archive(const archive_node& n, lst& syms);
132 protected:
133         bool match_same_type(const basic & other) const;
134         unsigned return_type() const { return return_types::noncommutative; };
135         
136         // non-virtual functions in this class
137 public:
138         unsigned rows() const        /// Get number of rows.
139                 { return row; }
140         unsigned cols() const        /// Get number of columns.
141                 { return col; }
142         matrix add(const matrix & other) const;
143         matrix sub(const matrix & other) const;
144         matrix mul(const matrix & other) const;
145         matrix mul(const numeric & other) const;
146         matrix mul_scalar(const ex & other) const;
147         matrix pow(const ex & expn) const;
148         const ex & operator() (unsigned ro, unsigned co) const;
149         ex & operator() (unsigned ro, unsigned co);
150         matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
151         matrix transpose() const;
152         ex determinant(unsigned algo = determinant_algo::automatic) const;
153         ex trace() const;
154         ex charpoly(const ex & lambda) const;
155         matrix inverse() const;
156         matrix solve(const matrix & vars, const matrix & rhs,
157                      unsigned algo = solve_algo::automatic) const;
158         unsigned rank() const;
159         bool is_zero_matrix() const;
160 protected:
161         ex determinant_minor() const;
162         int gauss_elimination(const bool det = false);
163         int division_free_elimination(const bool det = false);
164         int fraction_free_elimination(const bool det = false);
165         int pivot(unsigned ro, unsigned co, bool symbolic = true);
166
167         void print_elements(const print_context & c, const char *row_start, const char *row_end, const char *row_sep, const char *col_sep) const;
168         void do_print(const print_context & c, unsigned level) const;
169         void do_print_latex(const print_latex & c, unsigned level) const;
170         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
171         
172 // member variables
173 protected:
174         unsigned row;             ///< number of rows
175         unsigned col;             ///< number of columns
176         exvector m;               ///< representation (cols indexed first)
177 };
178 GINAC_DECLARE_UNARCHIVER(matrix); 
179
180
181 // wrapper functions around member functions
182
183 inline size_t nops(const matrix & m)
184 { return m.nops(); }
185
186 inline ex expand(const matrix & m, unsigned options = 0)
187 { return m.expand(options); }
188
189 inline ex eval(const matrix & m, int level = 0)
190 { return m.eval(level); }
191
192 inline ex evalf(const matrix & m, int level = 0)
193 { return m.evalf(level); }
194
195 inline unsigned rows(const matrix & m)
196 { return m.rows(); }
197
198 inline unsigned cols(const matrix & m)
199 { return m.cols(); }
200
201 inline matrix transpose(const matrix & m)
202 { return m.transpose(); }
203
204 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
205 { return m.determinant(options); }
206
207 inline ex trace(const matrix & m)
208 { return m.trace(); }
209
210 inline ex charpoly(const matrix & m, const ex & lambda)
211 { return m.charpoly(lambda); }
212
213 inline matrix inverse(const matrix & m)
214 { return m.inverse(); }
215
216 inline unsigned rank(const matrix & m)
217 { return m.rank(); }
218
219 // utility functions
220
221 /** Convert list of lists to matrix. */
222 extern ex lst_to_matrix(const lst & l);
223
224 /** Convert list of diagonal elements to matrix. */
225 extern ex diag_matrix(const lst & l);
226
227 /** Create an r times c unit matrix. */
228 extern ex unit_matrix(unsigned r, unsigned c);
229
230 /** Create a x times x unit matrix. */
231 inline ex unit_matrix(unsigned x)
232 { return unit_matrix(x, x); }
233
234 /** Create an r times c matrix of newly generated symbols consisting of the
235  *  given base name plus the numeric row/column position of each element.
236  *  The base name for LaTeX output is specified separately. */
237 extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
238
239 /** Return the reduced matrix that is formed by deleting the rth row and cth
240  *  column of matrix m. The determinant of the result is the Minor r, c. */
241 extern ex reduced_matrix(const matrix& m, unsigned r, unsigned c);
242
243 /** Return the nr times nc submatrix starting at position r, c of matrix m. */
244 extern ex sub_matrix(const matrix&m, unsigned r, unsigned nr, unsigned c, unsigned nc);
245
246 /** Create an r times c matrix of newly generated symbols consisting of the
247  *  given base name plus the numeric row/column position of each element. */
248 inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
249 { return symbolic_matrix(r, c, base_name, base_name); }
250
251 } // namespace GiNaC
252
253 #endif // ndef __GINAC_MATRIX_H__