]> www.ginac.de Git - ginac.git/blob - ginac/matrix.h
Tutorial: how to create noncommutative symbols?
[ginac.git] / ginac / matrix.h
1 /** @file matrix.h
2  *
3  *  Interface to symbolic matrices */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2015 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 "basic.h"
27 #include "ex.h"
28 #include "archive.h"
29
30 #include <string>
31 #include <vector>
32
33 namespace GiNaC {
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 lst & l);
102
103         // First step of initialization of matrix with a comma-separated sequence
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 protected:
112         matrix(unsigned r, unsigned c, const exvector & m2);
113         matrix(unsigned r, unsigned c, exvector && m2);
114         // functions overriding virtual functions from base classes
115 public:
116         size_t nops() const;
117         ex op(size_t i) const;
118         ex & let_op(size_t i);
119         ex eval(int level=0) const;
120         ex evalm() const {return *this;}
121         ex subs(const exmap & m, unsigned options = 0) const;
122         ex eval_indexed(const basic & i) const;
123         ex add_indexed(const ex & self, const ex & other) const;
124         ex scalar_mul_indexed(const ex & self, const numeric & other) const;
125         bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
126         ex conjugate() const;
127         ex real_part() const;
128         ex imag_part() const;
129
130         /** Save (a.k.a. serialize) object into archive. */
131         void archive(archive_node& n) const;
132         /** Read (a.k.a. deserialize) object from archive. */
133         void read_archive(const archive_node& n, lst& syms);
134 protected:
135         bool match_same_type(const basic & other) const;
136         unsigned return_type() const { return return_types::noncommutative; };
137         
138         // non-virtual functions in this class
139 public:
140         unsigned rows() const        /// Get number of rows.
141                 { return row; }
142         unsigned cols() const        /// Get number of columns.
143                 { return col; }
144         matrix add(const matrix & other) const;
145         matrix sub(const matrix & other) const;
146         matrix mul(const matrix & other) const;
147         matrix mul(const numeric & other) const;
148         matrix mul_scalar(const ex & other) const;
149         matrix pow(const ex & expn) const;
150         const ex & operator() (unsigned ro, unsigned co) const;
151         ex & operator() (unsigned ro, unsigned co);
152         matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
153         matrix transpose() const;
154         ex determinant(unsigned algo = determinant_algo::automatic) const;
155         ex trace() const;
156         ex charpoly(const ex & lambda) const;
157         matrix inverse() const;
158         matrix solve(const matrix & vars, const matrix & rhs,
159                      unsigned algo = solve_algo::automatic) const;
160         unsigned rank() const;
161         bool is_zero_matrix() const;
162 protected:
163         ex determinant_minor() const;
164         int gauss_elimination(const bool det = false);
165         int division_free_elimination(const bool det = false);
166         int fraction_free_elimination(const bool det = false);
167         int pivot(unsigned ro, unsigned co, bool symbolic = true);
168
169         void print_elements(const print_context & c, const char *row_start, const char *row_end, const char *row_sep, const char *col_sep) const;
170         void do_print(const print_context & c, unsigned level) const;
171         void do_print_latex(const print_latex & c, unsigned level) const;
172         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
173         
174 // member variables
175 protected:
176         unsigned row;             ///< number of rows
177         unsigned col;             ///< number of columns
178         exvector m;               ///< representation (cols indexed first)
179 };
180 GINAC_DECLARE_UNARCHIVER(matrix); 
181
182
183 // wrapper functions around member functions
184
185 inline size_t nops(const matrix & m)
186 { return m.nops(); }
187
188 inline ex expand(const matrix & m, unsigned options = 0)
189 { return m.expand(options); }
190
191 inline ex eval(const matrix & m, int level = 0)
192 { return m.eval(level); }
193
194 inline ex evalf(const matrix & m, int level = 0)
195 { return m.evalf(level); }
196
197 inline unsigned rows(const matrix & m)
198 { return m.rows(); }
199
200 inline unsigned cols(const matrix & m)
201 { return m.cols(); }
202
203 inline matrix transpose(const matrix & m)
204 { return m.transpose(); }
205
206 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
207 { return m.determinant(options); }
208
209 inline ex trace(const matrix & m)
210 { return m.trace(); }
211
212 inline ex charpoly(const matrix & m, const ex & lambda)
213 { return m.charpoly(lambda); }
214
215 inline matrix inverse(const matrix & m)
216 { return m.inverse(); }
217
218 inline unsigned rank(const matrix & m)
219 { return m.rank(); }
220
221 // utility functions
222
223 /** Convert list of lists to matrix. */
224 extern ex lst_to_matrix(const lst & l);
225
226 /** Convert list of diagonal elements to matrix. */
227 extern ex diag_matrix(const lst & l);
228
229 /** Create an r times c unit matrix. */
230 extern ex unit_matrix(unsigned r, unsigned c);
231
232 /** Create a x times x unit matrix. */
233 inline ex unit_matrix(unsigned x)
234 { return unit_matrix(x, x); }
235
236 /** Create an r times c matrix of newly generated symbols consisting of the
237  *  given base name plus the numeric row/column position of each element.
238  *  The base name for LaTeX output is specified separately. */
239 extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
240
241 /** Return the reduced matrix that is formed by deleting the rth row and cth
242  *  column of matrix m. The determinant of the result is the Minor r, c. */
243 extern ex reduced_matrix(const matrix& m, unsigned r, unsigned c);
244
245 /** Return the nr times nc submatrix starting at position r, c of matrix m. */
246 extern ex sub_matrix(const matrix&m, unsigned r, unsigned nr, unsigned c, unsigned nc);
247
248 /** Create an r times c matrix of newly generated symbols consisting of the
249  *  given base name plus the numeric row/column position of each element. */
250 inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
251 { return symbolic_matrix(r, c, base_name, base_name); }
252
253 } // namespace GiNaC
254
255 #endif // ndef GINAC_MATRIX_H