GiNaC  1.8.0
matrix.h
Go to the documentation of this file.
1 
5 /*
6  * GiNaC Copyright (C) 1999-2020 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifndef GINAC_MATRIX_H
24 #define GINAC_MATRIX_H
25 
26 #include "basic.h"
27 #include "ex.h"
28 #include "archive.h"
29 #include "compiler.h"
30 
31 #include <string>
32 #include <vector>
33 
34 namespace GiNaC {
35 
37 class matrix : public basic
38 {
40 
41  // other constructors
42 public:
43  matrix(unsigned r, unsigned c);
44  matrix(unsigned r, unsigned c, const lst & l);
45  matrix(std::initializer_list<std::initializer_list<ex>> l);
46 
47 protected:
48  matrix(unsigned r, unsigned c, const exvector & m2);
49  matrix(unsigned r, unsigned c, exvector && m2);
50  // functions overriding virtual functions from base classes
51 public:
52  size_t nops() const override;
53  ex op(size_t i) const override;
54  ex & let_op(size_t i) override;
55  ex evalm() const override {return *this;}
56  ex subs(const exmap & m, unsigned options = 0) const override;
57  ex eval_indexed(const basic & i) const override;
58  ex add_indexed(const ex & self, const ex & other) const override;
59  ex scalar_mul_indexed(const ex & self, const numeric & other) const override;
60  bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const override;
61  ex conjugate() const override;
62  ex real_part() const override;
63  ex imag_part() const override;
64 
66  void archive(archive_node& n) const override;
68  void read_archive(const archive_node& n, lst& syms) override;
69 protected:
70  bool match_same_type(const basic & other) const override;
71  unsigned return_type() const override { return return_types::noncommutative; };
72 
73  // non-virtual functions in this class
74 public:
75  unsigned rows() const
76  { return row; }
77  unsigned cols() const
78  { return col; }
79  matrix add(const matrix & other) const;
80  matrix sub(const matrix & other) const;
81  matrix mul(const matrix & other) const;
82  matrix mul(const numeric & other) const;
83  matrix mul_scalar(const ex & other) const;
84  matrix pow(const ex & expn) const;
85  const ex & operator() (unsigned ro, unsigned co) const;
86  ex & operator() (unsigned ro, unsigned co);
87  matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
88  matrix transpose() const;
89  ex determinant(unsigned algo = determinant_algo::automatic) const;
90  ex trace() const;
91  ex charpoly(const ex & lambda) const;
92  matrix inverse() const;
93  matrix inverse(unsigned algo) const;
94  matrix solve(const matrix & vars, const matrix & rhs,
95  unsigned algo = solve_algo::automatic) const;
96  unsigned rank() const;
97  unsigned rank(unsigned solve_algo) const;
98  bool is_zero_matrix() const;
99 protected:
100  ex determinant_minor() const;
101  std::vector<unsigned> echelon_form(unsigned algo, int n);
102  int gauss_elimination(const bool det = false);
103  int division_free_elimination(const bool det = false);
104  int fraction_free_elimination(const bool det = false);
105  std::vector<unsigned> markowitz_elimination(unsigned n);
106  int pivot(unsigned ro, unsigned co, bool symbolic = true);
107 
108  void print_elements(const print_context & c, const char *row_start, const char *row_end, const char *row_sep, const char *col_sep) const;
109  void do_print(const print_context & c, unsigned level) const;
110  void do_print_latex(const print_latex & c, unsigned level) const;
111  void do_print_python_repr(const print_python_repr & c, unsigned level) const;
112 
113 // member variables
114 protected:
115  unsigned row;
116  unsigned col;
118 };
120 
121 // wrapper functions around member functions
122 
123 inline size_t nops(const matrix & m)
124 { return m.nops(); }
125 
126 inline ex expand(const matrix & m, unsigned options = 0)
127 { return m.expand(options); }
128 
129 inline ex evalf(const matrix & m)
130 { return m.evalf(); }
131 
132 inline unsigned rows(const matrix & m)
133 { return m.rows(); }
134 
135 inline unsigned cols(const matrix & m)
136 { return m.cols(); }
137 
138 inline matrix transpose(const matrix & m)
139 { return m.transpose(); }
140 
142 { return m.determinant(options); }
143 
144 inline ex trace(const matrix & m)
145 { return m.trace(); }
146 
147 inline ex charpoly(const matrix & m, const ex & lambda)
148 { return m.charpoly(lambda); }
149 
150 inline matrix inverse(const matrix & m)
151 { return m.inverse(solve_algo::automatic); }
152 inline matrix inverse(const matrix & m, unsigned algo)
153 { return m.inverse(algo); }
154 
155 inline unsigned rank(const matrix & m)
156 { return m.rank(); }
157 inline unsigned rank(const matrix & m, unsigned solve_algo)
158 { return m.rank(solve_algo); }
159 
160 // utility functions
161 
163 extern ex lst_to_matrix(const lst & l);
164 
166 extern ex diag_matrix(const lst & l);
167 extern ex diag_matrix(std::initializer_list<ex> l);
168 
170 extern ex unit_matrix(unsigned r, unsigned c);
171 
173 inline ex unit_matrix(unsigned x)
174 { return unit_matrix(x, x); }
175 
179 extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
180 
183 extern ex reduced_matrix(const matrix& m, unsigned r, unsigned c);
184 
186 extern ex sub_matrix(const matrix&m, unsigned r, unsigned nr, unsigned c, unsigned nc);
187 
190 inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
191 { return symbolic_matrix(r, c, base_name, base_name); }
192 
193 } // namespace GiNaC
194 
195 #endif // ndef GINAC_MATRIX_H
GiNaC::charpoly
ex charpoly(const matrix &m, const ex &lambda)
Definition: matrix.h:147
GiNaC::print_python_repr
Context for python-parsable output.
Definition: print.h:139
GiNaC::trace
ex trace(const matrix &m)
Definition: matrix.h:144
GiNaC::rhs
ex rhs(const ex &thisex)
Definition: ex.h:817
GiNaC::lst
container< std::list > lst
Definition: lst.h:32
GiNaC::matrix::col
unsigned col
number of columns
Definition: matrix.h:116
r
size_t r
Definition: factor.cpp:770
GiNaC::determinant_algo::automatic
@ automatic
Let the system choose.
Definition: flags.h:93
GiNaC::matrix::division_free_elimination
int division_free_elimination(const bool det=false)
Perform the steps of division free elimination to bring the m x n matrix into an upper echelon form.
Definition: matrix.cpp:1441
GiNaC::matrix::nops
size_t nops() const override
nops is defined to be rows x columns.
Definition: matrix.cpp:206
GiNaC::reduced_matrix
ex reduced_matrix(const matrix &m, unsigned r, unsigned c)
Return the reduced matrix that is formed by deleting the rth row and cth column of matrix m.
Definition: matrix.cpp:1790
GiNaC::matrix::determinant
ex determinant(unsigned algo=determinant_algo::automatic) const
Determinant of square matrix.
Definition: matrix.cpp:737
GiNaC::print_context
Base class for print_contexts.
Definition: print.h:103
GiNaC::matrix::mul
matrix mul(const matrix &other) const
Product of matrices.
Definition: matrix.cpp:589
GiNaC::matrix::do_print
void do_print(const print_context &c, unsigned level) const
Definition: matrix.cpp:184
GiNaC::matrix::evalm
ex evalm() const override
Evaluate sums, products and integer powers of matrices.
Definition: matrix.h:55
GiNaC::matrix::read_archive
void read_archive(const archive_node &n, lst &syms) override
Read (a.k.a.
Definition: matrix.cpp:134
GiNaC::evalf
ex evalf(const ex &thisex)
Definition: ex.h:769
GiNaC::exvector
std::vector< ex > exvector
Definition: basic.h:46
GiNaC::rows
unsigned rows(const matrix &m)
Definition: matrix.h:132
GiNaC::matrix::do_print_python_repr
void do_print_python_repr(const print_python_repr &c, unsigned level) const
Definition: matrix.cpp:198
GiNaC::archive_node
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition: archive.h:49
GiNaC::matrix::matrix
matrix(unsigned r, unsigned c)
Very common ctor.
Definition: matrix.cpp:71
GiNaC::diag_matrix
ex diag_matrix(const lst &l)
Convert list of diagonal elements to matrix.
Definition: matrix.cpp:1711
GiNaC::matrix::pivot
int pivot(unsigned ro, unsigned co, bool symbolic=true)
Partial pivoting method for matrix elimination schemes.
Definition: matrix.cpp:1636
options
unsigned options
Definition: factor.cpp:2480
GiNaC::GINAC_DECLARE_UNARCHIVER
GINAC_DECLARE_UNARCHIVER(add)
GiNaC::matrix::trace
ex trace() const
Trace of a matrix.
Definition: matrix.cpp:866
m
mvec m
Definition: factor.cpp:771
GiNaC::matrix::solve
matrix solve(const matrix &vars, const matrix &rhs, unsigned algo=solve_algo::automatic) const
Solve a linear system consisting of a m x n matrix and a m x p right hand side by applying an elimina...
Definition: matrix.cpp:995
GiNaC::matrix::cols
unsigned cols() const
Get number of columns.
Definition: matrix.h:77
GiNaC
Definition: add.cpp:38
syms
exset syms
Definition: factor.cpp:2434
GiNaC::matrix::add_indexed
ex add_indexed(const ex &self, const ex &other) const override
Sum of two indexed matrices.
Definition: matrix.cpp:397
GiNaC::matrix::print_elements
void print_elements(const print_context &c, const char *row_start, const char *row_end, const char *row_sep, const char *col_sep) const
Definition: matrix.cpp:168
GiNaC::return_types::noncommutative
@ noncommutative
Definition: flags.h:281
x
ex x
Definition: factor.cpp:1641
GiNaC::matrix::scalar_mul_indexed
ex scalar_mul_indexed(const ex &self, const numeric &other) const override
Product of an indexed matrix with a number.
Definition: matrix.cpp:433
GINAC_DECLARE_REGISTERED_CLASS
#define GINAC_DECLARE_REGISTERED_CLASS(classname, supername)
Macro for inclusion in the declaration of each registered class.
Definition: registrar.h:153
GiNaC::matrix::do_print_latex
void do_print_latex(const print_latex &c, unsigned level) const
Definition: matrix.cpp:191
GiNaC::determinant
ex determinant(const matrix &m, unsigned options=determinant_algo::automatic)
Definition: matrix.h:141
GiNaC::matrix::charpoly
ex charpoly(const ex &lambda) const
Characteristic Polynomial.
Definition: matrix.cpp:894
GiNaC::ex
Lightweight wrapper for GiNaC's symbolic objects.
Definition: ex.h:72
GiNaC::matrix::determinant_minor
ex determinant_minor() const
Recursive determinant for small matrices having at least one symbolic entry.
Definition: matrix.cpp:1096
GiNaC::matrix::markowitz_elimination
std::vector< unsigned > markowitz_elimination(unsigned n)
Definition: matrix.cpp:1326
GiNaC::matrix::gauss_elimination
int gauss_elimination(const bool det=false)
Perform the steps of an ordinary Gaussian elimination to bring the m x n matrix into an upper echelon...
Definition: matrix.cpp:1269
GiNaC::matrix::echelon_form
std::vector< unsigned > echelon_form(unsigned algo, int n)
Definition: matrix.cpp:1197
GiNaC::solve_algo::automatic
@ automatic
Let the system choose.
Definition: flags.h:146
GiNaC::matrix::fraction_free_elimination
int fraction_free_elimination(const bool det=false)
Perform the steps of Bareiss' one-step fraction free elimination to bring the matrix into an upper ec...
Definition: matrix.cpp:1495
value
static const bool value
Definition: factor.cpp:231
GiNaC::matrix::inverse
matrix inverse() const
Inverse of this matrix, with automatic algorithm selection.
Definition: matrix.cpp:939
GiNaC::sub_matrix
ex sub_matrix(const matrix &m, unsigned r, unsigned nr, unsigned c, unsigned nc)
Return the nr times nc submatrix starting at position r, c of matrix m.
Definition: matrix.cpp:1821
GiNaC::matrix::transpose
matrix transpose() const
Transposed of an m x n matrix, producing a new n x m matrix object that represents the transposed.
Definition: matrix.cpp:712
GiNaC::expand
ex expand(const ex &thisex, unsigned options=0)
Definition: ex.h:715
ex.h
Interface to GiNaC's light-weight expression handles.
GiNaC::matrix::is_zero_matrix
bool is_zero_matrix() const
Function to check that all elements of the matrix are zero.
Definition: matrix.cpp:1676
GiNaC::print_latex
Context for latex-parsable output.
Definition: print.h:123
compiler.h
Definition of optimizing macros.
GiNaC::matrix::set
matrix & set(unsigned ro, unsigned co, const ex &value)
Definition: matrix.h:87
GiNaC::matrix::eval_indexed
ex eval_indexed(const basic &i) const override
Automatic symbolic evaluation of an indexed matrix.
Definition: matrix.cpp:320
GiNaC::inverse
matrix inverse(const matrix &m)
Definition: matrix.h:150
GiNaC::symbolic_matrix
ex symbolic_matrix(unsigned r, unsigned c, const std::string &base_name, const std::string &tex_base_name)
Create an r times c matrix of newly generated symbols consisting of the given base name plus the nume...
Definition: matrix.cpp:1753
GiNaC::matrix::pow
matrix pow(const ex &expn) const
Power of a matrix.
Definition: matrix.cpp:639
GiNaC::matrix::real_part
ex real_part() const override
Definition: matrix.cpp:264
GiNaC::rank
unsigned rank(const matrix &m)
Definition: matrix.h:155
GiNaC::matrix::operator()
const ex & operator()(unsigned ro, unsigned co) const
operator() to access elements for reading.
Definition: matrix.cpp:686
GiNaC::container
Wrapper template for making GiNaC classes out of STL containers.
Definition: container.h:73
GiNaC::cols
unsigned cols(const matrix &m)
Definition: matrix.h:135
GiNaC::matrix::contract_with
bool contract_with(exvector::iterator self, exvector::iterator other, exvector &v) const override
Contraction of an indexed matrix with something else.
Definition: matrix.cpp:448
GiNaC::matrix::rank
unsigned rank() const
Compute the rank of this matrix.
Definition: matrix.cpp:1058
c
size_t c
Definition: factor.cpp:770
GiNaC::exmap
std::map< ex, ex, ex_is_less > exmap
Definition: basic.h:50
GiNaC::matrix::imag_part
ex imag_part() const override
Definition: matrix.cpp:273
archive.h
Archiving of GiNaC expressions.
basic.h
Interface to GiNaC's ABC.
GiNaC::matrix::archive
void archive(archive_node &n) const override
Save (a.k.a.
Definition: matrix.cpp:152
n
size_t n
Definition: factor.cpp:1463
GiNaC::matrix::m
exvector m
representation (cols indexed first)
Definition: matrix.h:117
GiNaC::lst_to_matrix
ex lst_to_matrix(const lst &l)
Convert list of lists to matrix.
Definition: matrix.cpp:1684
GiNaC::matrix::match_same_type
bool match_same_type(const basic &other) const override
Returns true if the attributes of two objects are similar enough for a match.
Definition: matrix.cpp:309
GiNaC::matrix
Symbolic matrices.
Definition: matrix.h:38
GiNaC::matrix::sub
matrix sub(const matrix &other) const
Difference of matrices.
Definition: matrix.cpp:572
GiNaC::basic
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition: basic.h:105
GiNaC::matrix::conjugate
ex conjugate() const override
Complex conjugate every matrix entry.
Definition: matrix.cpp:239
GiNaC::matrix::rows
unsigned rows() const
Get number of rows.
Definition: matrix.h:75
GiNaC::matrix::row
unsigned row
number of rows
Definition: matrix.h:115
GiNaC::unit_matrix
ex unit_matrix(unsigned r, unsigned c)
Create an r times c unit matrix.
Definition: matrix.cpp:1743
GiNaC::matrix::subs
ex subs(const exmap &m, unsigned options=0) const override
Substitute a set of objects by arbitrary expressions.
Definition: matrix.cpp:228
GiNaC::nops
size_t nops(const ex &thisex)
Definition: ex.h:712
GiNaC::matrix::op
ex op(size_t i) const override
returns matrix entry at position (i/col, icol).
Definition: matrix.cpp:212
GiNaC::matrix::let_op
ex & let_op(size_t i) override
returns writable matrix entry at position (i/col, icol).
Definition: matrix.cpp:220
GiNaC::matrix::return_type
unsigned return_type() const override
Definition: matrix.h:71
GiNaC::numeric
This class is a wrapper around CLN-numbers within the GiNaC class hierarchy.
Definition: numeric.h:82
GiNaC::transpose
matrix transpose(const matrix &m)
Definition: matrix.h:138
GiNaC::matrix::add
matrix add(const matrix &other) const
Sum of matrices.
Definition: matrix.cpp:555
GiNaC::solve_algo
Switch to control algorithm for linear system solving.
Definition: flags.h:141
GiNaC::matrix::mul_scalar
matrix mul_scalar(const ex &other) const
Product of matrix and scalar expression.
Definition: matrix.cpp:623

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.