3 * Interface to symbolic matrices */
6 * GiNaC Copyright (C) 1999-2007 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__
34 /** Helper template to allow initialization of matrices via an overloaded
35 * comma operator (idea stolen from Blitz++). */
36 template <typename T, typename It>
39 matrix_init(It i) : iter(i) {}
41 matrix_init<T, It> operator,(const T & x)
44 return matrix_init<T, It>(++iter);
47 // The following specializations produce much tighter code than the
50 matrix_init<T, It> operator,(int x)
53 return matrix_init<T, It>(++iter);
56 matrix_init<T, It> operator,(unsigned int x)
59 return matrix_init<T, It>(++iter);
62 matrix_init<T, It> operator,(long x)
65 return matrix_init<T, It>(++iter);
68 matrix_init<T, It> operator,(unsigned long x)
71 return matrix_init<T, It>(++iter);
74 matrix_init<T, It> operator,(double x)
77 return matrix_init<T, It>(++iter);
80 matrix_init<T, It> operator,(const symbol & x)
83 return matrix_init<T, It>(++iter);
92 /** Symbolic matrices. */
93 class matrix : public basic
95 GINAC_DECLARE_REGISTERED_CLASS(matrix, basic)
99 matrix(unsigned r, unsigned c);
100 matrix(unsigned r, unsigned c, const exvector & m2);
101 matrix(unsigned r, unsigned c, const lst & l);
103 // First step of initialization of matrix with a comma-separated seqeuence
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());
111 // functions overriding virtual functions from base classes
114 ex op(size_t i) const;
115 ex & let_op(size_t i);
116 ex eval(int level=0) const;
117 ex evalm() const {return *this;}
118 ex subs(const exmap & m, unsigned options = 0) const;
119 ex eval_indexed(const basic & i) const;
120 ex add_indexed(const ex & self, const ex & other) const;
121 ex scalar_mul_indexed(const ex & self, const numeric & other) const;
122 bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
123 ex conjugate() const;
126 bool match_same_type(const basic & other) const;
127 unsigned return_type() const { return return_types::noncommutative; };
129 // non-virtual functions in this class
131 unsigned rows() const /// Get number of rows.
133 unsigned cols() const /// Get number of columns.
135 matrix add(const matrix & other) const;
136 matrix sub(const matrix & other) const;
137 matrix mul(const matrix & other) const;
138 matrix mul(const numeric & other) const;
139 matrix mul_scalar(const ex & other) const;
140 matrix pow(const ex & expn) const;
141 const ex & operator() (unsigned ro, unsigned co) const;
142 ex & operator() (unsigned ro, unsigned co);
143 matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
144 matrix transpose() const;
145 ex determinant(unsigned algo = determinant_algo::automatic) const;
147 ex charpoly(const ex & lambda) const;
148 matrix inverse() const;
149 matrix solve(const matrix & vars, const matrix & rhs,
150 unsigned algo = solve_algo::automatic) const;
151 unsigned rank() const;
153 ex determinant_minor() const;
154 int gauss_elimination(const bool det = false);
155 int division_free_elimination(const bool det = false);
156 int fraction_free_elimination(const bool det = false);
157 int pivot(unsigned ro, unsigned co, bool symbolic = true);
159 void print_elements(const print_context & c, const char *row_start, const char *row_end, const char *row_sep, const char *col_sep) const;
160 void do_print(const print_context & c, unsigned level) const;
161 void do_print_latex(const print_latex & c, unsigned level) const;
162 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
166 unsigned row; ///< number of rows
167 unsigned col; ///< number of columns
168 exvector m; ///< representation (cols indexed first)
172 // wrapper functions around member functions
174 inline size_t nops(const matrix & m)
177 inline ex expand(const matrix & m, unsigned options = 0)
178 { return m.expand(options); }
180 inline ex eval(const matrix & m, int level = 0)
181 { return m.eval(level); }
183 inline ex evalf(const matrix & m, int level = 0)
184 { return m.evalf(level); }
186 inline unsigned rows(const matrix & m)
189 inline unsigned cols(const matrix & m)
192 inline matrix transpose(const matrix & m)
193 { return m.transpose(); }
195 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
196 { return m.determinant(options); }
198 inline ex trace(const matrix & m)
199 { return m.trace(); }
201 inline ex charpoly(const matrix & m, const ex & lambda)
202 { return m.charpoly(lambda); }
204 inline matrix inverse(const matrix & m)
205 { return m.inverse(); }
207 inline unsigned rank(const matrix & m)
212 /** Specialization of is_exactly_a<matrix>(obj) for matrix objects. */
213 template<> inline bool is_exactly_a<matrix>(const basic & obj)
215 return obj.tinfo()==TINFO_matrix;
218 /** Convert list of lists to matrix. */
219 extern ex lst_to_matrix(const lst & l);
221 /** Convert list of diagonal elements to matrix. */
222 extern ex diag_matrix(const lst & l);
224 /** Create an r times c unit matrix. */
225 extern ex unit_matrix(unsigned r, unsigned c);
227 /** Create a x times x unit matrix. */
228 inline ex unit_matrix(unsigned x)
229 { return unit_matrix(x, x); }
231 /** Create an r times c matrix of newly generated symbols consisting of the
232 * given base name plus the numeric row/column position of each element.
233 * The base name for LaTeX output is specified separately. */
234 extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
236 /** Return the reduced matrix that is formed by deleting the rth row and cth
237 * column of matrix m. The determinant of the result is the Minor r, c. */
238 extern ex reduced_matrix(const matrix& m, unsigned r, unsigned c);
240 /** Return the nr times nc submatrix starting at position r, c of matrix m. */
241 extern ex sub_matrix(const matrix&m, unsigned r, unsigned nr, unsigned c, unsigned nc);
243 /** Create an r times c matrix of newly generated symbols consisting of the
244 * given base name plus the numeric row/column position of each element. */
245 inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
246 { return symbolic_matrix(r, c, base_name, base_name); }
250 #endif // ndef __GINAC_MATRIX_H__