gretl_matrix

gretl_matrix —

Synopsis




#define     R_DIAG_MIN
enum        GretlMatrixMod;
enum        GretlMatrixStructure;
            gretl_matrix;
typedef     gretl_vector;
#define     gretl_matrix_get                (m,i,j)
#define     gretl_vector_get                (v,i)
#define     gretl_matrix_set                (m,i,j,x)
#define     gretl_vector_set                (v,i,x)
#define     gretl_matrix_cols               (m)
#define     gretl_matrix_rows               (m)
#define     gretl_vector_get_length         (v)
#define     gretl_vector_alloc              (i)
#define     gretl_column_vector_alloc       (i)
#define     gretl_vector_free               (v)
#define     gretl_matrix_is_scalar          (m)
int         gretl_matrix_is_symmetric       (const gretl_matrix *m);
int         gretl_matrix_is_idempotent      (const gretl_matrix *m);
void        gretl_matrix_xtr_symmetric      (gretl_matrix *m);
void        gretl_matrix_set_equals_tolerance
                                            (double tol);
void        gretl_matrix_unset_equals_tolerance
                                            (void);
gretl_matrix* gretl_matrix_alloc            (int rows,
                                             int cols);
gretl_matrix* gretl_matrix_reuse            (gretl_matrix *m,
                                             int rows,
                                             int cols);
int         gretl_matrix_realloc            (gretl_matrix *m,
                                             int rows,
                                             int cols);
gretl_matrix* gretl_identity_matrix_new     (int n);
gretl_matrix* gretl_zero_matrix_new         (int r,
                                             int c);
gretl_matrix* gretl_unit_matrix_new         (int r,
                                             int c);
gretl_matrix* gretl_null_matrix_new         (void);
gretl_matrix* gretl_matrix_seq              (int start,
                                             int end);
gretl_matrix* gretl_matrix_copy             (const gretl_matrix *m);
int         gretl_matrix_inscribe_I         (gretl_matrix *m,
                                             int row,
                                             int col,
                                             int n);
gretl_matrix* gretl_matrix_copy_transpose   (const gretl_matrix *m);
gretl_matrix* gretl_matrix_get_diagonal     (const gretl_matrix *m,
                                             int *err);
double      gretl_matrix_trace              (const gretl_matrix *m,
                                             int *err);
int         gretl_matrix_random_fill        (gretl_matrix *m,
                                             int dist);
gretl_matrix* gretl_random_matrix_new       (int r,
                                             int c,
                                             int dist);
double      gretl_vector_mean               (const gretl_vector *v);
double      gretl_vector_variance           (const gretl_vector *v);
void        gretl_matrix_zero               (gretl_matrix *m);
int         gretl_matrix_zero_upper         (gretl_matrix *m);
int         gretl_matrix_zero_lower         (gretl_matrix *m);
void        gretl_matrix_fill               (gretl_matrix *m,
                                             double x);
void        gretl_matrix_multiply_by_scalar (gretl_matrix *m,
                                             double x);
int         gretl_matrix_divide_by_scalar   (gretl_matrix *m,
                                             double x);
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);
gretl_matrix* gretl_matrix_exp              (const gretl_matrix *m,
                                             int *err);
void        gretl_matrix_raise              (gretl_matrix *m,
                                             double x);
void        gretl_matrix_free               (gretl_matrix *m);
double*     gretl_matrix_steal_data         (gretl_matrix *m);
int         gretl_matrix_copy_values        (gretl_matrix *targ,
                                             const gretl_matrix *src);
int         gretl_matrix_add_to             (gretl_matrix *targ,
                                             const gretl_matrix *src);
int         gretl_matrix_add_transpose_to   (gretl_matrix *targ,
                                             const gretl_matrix *src);
int         gretl_matrix_subtract_from      (gretl_matrix *targ,
                                             const gretl_matrix *src);
int         gretl_matrix_I_minus            (gretl_matrix *m);
int         gretl_matrix_transpose          (gretl_matrix *targ,
                                             const gretl_matrix *src);
int         gretl_square_matrix_transpose   (gretl_matrix *m);
int         gretl_matrix_add_self_transpose (gretl_matrix *m);
int         gretl_matrix_vectorize          (gretl_matrix *targ,
                                             const gretl_matrix *src);
int         gretl_matrix_unvectorize        (gretl_matrix *targ,
                                             const gretl_matrix *src);
int         gretl_matrix_vectorize_h        (gretl_matrix *targ,
                                             const gretl_matrix *src);
int         gretl_matrix_unvectorize_h      (gretl_matrix *targ,
                                             const gretl_matrix *src);
int         gretl_matrix_inscribe_matrix    (gretl_matrix *targ,
                                             const gretl_matrix *src,
                                             int row,
                                             int col,
                                             GretlMatrixMod mod);
int         gretl_matrix_extract_matrix     (gretl_matrix *targ,
                                             const gretl_matrix *src,
                                             int row,
                                             int col,
                                             GretlMatrixMod mod);
int         gretl_matrix_multiply_mod       (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);
int         gretl_matrix_kronecker_product  (const gretl_matrix *A,
                                             const gretl_matrix *B,
                                             gretl_matrix *K);
gretl_matrix* gretl_matrix_kronecker_product_new
                                            (const gretl_matrix *A,
                                             const gretl_matrix *B);
gretl_matrix* gretl_matrix_pow              (const gretl_matrix *A,
                                             int s,
                                             int *err);
double      gretl_matrix_dot_product        (const gretl_matrix *a,
                                             GretlMatrixMod amod,
                                             const gretl_matrix *b,
                                             GretlMatrixMod bmod,
                                             int *errp);
double      gretl_vector_dot_product        (const gretl_vector *a,
                                             const gretl_vector *b,
                                             int *errp);
gretl_matrix* gretl_matrix_row_sum          (const gretl_matrix *m);
gretl_matrix* gretl_matrix_column_sum       (const gretl_matrix *m);
gretl_matrix* gretl_matrix_row_mean         (const gretl_matrix *m);
gretl_matrix* gretl_matrix_column_mean      (const gretl_matrix *m);
double      gretl_matrix_row_i_mean         (const gretl_matrix *m,
                                             int row);
double      gretl_matrix_column_j_mean      (const gretl_matrix *m,
                                             int col);
void        gretl_matrix_demean_by_row      (gretl_matrix *m);
void        gretl_matrix_demean_by_column   (gretl_matrix *m);
gretl_matrix* gretl_matrix_vcv              (gretl_matrix *m);
double      gretl_matrix_determinant        (gretl_matrix *a,
                                             int *err);
double      gretl_matrix_log_determinant    (gretl_matrix *a,
                                             int *err);
double      gretl_matrix_log_abs_determinant
                                            (gretl_matrix *a,
                                             int *err);
double      gretl_vcv_log_determinant       (const gretl_matrix *m);
double      gretl_matrix_one_norm           (const gretl_matrix *m);
double      gretl_matrix_infinity_norm      (const gretl_matrix *m);
int         gretl_LU_solve                  (gretl_matrix *a,
                                             gretl_vector *b);
int         gretl_cholesky_decomp_solve     (gretl_matrix *a,
                                             gretl_vector *b);
int         gretl_cholesky_solve            (const gretl_matrix *a,
                                             gretl_vector *b);
int         gretl_inverse_from_cholesky_decomp
                                            (gretl_matrix *targ,
                                             const gretl_matrix *src);
int         gretl_invert_general_matrix     (gretl_matrix *a);
int         gretl_invert_symmetric_indef_matrix
                                            (gretl_matrix *a);
int         gretl_invert_symmetric_matrix   (gretl_matrix *a);
int         gretl_invert_symmetric_matrix2  (gretl_matrix *a,
                                             double *ldet);
int         gretl_invert_packed_symmetric_matrix
                                            (gretl_matrix *v);
int         gretl_invert_triangular_matrix  (gretl_matrix *a,
                                             char uplo);
int         gretl_invert_diagonal_matrix    (gretl_matrix *a);
int         gretl_invert_matrix             (gretl_matrix *a);
int         gretl_SVD_invert_matrix         (gretl_matrix *a);
int         gretl_matrix_SVD                (const gretl_matrix *a,
                                             gretl_matrix **pu,
                                             gretl_vector **ps,
                                             gretl_matrix **pvt);
double      gretl_symmetric_matrix_rcond    (const gretl_matrix *m,
                                             int *err);
int         gretl_general_eigen_sort        (gretl_matrix *evals,
                                             gretl_matrix *evecs,
                                             int rank);
int         gretl_symmetric_eigen_sort      (gretl_matrix *evals,
                                             gretl_matrix *evecs,
                                             int rank);
gretl_matrix* gretl_general_matrix_eigenvals
                                            (gretl_matrix *m,
                                             int eigenvecs,
                                             int *err);
gretl_matrix* gretl_symmetric_matrix_eigenvals
                                            (gretl_matrix *m,
                                             int eigenvecs,
                                             int *err);
gretl_matrix* gretl_gensymm_eigenvals       (const gretl_matrix *A,
                                             const gretl_matrix *B,
                                             gretl_matrix *V,
                                             int *err);
gretl_matrix* gretl_matrix_right_nullspace  (const gretl_matrix *M,
                                             int *err);
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);
int         gretl_matrix_inplace_colcat     (gretl_matrix *a,
                                             const gretl_matrix *b,
                                             const char *mask);
gretl_matrix* gretl_matrix_lag              (const gretl_matrix *m,
                                             int k,
                                             double missval);
int         gretl_matrix_inplace_lag        (gretl_matrix *targ,
                                             const gretl_matrix *src,
                                             int k);
int         gretl_matrix_cholesky_decomp    (gretl_matrix *a);
int         gretl_matrix_QR_decomp          (gretl_matrix *M,
                                             gretl_matrix *R);
int         gretl_check_QR_rank             (const gretl_matrix *R,
                                             int *err);
int         gretl_matrix_rank               (const gretl_matrix *a,
                                             int *err);
int         gretl_matrix_ols                (const gretl_vector *y,
                                             const gretl_matrix *X,
                                             gretl_vector *b,
                                             gretl_matrix *vcv,
                                             gretl_vector *uhat,
                                             double *s2);
double      gretl_matrix_r_squared          (const gretl_matrix *y,
                                             const gretl_matrix *X,
                                             const gretl_matrix *b,
                                             int *err);
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);
int         gretl_matrix_svd_ols            (const gretl_vector *y,
                                             const gretl_matrix *X,
                                             gretl_vector *b,
                                             gretl_matrix *vcv,
                                             gretl_vector *uhat,
                                             double *s2);
int         gretl_matrix_qform              (const gretl_matrix *A,
                                             GretlMatrixMod amod,
                                             const gretl_matrix *X,
                                             gretl_matrix *C,
                                             GretlMatrixMod cmod);
double      gretl_scalar_qform              (const gretl_vector *b,
                                             const gretl_matrix *X,
                                             int *errp);
int         gretl_matrix_columnwise_product (const gretl_matrix *A,
                                             const gretl_matrix *B,
                                             gretl_matrix *C);
int         gretl_matrix_diagonal_sandwich  (const gretl_vector *d,
                                             const gretl_matrix *X,
                                             gretl_matrix *DXD);
void        gretl_matrix_print_to_prn       (const gretl_matrix *m,
                                             const char *msg,
                                             PRN *prn);
void        gretl_matrix_print              (const gretl_matrix *m,
                                             const char *msg);
void        gretl_packed_matrix_print       (const gretl_matrix *m,
                                             const char *msg);
void        debug_print_matrix              (const gretl_matrix *m,
                                             const char *msg);
void        gretl_matrix_set_t1             (gretl_matrix *m,
                                             int t);
void        gretl_matrix_set_t2             (gretl_matrix *m,
                                             int t);
int         gretl_matrix_get_t1             (const gretl_matrix *m);
int         gretl_matrix_get_t2             (const gretl_matrix *m);
int         gretl_is_identity_matrix        (const gretl_matrix *m);
int         gretl_is_zero_matrix            (const gretl_matrix *m);
int         gretl_matrix_get_structure      (const gretl_matrix *m);
int         gretl_matrices_are_equal        (const gretl_matrix *a,
                                             const gretl_matrix *b,
                                             int *err);
gretl_matrix* gretl_covariance_matrix       (const gretl_matrix *m,
                                             int corr,
                                             int *errp);
gretl_matrix** gretl_matrix_array_alloc     (int n);
gretl_matrix** gretl_matrix_array_alloc_with_size
                                            (int n,
                                             int rows,
                                             int cols);
void        gretl_matrix_array_free         (gretl_matrix **A,
                                             int n);
gretl_matrix* gretl_matrix_values           (const double *x,
                                             int n,
                                             int *err);
gretl_matrix* gretl_matrix_shape            (const gretl_matrix *A,
                                             int r,
                                             int c);
gretl_matrix* gretl_matrix_minmax           (const gretl_matrix *A,
                                             int mm,
                                             int rc,
                                             int idx,
                                             int *err);
gretl_matrix* gretl_matrix_pca              (const gretl_matrix *X,
                                             int p,
                                             int *err);
void        lapack_mem_free                 (void);

Description

Details

R_DIAG_MIN

#define R_DIAG_MIN 1.0e-8


enum GretlMatrixMod

typedef enum {
    GRETL_MOD_NONE = 0,
    GRETL_MOD_TRANSPOSE,
    GRETL_MOD_SQUARE,
    GRETL_MOD_CUMULATE,
    GRETL_MOD_DECUMULATE
} GretlMatrixMod;


enum GretlMatrixStructure

typedef enum {
    GRETL_MATRIX_SQUARE = 1,
    GRETL_MATRIX_LOWER_TRIANGULAR,
    GRETL_MATRIX_UPPER_TRIANGULAR,
    GRETL_MATRIX_SYMMETRIC,
    GRETL_MATRIX_DIAGONAL,
    GRETL_MATRIX_SCALAR
} GretlMatrixStructure;


gretl_matrix

typedef struct {
    int rows;
    int cols;
    int t1, t2;
    double *val;
} gretl_matrix;


gretl_vector

typedef struct _gretl_matrix gretl_vector;


gretl_matrix_get()

#define gretl_matrix_get(m,i,j) (m->val[(j)*m->rows+(i)])

Sets the i, j element of m to x.

m : matrix.
i : row. column.
j :
Returns :

gretl_vector_get()

#define gretl_vector_get(v,i) (v->val[i])

Gives element i of v.

v : vector.
i : index.
Returns :

gretl_matrix_set()

#define gretl_matrix_set(m,i,j,x) (m->val[(j)*m->rows+(i)]=x)

m :
i :
j :
x :
Returns :

gretl_vector_set()

#define gretl_vector_set(v,i,x) (v->val[i]=x)

Sets element i of v to x.

v : vector.
i : index.
x : value to set.
Returns :

gretl_matrix_cols()

#define gretl_matrix_cols(m) ((m == NULL)? 0 : m->cols)

Gives the number of columns in m.

m : matrix to query.

gretl_matrix_rows()

#define gretl_matrix_rows(m) ((m == NULL)? 0 : m->rows)

Gives the number of rows in m.

m : matrix to query.

gretl_vector_get_length()

#define     gretl_vector_get_length(v)

Gives the length of vector v (without regard to whether it is a row or column vector).

v : vector to examine.

gretl_vector_alloc()

#define gretl_vector_alloc(i) gretl_matrix_alloc(1,(i))

Allocates a new gretl_vector with i columns.

i : number of columns.

gretl_column_vector_alloc()

#define gretl_column_vector_alloc(i) gretl_matrix_alloc((i),1)

Allocates a new column gretl_vector with i rows.

i : number of rows.

gretl_vector_free()

#define gretl_vector_free(v) gretl_matrix_free(v)

Frees the vector v and its associated storage.

v : gretl_vector to free.

gretl_matrix_is_scalar()

#define     gretl_matrix_is_scalar(m)

Gives 1 if m is 1 x 1, else 0.

m : matrix to test.

gretl_matrix_is_symmetric ()

int         gretl_matrix_is_symmetric       (const gretl_matrix *m);

m : gretl_matrix.
Returns : 1 if m is symmetric (with a small relative tolerance for asymmetry), otherwise 0.

gretl_matrix_is_idempotent ()

int         gretl_matrix_is_idempotent      (const gretl_matrix *m);

m : gretl_matrix.
Returns : 1 if m is idempotent, otherwise 0.

gretl_matrix_xtr_symmetric ()

void        gretl_matrix_xtr_symmetric      (gretl_matrix *m);

Computes the symmetric part of m by averaging its off-diagonal elements.

m : gretl_matrix.

gretl_matrix_set_equals_tolerance ()

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.5e-12.

tol : tolerance value.

gretl_matrix_unset_equals_tolerance ()

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_alloc ()

gretl_matrix* gretl_matrix_alloc            (int rows,
                                             int cols);

rows : desired number of rows in matrix.
cols : desired number of columns.
Returns : pointer to a newly allocated gretl_matrix, or NULL on failure. Note that the actual data storage is not initialized.

gretl_matrix_reuse ()

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 the when the matrix is to be freed, gretl_matrix_free() should be applied only once.

m : matrix to reuse.
rows : desired number of rows in "new" matrix, or -1 to leave the current value unchanged.
cols : desired number of columns in "new" matrix, or -1 to leave the current value unchanged.
Returns : pointer to the "resized" gretl_matrix.

gretl_matrix_realloc ()

int         gretl_matrix_realloc            (gretl_matrix *m,
                                             int rows,
                                             int cols);

Reallocates the storage in m to the specified dimensions.

m : matrix to reallocate.
rows : desired number of rows in "new" matrix.
cols : desired number of columns in "new" matrix.
Returns : 0 on success, E_ALLOC on failure.

gretl_identity_matrix_new ()

gretl_matrix* gretl_identity_matrix_new     (int n);

n : desired number of rows and columns in the matrix.
Returns : pointer to a newly allocated identity matrix, or NULL on failure.

gretl_zero_matrix_new ()

gretl_matrix* gretl_zero_matrix_new         (int r,
                                             int c);

r : desired number of rows in the matrix.
c : desired number of columns in the matrix.
Returns : pointer to a newly allocated zero matrix, or NULL on failure.

gretl_unit_matrix_new ()

gretl_matrix* gretl_unit_matrix_new         (int r,
                                             int c);

r : desired number of rows in the matrix.
c : desired number of columns in the matrix.
Returns : pointer to a newly allocated matrix, all of whose elements equal 1, or NULL on failure.

gretl_null_matrix_new ()

gretl_matrix* gretl_null_matrix_new         (void);

Returns : pointer to a newly allocated null matrix (for use in declaration of a variable as a matrix), NULL on failure.

gretl_matrix_seq ()

gretl_matrix* gretl_matrix_seq              (int start,
                                             int end);

start: first element. end: last element.

start :
end :
Returns : pointer to a row vector, containing the numbers from start to end, in decreasing order if start > end -- or NULL on failure.

gretl_matrix_copy ()

gretl_matrix* gretl_matrix_copy             (const gretl_matrix *m);

m : source matrix to be copied.
Returns : an allocated copy of matrix m, or NULL on failure.

gretl_matrix_inscribe_I ()

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.
Returns : 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_copy_transpose ()

gretl_matrix* gretl_matrix_copy_transpose   (const gretl_matrix *m);

m : source matrix to be copied.
Returns : an allocated copy of the tranpose of m, or NULL on failure.

gretl_matrix_get_diagonal ()

gretl_matrix* gretl_matrix_get_diagonal     (const gretl_matrix *m,
                                             int *err);

m : input matrix.
err : location to receive error code.
Returns : a column vector containing the diagonal elements of m, otherwise NULL. A non-zero value is assigned via err on failure.

gretl_matrix_trace ()

double      gretl_matrix_trace              (const gretl_matrix *m,
                                             int *err);

m : square input matrix.
err : location to receive error code.
Returns : the trace (sum of diagonal elements) of m, if m is square, otherwise NADBL.

gretl_matrix_random_fill ()

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.

m : input matrix.
dist : either D_UNIFORM or D_NORMAL.
Returns : 0 on success, 1 on failure.

gretl_random_matrix_new ()

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.

r : number of rows.
c : number of columns.
dist : either D_UNIFORM or D_NORMAL.
Returns : allocated matrix or NULL on failure.

gretl_vector_mean ()

double      gretl_vector_mean               (const gretl_vector *v);

v : input vector.
Returns : the arithmetic mean of the elements of v, or NADBL on failure.

gretl_vector_variance ()

double      gretl_vector_variance           (const gretl_vector *v);

v : input vector.
Returns : the variance of the elements of v, or NADBL on failure.

gretl_matrix_zero ()

void        gretl_matrix_zero               (gretl_matrix *m);

Sets all elements of m to zero.

m : matrix to be set to zero.

gretl_matrix_zero_upper ()

int         gretl_matrix_zero_upper         (gretl_matrix *m);

Sets the elements of m outside of the lower triangle to zero.

m : square matrix to operate on.
Returns : 0 on success, non-zero error code otherwise.

gretl_matrix_zero_lower ()

int         gretl_matrix_zero_lower         (gretl_matrix *m);

Sets the elements of m outside of the upper triangle to zero.

m : square matrix to operate on.
Returns : 0 on success, non-zero error code otherwise.

gretl_matrix_fill ()

void        gretl_matrix_fill               (gretl_matrix *m,
                                             double x);

Sets all entries in m to the value x.

m : matrix to fill.
x : value with which to fill.

gretl_matrix_multiply_by_scalar ()

void        gretl_matrix_multiply_by_scalar (gretl_matrix *m,
                                             double x);

Multiplies all elements of m by x.

m : matrix to operate on.
x : scalar by which to multiply.

gretl_matrix_divide_by_scalar ()

int         gretl_matrix_divide_by_scalar   (gretl_matrix *m,
                                             double x);

Divides all elements of m by x.

m : matrix to operate on.
x : scalar by which to divide.
Returns : 0 on success, 1 if x = 0.

gretl_matrix_dot_op ()

gretl_matrix* gretl_matrix_dot_op           (const gretl_matrix *a,
                                             const gretl_matrix *b,
                                             int op,
                                             int *err);

a : left-hand matrix.
b : right-hand matrix.
op : operator.
err : location to receive error code.
Returns : a new matrix, each of whose elements is the result of (x op y), where x and y are the corresponding elements of the matrices a and b (or NULL on failure).

gretl_matrix_complex_multiply ()

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.

a : m x (1 or 2) matrix.
b : m x (1 or 2) matrix.
err : location to receive error code.
Returns : an m x 2 matrix with the result of the multiplication of the two vectors of complex numbers. If both a and b have no imaginary part, the return value will be m x 1. Or NULL on failure.

gretl_matrix_exp ()

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.

m : square matrix to operate on.
err : location to receive error code.
Returns : the exponential, or NULL on failure.

gretl_matrix_raise ()

void        gretl_matrix_raise              (gretl_matrix *m,
                                             double x);

Raises each element of m to the power x.

m : matrix to operate on.
x : exponent.

gretl_matrix_free ()

void        gretl_matrix_free               (gretl_matrix *m);

Frees the allocated storage in m, then m itself.

m : matrix to be freed.

gretl_matrix_steal_data ()

double*     gretl_matrix_steal_data         (gretl_matrix *m);

"Steals" the allocated data from m, which is left with a NULL data pointer.

m : matrix to operate on.
Returns : a pointer to the "stolen" data.

gretl_matrix_copy_values ()

int         gretl_matrix_copy_values        (gretl_matrix *targ,
                                             const gretl_matrix *src);

Copies the elements of src into the corresponding elements of targ.

targ : target matrix.
src : source matrix.
Returns : 0 on successful completion, or E_NONCONF if the two matrices are not conformable for the operation.

gretl_matrix_add_to ()

int         gretl_matrix_add_to             (gretl_matrix *targ,
                                             const gretl_matrix *src);

Adds the elements of src to the corresponding elements of targ.

targ : target matrix.
src : source matrix.
Returns : 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.

gretl_matrix_add_transpose_to ()

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.

targ : target matrix.
src : source matrix.
Returns : 0 on successful completion, or E_NONCONF if the two matrices are not conformable for the operation.

gretl_matrix_subtract_from ()

int         gretl_matrix_subtract_from      (gretl_matrix *targ,
                                             const gretl_matrix *src);

Subtracts the elements of src from the corresponding elements of targ.

targ : target matrix.
src : source matrix.
Returns : 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.

gretl_matrix_I_minus ()

int         gretl_matrix_I_minus            (gretl_matrix *m);

Rewrites m as (I - m), where I is the n x n identity matrix.

m : original square matrix, n x n.
Returns : 0 on successful completion, or E_NONCONF if m is not square.

gretl_matrix_transpose ()

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.

targ : target matrix.
src : source matrix.
Returns : 0 on success, non-zero error code otherwise.

gretl_square_matrix_transpose ()

int         gretl_square_matrix_transpose   (gretl_matrix *m);

Transposes the matrix m.

m : square matrix to operate on.
Returns : 0 on success, non-zero error code otherwise.

gretl_matrix_add_self_transpose ()

int         gretl_matrix_add_self_transpose (gretl_matrix *m);

Adds the transpose of m to m itself, yielding a symmetric matrix.

m : (square) matrix to operate on.
Returns : 0 on successful completion, or 1 if the source matrix is not square.

gretl_matrix_vectorize ()

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.

targ : target vector, (m * n) x 1.
src : source matrix, m x n.
Returns : 0 on successful completion, or E_NONCONF if targ is not correctly dimensioned.

gretl_matrix_unvectorize ()

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).

targ : target matrix, m x n.
src : source vector, (m * n) x 1.
Returns : 0 on successful completion, or E_NONCONF if targ is not correctly dimensioned.

gretl_matrix_vectorize_h ()

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.

targ : target vector, (m * (m+1)/2) x 1.
src : source square matrix, m x m.
Returns : 0 on successful completion, or E_NONCONF if targ is not correctly dimensioned.

gretl_matrix_unvectorize_h ()

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.

targ : target matrix, n x n.
src : source vector, m x 1.
Returns : 0 on successful completion, or E_NONCONF if targ is not correctly dimensioned.

gretl_matrix_inscribe_matrix ()

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 GRETL_MOD_TRANSPOSE or GRETL_MOD_NONE.
Returns : 0 on success, E_NONCONF if the matrices are not conformable for the operation.

gretl_matrix_extract_matrix ()

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 GRETL_MOD_TRANSPOSE or GRETL_MOD_NONE.
Returns : 0 on success, E_NONCONF if the matrices are not conformable for the operation.

gretl_matrix_multiply_mod ()

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: GRETL_MOD_NONE or GRETL_MOD_TRANSPOSE.
b : right-hand matrix.
bmod : modifier: GRETL_MOD_NONE or GRETL_MOD_TRANSPOSE.
c : matrix to hold the product.
cmod : modifier: GRETL_MOD_NONE or GRETL_MOD_CUMULATE to add the result to the existing value of c.
Returns : 0 on success; non-zero error code on failure.

gretl_matrix_multiply ()

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.

a : left-hand matrix.
b : right-hand matrix.
c : matrix to hold the product.
Returns : 0 on success; non-zero error code on failure.

gretl_matrix_kronecker_product ()

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.

A : left-hand matrix, p x q.
B : right-hand matrix, r x s.
K : target matrix, (p * r) x (q * s).
Returns : 0 on success, E_NONCONF if matrix K is not correctly dimensioned for the operation.

gretl_matrix_kronecker_product_new ()

gretl_matrix* gretl_matrix_kronecker_product_new
                                            (const gretl_matrix *A,
                                             const gretl_matrix *B);

A : left-hand matrix, p x q.
B : right-hand matrix, r x s.
Returns : A newly allocated (p * r) x (q * s) matrix which is the Kronecker product of matrices A and B, or NULL on failure.

gretl_matrix_pow ()

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").

A : square source matrix.
s : exponent >= 0.
err : location to receive error code.
Returns : allocated matrix, or NULL on failure.

gretl_matrix_dot_product ()

double      gretl_matrix_dot_product        (const gretl_matrix *a,
                                             GretlMatrixMod amod,
                                             const gretl_matrix *b,
                                             GretlMatrixMod bmod,
                                             int *errp);

a : left-hand matrix.
amod : modifier: GRETL_MOD_NONE or GRETL_MOD_TRANSPOSE.
b : right-hand matrix.
bmod : modifier: GRETL_MOD_NONE or GRETL_MOD_TRANSPOSE.
errp : pointer to receive error code (zero on success, non-zero on failure), or NULL.
Returns : The dot (scalar) product of a (or a-transpose) and b (or b-transpose), or NADBL on failure.

gretl_vector_dot_product ()

double      gretl_vector_dot_product        (const gretl_vector *a,
                                             const gretl_vector *b,
                                             int *errp);

a : first vector.
b : second vector.
errp : pointer to receive error code (zero on success, non-zero on failure), or NULL.
Returns : The dot (scalar) product of a and b, or NADBL on failure.

gretl_matrix_row_sum ()

gretl_matrix* gretl_matrix_row_sum          (const gretl_matrix *m);

m : source matrix.
Returns : a column vector containing the sums of the rows of m, or NULL on failure.

gretl_matrix_column_sum ()

gretl_matrix* gretl_matrix_column_sum       (const gretl_matrix *m);

m : source matrix.
Returns : a row vector containing the sums of the columns of m, or NULL on failure.

gretl_matrix_row_mean ()

gretl_matrix* gretl_matrix_row_mean         (const gretl_matrix *m);

m : source matrix.
Returns : a column vector containing the means of the rows of m, or NULL on failure.

gretl_matrix_column_mean ()

gretl_matrix* gretl_matrix_column_mean      (const gretl_matrix *m);

m : source matrix.
Returns : a row vector containing the means of the columns of m, or NULL on failure.

gretl_matrix_row_i_mean ()

double      gretl_matrix_row_i_mean         (const gretl_matrix *m,
                                             int row);

m : source matrix.
row : zero-based index of row.
Returns : the mean of the elements in row row of m, or NADBL if the row is out of bounds.

gretl_matrix_column_j_mean ()

double      gretl_matrix_column_j_mean      (const gretl_matrix *m,
                                             int col);

m : source matrix.
col : zero-based index of column.
Returns : the mean of the elements in column col of m, or NADBL if the column is out of bounds.

gretl_matrix_demean_by_row ()

void        gretl_matrix_demean_by_row      (gretl_matrix *m);

For each row of m, subtracts the row mean from each element on the row.

m : matrix on which to operate.

gretl_matrix_demean_by_column ()

void        gretl_matrix_demean_by_column   (gretl_matrix *m);

For each column of m, subtracts the column mean from each element in the column.

m : matrix on which to operate.

gretl_matrix_vcv ()

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.

m : source matrix (expected to have rows >= cols).
Returns : the allocated variance-covariance matrix, or NULL on failure. Note that on return the column means have been subtracted from m.

gretl_matrix_determinant ()

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.

a : gretl_matrix.
err : location to receive error code.
Returns : the determinant, or NABDL on failure.

gretl_matrix_log_determinant ()

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.

a : gretl_matrix.
err : location to receive error code.
Returns : the determinant, or NABDL on failure.

gretl_matrix_log_abs_determinant ()

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.

<
a : gretl_matrix.
err : location to receive error code.
Returns : the determinant, or NABDL on failure.