3 * Interface to symbolic matrices */
6 * GiNaC Copyright (C) 1999-2024 Johannes Gutenberg University Mainz, Germany
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.
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.
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
23 #ifndef GINAC_MATRIX_H
24 #define GINAC_MATRIX_H
36 /** Symbolic matrices. */
37 class matrix : public basic
39 GINAC_DECLARE_REGISTERED_CLASS(matrix, basic)
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);
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
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;
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;
70 bool match_same_type(const basic & other) const override;
71 unsigned return_type() const override { return return_types::noncommutative; };
73 // non-virtual functions in this class
75 unsigned rows() const /// Get number of rows.
77 unsigned cols() const /// Get number of columns.
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;
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;
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);
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;
115 unsigned row; ///< number of rows
116 unsigned col; ///< number of columns
117 exvector m; ///< representation (cols indexed first)
119 GINAC_DECLARE_UNARCHIVER(matrix);
121 // wrapper functions around member functions
123 inline size_t nops(const matrix & m)
126 inline ex expand(const matrix & m, unsigned options = 0)
127 { return m.expand(options); }
129 inline ex evalf(const matrix & m)
130 { return m.evalf(); }
132 inline unsigned rows(const matrix & m)
135 inline unsigned cols(const matrix & m)
138 inline matrix transpose(const matrix & m)
139 { return m.transpose(); }
141 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
142 { return m.determinant(options); }
144 inline ex trace(const matrix & m)
145 { return m.trace(); }
147 inline ex charpoly(const matrix & m, const ex & lambda)
148 { return m.charpoly(lambda); }
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); }
155 inline unsigned rank(const matrix & m)
157 inline unsigned rank(const matrix & m, unsigned solve_algo)
158 { return m.rank(solve_algo); }
162 /** Convert list of lists to matrix. */
163 extern ex lst_to_matrix(const lst & l);
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);
169 /** Create an r times c unit matrix. */
170 extern ex unit_matrix(unsigned r, unsigned c);
172 /** Create a x times x unit matrix. */
173 inline ex unit_matrix(unsigned x)
174 { return unit_matrix(x, x); }
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);
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);
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);
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); }
195 #endif // ndef GINAC_MATRIX_H