3 * Interface to symbolic matrices */
6 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
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.
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.
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
23 #ifndef __GINAC_MATRIX_H__
24 #define __GINAC_MATRIX_H__
32 /** Symbolic matrices. */
33 class matrix : public basic
35 GINAC_DECLARE_REGISTERED_CLASS(matrix, basic)
39 matrix(unsigned r, unsigned c);
40 matrix(unsigned r, unsigned c, const exvector & m2);
41 matrix(unsigned r, unsigned c, const lst & l);
43 // functions overriding virtual functions from bases classes
45 void print(const print_context & c, unsigned level = 0) const;
46 unsigned nops() const;
49 ex eval(int level=0) const;
50 ex evalm(void) const {return *this;}
51 ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
52 ex eval_indexed(const basic & i) const;
53 ex add_indexed(const ex & self, const ex & other) const;
54 ex scalar_mul_indexed(const ex & self, const numeric & other) const;
55 bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
58 bool match_same_type(const basic & other) const;
59 unsigned return_type(void) const { return return_types::noncommutative; };
61 // non-virtual functions in this class
63 unsigned rows(void) const /// Get number of rows.
65 unsigned cols(void) const /// Get number of columns.
67 matrix add(const matrix & other) const;
68 matrix sub(const matrix & other) const;
69 matrix mul(const matrix & other) const;
70 matrix mul(const numeric & other) const;
71 matrix mul_scalar(const ex & other) const;
72 matrix pow(const ex & expn) const;
73 const ex & operator() (unsigned ro, unsigned co) const;
74 ex & operator() (unsigned ro, unsigned co);
75 matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
76 matrix transpose(void) const;
77 ex determinant(unsigned algo = determinant_algo::automatic) const;
79 ex charpoly(const symbol & lambda) const;
80 matrix inverse(void) const;
81 matrix solve(const matrix & vars, const matrix & rhs,
82 unsigned algo = solve_algo::automatic) const;
84 ex determinant_minor(void) const;
85 int gauss_elimination(const bool det = false);
86 int division_free_elimination(const bool det = false);
87 int fraction_free_elimination(const bool det = false);
88 int pivot(unsigned ro, unsigned co, bool symbolic = true);
92 unsigned row; ///< number of rows
93 unsigned col; ///< number of columns
94 exvector m; ///< representation (cols indexed first)
98 // wrapper functions around member functions
100 inline unsigned nops(const matrix & m)
103 inline ex expand(const matrix & m, unsigned options = 0)
104 { return m.expand(options); }
106 inline ex eval(const matrix & m, int level = 0)
107 { return m.eval(level); }
109 inline ex evalf(const matrix & m, int level = 0)
110 { return m.evalf(level); }
112 inline unsigned rows(const matrix & m)
115 inline unsigned cols(const matrix & m)
118 inline matrix transpose(const matrix & m)
119 { return m.transpose(); }
121 inline ex determinant(const matrix & m, unsigned options = determinant_algo::automatic)
122 { return m.determinant(options); }
124 inline ex trace(const matrix & m)
125 { return m.trace(); }
127 inline ex charpoly(const matrix & m, const symbol & lambda)
128 { return m.charpoly(lambda); }
130 inline matrix inverse(const matrix & m)
131 { return m.inverse(); }
135 /** Return the matrix object handled by an ex. Deprecated: use ex_to<matrix>().
136 * This is unsafe: you need to check the type first. */
137 inline const matrix &ex_to_matrix(const ex &e)
139 return static_cast<const matrix &>(*e.bp);
142 /** Specialization of is_exactly_a<matrix>(obj) for matrix objects. */
143 template<> inline bool is_exactly_a<matrix>(const basic & obj)
145 return obj.tinfo()==TINFO_matrix;
148 /** Convert list of lists to matrix. */
149 extern ex lst_to_matrix(const lst & l);
151 /** Convert list of diagonal elements to matrix. */
152 extern ex diag_matrix(const lst & l);
156 #endif // ndef __GINAC_MATRIX_H__