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