3 * Interface to symbolic matrices */
6 * GiNaC Copyright (C) 1999-2015 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 /** Helper template to allow initialization of matrices via an overloaded
37 * comma operator (idea stolen from Blitz++). */
38 template <typename T, typename It>
41 matrix_init(It i) : iter(i) {}
43 matrix_init<T, It> operator,(const T & x)
46 return matrix_init<T, It>(++iter);
49 // The following specializations produce much tighter code than the
52 matrix_init<T, It> operator,(int x)
55 return matrix_init<T, It>(++iter);
58 matrix_init<T, It> operator,(unsigned int x)
61 return matrix_init<T, It>(++iter);
64 matrix_init<T, It> operator,(long x)
67 return matrix_init<T, It>(++iter);
70 matrix_init<T, It> operator,(unsigned long x)
73 return matrix_init<T, It>(++iter);
76 matrix_init<T, It> operator,(double x)
79 return matrix_init<T, It>(++iter);
82 matrix_init<T, It> operator,(const symbol & x)
85 return matrix_init<T, It>(++iter);
94 /** Symbolic matrices. */
95 class matrix : public basic
97 GINAC_DECLARE_REGISTERED_CLASS(matrix, basic)
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);
105 matrix_init<ex, exvector::iterator> operator=(const ex & x) deprecated;
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
111 size_t nops() const override;
112 ex op(size_t i) const override;
113 ex & let_op(size_t i) override;
114 ex eval(int level=0) const override;
115 ex evalm() const override {return *this;}
116 ex subs(const exmap & m, unsigned options = 0) const override;
117 ex eval_indexed(const basic & i) const override;
118 ex add_indexed(const ex & self, const ex & other) const override;
119 ex scalar_mul_indexed(const ex & self, const numeric & other) const override;
120 bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const override;
121 ex conjugate() const override;
122 ex real_part() const override;
123 ex imag_part() const override;
125 /** Save (a.k.a. serialize) object into archive. */
126 void archive(archive_node& n) const override;
127 /** Read (a.k.a. deserialize) object from archive. */
128 void read_archive(const archive_node& n, lst& syms) override;
130 bool match_same_type(const basic & other) const override;
131 unsigned return_type() const override { return return_types::noncommutative; };
133 // non-virtual functions in this class
135 unsigned rows() const /// Get number of rows.
137 unsigned cols() const /// Get number of columns.
139 matrix add(const matrix & other) const;
140 matrix sub(const matrix & other) const;
141 matrix mul(const matrix & other) const;
142 matrix mul(const numeric & other) const;
143 matrix mul_scalar(const ex & other) const;
144 matrix pow(const ex & expn) const;
145 const ex & operator() (unsigned ro, unsigned co) const;
146 ex & operator() (unsigned ro, unsigned co);
147 matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
148 matrix transpose() const;
149 ex determinant(unsigned algo = determinant_algo::automatic) const;
151 ex charpoly(const ex & lambda) const;
152 matrix inverse() const;
153 matrix solve(const matrix & vars, const matrix & rhs,
154 unsigned algo = solve_algo::automatic) const;
155 unsigned rank() const;
156 bool is_zero_matrix() const;
158 ex determinant_minor() const;
159 int gauss_elimination(const bool det = false);
160 int division_free_elimination(const bool det = false);
161 int fraction_free_elimination(const bool det = false);
162 int pivot(unsigned ro, unsigned co, bool symbolic = true);
164 void print_elements(const print_context & c, const char *row_start, const char *row_end, const char *row_sep, const char *col_sep) const;
165 void do_print(const print_context & c, unsigned level) const;
166 void do_print_latex(const print_latex & c, unsigned level) const;
167 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
171 unsigned row; ///< number of rows
172 unsigned col; ///< number of columns
173 exvector m; ///< representation (cols indexed first)
175 GINAC_DECLARE_UNARCHIVER(matrix);
177 // First step of initialization of matrix with a comma-separated sequence
178 // of expressions. Subsequent steps are handled by matrix_init<>::operator,().
179 inline matrix_init<ex, exvector::iterator> matrix::operator=(const ex & x)
182 return matrix_init<ex, exvector::iterator>(++m.begin());
185 // wrapper functions around member functions
187 inline size_t nops(const matrix & m)
190 inline ex expand(const matrix & m, unsigned options = 0)
191 { return m.expand(options); }
193 inline ex eval(const matrix & m, int level = 0)
194 { return m.eval(level); }
196 inline ex evalf(const matrix & m, int level = 0)
197 { return m.evalf(level); }
199 inline unsigned rows(const matrix & m)
202 inline unsigned cols(const matrix & m)
205 inline matrix transpose(const matrix & m)
206 { return m.transpose(); }
208 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
209 { return m.determinant(options); }
211 inline ex trace(const matrix & m)
212 { return m.trace(); }
214 inline ex charpoly(const matrix & m, const ex & lambda)
215 { return m.charpoly(lambda); }
217 inline matrix inverse(const matrix & m)
218 { return m.inverse(); }
220 inline unsigned rank(const matrix & m)
225 /** Convert list of lists to matrix. */
226 extern ex lst_to_matrix(const lst & l);
228 /** Convert list of diagonal elements to matrix. */
229 extern ex diag_matrix(const lst & l);
230 extern ex diag_matrix(std::initializer_list<ex> l);
232 /** Create an r times c unit matrix. */
233 extern ex unit_matrix(unsigned r, unsigned c);
235 /** Create a x times x unit matrix. */
236 inline ex unit_matrix(unsigned x)
237 { return unit_matrix(x, x); }
239 /** Create an r times c matrix of newly generated symbols consisting of the
240 * given base name plus the numeric row/column position of each element.
241 * The base name for LaTeX output is specified separately. */
242 extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
244 /** Return the reduced matrix that is formed by deleting the rth row and cth
245 * column of matrix m. The determinant of the result is the Minor r, c. */
246 extern ex reduced_matrix(const matrix& m, unsigned r, unsigned c);
248 /** Return the nr times nc submatrix starting at position r, c of matrix m. */
249 extern ex sub_matrix(const matrix&m, unsigned r, unsigned nr, unsigned c, unsigned nc);
251 /** Create an r times c matrix of newly generated symbols consisting of the
252 * given base name plus the numeric row/column position of each element. */
253 inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
254 { return symbolic_matrix(r, c, base_name, base_name); }
258 #endif // ndef GINAC_MATRIX_H