3 * Interface to symbolic matrices */
6 * GiNaC Copyright (C) 1999-2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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;
125 bool match_same_type(const basic & other) const;
126 unsigned return_type() const { return return_types::noncommutative; };
128 // non-virtual functions in this class
130 unsigned rows() const /// Get number of rows.
132 unsigned cols() const /// Get number of columns.
134 matrix add(const matrix & other) const;
135 matrix sub(const matrix & other) const;
136 matrix mul(const matrix & other) const;
137 matrix mul(const numeric & other) const;
138 matrix mul_scalar(const ex & other) const;
139 matrix pow(const ex & expn) const;
140 const ex & operator() (unsigned ro, unsigned co) const;
141 ex & operator() (unsigned ro, unsigned co);
142 matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
143 matrix transpose() const;
144 ex determinant(unsigned algo = determinant_algo::automatic) const;
146 ex charpoly(const ex & lambda) const;
147 matrix inverse() const;
148 matrix solve(const matrix & vars, const matrix & rhs,
149 unsigned algo = solve_algo::automatic) const;
151 ex determinant_minor() const;
152 int gauss_elimination(const bool det = false);
153 int division_free_elimination(const bool det = false);
154 int fraction_free_elimination(const bool det = false);
155 int pivot(unsigned ro, unsigned co, bool symbolic = true);
157 void print_elements(const print_context & c, const char *row_start, const char *row_end, const char *row_sep, const char *col_sep) const;
158 void do_print(const print_context & c, unsigned level) const;
159 void do_print_latex(const print_latex & c, unsigned level) const;
160 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
164 unsigned row; ///< number of rows
165 unsigned col; ///< number of columns
166 exvector m; ///< representation (cols indexed first)
170 // wrapper functions around member functions
172 inline size_t nops(const matrix & m)
175 inline ex expand(const matrix & m, unsigned options = 0)
176 { return m.expand(options); }
178 inline ex eval(const matrix & m, int level = 0)
179 { return m.eval(level); }
181 inline ex evalf(const matrix & m, int level = 0)
182 { return m.evalf(level); }
184 inline unsigned rows(const matrix & m)
187 inline unsigned cols(const matrix & m)
190 inline matrix transpose(const matrix & m)
191 { return m.transpose(); }
193 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
194 { return m.determinant(options); }
196 inline ex trace(const matrix & m)
197 { return m.trace(); }
199 inline ex charpoly(const matrix & m, const ex & lambda)
200 { return m.charpoly(lambda); }
202 inline matrix inverse(const matrix & m)
203 { return m.inverse(); }
207 /** Specialization of is_exactly_a<matrix>(obj) for matrix objects. */
208 template<> inline bool is_exactly_a<matrix>(const basic & obj)
210 return obj.tinfo()==TINFO_matrix;
213 /** Convert list of lists to matrix. */
214 extern ex lst_to_matrix(const lst & l);
216 /** Convert list of diagonal elements to matrix. */
217 extern ex diag_matrix(const lst & l);
219 /** Create an r times c unit matrix. */
220 extern ex unit_matrix(unsigned r, unsigned c);
222 /** Create a x times x unit matrix. */
223 inline ex unit_matrix(unsigned x)
224 { return unit_matrix(x, x); }
226 /** Create an r times c matrix of newly generated symbols consisting of the
227 * given base name plus the numeric row/column position of each element.
228 * The base name for LaTeX output is specified separately. */
229 extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
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 inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
234 { return symbolic_matrix(r, c, base_name, base_name); }
238 #endif // ndef __GINAC_MATRIX_H__