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