]> www.ginac.de Git - ginac.git/blob - ginac/matrix.h
235fbda4905fdcb0f387a061d3b41ee07d6eb8fc
[ginac.git] / ginac / matrix.h
1 /** @file matrix.h
2  *
3  *  Interface to symbolic matrices */
4
5 /*
6  *  GiNaC Copyright (C) 1999 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 <ginac/basic.h>
28 #include <ginac/ex.h>
29
30 /** Symbolic matrices. */
31 class matrix : public basic
32 {
33 // friends
34     friend ex determinant_numeric(const matrix & m);
35     friend ex determinant_symbolic_perm(const matrix & m);
36     friend ex determinant_symbolic_minor(const matrix & m);
37 // member functions
38
39     // default constructor, destructor, copy constructor, assignment operator
40     // and helpers:
41 public:
42     matrix();
43     ~matrix();
44     matrix(matrix const & other);
45     matrix const & operator=(matrix const & other);
46 protected:
47     void copy(matrix const & other);
48     void destroy(bool call_parent);
49
50     // other constructors
51 public:
52     matrix(int r, int c);
53     matrix(int r, int c, vector<ex> const & m2);
54    
55     // functions overriding virtual functions from bases classes
56 public:
57     basic * duplicate() const;
58     void printraw(ostream & os) const;
59     void print(ostream & os, unsigned upper_precedence=0) const;
60     int nops() const;
61     ex & let_op(int const i);
62     ex expand(unsigned options=0) const;
63     bool has(ex const & other) const;
64     ex eval(int level=0) const;
65     ex evalf(int level=0) const;
66     // ex subs(lst const & ls, lst const & lr) const;
67 protected:
68     int compare_same_type(basic const & other) const;
69     unsigned return_type(void) const { return return_types::noncommutative; };
70     // new virtual functions which can be overridden by derived classes
71     // (none)
72     
73     // non-virtual functions in this class
74 public:
75     int rows() const            //! get number of rows.
76         { return row; }
77     int cols() const            //! get number of columns.
78         { return col; }
79     matrix add(matrix const & other) const;
80     matrix sub(matrix const & other) const;
81     matrix mul(matrix const & other) const;
82     ex const & operator() (int ro, int co) const;
83     matrix & set(int ro, int co, ex value);
84     matrix transpose(void) const;
85     ex determinant(bool normalized=true) const;
86     ex trace(void) const;
87     ex charpoly(ex const & lambda) const;
88     matrix inverse(void) const;
89     matrix fraction_free_elim(matrix const & vars, matrix const & v) const;
90     matrix solve(matrix const & v) const;
91 protected:
92     int pivot(int ro);
93     void ffe_swap(int r1, int c1, int r2 ,int c2);
94     void ffe_set(int r, int c, ex e);
95     ex ffe_get(int r, int c) const;
96     
97 // member variables
98 protected:
99     int row;                    /**< number of rows      */
100     int col;                    /**< number of columns   */
101     vector<ex> m;               /**< representation (cols indexed first) */
102     static unsigned precedence;
103 };
104
105 // global constants
106 extern const matrix some_matrix;
107 extern type_info const & typeid_matrix;
108
109 // wrapper functions around member functions
110
111 inline int nops(matrix const & m)
112 { return m.nops(); }
113
114 inline ex expand(matrix const & m, unsigned options=0)
115 { return m.expand(options); }
116
117 inline bool has(matrix const & m, ex const & other)
118 { return m.has(other); }
119
120 inline ex eval(matrix const & m, int level=0)
121 { return m.eval(level); }
122
123 inline ex evalf(matrix const & m, int level=0)
124 { return m.evalf(level); }
125
126 inline int rows(matrix const & m)
127 { return m.rows(); }
128
129 inline int cols(matrix const & m)
130 { return m.cols(); }
131
132 inline matrix transpose(matrix const & m)
133 { return m.transpose(); }
134
135 inline ex determinant(matrix const & m, bool normalized=true)
136 { return m.determinant(normalized); }
137
138 inline ex trace(matrix const & m)
139 { return m.trace(); }
140
141 inline ex charpoly(matrix const & m, ex const & lambda)
142 { return m.charpoly(lambda); }
143
144 inline matrix inverse(matrix const & m)
145 { return m.inverse(); }
146
147 // macros
148
149 #define ex_to_matrix(X) (static_cast<matrix const &>(*(X).bp))
150
151 #endif // ndef __GINAC_MATRIX_H__