GiNaC 1.8.7
matrix.h
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2023 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
34namespace GiNaC {
35
37class matrix : public basic
38{
40
41 // other constructors
42public:
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
47protected:
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
51public:
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;
69protected:
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
74public:
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;
99protected:
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
114protected:
115 unsigned row;
116 unsigned col;
118};
120
121// wrapper functions around member functions
122
123inline size_t nops(const matrix & m)
124{ return m.nops(); }
125
126inline ex expand(const matrix & m, unsigned options = 0)
127{ return m.expand(options); }
128
129inline ex evalf(const matrix & m)
130{ return m.evalf(); }
131
132inline unsigned rows(const matrix & m)
133{ return m.rows(); }
134
135inline unsigned cols(const matrix & m)
136{ return m.cols(); }
137
138inline matrix transpose(const matrix & m)
139{ return m.transpose(); }
140
142{ return m.determinant(options); }
143
144inline ex trace(const matrix & m)
145{ return m.trace(); }
146
147inline ex charpoly(const matrix & m, const ex & lambda)
148{ return m.charpoly(lambda); }
149
150inline matrix inverse(const matrix & m)
151{ return m.inverse(solve_algo::automatic); }
152inline matrix inverse(const matrix & m, unsigned algo)
153{ return m.inverse(algo); }
154
155inline unsigned rank(const matrix & m)
156{ return m.rank(); }
157inline unsigned rank(const matrix & m, unsigned solve_algo)
158{ return m.rank(solve_algo); }
159
160// utility functions
161
163extern ex lst_to_matrix(const lst & l);
164
166extern ex diag_matrix(const lst & l);
167extern ex diag_matrix(std::initializer_list<ex> l);
168
170extern ex unit_matrix(unsigned r, unsigned c);
171
173inline ex unit_matrix(unsigned x)
174{ return unit_matrix(x, x); }
175
179extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
180
183extern ex reduced_matrix(const matrix& m, unsigned r, unsigned c);
184
186extern ex sub_matrix(const matrix&m, unsigned r, unsigned nr, unsigned c, unsigned nc);
187
190inline 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
Archiving of GiNaC expressions.
Interface to GiNaC's ABC.
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition: archive.h:49
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition: basic.h:105
Wrapper template for making GiNaC classes out of STL containers.
Definition: container.h:73
@ automatic
Let the system choose.
Definition: flags.h:93
Lightweight wrapper for GiNaC's symbolic objects.
Definition: ex.h:72
Symbolic matrices.
Definition: matrix.h:38
matrix inverse() const
Inverse of this matrix, with automatic algorithm selection.
Definition: matrix.cpp:939
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
ex scalar_mul_indexed(const ex &self, const numeric &other) const override
Product of an indexed matrix with a number.
Definition: matrix.cpp:433
ex eval_indexed(const basic &i) const override
Automatic symbolic evaluation of an indexed matrix.
Definition: matrix.cpp:320
unsigned cols() const
Get number of columns.
Definition: matrix.h:77
ex charpoly(const ex &lambda) const
Characteristic Polynomial.
Definition: matrix.cpp:894
void do_print_latex(const print_latex &c, unsigned level) const
Definition: matrix.cpp:191
exvector m
representation (cols indexed first)
Definition: matrix.h:117
ex determinant(unsigned algo=determinant_algo::automatic) const
Determinant of square matrix.
Definition: matrix.cpp:737
const ex & operator()(unsigned ro, unsigned co) const
operator() to access elements for reading.
Definition: matrix.cpp:686
void archive(archive_node &n) const override
Save (a.k.a.
Definition: matrix.cpp:152
ex add_indexed(const ex &self, const ex &other) const override
Sum of two indexed matrices.
Definition: matrix.cpp:397
bool is_zero_matrix() const
Function to check that all elements of the matrix are zero.
Definition: matrix.cpp:1676
ex trace() const
Trace of a matrix.
Definition: matrix.cpp:866
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
matrix & set(unsigned ro, unsigned co, const ex &value)
Definition: matrix.h:87
matrix add(const matrix &other) const
Sum of matrices.
Definition: matrix.cpp:555
matrix pow(const ex &expn) const
Power of a matrix.
Definition: matrix.cpp:639
ex subs(const exmap &m, unsigned options=0) const override
Substitute a set of objects by arbitrary expressions.
Definition: matrix.cpp:228
matrix(unsigned r, unsigned c)
Very common ctor.
Definition: matrix.cpp:71
std::vector< unsigned > markowitz_elimination(unsigned n)
Definition: matrix.cpp:1326
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
void do_print_python_repr(const print_python_repr &c, unsigned level) const
Definition: matrix.cpp:198
ex imag_part() const override
Definition: matrix.cpp:273
void read_archive(const archive_node &n, lst &syms) override
Read (a.k.a.
Definition: matrix.cpp:134
matrix mul_scalar(const ex &other) const
Product of matrix and scalar expression.
Definition: matrix.cpp:623
void do_print(const print_context &c, unsigned level) const
Definition: matrix.cpp:184
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
size_t nops() const override
nops is defined to be rows x columns.
Definition: matrix.cpp:206
ex real_part() const override
Definition: matrix.cpp:264
unsigned rank() const
Compute the rank of this matrix.
Definition: matrix.cpp:1058
ex determinant_minor() const
Recursive determinant for small matrices having at least one symbolic entry.
Definition: matrix.cpp:1096
matrix mul(const matrix &other) const
Product of matrices.
Definition: matrix.cpp:589
ex evalm() const override
Evaluate sums, products and integer powers of matrices.
Definition: matrix.h:55
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
unsigned rows() const
Get number of rows.
Definition: matrix.h:75
ex op(size_t i) const override
returns matrix entry at position (i/col, icol).
Definition: matrix.cpp:212
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
unsigned col
number of columns
Definition: matrix.h:116
ex & let_op(size_t i) override
returns writable matrix entry at position (i/col, icol).
Definition: matrix.cpp:220
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
matrix sub(const matrix &other) const
Difference of matrices.
Definition: matrix.cpp:572
std::vector< unsigned > echelon_form(unsigned algo, int n)
Definition: matrix.cpp:1197
unsigned return_type() const override
Definition: matrix.h:71
ex conjugate() const override
Complex conjugate every matrix entry.
Definition: matrix.cpp:239
int pivot(unsigned ro, unsigned co, bool symbolic=true)
Partial pivoting method for matrix elimination schemes.
Definition: matrix.cpp:1636
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
unsigned row
number of rows
Definition: matrix.h:115
This class is a wrapper around CLN-numbers within the GiNaC class hierarchy.
Definition: numeric.h:82
Base class for print_contexts.
Definition: print.h:103
Context for latex-parsable output.
Definition: print.h:123
Context for python-parsable output.
Definition: print.h:139
Switch to control algorithm for linear system solving.
Definition: flags.h:141
@ automatic
Let the system choose.
Definition: flags.h:146
Definition of optimizing macros.
Interface to GiNaC's light-weight expression handles.
static const bool value
Definition: factor.cpp:199
unsigned options
Definition: factor.cpp:2475
size_t n
Definition: factor.cpp:1432
size_t c
Definition: factor.cpp:757
ex x
Definition: factor.cpp:1610
size_t r
Definition: factor.cpp:757
exset syms
Definition: factor.cpp:2429
mvec m
Definition: factor.cpp:758
Definition: add.cpp:38
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
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
container< std::list > lst
Definition: lst.h:32
unsigned rank(const matrix &m)
Definition: matrix.h:155
std::map< ex, ex, ex_is_less > exmap
Definition: basic.h:50
matrix inverse(const matrix &m)
Definition: matrix.h:150
ex rhs(const ex &thisex)
Definition: ex.h:832
GINAC_DECLARE_UNARCHIVER(add)
ex diag_matrix(const lst &l)
Convert list of diagonal elements to matrix.
Definition: matrix.cpp:1711
matrix transpose(const matrix &m)
Definition: matrix.h:138
unsigned rows(const matrix &m)
Definition: matrix.h:132
ex charpoly(const matrix &m, const ex &lambda)
Definition: matrix.h:147
ex evalf(const ex &thisex)
Definition: ex.h:784
ex trace(const matrix &m)
Definition: matrix.h:144
ex determinant(const matrix &m, unsigned options=determinant_algo::automatic)
Definition: matrix.h:141
unsigned cols(const matrix &m)
Definition: matrix.h:135
ex lst_to_matrix(const lst &l)
Convert list of lists to matrix.
Definition: matrix.cpp:1684
std::vector< ex > exvector
Definition: basic.h:48
size_t nops(const ex &thisex)
Definition: ex.h:727
ex unit_matrix(unsigned r, unsigned c)
Create an r times c unit matrix.
Definition: matrix.cpp:1743
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
ex expand(const ex &thisex, unsigned options=0)
Definition: ex.h:730
#define GINAC_DECLARE_REGISTERED_CLASS(classname, supername)
Macro for inclusion in the declaration of each registered class.
Definition: registrar.h:153

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