| Top |
| #define | M_NA |
| #define | R_DIAG_MIN |
| enum | GretlMatrixMod |
| enum | GretlMatrixStructure |
| typedef | gretl_vector |
| typedef | matrix_info |
| gretl_matrix | |
| typedef | gretl_matrix_block |
Libgretl implements most of the matrix functionality that is likely to be required in econometric calculation. For basics such as decomposition and inversion we use LAPACK as the underlying engine.
To get yourself a gretl matrix, use gretl_matrix_alloc() or
one of the more specialized constructors; to free such a
matrix use gretl_matrix_free().
#define gretl_matrix_set(m,i,j,x) ((m)->val[(j)*(m)->rows+(i)]=x)
Sets the i
, j
element of m
to x
.
#define gretl_vector_free(v) gretl_matrix_free(v)
Frees the vector v
and its associated storage.
void gretl_matrix_print (const gretl_matrix *m,const char *msg);
Prints the given matrix to stderr.
void
gretl_matrix_xtr_symmetric (gretl_matrix *m);
Computes the symmetric part of m
by averaging its off-diagonal
elements.
void
gretl_matrix_set_equals_tolerance (double tol);
Sets the tolerance for judging whether or not a matrix is symmetric
(see gretl_matrix_is_symmetric() and also
gretl_invert_symmetric_matrix()). The tolerance is the maximum
relative difference between corresponding off-diagonal elements that
is acceptable in a supposedly "symmetric" matrix. The default
value is 1.0e-9.
void
gretl_matrix_unset_equals_tolerance (void);
Sets the tolerance for judging whether or not a matrix is symmetric
to its default value. See also gretl_matrix_set_equals_tolerance().
gretl_matrix * gretl_matrix_reuse (gretl_matrix *m,int rows,int cols);
An "experts only" memory-conservation trick. If m
is an
already-allocated gretl matrix, you can "resize" it by
specifying a new number of rows and columns. This works
only if the product of rows
and cols
is less than or equal
to the product of the number of rows and columns in the
matrix as originally allocated; no actual reallocation of memory
is performed. If you "reuse" with an excessive number of rows
or columns you will surely crash your program or smash the
stack. Note also that the matrix-pointer returned is not really
new, and when the matrix is to be freed, gretl_matrix_free()
should be applied only once.
int gretl_matrix_realloc (gretl_matrix *m,int rows,int cols);
Reallocates the storage in m
to the specified dimensions.
void
gretl_matrix_init (gretl_matrix *m);
Initializes m
to be zero by zero with NULL data.
gretl_matrix * gretl_matrix_replace (gretl_matrix **pa,gretl_matrix *b);
Frees the matrix at location pa
and substitutes b
.
int gretl_matrix_replace_content (gretl_matrix *targ,gretl_matrix *donor);
Moves the content of donor
into targ
; donor
becomes
a null matrix in consequence.
gretl_matrix * gretl_matrix_block_get_matrix (gretl_matrix_block *B,int i);
gretl_matrix * gretl_matrix_seq (double start,double end,double step,int *err);
int gretl_matrix_copy_row (gretl_matrix *dest,int di,const gretl_matrix *src,int si);
Copies the values from row si
of src
into row di
of dest
, provided src
and dest
have the same number
of columns.
int gretl_matrix_inscribe_I (gretl_matrix *m,int row,int col,int n);
Writes an n x n identity matrix into matrix m
, the top left-hand
corner of the insertion being given by row
and col
(which are
0-based).
m |
original matrix. |
|
row |
top row for insertion. |
|
col |
leftmost row for insertion. |
|
n |
dimension (rows and columns) of identity matrix. |
0 on successful completion, or E_NONCONF if an identity
matrix of the specified size cannot be fitted into m
at the
specified location.
gretl_matrix * gretl_matrix_get_diagonal (const gretl_matrix *m,int *err);
int gretl_matrix_get_row (const gretl_matrix *m,int i,gretl_vector *v);
Copies row i
of matrix m
into vector v
.
int gretl_matrix_random_fill (gretl_matrix *m,int dist);
Fills m
with pseudo-random values from either the uniform
or the standard normal distribution.
gretl_matrix * gretl_random_matrix_new (int r,int c,int dist);
Creates a new $r x c
matrix and filles it with pseudo-random
values from either the uniform or the standard normal
distribution.
gretl_matrix * gretl_matrix_block_resample (const gretl_matrix *m,int blocklen,int *err);
int gretl_matrix_block_resample2 (const gretl_matrix *src,gretl_matrix *targ,int blocklen,int *z);
An "in-place" version of gretl_matrix_block_resample().
It is assumed that targ
is a matrix of the same dimensions
as src
, that blocklen
is greater than 1, and that z
is long enough to hold n integers, where n is the number
of rows in src
divided by blocklen
, rounded up to the
nearest integer.
int
gretl_matrix_zero_upper (gretl_matrix *m);
Sets the elements of m
outside of the lower triangle to zero.
int
gretl_matrix_zero_lower (gretl_matrix *m);
Sets the elements of m
outside of the upper triangle to zero.
int gretl_matrix_mirror (gretl_matrix *m,char uplo);
If uplo
= 'L', copy the lower triangle of m
into
the upper triangle; or if uplo
= 'U' copy the upper
triangle into the lower, in either case producing a
symmetric result.
void gretl_matrix_fill (gretl_matrix *m,double x);
Sets all entries in m
to the value x
.
void gretl_matrix_multiply_by_scalar (gretl_matrix *m,double x);
Multiplies all elements of m
by x
.
int gretl_matrix_divide_by_scalar (gretl_matrix *m,double x);
Divides all elements of m
by x
.
void
gretl_matrix_switch_sign (gretl_matrix *m);
Changes the sign of each element of m
.
gretl_matrix * gretl_matrix_dot_op (const gretl_matrix *a,const gretl_matrix *b,int op,int *err);
gretl_matrix * gretl_matrix_complex_multiply (const gretl_matrix *a,const gretl_matrix *b,int *err);
Computes the complex product of a
and b
. The first
column in these matrices is assumed to contain real
values, and the second column (if present) imaginary
coefficients.
gretl_matrix * gretl_matrix_divide (const gretl_matrix *a,const gretl_matrix *b,GretlMatrixMod mod,int *err);
Follows the semantics of Matlab/Octave for left and right matrix "division". In left division, A \ B is in principle equivalent to A^{-1} * B, and in right division A / B is in principle equivalent to A * B^{-1}, but the result is obtained without explicit computation of the inverse.
In left division a
and b
must have the same number of
rows; in right division they must have the same number
of columns.
a |
left-hand matrix. |
|
b |
right-hand matrix. |
|
mod |
|
|
err |
location to receive error code. |
gretl_matrix * gretl_matrix_complex_divide (const gretl_matrix *a,const gretl_matrix *b,int *err);
Computes the complex division of a
over b
. The first
column in these matrices is assumed to contain real
values, and the second column (if present) imaginary
coefficients.
gretl_matrix * gretl_matrix_exp (const gretl_matrix *m,int *err);
Calculates the matrix exponential of m
, using algorithm
11.3.1 from Golub and Van Loan, "Matrix Computations", 3e.
gretl_matrix * gretl_matrix_polroots (const gretl_matrix *a,int *err);
Calculates the roots of the polynomial with coefficients
given by a
. If the degree of the polynomial is p, then
a
should contain p + 1 coefficients in ascending order,
i.e. starting with the constant and ending with the
coefficient on x^p.
void gretl_matrix_raise (gretl_matrix *m,double x);
Raises each element of m
to the power x
.
void
gretl_matrix_free (gretl_matrix *m);
Frees the allocated storage in m
, then m
itself.
double *
gretl_matrix_steal_data (gretl_matrix *m);
"Steals" the allocated data from m
, which is left with a
NULL data pointer.
int gretl_vector_copy_values (gretl_vector *targ,const gretl_vector *src);
Copies the elements of src
into the corresponding elements
of targ
.
int gretl_matrix_copy_values (gretl_matrix *targ,const gretl_matrix *src);
Copies the elements of src
into the corresponding elements
of targ
.
0 on successful completion, or
E_NONCONF if the two matrices are not
conformable for the operation.
int gretl_matrix_copy_data (gretl_matrix *targ,const gretl_matrix *src);
Copies all data from src
to targ
: this does the same
as gretl_matrix_copy_values() but also transcribes
t1, t2, colnames and rownames information, if present.
int gretl_matrix_copy_values_shaped (gretl_matrix *targ,const gretl_matrix *src);
Copies the elements of src
into targ
, column
by column.
0 on successful completion, or E_NONCONF if
the two matrices do not contain the same number of
elements.
int gretl_matrix_add_to (gretl_matrix *targ,const gretl_matrix *src);
Adds the elements of src
to the corresponding elements
of targ
.
0 on successful completion, or E_NONCONF if the
two matrices are not conformable for the operation.
In the special case where src
is in fact a scalar, the
operation always goes through OK, with the scalar being
added to each element of targ
.
int gretl_matrix_add (const gretl_matrix *a,const gretl_matrix *b,gretl_matrix *c);
Adds the elements of a
and b
, the result going to c
.
0 on successful completion, or E_NONCONF if the
matrices are not conformable for the operation.
int gretl_matrix_add_transpose_to (gretl_matrix *targ,const gretl_matrix *src);
Adds the elements of src
, transposed, to the corresponding
elements of targ
.
0 on successful completion, or E_NONCONF if the
two matrices are not conformable for the operation.
int gretl_matrix_subtract_from (gretl_matrix *targ,const gretl_matrix *src);
Subtracts the elements of src
from the corresponding elements
of targ
.
0 on successful completion, or E_NONCONF if the
two matrices are not conformable for the operation.
In the special case where src
is in fact a scalar, the
operation always goes through OK, with the scalar being
subtracted from each element of targ
.
int gretl_matrix_subtract (const gretl_matrix *a,const gretl_matrix *b,gretl_matrix *c);
Subtracts the elements of b
from the corresponding elements
of a
, the result going to c
.
0 on successful completion, or E_NONCONF if the
matrices are not conformable for the operation.
int gretl_matrix_subtract_reversed (const gretl_matrix *a,gretl_matrix *b);
Operates on b
such that b_{ij} = a_{ij} - b_{ij}.
0 on successful completion, or E_NONCONF if the
two matrices are not conformable for the operation.
int
gretl_matrix_I_minus (gretl_matrix *m);
Rewrites m
as (I - m), where I is the n x n identity
matrix.
int
gretl_matrix_transpose_in_place (gretl_matrix *m);
Tranposes m
in place.
int gretl_matrix_transpose (gretl_matrix *targ,const gretl_matrix *src);
Fills out targ
(which must be pre-allocated and of the right
dimensions) with the transpose of src
.
int
gretl_square_matrix_transpose (gretl_matrix *m);
Transposes the matrix m
.
int
gretl_matrix_add_self_transpose (gretl_matrix *m);
Adds the transpose of m
to m
itself, yielding a symmetric
matrix.
int gretl_matrix_vectorize (gretl_matrix *targ,const gretl_matrix *src);
Writes into targ
vec(src
), that is, a column vector
formed by stacking the columns of src
.
int gretl_matrix_unvectorize (gretl_matrix *targ,const gretl_matrix *src);
Writes successive blocks of length m from src
into
the successive columns of targ
(that is, performs the
inverse of the vec() operation).
int gretl_matrix_vectorize_h (gretl_matrix *targ,const gretl_matrix *src);
Writes into targ
vech(src
), that is, a column vector
containing the lower-triangular elements of src
.
This is only useful for symmetric matrices, but for the
sake of performance we don't check for that.
int gretl_matrix_unvectorize_h (gretl_matrix *targ,const gretl_matrix *src);
Rearranges successive blocks of decreasing length from src
into
the successive columns of targ
(that is, performs the
inverse of the vech() operation): targ
comes out symmetric.
int gretl_matrix_inscribe_matrix (gretl_matrix *targ,const gretl_matrix *src,int row,int col,GretlMatrixMod mod);
Writes src
into targ
, starting at offset row
, col
.
The targ
matrix must be large enough to sustain the
inscription of src
at the specified point. If mod
is GRETL_MOD_TRANSPOSE it is in fact the transpose of
src
that is written into targ
.
targ |
target matrix. |
|
src |
source matrix. |
|
row |
row offset for insertion (0-based). |
|
col |
column offset for insertion. |
|
mod |
either |
int gretl_matrix_extract_matrix (gretl_matrix *targ,const gretl_matrix *src,int row,int col,GretlMatrixMod mod);
Writes into targ
a sub-matrix of src
, taken from the
offset row
, col
. The targ
matrix must be large enough
to provide a sub-matrix of the dimensions of src
.
If mod
is GRETL_MOD_TRANSPOSE it is in fact the transpose
of the sub-matrix that that is written into targ
.
targ |
target matrix. |
|
src |
source matrix. |
|
row |
row offset for extraction (0-based). |
|
col |
column offset for extraction. |
|
mod |
either |
int gretl_matrix_multiply_mod (const gretl_matrix *a,GretlMatrixMod amod,const gretl_matrix *b,GretlMatrixMod bmod,gretl_matrix *c,GretlMatrixMod cmod);
Multiplies a
(or a-transpose) into b
(or b transpose),
with the result written into c
.
a |
left-hand matrix. |
|
amod |
modifier: |
|
b |
right-hand matrix. |
|
bmod |
modifier: |
|
c |
matrix to hold the product. |
|
cmod |
modifier: |
int gretl_matrix_multiply_mod_single (const gretl_matrix *a,GretlMatrixMod amod,const gretl_matrix *b,GretlMatrixMod bmod,gretl_matrix *c,GretlMatrixMod cmod);
int gretl_matrix_multiply (const gretl_matrix *a,const gretl_matrix *b,gretl_matrix *c);
Multiplies a
into b
, with the result written into c
.
int gretl_matrix_multiply_single (const gretl_matrix *a,const gretl_matrix *b,gretl_matrix *c);
gretl_matrix * gretl_matrix_multiply_new (const gretl_matrix *a,const gretl_matrix *b,int *err);
Multiplies a
into b
, with the result written into a newly
allocated matrix.
int gretl_matrix_kronecker_product (const gretl_matrix *A,const gretl_matrix *B,gretl_matrix *K);
Writes the Kronecker product of A
and B
into K
.
gretl_matrix * gretl_matrix_kronecker_product_new (const gretl_matrix *A,const gretl_matrix *B,int *err);
int gretl_matrix_hdproduct (const gretl_matrix *A,const gretl_matrix *B,gretl_matrix *C);
Writes into C
the horizontal direct product of A
and B
.
That is, $C_i' = A_i' \otimes B_i'$ (in TeX notation)
0 on success, E_NONCONF if A
and B
have different
numbers of rows or matrix C
is not correctly dimensioned for the
operation.
gretl_matrix * gretl_matrix_hdproduct_new (const gretl_matrix *A,const gretl_matrix *B,int *err);
int gretl_matrix_I_kronecker (int p,const gretl_matrix *B,gretl_matrix *K);
Writes the Kronecker product of the identity matrix
of order r
and B
into K
.
gretl_matrix * gretl_matrix_I_kronecker_new (int p,const gretl_matrix *B,int *err);
Writes the Kronecker product of the identity matrix
of order r
and B
into a newly allocated matrix.
int gretl_matrix_kronecker_I (const gretl_matrix *A,int r,gretl_matrix *K);
Writes the Kronecker product of A
and the identity
matrix of order r
into K
.
gretl_matrix * gretl_matrix_kronecker_I_new (const gretl_matrix *A,int r,int *err);
Writes into a newl allocated matrix the Kronecker
product of A
and the identity matrix of order r
.
gretl_matrix * gretl_matrix_pow (const gretl_matrix *A,int s,int *err);
Calculates the matrix A^k using Golub and Van Loan's Algorithm 11.2.2 ("Binary Powering").
double gretl_matrix_dot_product (const gretl_matrix *a,GretlMatrixMod amod,const gretl_matrix *b,GretlMatrixMod bmod,int *err);
a |
left-hand matrix. |
|
amod |
modifier: |
|
b |
right-hand matrix. |
|
bmod |
modifier: |
|
err |
pointer to receive error code (zero on success, non-zero on failure), or NULL. |
The dot (scalar) product of a
(or a
-transpose) and
b
(or b
-transpose), or NADBL on failure.
double gretl_vector_dot_product (const gretl_vector *a,const gretl_vector *b,int *err);
gretl_matrix * gretl_matrix_column_sum (const gretl_matrix *m,int *err);
gretl_matrix * gretl_matrix_column_prod (const gretl_matrix *m,int *err);
gretl_matrix * gretl_matrix_column_mean (const gretl_matrix *m,int *err);
gretl_matrix * gretl_matrix_column_sd2 (const gretl_matrix *m,int df,int *err);
void
gretl_matrix_demean_by_row (gretl_matrix *m);
For each row of m
, subtracts the row mean from each
element on the row.
void
gretl_matrix_demean_by_column (gretl_matrix *m);
For each column of m
, subtracts the column mean from each
element in the column.
gretl_matrix *
gretl_matrix_vcv (gretl_matrix *m);
Forms a variance-covariance matrix based on m
, thus:
(1) subtract the column means from the column elements of m
;
(2) multiply m
-transpose into m
; and
(3) divide the elements of the product by the number of rows
in m
.
gretl_matrix * gretl_matrix_quantiles (const gretl_matrix *m,const gretl_matrix *p,int *err);
double gretl_matrix_determinant (gretl_matrix *a,int *err);
Compute the determinant of the square matrix a
using the LU
factorization. Matrix a
is not preserved: it is overwritten
by the factorization.
double gretl_matrix_log_determinant (gretl_matrix *a,int *err);
Compute the log of the determinant of the square matrix a
using LU
factorization. Matrix a
is not preserved: it is overwritten
by the factorization.
double gretl_matrix_log_abs_determinant (gretl_matrix *a,int *err);
Compute the log of the absolute value of the determinant of the
square matrix a
using LU factorization. Matrix a
is not
preserved: it is overwritten by the factorization.
double gretl_vcv_log_determinant (const gretl_matrix *m,int *err);
Compute the log determinant of the symmetric positive-definite
matrix m
using Cholesky decomposition.
int gretl_LU_solve (gretl_matrix *a,gretl_matrix *b);
Solves ax = b for the unknown x, via LU decomposition
using partial pivoting with row interchanges.
On exit, b
is replaced by the solution and a
is replaced
by its decomposition. Calls the LAPACK functions dgetrf()
and dgetrs().
int gretl_LU_solve_invert (gretl_matrix *a,gretl_matrix *b);
Solves ax = b for the unknown x, using LU decomposition,
then proceeds to use the decomposition to invert a
. Calls
the LAPACK functions dgetrf(), dgetrs() and dgetri(); the
decomposition proceeds via partial pivoting with row
interchanges.
On exit, b
is replaced by the solution and a
is replaced
by its inverse.
int gretl_cholesky_decomp_solve (gretl_matrix *a,gretl_matrix *b);
Solves ax = b for the unknown vector x, using Cholesky decomposition
via the LAPACK functions dpotrf() and dpotrs().
On exit, b
is replaced by the solution and a
is replaced by its
Cholesky decomposition.
int gretl_cholesky_solve (const gretl_matrix *a,gretl_vector *b);
Solves ax = b for the unknown vector x, using the pre-computed
Cholesky decomposition of a
. On exit, b
is replaced by the
solution.
int
gretl_cholesky_invert (gretl_matrix *a);
Inverts a
, which must contain the pre-computed Cholesky
decomposition of a p.d. matrix, as may be obtained using
gretl_cholesky_decomp_solve(). For speed there's no error
checking of the input -- the caller should make sure it's OK.
gretl_vector * gretl_toeplitz_solve (const gretl_vector *c,const gretl_vector *r,const gretl_vector *b,int *err);
Solves Tx = b for the unknown vector x, where T is a Toeplitz matrix, that is (zero-based)
T_{ij} = c_{i-j} for i <= j T_{ij} = r_{i-j} for i > j
Note that c[0] should equal r[0].
int gretl_inverse_from_cholesky_decomp (gretl_matrix *targ,const gretl_matrix *src);
Computes in targ
the inverse of a symmetric positive definite
matrix, on the assumption that the original matrix this has already
been Cholesky-decomposed in src
.
int
gretl_invert_general_matrix (gretl_matrix *a);
Computes the inverse of a general matrix using LU
factorization. On exit a
is overwritten with the inverse.
Uses the LAPACK functions dgetrf() and dgetri().
int
gretl_invert_symmetric_indef_matrix (gretl_matrix *a);
Computes the inverse of a real symmetric matrix via the
Bunch-Kaufman diagonal pivoting method. Uses the LAPACK
functions dsytrf() and dsytri(). On exit a
is overwritten
with the inverse.
int
gretl_invert_symmetric_matrix (gretl_matrix *a);
Computes the inverse of a symmetric positive definite matrix
using Cholesky factorization. On exit a
is overwritten with
the inverse. Uses the LAPACK functions dpotrf() and dpotri().
int gretl_invert_symmetric_matrix2 (gretl_matrix *a,double *ldet);
Computes the inverse of a symmetric positive definite matrix
using Cholesky factorization, computing the log-determinant
in the process. On exit a
is overwritten with the inverse
and if ldet
is not NULL the log-determinant is written to
that location. Uses the LAPACK functions dpotrf() and dpotri().
int
gretl_invert_packed_symmetric_matrix (gretl_matrix *v);
Computes the inverse of a symmetric positive definite matrix,
stored in vech form, using Cholesky factorization. On exit
v
is overwritten with the lower triangle of the inverse.
Uses the LAPACK functions dpptrf() and dpptri().
int gretl_invert_triangular_matrix (gretl_matrix *a,char uplo);
Computes the inverse of a triangular matrix. On exit
a
is overwritten with the inverse. Uses the lapack
function dtrtri.
int
gretl_invert_diagonal_matrix (gretl_matrix *a);
Computes the inverse of a diagonal matrix.
On exit a
is overwritten with the inverse.
int
gretl_invert_matrix (gretl_matrix *a);
Computes the inverse of matrix a
: on exit a
is
overwritten with the inverse. If a
is diagonal
or symmetric, appropriate simple inversion routines
are called.
int
gretl_matrix_moore_penrose (gretl_matrix *A);
Computes the generalized inverse of matrix a
via its SVD
factorization, with the help of the lapack function
dgesvd. On exit the original matrix is overwritten by
the inverse.
int
gretl_SVD_invert_matrix (gretl_matrix *a);
Computes the inverse (or generalized inverse) of a general square
matrix using SVD factorization, with the help of the lapack function
dgesvd. If any of the singular values of a
are less than 1.0e-9
the Moore-Penrose generalized inverse is computed instead of the
standard inverse. On exit the original matrix is overwritten by
the inverse.
int
gretl_invpd (gretl_matrix *a);
Computes the inverse of a symmetric positive definite matrix
using Cholesky factorization. On exit a
is overwritten with
the inverse. Uses the LAPACK functions dpotrf() and dpotri().
Little checking is done, for speed: we assume the caller
knows what he's doing.
int
gretl_maybe_invpd (gretl_matrix *a);
Attempts to computes the inverse of a matrix which may be
positive definite. On exit a
is overwritten with
the inverse. Uses the LAPACK functions dpotrf() and dpotri().
Little checking is done, for speed: we assume the caller
knows what he's doing. Unlike gretl_invpd() this function
does not dump error messages to stderr in case the matrix
is not in fact positive definite.
int gretl_matrix_SVD (const gretl_matrix *a,gretl_matrix **pu,gretl_vector **ps,gretl_matrix **pvt);
Computes SVD factorization of a general matrix using the lapack function dgesvd. A = u * diag(s) * vt.
double gretl_symmetric_matrix_rcond (const gretl_matrix *m,int *err);
Estimates the reciprocal condition number of the real symmetric
positive definite matrix m
(in the 1-norm), using the LAPACK
functions dpotrf() and dpocon().
double gretl_matrix_rcond (const gretl_matrix *m,int *err);
Estimates the reciprocal condition number of the real
matrix m
(in the 1-norm).
double gretl_matrix_cond_index (const gretl_matrix *m,int *err);
Estimates the condition number (a la Belsley) of the real
matrix m
.
int gretl_symmetric_eigen_sort (gretl_matrix *evals,gretl_matrix *evecs,int rank);
Sorts the eigenvalues in evals
from largest to smallest, and
rearranges the columns in evecs
correspondingly. If rank
is
greater than zero and less than the number of columns in evecs
,
then on output evecs
is shrunk so that it contains only the
columns associated with the largest rank
eigenvalues.
gretl_matrix * gretl_general_matrix_eigenvals (gretl_matrix *m,int eigenvecs,int *err);
Computes the eigenvalues of the general matrix m
.
If eigenvecs
is non-zero, also compute the right
eigenvectors of m
, which are stored in m
. Uses the lapack
function dgeev.
gretl_matrix * gretl_symmetric_matrix_eigenvals (gretl_matrix *m,int eigenvecs,int *err);
Computes the eigenvalues of the real symmetric matrix m
.
If eigenvecs
is non-zero, also compute the orthonormal
eigenvectors of m
, which are stored in m
. Uses the lapack
function dsyev().
gretl_matrix * gretl_symm_matrix_eigenvals_descending (gretl_matrix *m,int eigenvecs,int *err);
Computes the eigenvalues of the real symmetric matrix m
.
If eigenvecs
is non-zero, also compute the orthonormal
eigenvectors of m
, which are stored in m
. Uses the lapack
function dsyev().
gretl_matrix * gretl_gensymm_eigenvals (const gretl_matrix *A,const gretl_matrix *B,gretl_matrix *V,int *err);
Solves the generalized eigenvalue problem | A - \lambda B | = 0 , where both A and B are symmetric and B is positive definite.
double gretl_symm_matrix_lambda_min (const gretl_matrix *m,int *err);
double gretl_symm_matrix_lambda_max (const gretl_matrix *m,int *err);
gretl_matrix * gretl_matrix_right_nullspace (const gretl_matrix *M,int *err);
Given an m x n matrix M
, construct a conformable matrix
R such that MR = 0 (that is, all the columns of R are
orthogonal to the space spanned by the rows of M
).
gretl_matrix * gretl_matrix_left_nullspace (const gretl_matrix *M,GretlMatrixMod mod,int *err);
Given an m x n matrix M
, construct a conformable matrix
L such that LM = 0 (that is, all the columns of M
are
orthogonal to the space spanned by the rows of L).
gretl_matrix * gretl_matrix_row_concat (const gretl_matrix *a,const gretl_matrix *b,int *err);
gretl_matrix * gretl_matrix_col_concat (const gretl_matrix *a,const gretl_matrix *b,int *err);
gretl_matrix * gretl_matrix_direct_sum (const gretl_matrix *a,const gretl_matrix *b,int *err);
int gretl_matrix_inplace_colcat (gretl_matrix *a,const gretl_matrix *b,const char *mask);
Concatenates onto a
the selected columns of b
, if the
two matrices are conformable.
gretl_matrix * gretl_matrix_diffcol (const gretl_matrix *m,double missval,int *err);
gretl_matrix * gretl_matrix_lag (const gretl_matrix *m,const gretl_vector *k,gretlopt opt,double missval);
int gretl_matrix_inplace_lag (gretl_matrix *targ,const gretl_matrix *src,int k);
Fills out targ
(if it is of the correct dimensions),
with (columnwise) lags of src
, using 0 for missing
values.
int
gretl_matrix_cholesky_decomp (gretl_matrix *a);
Computes the Cholesky factorization of the symmetric,
positive definite matrix a
. On exit the lower triangle of
a
is replaced by the factor L, as in a = LL', and the
upper triangle is set to zero. Uses the lapack function
dpotrf.
int gretl_matrix_psd_root (gretl_matrix *a,const gretl_matrix *a0);
Computes the LL' factorization of the symmetric,
positive semidefinite matrix a
. On successful exit
the lower triangle of a
is replaced by the factor L
and the upper triangle is set to zero.
int gretl_matrix_QR_decomp (gretl_matrix *M,gretl_matrix *R);
Computes the QR factorization of M
. On successful exit
the matrix M
holds Q, and, if R
is not NULL, the upper
triangle of R
holds R. Uses the LAPACK functions
dgeqrf() and dorgqr().
int gretl_matrix_QR_pivot_decomp (gretl_matrix *M,gretl_matrix *R,int **order);
int gretl_check_QR_rank (const gretl_matrix *R,int *err,double *rcnd);
Checks the reciprocal condition number of R and calculates
the rank of the matrix QR. If rcnd
is not NULL it receives
the reciprocal condition number.
int gretl_matrix_rank (const gretl_matrix *a,int *err);
Computes the rank of a
via its SV decomposition.
int gretl_matrix_ols (const gretl_vector *y,const gretl_matrix *X,gretl_vector *b,gretl_matrix *vcv,gretl_vector *uhat,double *s2);
Computes OLS estimates using Cholesky factorization by default,
but with a fallback to QR decomposition if the data are highly
ill-conditioned, and puts the coefficient estimates in b
.
Optionally, calculates the covariance matrix in vcv
and the
residuals in uhat
.
y |
dependent variable vector. |
|
X |
matrix of independent variables. |
|
b |
vector to hold coefficient estimates. |
|
vcv |
matrix to hold the covariance matrix of the coefficients, or NULL if this is not needed. |
|
uhat |
vector to hold the regression residuals, or NULL if these are not needed. |
|
s2 |
pointer to receive residual variance, or NULL. Note:
if |
int gretl_matrix_multi_ols (const gretl_matrix *Y,const gretl_matrix *X,gretl_matrix *B,gretl_matrix *E,gretl_matrix **XTXi);
Computes OLS estimates using Cholesky decomposition by default, but
with a fallback to QR decomposition if the data are highly
ill-conditioned, and puts the coefficient estimates in B
.
Optionally, calculates the residuals in E
.
int gretl_matrix_multi_SVD_ols (const gretl_matrix *Y,const gretl_matrix *X,gretl_matrix *B,gretl_matrix *E,gretl_matrix **XTXi);
Computes OLS estimates using SVD decomposition, and puts the
coefficient estimates in B
. Optionally, calculates the
residuals in E
, (X'X)^{-1} in XTXi
.
int gretl_matrix_QR_ols (const gretl_matrix *Y,const gretl_matrix *X,gretl_matrix *B,gretl_matrix *E,gretl_matrix **XTXi,gretl_matrix **Qout);
Computes OLS estimates using QR decomposition, and puts the
coefficient estimates in B
. Optionally, calculates the
residuals in E
, (X'X)^{-1} in XTXi
, and/or the matrix Q
in Qout
.
y |
T x g matrix of dependent variables. |
|
X |
T x k matrix of independent variables. |
|
B |
k x g matrix to hold coefficient estimates. |
|
E |
T x g matrix to hold the regression residuals, or NULL if these are not needed. |
|
XTXi |
location to receive (X'X)^{-1}, or NULL if this is not needed. |
|
Qout |
location to receive Q on output, or NULL. |
double gretl_matrix_r_squared (const gretl_matrix *y,const gretl_matrix *X,const gretl_matrix *b,int *err);
y |
dependent variable, T-vector. |
|
X |
independent variables matrix, T x k. |
|
b |
coefficients, k-vector. |
|
err |
location to receive error code. |
the unadjusted R-squared, based on the regression
represented by y
, X
and b
, or NADBL on failure.
int gretl_matrix_SVD_johansen_solve (const gretl_matrix *R0,const gretl_matrix *R1,gretl_matrix *evals,gretl_matrix *B,gretl_matrix *A,int jrank);
Solves the Johansen generalized eigenvalue problem via SVD decomposition. See J. A. Doornik and R. J. O'Brien, "Numerically stable cointegration analysis", Computational Statistics and Data Analysis, 41 (2002), pp. 185-193, Algorithm 4.
If B
is non-null it should be p1 x p on input; it will
be trimmed to p1 x jrank
on output if jrank
< p.
If A
is non-null it should be p x p on input; it will
be trimmed to p x jrank
on output if jrank
< p.
evals
should be a vector of length jrank
.
int gretl_matrix_restricted_ols (const gretl_vector *y,const gretl_matrix *X,const gretl_matrix *R,const gretl_vector *q,gretl_vector *b,gretl_matrix *vcv,gretl_vector *uhat,double *s2);
Computes OLS estimates restricted by R and q, using the lapack
function dgglse(), and puts the coefficient estimates in b
.
Optionally, calculates the covariance matrix in vcv
. If q
is
NULL this is taken as equivalent to a zero vector.
y |
dependent variable vector. |
|
X |
matrix of independent variables. |
|
R |
left-hand restriction matrix, as in Rb = q. |
|
q |
right-hand restriction vector or NULL. |
|
b |
vector to hold coefficient estimates. |
|
vcv |
matrix to hold the covariance matrix of the coefficients, or NULL if this is not needed. |
|
uhat |
vector to hold residuals, if wanted. |
|
s2 |
pointer to receive residual variance, or NULL. If vcv is non-NULL and s2 is NULL, the vcv estimate is just W^{-1}. |
int gretl_matrix_restricted_multi_ols (const gretl_matrix *Y,const gretl_matrix *X,const gretl_matrix *R,const gretl_matrix *q,gretl_matrix *B,gretl_matrix *U,gretl_matrix **W);
Computes LS estimates restricted by R
and q
, putting the
coefficient estimates into B
. The R
matrix must have
g*k columns; each row represents a linear restriction;
q
must be a column vector of length equal to the
number of rows of R
.
Y |
dependent variable matrix, T x g. |
|
X |
matrix of independent variables, T x k. |
|
R |
left-hand restriction matrix, as in RB = q. |
|
q |
right-hand restriction matrix. |
|
B |
matrix to hold coefficient estimates, k x g. |
|
U |
matrix to hold residuals (T x g), if wanted. |
|
W |
matrix to hold the RLS counterpart to (X'X)^{-1}, if wanted. |
int gretl_matrix_SVD_ols (const gretl_vector *y,const gretl_matrix *X,gretl_vector *b,gretl_matrix *vcv,gretl_vector *uhat,double *s2);
Computes OLS estimates using SVD decomposition, and puts the
coefficient estimates in b
. Optionally, calculates the
covariance matrix in vcv
and the residuals in uhat
.
y |
dependent variable vector. |
|
X |
matrix of independent variables. |
|
b |
vector to hold coefficient estimates. |
|
vcv |
matrix to hold the covariance matrix of the coefficients, or NULL if this is not needed. |
|
uhat |
vector to hold the regression residuals, or NULL if these are not needed. |
|
s2 |
pointer to receive residual variance, or NULL. Note:
if |
int gretl_matrix_qform (const gretl_matrix *A,GretlMatrixMod amod,const gretl_matrix *X,gretl_matrix *C,GretlMatrixMod cmod);
Computes either A * X * A' (if amod = GRETL_MOD_NONE) or
A' * X * A (if amod = GRETL_MOD_TRANSPOSE), with the result
written into C
. The matrix X
must be symmetric, but this
is not checked, to save time. If you are in doubt on this
point you can call gretl_matrix_is_symmetric() first.
A |
m * k matrix or k * m matrix, depending on |
|
amod |
|
|
X |
symmetric k * k matrix. |
|
C |
matrix to hold the product. |
|
cmod |
modifier: |
double gretl_scalar_qform (const gretl_vector *b,const gretl_matrix *X,int *err);
Computes the scalar product bXb', or b'Xb if b
is a column
vector. The content of err
is set to a non-zero code on
failure.
int gretl_matrix_columnwise_product (const gretl_matrix *A,const gretl_matrix *B,const gretl_matrix *S,gretl_matrix *C);
int gretl_matrix_diagonal_sandwich (const gretl_vector *d,const gretl_matrix *X,gretl_matrix *DXD);
Computes in DXD
(which must be pre-allocated), the matrix
product D * X * D, where D is a diagonal matrix with elements
given by the vector d
.
int gretl_matrix_set_t1 (gretl_matrix *m,int t);
Sets an integer value on m
, which can be retrieved using
gretl_matrix_get_t1().
int gretl_matrix_set_t2 (gretl_matrix *m,int t);
Sets an integer value on m
, which can be retrieved using
gretl_matrix_get_t2().
int
gretl_matrix_get_t1 (const gretl_matrix *m);
the integer that has been set on m
using
gretl_matrix_set_t1(), or zero if no such value has
been set.
int
gretl_matrix_get_t2 (const gretl_matrix *m);
the integer that has been set on m
using
gretl_matrix_set_t2(), or zero if no such value has
been set.
int
gretl_matrix_is_dated (const gretl_matrix *m);
1 if matrix m
has integer indices recorded
via gretl_matrix_set_t1() and gretl_matrix_set_t2(),
such that t1 >= 0 and t2 > t1, otherwise zero.
int gretl_matrices_are_equal (const gretl_matrix *a,const gretl_matrix *b,int *err);
a |
first matrix in comparison. |
|
b |
second matrix in comparison. |
|
err |
location to receive error code. |
1 if the matrices a
and b
compare equal, 0 if they
differ, and -1 if the comparison is invalid, in which case
E_NONCONF is written to err
.
gretl_matrix * gretl_covariance_matrix (const gretl_matrix *m,int corr,int *err);
gretl_matrix * gretl_matrix_GG_inverse (const gretl_matrix *G,int *err);
Multiples G' into G and inverts the result. A shortcut function intended for producing an approximation to the Hessian given a gradient matrix.
gretl_matrix * gretl_matrix_varsimul (const gretl_matrix *A,const gretl_matrix *U,const gretl_matrix *x0,int *err);
Simulates a p-order n-variable VAR: x_t = \sum A_i x_{t-i} + u_t
The A_i matrices must be stacked horizontally into the A
argument, that is: A = A_1 ~ A_2 ~ A_p. The u_t vectors are
contained (as rows) in U
. Initial values are in x0
.
Note the that the arrangement of the A
matrix is somewhat
sub-optimal computationally, since its elements have to be
reordered by the function reorder_A (see above). However, the
present form is more intuitive for a human being, and that's
what counts.
gretl_matrix **
gretl_matrix_array_new (int n);
Allocates an array of n
gretl matrix pointers. On successful
allocation of the array, each element is initialized to NULL.
gretl_matrix ** gretl_matrix_array_new_with_size (int n,int rows,int cols);
Allocates an array of n
gretl matrix pointers, each one
with size rows
* cols
.
void gretl_matrix_array_free (gretl_matrix **A,int n);
Frees each of the n
gretl matrices in the array A
, and
the array itself. See also gretl_matrix_array_alloc().
gretl_matrix * gretl_matrix_values (const double *x,int n,gretlopt opt,int *err);
gretl_matrix * gretl_matrix_shape (const gretl_matrix *A,int r,int c,int *err);
Creates an (r x c) matrix containing the re-arranged values of A. Elements are read from A by column and written into the target, also by column. If A contains less elements than n = r*c, they are repeated cyclically; if A has more elements, only the first n are used.
gretl_matrix * gretl_matrix_trim_rows (const gretl_matrix *A,int ttop,int tbot,int *err);
Creates a new matrix which is a copy of A
with ttop
rows
trimmed from the top and tbot
rows trimmed from the
bottom.
gretl_matrix * gretl_matrix_minmax (const gretl_matrix *A,int mm,int rc,int idx,int *err);
Creates a matrix holding the row or column mimima or
maxima from A
, either as values or as location indices.
For example, if mm
= 0, rc
= 0, and idx
= 0, the
created matrix is m x 1 and holds the values of the row
minima.
gretl_matrix * gretl_matrix_pca (const gretl_matrix *X,int p,gretlopt opt,int *err);
Carries out a Principal Components analysis of X
and
returns the first p
components: the component corresponding
to the largest eigenvalue of the correlation matrix of X
is placed in column 1, and so on.
gretl_matrix * gretl_matrix_xtab (int t1,int t2,const double *x,const double *y,int *err);
Computes the cross tabulation of the values contained in the vectors x (by row) and y (by column). These must be integer values.
gretl_matrix * matrix_matrix_xtab (const gretl_matrix *x,const gretl_matrix *y,int *err);
Computes the cross tabulation of the values contained in the vectors x (by row) and y (by column). These must be integer values.
gretl_matrix * gretl_matrix_bool_sel (const gretl_matrix *A,const gretl_matrix *sel,int rowsel,int *err);
If rowsel
= 1, constructs a matrix which contains the rows
of A corresponding to non-zero values in the vector sel
;
if rowsel
= 0, does the same thing but column-wise.
gretl_matrix * gretl_matrix_sort_by_column (const gretl_matrix *m,int k,int *err);
Produces a matrix which contains the rows of m
, re-
ordered by increasing value of the elements in column
k
.
gretl_matrix * gretl_vector_sort (const gretl_matrix *v,int descending,int *err);
gretl_matrix * gretl_matrix_covariogram (const gretl_matrix *X,const gretl_matrix *u,const gretl_matrix *w,int p,int *err);
Produces the matrix covariogram,
\sum_{j=-p}^{p} \sum_j w_{|j|} (X_t' u_t u_{t-j} X_{t-j})
If u
is not given the u terms are omitted, and if w
is not given, all the weights are 1.0.
void gretl_matrix_transcribe_obs_info (gretl_matrix *targ,const gretl_matrix *src);
If targ
and src
have the same number of rows, and if
the rows of src
are identified by observation stamps
while those of targ
are not so identified, copy the
stamp information across to targ
. (Or if the given
conditions are not satified, do nothing.)
int gretl_matrix_set_colnames (gretl_matrix *m,char **S);
Sets an array of strings on m
which can be retrieved
using gretl_matrix_get_colnames(). Note that S
must
contain as many strings as m
has columns. The matrix
takes ownership of S
, which should be allocated and
not subsequently touched by the caller.
int gretl_matrix_set_rownames (gretl_matrix *m,char **S);
Sets an array of strings on m
which can be retrieved
using gretl_matrix_get_rownames(). Note that S
must
contain as many strings as m
has rows. The matrix
takes ownership of S
, which should be allocated and
not subsequently touched by the caller.
const char **
gretl_matrix_get_colnames (const gretl_matrix *m);
The array of strings set on m
using
gretl_matrix_set_colnames(), or NULL if no such
strings have been set. The returned array will
contain as many strings as m
has columns.
const char **
gretl_matrix_get_rownames (const gretl_matrix *m);
The array of strings set on m
using
gretl_matrix_set_rownames(), or NULL if no such
strings have been set. The returned array will
contain as many strings as m
has rows.
void
lapack_mem_free (void);
Cleanup function, called by libgretl_cleanup(). Frees
any memory that has been allocated internally as
temporary workspace for LAPACK functions.
void
set_blas_mnk_min (int mnk);
By default all matrix multiplication within libgretl is done using native code, but there is the possibility of farming out multiplication to the BLAS.
When multiplying an m x n matrix into an n x k matrix libgretl finds the product of the dimensions, m*n*k, and compares this with an internal threshhold variable, blas_mnk_min. If and only if blas_mnk_min >= 0 and n*m*k >= blas_mnk_min, then we use the BLAS. By default blas_mnk_min is set to -1 (BLAS never used).
If you have an optimized version of the BLAS you may want to set blas_mnk_min to some suitable positive value. (Setting it to 0 would result in external calls to the BLAS for all matrix multiplications, however small, which is unlikely to be optimal.)
typedef struct {
int rows;
int cols;
double *val;
} gretl_matrix;
The basic libgretl matrix type; gretl_vector is an alias
that can be used for matrices with rows
or cols
= 1.