]> www.ginac.de Git - ginac.git/blob - ginac/matrix.h
ex_to<>(), is_a<>() and is_exactly_a<>() weren't inlined
[ginac.git] / ginac / matrix.h
1 /** @file matrix.h
2  *
3  *  Interface to symbolic matrices */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2003 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 /** Symbolic matrices. */
34 class matrix : public basic
35 {
36         GINAC_DECLARE_REGISTERED_CLASS(matrix, basic)
37         
38         // other constructors
39 public:
40         matrix(unsigned r, unsigned c);
41         matrix(unsigned r, unsigned c, const exvector & m2);
42         matrix(unsigned r, unsigned c, const lst & l);
43         
44         // functions overriding virtual functions from base classes
45 public:
46         size_t nops() const;
47         ex op(size_t i) const;
48         ex & let_op(size_t i);
49         ex eval(int level=0) const;
50         ex evalm() const {return *this;}
51         ex subs(const exmap & m, unsigned options = 0) const;
52         ex eval_indexed(const basic & i) const;
53         ex add_indexed(const ex & self, const ex & other) const;
54         ex scalar_mul_indexed(const ex & self, const numeric & other) const;
55         bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
56
57 protected:
58         bool match_same_type(const basic & other) const;
59         unsigned return_type() const { return return_types::noncommutative; };
60         
61         // non-virtual functions in this class
62 public:
63         unsigned rows() const        /// Get number of rows.
64                 { return row; }
65         unsigned cols() const        /// Get number of columns.
66                 { return col; }
67         matrix add(const matrix & other) const;
68         matrix sub(const matrix & other) const;
69         matrix mul(const matrix & other) const;
70         matrix mul(const numeric & other) const;
71         matrix mul_scalar(const ex & other) const;
72         matrix pow(const ex & expn) const;
73         const ex & operator() (unsigned ro, unsigned co) const;
74         ex & operator() (unsigned ro, unsigned co);
75         matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
76         matrix transpose() const;
77         ex determinant(unsigned algo = determinant_algo::automatic) const;
78         ex trace() const;
79         ex charpoly(const symbol & lambda) const;
80         matrix inverse() const;
81         matrix solve(const matrix & vars, const matrix & rhs,
82                      unsigned algo = solve_algo::automatic) const;
83 protected:
84         ex determinant_minor() const;
85         int gauss_elimination(const bool det = false);
86         int division_free_elimination(const bool det = false);
87         int fraction_free_elimination(const bool det = false);
88         int pivot(unsigned ro, unsigned co, bool symbolic = true);
89
90         void print_elements(const print_context & c, const char *row_start, const char *row_end, const char *row_sep, const char *col_sep) const;
91         void do_print(const print_context & c, unsigned level) const;
92         void do_print_latex(const print_latex & c, unsigned level) const;
93         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
94         
95 // member variables
96 protected:
97         unsigned row;             ///< number of rows
98         unsigned col;             ///< number of columns
99         exvector m;               ///< representation (cols indexed first)
100 };
101
102
103 // wrapper functions around member functions
104
105 inline size_t nops(const matrix & m)
106 { return m.nops(); }
107
108 inline ex expand(const matrix & m, unsigned options = 0)
109 { return m.expand(options); }
110
111 inline ex eval(const matrix & m, int level = 0)
112 { return m.eval(level); }
113
114 inline ex evalf(const matrix & m, int level = 0)
115 { return m.evalf(level); }
116
117 inline unsigned rows(const matrix & m)
118 { return m.rows(); }
119
120 inline unsigned cols(const matrix & m)
121 { return m.cols(); }
122
123 inline matrix transpose(const matrix & m)
124 { return m.transpose(); }
125
126 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
127 { return m.determinant(options); }
128
129 inline ex trace(const matrix & m)
130 { return m.trace(); }
131
132 inline ex charpoly(const matrix & m, const symbol & lambda)
133 { return m.charpoly(lambda); }
134
135 inline matrix inverse(const matrix & m)
136 { return m.inverse(); }
137
138 // utility functions
139
140 /** Specialization of is_exactly_a<matrix>(obj) for matrix objects. */
141 template<> inline bool is_exactly_a<matrix>(const basic & obj)
142 {
143         return obj.tinfo()==TINFO_matrix;
144 }
145
146 /** Convert list of lists to matrix. */
147 extern ex lst_to_matrix(const lst & l);
148
149 /** Convert list of diagonal elements to matrix. */
150 extern ex diag_matrix(const lst & l);
151
152 /** Create an r times c unit matrix. */
153 extern ex unit_matrix(unsigned r, unsigned c);
154
155 /** Create a x times x unit matrix. */
156 inline ex unit_matrix(unsigned x)
157 { return unit_matrix(x, x); }
158
159 /** Create an r times c matrix of newly generated symbols consisting of the
160  *  given base name plus the numeric row/column position of each element.
161  *  The base name for LaTeX output is specified separately. */
162 extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
163
164 /** Create an r times c matrix of newly generated symbols consisting of the
165  *  given base name plus the numeric row/column position of each element. */
166 inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
167 { return symbolic_matrix(r, c, base_name, base_name); }
168
169 } // namespace GiNaC
170
171 #endif // ndef __GINAC_MATRIX_H__