Skip to main content

Overview

Matrices in Giac are represented as vectors of vectors. The matrice type is an alias for vecteur, where each element is itself a vecteur representing a row.

Type Definition

typedef vecteur matrice;
A matrix is a vecteur where each element is a vecteur (row vector).

Matrix Properties

ckmatrix

Check if a structure is a valid matrix.
bool ckmatrix(const matrice & a);
bool ckmatrix(const matrice & a, bool allow_embedded_vect, bool ckundef = true);
bool ckmatrix(const gen & a);
a
const matrice &
Matrix to validate
allow_embedded_vect
bool
Allow nested vectors
ckundef
bool
default:"true"
Check for undefined values
return
bool
true if a is a valid matrix

is_squarematrix

Check if a matrix is square.
bool is_squarematrix(const matrice & a);
bool is_squarematrix(const gen & a);
a
const matrice &
Matrix to check
return
bool
true if matrix has equal rows and columns

Matrix Dimensions

int mrows(const matrice & a);
int mcols(const matrice & a);
void mdims(const matrice & m, int & r, int & c);
a
const matrice &
Input matrix
return
int
Number of rows (mrows) or columns (mcols)

Matrix Construction

midn

Create an identity matrix.
matrice midn(int n);
void midn(int n, matrice & res);
n
int
Size of the identity matrix
return
matrice
n×n identity matrix
Giac Command: _idn Example:
matrice I = midn(3); // 3×3 identity matrix

mranm

Generate a random matrix.
matrice mranm(int n, int m, const gen & f, GIAC_CONTEXT);
n
int
Number of rows
m
int
Number of columns
f
const gen &
Random element generator function
return
matrice
n×m random matrix
Giac Command: _ranm

makefreematrice

Create a matrix with modifiable rows.
matrice makefreematrice(const matrice & m);
m
const matrice &
Input matrix
return
matrice
Matrix with independent row vectors (safe for in-place modification)

Matrix Operations

mtran

Transpose a matrix.
matrice mtran(const matrice & a);
void mtran(const matrice & a, matrice & res, int ncolres = 0, bool ckundef = true);
a
const matrice &
Matrix to transpose
ncolres
int
default:"0"
Expected number of columns in result
return
matrice
Transposed matrix
Giac Command: _tran

mmult

Matrix multiplication.
matrice mmult(const matrice & a, const matrice & b);
void mmult(const matrice & a, const matrice & b, matrice & res);
bool mmultck(const matrice & a, const matrice & b, matrice & res);
a
const matrice &
First matrix
b
const matrice &
Second matrix
return
matrice
Product matrix a × b
mmult assumes dimensions are compatible. Use mmultck for dimension checking.

Matrix-Vector Operations

vecteur multmatvecteur(const matrice & a, const vecteur & b);
void multmatvecteur(const matrice & a, const vecteur & b, vecteur & res);

vecteur multvecteurmat(const vecteur & a, const matrice & b);
void multvecteurmat(const vecteur & a, const matrice & b, vecteur & res);
a
const matrice &
Matrix
b
const vecteur &
Vector
return
vecteur
Result of matrix-vector multiplication

mtrace

Compute matrix trace.
gen mtrace(const matrice & a);
gen ckmtrace(const gen & a, GIAC_CONTEXT);
a
const matrice &
Square matrix
return
gen
Sum of diagonal elements
Giac Command: _trace

Determinant and Inverse

mdet

Compute matrix determinant.
gen mdet(const matrice & a, GIAC_CONTEXT);
a
const matrice &
Square matrix
return
gen
Determinant of a
Giac Command: _det

minv

Compute matrix inverse.
matrice minv(const matrice & a, GIAC_CONTEXT);
bool minv(const matrice & a, matrice & res, bool convert_internal, int algorithm, GIAC_CONTEXT);
a
const matrice &
Square matrix to invert
convert_internal
bool
Convert to internal representation
algorithm
int
Algorithm selection (if unsure, use 1)
return
matrice
Inverse matrix a⁻¹
Returns empty matrix if a is singular.

Eigenvalues and Eigenvectors

megvl

Compute eigenvalues.
vecteur megvl(const matrice & a, GIAC_CONTEXT);
a
const matrice &
Square matrix
return
vecteur
Vector of eigenvalues
Giac Command: _egvl

megv

Compute eigenvectors.
matrice megv(const matrice & a, GIAC_CONTEXT);
a
const matrice &
Square matrix
return
matrice
Matrix of eigenvectors
Giac Command: _egv

egv

Full eigenvalue decomposition.
bool egv(const matrice & m, matrice & p, vecteur & d, GIAC_CONTEXT, 
         bool jordan, bool rational_jordan_form, bool eigenvalues_only);
m
const matrice &
Input square matrix
p
matrice &
Output eigenvector matrix
d
vecteur &
Output eigenvalues (or Jordan form if jordan=true)
jordan
bool
Return Jordan form instead of diagonal
rational_jordan_form
bool
Use rational Jordan form
eigenvalues_only
bool
Only compute eigenvalues
return
bool
true if successful

Characteristic Polynomial

mpcar

Compute characteristic polynomial.
vecteur mpcar(const matrice & a, vecteur & B, bool compute_B, GIAC_CONTEXT);
vecteur mpcar(const matrice & a, vecteur & Bv, bool compute_Bv, 
              bool convert_internal, GIAC_CONTEXT);
a
const matrice &
Square matrix
B
vecteur &
Output parameter for additional computation
compute_B
bool
Whether to compute B parameter
return
vecteur
Coefficients of characteristic polynomial
Giac Command: _pcar

Kernel and Image

mker

Compute kernel (null space) of a matrix.
vecteur mker(const matrice & a, GIAC_CONTEXT);
bool mker(const matrice & a, vecteur & v, int algorithm, GIAC_CONTEXT);
a
const matrice &
Input matrix
algorithm
int
default:"0"
Algorithm selection
return
vecteur
Basis vectors for the kernel
Giac Command: _ker

mimage

Compute image (column space) of a matrix.
vecteur mimage(const matrice & a, GIAC_CONTEXT);
bool mimage(const matrice & a, vecteur & v, GIAC_CONTEXT);
a
const matrice &
Input matrix
return
vecteur
Basis vectors for the column space
Giac Command: _image

Matrix Decompositions

mlu

LU decomposition.
bool mlu(const matrice & a0, vecteur & P, matrice & L, matrice & U, GIAC_CONTEXT);
a0
const matrice &
Input matrix
P
vecteur &
Permutation vector
L
matrice &
Lower triangular matrix
U
matrice &
Upper triangular matrix
return
bool
true if decomposition successful
Giac Command: _lu

QR Decomposition

Giac Command: _qr

Cholesky Decomposition

Giac Command: _cholesky

SVD (Singular Value Decomposition)

Giac Command: _svd

LDL Decomposition

bool ldl(matrice & a, std::vector<int> & perm, int mat_type, bool & sing, 
         double time_limit, GIAC_CONTEXT);
a
matrice &
Input/output matrix
perm
std::vector<int> &
Permutation vector
mat_type
int
Matrix type indicator
sing
bool &
Set to true if matrix is singular
time_limit
double
Maximum computation time
return
bool
true if decomposition successful
Giac Command: _ldl

Jordan Form

mjordan

Compute Jordan normal form.
vecteur mjordan(const matrice & a, bool rational_jordan, GIAC_CONTEXT);
gen jordan(const gen & a, bool rational_jordan, GIAC_CONTEXT);
a
const matrice &
Square matrix
rational_jordan
bool
Use rational Jordan form
return
vecteur
Jordan normal form decomposition
Giac Commands: _jordan, _rat_jordan

Special Matrix Operations

matpow

Matrix power.
matrice matpow(const matrice & m, const gen & n, GIAC_CONTEXT);
m
const matrice &
Base matrix
n
const gen &
Exponent
return
matrice
Matrix power mⁿ
Giac Command: _matpow

Hessenberg Form

void hessenberg(std_matrix<gen> & H, std_matrix<gen> & P, GIAC_CONTEXT);
void hessenberg_ortho(std_matrix<gen> & H, std_matrix<gen> & P, GIAC_CONTEXT);
Giac Command: _hessenberg

Gram-Schmidt Orthogonalization

matrice gramschmidt(const matrice & m, bool normalize, GIAC_CONTEXT);
matrice gramschmidt(const matrice & m, matrice & r, bool normalize, GIAC_CONTEXT);
m
const matrice &
Input matrix with column vectors
normalize
bool
Normalize the output vectors
r
matrice &
R matrix from QR decomposition
return
matrice
Orthogonalized matrix

LLL Algorithm

matrice lll(const matrice & m, GIAC_CONTEXT);
matrice lll(const matrice & M, matrice & L, matrice & O, matrice & A, GIAC_CONTEXT);
m
const matrice &
Input lattice basis
return
matrice
LLL-reduced basis
Giac Command: _lll

Integer Matrix Operations

Smith Normal Form

bool ismith(const matrice & Aorig, matrice & U, matrice & A, matrice & V, GIAC_CONTEXT);
Giac Command: _ismith

Hermite Normal Form

bool ihermite(const matrice & Aorig, matrice & U, matrice & A, GIAC_CONTEXT);
Giac Command: _ihermite

See Also

Build docs developers (and LLMs) love