GiNaC 1.8.10
matrix.h
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2026 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef GINAC_MATRIX_H
23#define GINAC_MATRIX_H
24
25#include "basic.h"
26#include "ex.h"
27#include "archive.h"
28#include "compiler.h"
29
30#include <string>
31#include <vector>
32
33namespace GiNaC {
34
36class matrix : public basic
37{
39
40 // other constructors
41public:
42 matrix(unsigned r, unsigned c);
43 matrix(unsigned r, unsigned c, const lst & l);
44 matrix(std::initializer_list<std::initializer_list<ex>> l);
45
46protected:
47 matrix(unsigned r, unsigned c, const exvector & m2);
48 matrix(unsigned r, unsigned c, exvector && m2);
49 // functions overriding virtual functions from base classes
50public:
51 size_t nops() const override;
52 ex op(size_t i) const override;
53 ex & let_op(size_t i) override;
54 ex evalm() const override {return *this;}
55 ex subs(const exmap & m, unsigned options = 0) const override;
56 ex eval_indexed(const basic & i) const override;
57 ex add_indexed(const ex & self, const ex & other) const override;
58 ex scalar_mul_indexed(const ex & self, const numeric & other) const override;
59 bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const override;
60 ex conjugate() const override;
61 ex real_part() const override;
62 ex imag_part() const override;
63
65 void archive(archive_node& n) const override;
67 void read_archive(const archive_node& n, lst& syms) override;
68protected:
69 bool match_same_type(const basic & other) const override;
70 unsigned return_type() const override { return return_types::noncommutative; };
71
72 // non-virtual functions in this class
73public:
74 unsigned rows() const
75 { return row; }
76 unsigned cols() const
77 { return col; }
78 matrix add(const matrix & other) const;
79 matrix sub(const matrix & other) const;
80 matrix mul(const matrix & other) const;
81 matrix mul(const numeric & other) const;
82 matrix mul_scalar(const ex & other) const;
83 matrix pow(const ex & expn) const;
84 const ex & operator() (unsigned ro, unsigned co) const;
85 ex & operator() (unsigned ro, unsigned co);
86 matrix & set(unsigned ro, unsigned co, const ex & value) { (*this)(ro, co) = value; return *this; }
87 matrix transpose() const;
88 ex determinant(unsigned algo = determinant_algo::automatic) const;
89 ex trace() const;
90 ex charpoly(const ex & lambda) const;
91 matrix inverse() const;
92 matrix inverse(unsigned algo) const;
93 matrix solve(const matrix & vars, const matrix & rhs,
94 unsigned algo = solve_algo::automatic) const;
95 unsigned rank() const;
96 unsigned rank(unsigned solve_algo) const;
97 bool is_zero_matrix() const;
98protected:
99 ex determinant_minor() const;
100 std::vector<unsigned> echelon_form(unsigned algo, int n);
101 int gauss_elimination(const bool det = false);
102 int division_free_elimination(const bool det = false);
103 int fraction_free_elimination(const bool det = false);
104 std::vector<unsigned> markowitz_elimination(unsigned n);
105 int pivot(unsigned ro, unsigned co, bool symbolic = true);
106
107 void print_elements(const print_context & c, const char *row_start, const char *row_end, const char *row_sep, const char *col_sep) const;
108 void do_print(const print_context & c, unsigned level) const;
109 void do_print_latex(const print_latex & c, unsigned level) const;
110 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
111
112// member variables
113protected:
114 unsigned row;
115 unsigned col;
117};
119
120// wrapper functions around member functions
121
122inline size_t nops(const matrix & m)
123{ return m.nops(); }
124
125inline ex expand(const matrix & m, unsigned options = 0)
126{ return m.expand(options); }
127
128inline ex evalf(const matrix & m)
129{ return m.evalf(); }
130
131inline unsigned rows(const matrix & m)
132{ return m.rows(); }
133
134inline unsigned cols(const matrix & m)
135{ return m.cols(); }
136
137inline matrix transpose(const matrix & m)
138{ return m.transpose(); }
139
141{ return m.determinant(options); }
142
143inline ex trace(const matrix & m)
144{ return m.trace(); }
145
146inline ex charpoly(const matrix & m, const ex & lambda)
147{ return m.charpoly(lambda); }
148
149inline matrix inverse(const matrix & m)
151inline matrix inverse(const matrix & m, unsigned algo)
152{ return m.inverse(algo); }
153
154inline unsigned rank(const matrix & m)
155{ return m.rank(); }
156inline unsigned rank(const matrix & m, unsigned solve_algo)
157{ return m.rank(solve_algo); }
158
159// utility functions
160
162extern ex lst_to_matrix(const lst & l);
163
165extern ex diag_matrix(const lst & l);
166extern ex diag_matrix(std::initializer_list<ex> l);
167
169extern ex unit_matrix(unsigned r, unsigned c);
170
172inline ex unit_matrix(unsigned x)
173{ return unit_matrix(x, x); }
174
178extern ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name);
179
182extern ex reduced_matrix(const matrix& m, unsigned r, unsigned c);
183
185extern ex sub_matrix(const matrix&m, unsigned r, unsigned nr, unsigned c, unsigned nc);
186
189inline ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name)
190{ return symbolic_matrix(r, c, base_name, base_name); }
191
192} // namespace GiNaC
193
194#endif // ndef GINAC_MATRIX_H
Archiving of GiNaC expressions.
#define GINAC_DECLARE_UNARCHIVER(classname)
Helper macros to register a class with (un)archiving (a.k.a.
Definition archive.h:218
Interface to GiNaC's ABC.
Sum of expressions.
Definition add.h:31
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition archive.h:48
This class holds archived versions of GiNaC expressions (class ex).
Definition archive.h:254
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition basic.h:104
Wrapper template for making GiNaC classes out of STL containers.
Definition container.h:72
@ automatic
Let the system choose.
Definition flags.h:92
Lightweight wrapper for GiNaC's symbolic objects.
Definition ex.h:72
ex expand(unsigned options=0) const
Expand an expression.
Definition ex.cpp:73
ex evalf() const
Definition ex.h:121
Symbolic matrices.
Definition matrix.h:37
matrix inverse() const
Inverse of this matrix, with automatic algorithm selection.
Definition matrix.cpp:936
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:1266
ex scalar_mul_indexed(const ex &self, const numeric &other) const override
Product of an indexed matrix with a number.
Definition matrix.cpp:430
ex eval_indexed(const basic &i) const override
Automatic symbolic evaluation of an indexed matrix.
Definition matrix.cpp:317
unsigned cols() const
Get number of columns.
Definition matrix.h:76
ex charpoly(const ex &lambda) const
Characteristic Polynomial.
Definition matrix.cpp:891
void do_print_latex(const print_latex &c, unsigned level) const
Definition matrix.cpp:188
exvector m
representation (cols indexed first)
Definition matrix.h:116
ex determinant(unsigned algo=determinant_algo::automatic) const
Determinant of square matrix.
Definition matrix.cpp:734
const ex & operator()(unsigned ro, unsigned co) const
operator() to access elements for reading.
Definition matrix.cpp:683
ex add_indexed(const ex &self, const ex &other) const override
Sum of two indexed matrices.
Definition matrix.cpp:394
bool is_zero_matrix() const
Function to check that all elements of the matrix are zero.
Definition matrix.cpp:1671
ex trace() const
Trace of a matrix.
Definition matrix.cpp:863
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:306
matrix & set(unsigned ro, unsigned co, const ex &value)
Definition matrix.h:86
matrix pow(const ex &expn) const
Power of a matrix.
Definition matrix.cpp:636
ex subs(const exmap &m, unsigned options=0) const override
Substitute a set of objects by arbitrary expressions.
Definition matrix.cpp:225
std::vector< unsigned > markowitz_elimination(unsigned n)
Definition matrix.cpp:1321
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:992
void do_print_python_repr(const print_python_repr &c, unsigned level) const
Definition matrix.cpp:195
ex imag_part() const override
Definition matrix.cpp:270
void read_archive(const archive_node &n, lst &syms) override
Read (a.k.a.
Definition matrix.cpp:131
matrix mul_scalar(const ex &other) const
Product of matrix and scalar expression.
Definition matrix.cpp:620
void do_print(const print_context &c, unsigned level) const
Definition matrix.cpp:181
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:165
size_t nops() const override
nops is defined to be rows x columns.
Definition matrix.cpp:203
ex real_part() const override
Definition matrix.cpp:261
unsigned rank() const
Compute the rank of this matrix.
Definition matrix.cpp:1055
ex determinant_minor() const
Recursive determinant for small matrices having at least one symbolic entry.
Definition matrix.cpp:1093
ex evalm() const override
Evaluate sums, products and integer powers of matrices.
Definition matrix.h:54
bool contract_with(exvector::iterator self, exvector::iterator other, exvector &v) const override
Contraction of an indexed matrix with something else.
Definition matrix.cpp:445
unsigned rows() const
Get number of rows.
Definition matrix.h:74
ex op(size_t i) const override
returns matrix entry at position (i/col, icol).
Definition matrix.cpp:209
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:1436
unsigned col
number of columns
Definition matrix.h:115
ex & let_op(size_t i) override
returns writable matrix entry at position (i/col, icol).
Definition matrix.cpp:217
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:709
matrix sub(const matrix &other) const
Difference of matrices.
Definition matrix.cpp:569
std::vector< unsigned > echelon_form(unsigned algo, int n)
Definition matrix.cpp:1194
unsigned return_type() const override
Definition matrix.h:70
ex conjugate() const override
Complex conjugate every matrix entry.
Definition matrix.cpp:236
int pivot(unsigned ro, unsigned co, bool symbolic=true)
Partial pivoting method for matrix elimination schemes.
Definition matrix.cpp:1631
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:1490
unsigned row
number of rows
Definition matrix.h:114
Product of expressions.
Definition mul.h:31
This class is a wrapper around CLN-numbers within the GiNaC class hierarchy.
Definition numeric.h:81
Base class for print_contexts.
Definition print.h:101
Context for latex-parsable output.
Definition print.h:121
Context for python-parsable output.
Definition print.h:137
Switch to control algorithm for linear system solving.
Definition flags.h:140
@ automatic
Let the system choose.
Definition flags.h:145
Definition of optimizing macros.
Interface to GiNaC's light-weight expression handles.
static const bool value
Definition factor.cpp:198
unsigned options
Definition factor.cpp:2473
size_t n
Definition factor.cpp:1431
size_t c
Definition factor.cpp:756
ex x
Definition factor.cpp:1609
size_t r
Definition factor.cpp:756
exset syms
Definition factor.cpp:2427
mvec m
Definition factor.cpp:757
Definition add.cpp:35
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:1748
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:1816
container< std::list > lst
Definition lst.h:31
unsigned rank(const matrix &m)
Definition matrix.h:154
std::map< ex, ex, ex_is_less > exmap
Definition basic.h:49
matrix inverse(const matrix &m)
Definition matrix.h:149
ex rhs(const ex &thisex)
Definition ex.h:832
ex diag_matrix(const lst &l)
Convert list of diagonal elements to matrix.
Definition matrix.cpp:1706
matrix transpose(const matrix &m)
Definition matrix.h:137
unsigned rows(const matrix &m)
Definition matrix.h:131
ex charpoly(const matrix &m, const ex &lambda)
Definition matrix.h:146
ex evalf(const ex &thisex)
Definition ex.h:784
ex trace(const matrix &m)
Definition matrix.h:143
ex determinant(const matrix &m, unsigned options=determinant_algo::automatic)
Definition matrix.h:140
unsigned cols(const matrix &m)
Definition matrix.h:134
ex lst_to_matrix(const lst &l)
Convert list of lists to matrix.
Definition matrix.cpp:1679
std::vector< ex > exvector
Definition basic.h:47
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:1738
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:1785
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:151

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