]> www.ginac.de Git - ginac.git/blob - ginac/matrix.h
* Supplement some (now deprecated) macros by inlined template functions:
[ginac.git] / ginac / matrix.h
1 /** @file matrix.h
2  *
3  *  Interface to symbolic matrices */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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 "basic.h"
28 #include "ex.h"
29
30 namespace GiNaC {
31
32 /** Symbolic matrices. */
33 class matrix : public basic
34 {
35         GINAC_DECLARE_REGISTERED_CLASS(matrix, basic)
36         
37         // other ctors
38 public:
39         matrix(unsigned r, unsigned c);
40         matrix(unsigned r, unsigned c, const exvector & m2);
41         matrix(unsigned r, unsigned c, const lst & l);
42         
43         // functions overriding virtual functions from bases classes
44 public:
45         void print(const print_context & c, unsigned level = 0) const;
46         unsigned nops() const;
47         ex op(int i) const;
48         ex & let_op(int i);
49         ex expand(unsigned options=0) const;
50         ex eval(int level=0) const;
51         ex evalf(int level=0) const;
52         ex evalm(void) const {return *this;}
53         ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
54         ex eval_indexed(const basic & i) const;
55         ex add_indexed(const ex & self, const ex & other) const;
56         ex scalar_mul_indexed(const ex & self, const numeric & other) const;
57         bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
58 protected:
59         unsigned return_type(void) const { return return_types::noncommutative; };
60         // new virtual functions which can be overridden by derived classes
61         // (none)
62         
63         // non-virtual functions in this class
64 public:
65         unsigned rows(void) const        /// Get number of rows.
66                 { return row; }
67         unsigned cols(void) const        /// Get number of columns.
68                 { return col; }
69         matrix add(const matrix & other) const;
70         matrix sub(const matrix & other) const;
71         matrix mul(const matrix & other) const;
72         matrix mul(const numeric & other) const;
73         matrix mul_scalar(const ex & other) const;
74         matrix pow(const ex & expn) const;
75         const ex & operator() (unsigned ro, unsigned co) const;
76         ex & operator() (unsigned ro, unsigned co);
77         matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
78         matrix transpose(void) const;
79         ex determinant(unsigned algo = determinant_algo::automatic) const;
80         ex trace(void) const;
81         ex charpoly(const symbol & lambda) const;
82         matrix inverse(void) const;
83         matrix solve(const matrix & vars, const matrix & rhs,
84                      unsigned algo = solve_algo::automatic) const;
85 protected:
86         ex determinant_minor(void) const;
87         int gauss_elimination(const bool det = false);
88         int division_free_elimination(const bool det = false);
89         int fraction_free_elimination(const bool det = false);
90         int pivot(unsigned ro, unsigned co, bool symbolic = true);
91         
92 // member variables
93 protected:
94         unsigned row;             ///< number of rows
95         unsigned col;             ///< number of columns
96         exvector m;               ///< representation (cols indexed first)
97 };
98
99
100 // wrapper functions around member functions
101
102 inline unsigned nops(const matrix & m)
103 { return m.nops(); }
104
105 inline ex expand(const matrix & m, unsigned options = 0)
106 { return m.expand(options); }
107
108 inline ex eval(const matrix & m, int level = 0)
109 { return m.eval(level); }
110
111 inline ex evalf(const matrix & m, int level = 0)
112 { return m.evalf(level); }
113
114 inline unsigned rows(const matrix & m)
115 { return m.rows(); }
116
117 inline unsigned cols(const matrix & m)
118 { return m.cols(); }
119
120 inline matrix transpose(const matrix & m)
121 { return m.transpose(); }
122
123 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
124 { return m.determinant(options); }
125
126 inline ex trace(const matrix & m)
127 { return m.trace(); }
128
129 inline ex charpoly(const matrix & m, const symbol & lambda)
130 { return m.charpoly(lambda); }
131
132 inline matrix inverse(const matrix & m)
133 { return m.inverse(); }
134
135 // utility functions
136
137 /** Return the matrix object handled by an ex.
138  *  This is unsafe: you need to check the type first. */
139 inline const matrix &ex_to_matrix(const ex &e)
140 {
141         return static_cast<const matrix &>(*e.bp);
142 }
143
144 /** Specialization of is_exactly_a<matrix>(obj) for matrix objects. */
145 template<> inline bool is_exactly_a<matrix>(const basic & obj)
146 {
147         return obj.tinfo()==TINFO_matrix;
148 }
149
150 /** Convert list of lists to matrix. */
151 extern ex lst_to_matrix(const lst & l);
152
153 /** Convert list of diagonal elements to matrix. */
154 extern ex diag_matrix(const lst & l);
155
156 } // namespace GiNaC
157
158 #endif // ndef __GINAC_MATRIX_H__