]> www.ginac.de Git - ginac.git/blob - ginac/matrix.h
cef3dc36667b3c08d5b4c2c4a7525f5a6efcec0e
[ginac.git] / ginac / matrix.h
1 /** @file matrix.h
2  *
3  *  Interface to symbolic matrices */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 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 std {
31     // forward declaration, so <stdexcept> need not be included:
32     class range_error;
33 }
34
35 #ifndef NO_NAMESPACE_GINAC
36 namespace GiNaC {
37 #endif // ndef NO_NAMESPACE_GINAC
38
39 /** Symbolic matrices. */
40 class matrix : public basic
41 {
42     GINAC_DECLARE_REGISTERED_CLASS(matrix, basic)
43
44 // friends
45 // (none)
46 // member functions
47
48     // default constructor, destructor, copy constructor, assignment operator
49     // and helpers:
50 public:
51     matrix();
52     ~matrix();
53     matrix(const matrix & other);
54     const matrix & operator=(const matrix & other);
55 protected:
56     void copy(const matrix & other);
57     void destroy(bool call_parent);
58
59     // other constructors
60 public:
61     matrix(unsigned r, unsigned c);
62     matrix(unsigned r, unsigned c, const exvector & m2);
63    
64     // functions overriding virtual functions from bases classes
65 public:
66     basic * duplicate() const;
67     void print(std::ostream & os, unsigned upper_precedence=0) const;
68     void printraw(std::ostream & os) const;
69     unsigned nops() const;
70     ex op(int i) const;
71     ex & let_op(int i);
72     ex expand(unsigned options=0) const;
73     bool has(const ex & other) const;
74     ex eval(int level=0) const;
75     ex evalf(int level=0) const;
76     // ex subs(const lst & ls, const lst & lr) const;
77 protected:
78     int compare_same_type(const basic & other) const;
79     unsigned return_type(void) const { return return_types::noncommutative; };
80     // new virtual functions which can be overridden by derived classes
81     // (none)
82     
83     // non-virtual functions in this class
84 public:
85     unsigned rows(void) const        //! get number of rows.
86         { return row; }
87     unsigned cols(void) const        //! get number of columns.
88         { return col; }
89     matrix add(const matrix & other) const;
90     matrix sub(const matrix & other) const;
91     matrix mul(const matrix & other) const;
92     const ex & operator() (unsigned ro, unsigned co) const;
93     matrix & set(unsigned ro, unsigned co, ex value);
94     matrix transpose(void) const;
95     ex determinant(void) const;
96     ex trace(void) const;
97     ex charpoly(const symbol & lambda) const;
98     matrix inverse(void) const;
99     matrix fraction_free_elim(const matrix & vars, const matrix & v) const;
100     matrix solve(const matrix & vars, const matrix & rhs) const;
101     matrix old_solve(const matrix & v) const;  // FIXME: may be removed
102 protected:
103     ex determinant_minor(void) const;
104     int gauss_elimination(void);
105     int division_free_elimination(void);
106     int fraction_free_elimination(bool det = false);
107     int pivot(unsigned ro, bool symbolic=true);
108     void swap(unsigned r1, unsigned c1, unsigned r2 ,unsigned c2);
109     
110 // member variables
111 protected:
112     unsigned row;             /**< number of rows      */
113     unsigned col;             /**< number of columns   */
114     exvector m;               /**< representation (cols indexed first) */
115     static unsigned precedence;
116 };
117
118
119 // global constants
120 extern const matrix some_matrix;
121 extern const type_info & typeid_matrix;
122
123
124 // wrapper functions around member functions
125
126 inline unsigned nops(const matrix & m)
127 { return m.nops(); }
128
129 inline ex expand(const matrix & m, unsigned options=0)
130 { return m.expand(options); }
131
132 inline bool has(const matrix & m, const ex & other)
133 { return m.has(other); }
134
135 inline ex eval(const matrix & m, int level=0)
136 { return m.eval(level); }
137
138 inline ex evalf(const matrix & m, int level=0)
139 { return m.evalf(level); }
140
141 inline unsigned rows(const matrix & m)
142 { return m.rows(); }
143
144 inline unsigned cols(const matrix & m)
145 { return m.cols(); }
146
147 inline matrix transpose(const matrix & m)
148 { return m.transpose(); }
149
150 inline ex determinant(const matrix & m)
151 { return m.determinant(); }
152
153 inline ex trace(const matrix & m)
154 { return m.trace(); }
155
156 inline ex charpoly(const matrix & m, const symbol & lambda)
157 { return m.charpoly(lambda); }
158
159 inline matrix inverse(const matrix & m)
160 { return m.inverse(); }
161
162 // utility functions
163 inline const matrix &ex_to_matrix(const ex &e)
164 {
165         return static_cast<const matrix &>(*e.bp);
166 }
167
168 extern ex lst_to_matrix(const ex &l);
169
170 #ifndef NO_NAMESPACE_GINAC
171 } // namespace GiNaC
172 #endif // ndef NO_NAMESPACE_GINAC
173
174 #endif // ndef __GINAC_MATRIX_H__