3 * Interface to symbolic matrices */
6 * GiNaC Copyright (C) 1999-2006 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;
124 ex real_part() const;
125 ex imag_part() const;
128 bool match_same_type(const basic & other) const;
129 unsigned return_type() const { return return_types::noncommutative; };
131 // non-virtual functions in this class
133 unsigned rows() const /// Get number of rows.
135 unsigned cols() const /// Get number of columns.
137 matrix add(const matrix & other) const;
138 matrix sub(const matrix & other) const;
139 matrix mul(const matrix & other) const;
140 matrix mul(const numeric & other) const;
141 matrix mul_scalar(const ex & other) const;
142 matrix pow(const ex & expn) const;
143 const ex & operator() (unsigned ro, unsigned co) const;
144 ex & operator() (unsigned ro, unsigned co);
145 matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
146 matrix transpose() const;
147 ex determinant(unsigned algo = determinant_algo::automatic) const;
149 ex charpoly(const ex & lambda) const;
150 matrix inverse() const;
151 matrix solve(const matrix & vars, const matrix & rhs,
152 unsigned algo = solve_algo::automatic) const;
153 unsigned rank() const;
155 ex determinant_minor() const;
156 int gauss_elimination(const bool det = false);
157 int division_free_elimination(const bool det = false);
158 int fraction_free_elimination(const bool det = false);
159 int pivot(unsigned ro, unsigned co, bool symbolic = true);
161 void print_elements(const print_context & c, const char *row_start, const char *row_end, const char *row_sep, const char *col_sep) const;
162 void do_print(const print_context & c, unsigned level) const;
163 void do_print_latex(const print_latex & c, unsigned level) const;
164 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
168 unsigned row; ///< number of rows
169 unsigned col; ///< number of columns
170 exvector m; ///< representation (cols indexed first)
174 // wrapper functions around member functions
176 inline size_t nops(const matrix & m)
179 inline ex expand(const matrix & m, unsigned options = 0)
180 { return m.expand(options); }
182 inline ex eval(const matrix & m, int level = 0)
183 { return m.eval(level); }
185 inline ex evalf(const matrix & m, int level = 0)
186 { return m.evalf(level); }
188 inline unsigned rows(const matrix & m)
191 inline unsigned cols(const matrix & m)
194 inline matrix transpose(const matrix & m)
195 { return m.transpose(); }
197 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
198 { return m.determinant(options); }
200 inline ex trace(const matrix & m)
201 { return m.trace(); }
203 inline ex charpoly(const matrix & m, const ex & lambda)
204 { return m.charpoly(lambda); }
206 inline matrix inverse(const matrix & m)
207 { return m.inverse(); }
209 inline unsigned rank(const matrix & m)
214 /** Convert list of lists to matrix. */
215 extern ex lst_to_matrix(const lst & l);
217 /** Convert list of diagonal elements to matrix. */
218 extern ex diag_matrix(const lst & l);
220 /** Create an r times c unit matrix. */
221 extern ex unit_matrix(unsigned r, unsigned c);
223 /** Create a x times x unit matrix. */
224 inline ex unit_matrix(unsigned x)
225 { return unit_matrix(x, x); }
227 /** Create an r times c matrix of newly generated symbols consisting of the
228 * given base name plus the numeric row/column position of each element.
229 * The base name for LaTeX output is specified separately. */
230 extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
232 /** Return the reduced matrix that is formed by deleting the rth row and cth
233 * column of matrix m. The determinant of the result is the Minor r, c. */
234 extern ex reduced_matrix(const matrix& m, unsigned r, unsigned c);
236 /** Return the nr times nc submatrix starting at position r, c of matrix m. */
237 extern ex sub_matrix(const matrix&m, unsigned r, unsigned nr, unsigned c, unsigned nc);
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 inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
242 { return symbolic_matrix(r, c, base_name, base_name); }
246 #endif // ndef __GINAC_MATRIX_H__