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
35 /** Helper template to allow initialization of matrices via an overloaded
36 * comma operator (idea stolen from Blitz++). */
37 template <typename T, typename It>
40 matrix_init(It i) : iter(i) {}
42 matrix_init<T, It> operator,(const T & x)
45 return matrix_init<T, It>(++iter);
48 // The following specializations produce much tighter code than the
51 matrix_init<T, It> operator,(int x)
54 return matrix_init<T, It>(++iter);
57 matrix_init<T, It> operator,(unsigned int x)
60 return matrix_init<T, It>(++iter);
63 matrix_init<T, It> operator,(long x)
66 return matrix_init<T, It>(++iter);
69 matrix_init<T, It> operator,(unsigned long x)
72 return matrix_init<T, It>(++iter);
75 matrix_init<T, It> operator,(double x)
78 return matrix_init<T, It>(++iter);
81 matrix_init<T, It> operator,(const symbol & x)
84 return matrix_init<T, It>(++iter);
93 /** Symbolic matrices. */
94 class matrix : public basic
96 GINAC_DECLARE_REGISTERED_CLASS(matrix, basic)
100 matrix(unsigned r, unsigned c);
101 matrix(unsigned r, unsigned c, const lst & l);
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)
108 return matrix_init<ex, exvector::iterator>(++m.begin());
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
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;
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);
135 bool match_same_type(const basic & other) const;
136 unsigned return_type() const { return return_types::noncommutative; };
138 // non-virtual functions in this class
140 unsigned rows() const /// Get number of rows.
142 unsigned cols() const /// Get number of columns.
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;
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;
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);
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;
176 unsigned row; ///< number of rows
177 unsigned col; ///< number of columns
178 exvector m; ///< representation (cols indexed first)
180 GINAC_DECLARE_UNARCHIVER(matrix);
183 // wrapper functions around member functions
185 inline size_t nops(const matrix & m)
188 inline ex expand(const matrix & m, unsigned options = 0)
189 { return m.expand(options); }
191 inline ex eval(const matrix & m, int level = 0)
192 { return m.eval(level); }
194 inline ex evalf(const matrix & m, int level = 0)
195 { return m.evalf(level); }
197 inline unsigned rows(const matrix & m)
200 inline unsigned cols(const matrix & m)
203 inline matrix transpose(const matrix & m)
204 { return m.transpose(); }
206 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
207 { return m.determinant(options); }
209 inline ex trace(const matrix & m)
210 { return m.trace(); }
212 inline ex charpoly(const matrix & m, const ex & lambda)
213 { return m.charpoly(lambda); }
215 inline matrix inverse(const matrix & m)
216 { return m.inverse(); }
218 inline unsigned rank(const matrix & m)
223 /** Convert list of lists to matrix. */
224 extern ex lst_to_matrix(const lst & l);
226 /** Convert list of diagonal elements to matrix. */
227 extern ex diag_matrix(const lst & l);
229 /** Create an r times c unit matrix. */
230 extern ex unit_matrix(unsigned r, unsigned c);
232 /** Create a x times x unit matrix. */
233 inline ex unit_matrix(unsigned x)
234 { return unit_matrix(x, x); }
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);
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);
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);
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); }
255 #endif // ndef GINAC_MATRIX_H