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