]> www.ginac.de Git - ginac.git/blob - ginac/matrix.h
* Added fsolve() numerical univariate real-valued function solver.
[ginac.git] / ginac / matrix.h
1 /** @file matrix.h
2  *
3  *  Interface to symbolic matrices */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #ifndef __GINAC_MATRIX_H__
24 #define __GINAC_MATRIX_H__
25
26 #include <vector>
27 #include <string>
28 #include "basic.h"
29 #include "ex.h"
30
31 namespace GiNaC {
32
33
34 /** Helper template to allow initialization of matrices via an overloaded
35  *  comma operator (idea stolen from Blitz++). */
36 template <typename T, typename It>
37 class matrix_init {
38 public:
39         matrix_init(It i) : iter(i) {}
40
41         matrix_init<T, It> operator,(const T & x)
42         {
43                 *iter = x;
44                 return matrix_init<T, It>(++iter);
45         }
46
47         // The following specializations produce much tighter code than the
48         // general case above
49
50         matrix_init<T, It> operator,(int x)
51         {
52                 *iter = T(x);
53                 return matrix_init<T, It>(++iter);
54         }
55
56         matrix_init<T, It> operator,(unsigned int x)
57         {
58                 *iter = T(x);
59                 return matrix_init<T, It>(++iter);
60         }
61
62         matrix_init<T, It> operator,(long x)
63         {
64                 *iter = T(x);
65                 return matrix_init<T, It>(++iter);
66         }
67
68         matrix_init<T, It> operator,(unsigned long x)
69         {
70                 *iter = T(x);
71                 return matrix_init<T, It>(++iter);
72         }
73
74         matrix_init<T, It> operator,(double x)
75         {
76                 *iter = T(x);
77                 return matrix_init<T, It>(++iter);
78         }
79
80         matrix_init<T, It> operator,(const symbol & x)
81         {
82                 *iter = T(x);
83                 return matrix_init<T, It>(++iter);
84         }
85
86 private:
87         matrix_init();
88         It iter;
89 };
90
91
92 /** Symbolic matrices. */
93 class matrix : public basic
94 {
95         GINAC_DECLARE_REGISTERED_CLASS(matrix, basic)
96         
97         // other constructors
98 public:
99         matrix(unsigned r, unsigned c);
100         matrix(unsigned r, unsigned c, const exvector & m2);
101         matrix(unsigned r, unsigned c, const lst & l);
102
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)
106         {
107                 m[0] = x;
108                 return matrix_init<ex, exvector::iterator>(++m.begin());
109         }
110         
111         // functions overriding virtual functions from base classes
112 public:
113         size_t nops() const;
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
125 protected:
126         bool match_same_type(const basic & other) const;
127         unsigned return_type() const { return return_types::noncommutative; };
128         
129         // non-virtual functions in this class
130 public:
131         unsigned rows() const        /// Get number of rows.
132                 { return row; }
133         unsigned cols() const        /// Get number of columns.
134                 { return col; }
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;
146         ex trace() 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;
152 protected:
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);
158
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;
163         
164 // member variables
165 protected:
166         unsigned row;             ///< number of rows
167         unsigned col;             ///< number of columns
168         exvector m;               ///< representation (cols indexed first)
169 };
170
171
172 // wrapper functions around member functions
173
174 inline size_t nops(const matrix & m)
175 { return m.nops(); }
176
177 inline ex expand(const matrix & m, unsigned options = 0)
178 { return m.expand(options); }
179
180 inline ex eval(const matrix & m, int level = 0)
181 { return m.eval(level); }
182
183 inline ex evalf(const matrix & m, int level = 0)
184 { return m.evalf(level); }
185
186 inline unsigned rows(const matrix & m)
187 { return m.rows(); }
188
189 inline unsigned cols(const matrix & m)
190 { return m.cols(); }
191
192 inline matrix transpose(const matrix & m)
193 { return m.transpose(); }
194
195 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
196 { return m.determinant(options); }
197
198 inline ex trace(const matrix & m)
199 { return m.trace(); }
200
201 inline ex charpoly(const matrix & m, const ex & lambda)
202 { return m.charpoly(lambda); }
203
204 inline matrix inverse(const matrix & m)
205 { return m.inverse(); }
206
207 inline unsigned rank(const matrix & m)
208 { return m.rank(); }
209
210 // utility functions
211
212 /** Specialization of is_exactly_a<matrix>(obj) for matrix objects. */
213 template<> inline bool is_exactly_a<matrix>(const basic & obj)
214 {
215         return obj.tinfo()==TINFO_matrix;
216 }
217
218 /** Convert list of lists to matrix. */
219 extern ex lst_to_matrix(const lst & l);
220
221 /** Convert list of diagonal elements to matrix. */
222 extern ex diag_matrix(const lst & l);
223
224 /** Create an r times c unit matrix. */
225 extern ex unit_matrix(unsigned r, unsigned c);
226
227 /** Create a x times x unit matrix. */
228 inline ex unit_matrix(unsigned x)
229 { return unit_matrix(x, x); }
230
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);
235
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 inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
239 { return symbolic_matrix(r, c, base_name, base_name); }
240
241 } // namespace GiNaC
242
243 #endif // ndef __GINAC_MATRIX_H__