3 * Interface to symbolic matrices */
6 * GiNaC Copyright (C) 1999-2014 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 exvector & m2);
102 matrix(unsigned r, unsigned c, const lst & l);
104 // First step of initialization of matrix with a comma-separated seqeuence
105 // of expressions. Subsequent steps are handled by matrix_init<>::operator,().
106 matrix_init<ex, exvector::iterator> operator=(const ex & x)
109 return matrix_init<ex, exvector::iterator>(++m.begin());
112 // functions overriding virtual functions from base classes
115 ex op(size_t i) const;
116 ex & let_op(size_t i);
117 ex eval(int level=0) const;
118 ex evalm() const {return *this;}
119 ex subs(const exmap & m, unsigned options = 0) const;
120 ex eval_indexed(const basic & i) const;
121 ex add_indexed(const ex & self, const ex & other) const;
122 ex scalar_mul_indexed(const ex & self, const numeric & other) const;
123 bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
124 ex conjugate() const;
125 ex real_part() const;
126 ex imag_part() const;
128 /** Save (a.k.a. serialize) object into archive. */
129 void archive(archive_node& n) const;
130 /** Read (a.k.a. deserialize) object from archive. */
131 void read_archive(const archive_node& n, lst& syms);
133 bool match_same_type(const basic & other) const;
134 unsigned return_type() const { return return_types::noncommutative; };
136 // non-virtual functions in this class
138 unsigned rows() const /// Get number of rows.
140 unsigned cols() const /// Get number of columns.
142 matrix add(const matrix & other) const;
143 matrix sub(const matrix & other) const;
144 matrix mul(const matrix & other) const;
145 matrix mul(const numeric & other) const;
146 matrix mul_scalar(const ex & other) const;
147 matrix pow(const ex & expn) const;
148 const ex & operator() (unsigned ro, unsigned co) const;
149 ex & operator() (unsigned ro, unsigned co);
150 matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
151 matrix transpose() const;
152 ex determinant(unsigned algo = determinant_algo::automatic) const;
154 ex charpoly(const ex & lambda) const;
155 matrix inverse() const;
156 matrix solve(const matrix & vars, const matrix & rhs,
157 unsigned algo = solve_algo::automatic) const;
158 unsigned rank() const;
159 bool is_zero_matrix() const;
161 ex determinant_minor() const;
162 int gauss_elimination(const bool det = false);
163 int division_free_elimination(const bool det = false);
164 int fraction_free_elimination(const bool det = false);
165 int pivot(unsigned ro, unsigned co, bool symbolic = true);
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;
174 unsigned row; ///< number of rows
175 unsigned col; ///< number of columns
176 exvector m; ///< representation (cols indexed first)
178 GINAC_DECLARE_UNARCHIVER(matrix);
181 // wrapper functions around member functions
183 inline size_t nops(const matrix & m)
186 inline ex expand(const matrix & m, unsigned options = 0)
187 { return m.expand(options); }
189 inline ex eval(const matrix & m, int level = 0)
190 { return m.eval(level); }
192 inline ex evalf(const matrix & m, int level = 0)
193 { return m.evalf(level); }
195 inline unsigned rows(const matrix & m)
198 inline unsigned cols(const matrix & m)
201 inline matrix transpose(const matrix & m)
202 { return m.transpose(); }
204 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
205 { return m.determinant(options); }
207 inline ex trace(const matrix & m)
208 { return m.trace(); }
210 inline ex charpoly(const matrix & m, const ex & lambda)
211 { return m.charpoly(lambda); }
213 inline matrix inverse(const matrix & m)
214 { return m.inverse(); }
216 inline unsigned rank(const matrix & m)
221 /** Convert list of lists to matrix. */
222 extern ex lst_to_matrix(const lst & l);
224 /** Convert list of diagonal elements to matrix. */
225 extern ex diag_matrix(const lst & l);
227 /** Create an r times c unit matrix. */
228 extern ex unit_matrix(unsigned r, unsigned c);
230 /** Create a x times x unit matrix. */
231 inline ex unit_matrix(unsigned x)
232 { return unit_matrix(x, x); }
234 /** Create an r times c matrix of newly generated symbols consisting of the
235 * given base name plus the numeric row/column position of each element.
236 * The base name for LaTeX output is specified separately. */
237 extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
239 /** Return the reduced matrix that is formed by deleting the rth row and cth
240 * column of matrix m. The determinant of the result is the Minor r, c. */
241 extern ex reduced_matrix(const matrix& m, unsigned r, unsigned c);
243 /** Return the nr times nc submatrix starting at position r, c of matrix m. */
244 extern ex sub_matrix(const matrix&m, unsigned r, unsigned nr, unsigned c, unsigned nc);
246 /** Create an r times c matrix of newly generated symbols consisting of the
247 * given base name plus the numeric row/column position of each element. */
248 inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
249 { return symbolic_matrix(r, c, base_name, base_name); }
253 #endif // ndef GINAC_MATRIX_H