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