]> www.ginac.de Git - ginac.git/blob - ginac/matrix.h
Avoid multiple definitions of `lst::info` (MinGW compile fix)
[ginac.git] / ginac / matrix.h
1 /** @file matrix.h
2  *
3  *  Interface to symbolic matrices */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2021 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 /** Symbolic matrices. */
37 class matrix : public basic
38 {
39         GINAC_DECLARE_REGISTERED_CLASS(matrix, basic)
40         
41         // other constructors
42 public:
43         matrix(unsigned r, unsigned c);
44         matrix(unsigned r, unsigned c, const lst & l);
45         matrix(std::initializer_list<std::initializer_list<ex>> l);
46
47 protected:
48         matrix(unsigned r, unsigned c, const exvector & m2);
49         matrix(unsigned r, unsigned c, exvector && m2);
50         // functions overriding virtual functions from base classes
51 public:
52         size_t nops() const override;
53         ex op(size_t i) const override;
54         ex & let_op(size_t i) override;
55         ex evalm() const override {return *this;}
56         ex subs(const exmap & m, unsigned options = 0) const override;
57         ex eval_indexed(const basic & i) const override;
58         ex add_indexed(const ex & self, const ex & other) const override;
59         ex scalar_mul_indexed(const ex & self, const numeric & other) const override;
60         bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const override;
61         ex conjugate() const override;
62         ex real_part() const override;
63         ex imag_part() const override;
64
65         /** Save (a.k.a. serialize) object into archive. */
66         void archive(archive_node& n) const override;
67         /** Read (a.k.a. deserialize) object from archive. */
68         void read_archive(const archive_node& n, lst& syms) override;
69 protected:
70         bool match_same_type(const basic & other) const override;
71         unsigned return_type() const override { return return_types::noncommutative; };
72         
73         // non-virtual functions in this class
74 public:
75         unsigned rows() const        /// Get number of rows.
76                 { return row; }
77         unsigned cols() const        /// Get number of columns.
78                 { return col; }
79         matrix add(const matrix & other) const;
80         matrix sub(const matrix & other) const;
81         matrix mul(const matrix & other) const;
82         matrix mul(const numeric & other) const;
83         matrix mul_scalar(const ex & other) const;
84         matrix pow(const ex & expn) const;
85         const ex & operator() (unsigned ro, unsigned co) const;
86         ex & operator() (unsigned ro, unsigned co);
87         matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
88         matrix transpose() const;
89         ex determinant(unsigned algo = determinant_algo::automatic) const;
90         ex trace() const;
91         ex charpoly(const ex & lambda) const;
92         matrix inverse() const;
93         matrix inverse(unsigned algo) const;
94         matrix solve(const matrix & vars, const matrix & rhs,
95                      unsigned algo = solve_algo::automatic) const;
96         unsigned rank() const;
97         unsigned rank(unsigned solve_algo) const;
98         bool is_zero_matrix() const;
99 protected:
100         ex determinant_minor() const;
101         std::vector<unsigned> echelon_form(unsigned algo, int n);
102         int gauss_elimination(const bool det = false);
103         int division_free_elimination(const bool det = false);
104         int fraction_free_elimination(const bool det = false);
105         std::vector<unsigned> markowitz_elimination(unsigned n);
106         int pivot(unsigned ro, unsigned co, bool symbolic = true);
107
108         void print_elements(const print_context & c, const char *row_start, const char *row_end, const char *row_sep, const char *col_sep) const;
109         void do_print(const print_context & c, unsigned level) const;
110         void do_print_latex(const print_latex & c, unsigned level) const;
111         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
112         
113 // member variables
114 protected:
115         unsigned row;             ///< number of rows
116         unsigned col;             ///< number of columns
117         exvector m;               ///< representation (cols indexed first)
118 };
119 GINAC_DECLARE_UNARCHIVER(matrix); 
120
121 // wrapper functions around member functions
122
123 inline size_t nops(const matrix & m)
124 { return m.nops(); }
125
126 inline ex expand(const matrix & m, unsigned options = 0)
127 { return m.expand(options); }
128
129 inline ex evalf(const matrix & m)
130 { return m.evalf(); }
131
132 inline unsigned rows(const matrix & m)
133 { return m.rows(); }
134
135 inline unsigned cols(const matrix & m)
136 { return m.cols(); }
137
138 inline matrix transpose(const matrix & m)
139 { return m.transpose(); }
140
141 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
142 { return m.determinant(options); }
143
144 inline ex trace(const matrix & m)
145 { return m.trace(); }
146
147 inline ex charpoly(const matrix & m, const ex & lambda)
148 { return m.charpoly(lambda); }
149
150 inline matrix inverse(const matrix & m)
151 { return m.inverse(solve_algo::automatic); }
152 inline matrix inverse(const matrix & m, unsigned algo)
153 { return m.inverse(algo); }
154
155 inline unsigned rank(const matrix & m)
156 { return m.rank(); }
157 inline unsigned rank(const matrix & m, unsigned solve_algo)
158 { return m.rank(solve_algo); }
159
160 // utility functions
161
162 /** Convert list of lists to matrix. */
163 extern ex lst_to_matrix(const lst & l);
164
165 /** Convert list of diagonal elements to matrix. */
166 extern ex diag_matrix(const lst & l);
167 extern ex diag_matrix(std::initializer_list<ex> l);
168
169 /** Create an r times c unit matrix. */
170 extern ex unit_matrix(unsigned r, unsigned c);
171
172 /** Create a x times x unit matrix. */
173 inline ex unit_matrix(unsigned x)
174 { return unit_matrix(x, x); }
175
176 /** Create an r times c matrix of newly generated symbols consisting of the
177  *  given base name plus the numeric row/column position of each element.
178  *  The base name for LaTeX output is specified separately. */
179 extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
180
181 /** Return the reduced matrix that is formed by deleting the rth row and cth
182  *  column of matrix m. The determinant of the result is the Minor r, c. */
183 extern ex reduced_matrix(const matrix& m, unsigned r, unsigned c);
184
185 /** Return the nr times nc submatrix starting at position r, c of matrix m. */
186 extern ex sub_matrix(const matrix&m, unsigned r, unsigned nr, unsigned c, unsigned nc);
187
188 /** Create an r times c matrix of newly generated symbols consisting of the
189  *  given base name plus the numeric row/column position of each element. */
190 inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
191 { return symbolic_matrix(r, c, base_name, base_name); }
192
193 } // namespace GiNaC
194
195 #endif // ndef GINAC_MATRIX_H