Top |
This object defines the functions for allocating and accessing matrices. Also includes several matrix operations.
NcmMatrix * ncm_matrix_new (const guint nrows
,const guint ncols
);
This function allocates memory for a new NcmMatrix of doubles
with nrows
rows and ncols
columns.
NcmMatrix * ncm_matrix_new0 (const guint nrows
,const guint ncols
);
This function allocates memory for a new NcmMatrix of doubles
with nrows
rows and ncols
columns and sets all elements to zero.
NcmMatrix * ncm_matrix_new_full (gdouble *d
,guint nrows
,guint ncols
,guint tda
,gpointer pdata
,GDestroyNotify pfree
);
This function allocates memory for a new NcmMatrix of doubles
with nrows
rows and ncols
columns.
NcmMatrix *
ncm_matrix_new_gsl (gsl_matrix *gm
);
This function saves gm
internally and frees it when it is no longer necessary.
The gm
matrix must not be freed.
[skip]
NcmMatrix *
ncm_matrix_new_gsl_static (gsl_matrix *gm
);
This function saves gm
internally and does not frees it.
The gm
matrix must be valid during the life of the created NcmMatrix.
[skip]
NcmMatrix * ncm_matrix_new_array (GArray *a
,const guint ncols
);
The number of rows is defined dividing the lenght of a
by ncols
.
This function saves a
internally and frees it when it is no longer necessary.
The GArray a
must not be freed.
a |
GArray of doubles to be converted into a NcmMatrix. |
[array][element-type double] |
ncols |
number of columns |
NcmMatrix * ncm_matrix_new_data_slice (gdouble *d
,const guint nrows
,const guint ncols
);
This function returns a NcmMatrix of the array d
allocated using g_slice function.
It saves d
internally and frees it when it is no longer necessary.
The matrix has nrows
rows and ncols
columns.
The physical number of columns in memory is also given by ncols
.
[skip]
NcmMatrix * ncm_matrix_new_data_malloc (gdouble *d
,const guint nrows
,const guint ncols
);
This function returns a NcmMatrix of the array d
allocated using malloc.
It saves d
internally and frees it when it is no longer necessary.
[skip]
NcmMatrix * ncm_matrix_new_data_static (gdouble *d
,const guint nrows
,const guint ncols
);
This function returns a NcmMatrix of the array d
.
The memory allocated is kept during all time life of the object and
must not be freed during this period.
[skip]
NcmMatrix * ncm_matrix_new_data_static_tda (gdouble *d
,const guint nrows
,const guint ncols
,const guint tda
);
This function returns a NcmMatrix of the array d
with a physical number of columns tda which may differ
from the corresponding dimension of the matrix. The matrix has nrows
rows and ncols
columns, and the physical
number of columns in memory is given by tda.
[skip]
NcmMatrix *
ncm_matrix_new_variant (GVariant *var
);
Creates a new matrix using the values from var
.
NcmMatrix *
ncm_matrix_ref (NcmMatrix *cm
);
Increase the reference count of cm
by one.
const NcmMatrix * ncm_matrix_const_new_data (const gdouble *d
,guint nrows
,guint ncols
);
This function returns a constant NcmMatrix of the array d
.
The memory allocated is kept during all time life of the object and
must not be freed during this period.
const NcmMatrix *
ncm_matrix_const_new_variant (GVariant *var
);
Creates a new constant matrix using the same memory of var
.
NcmMatrix * ncm_matrix_get_submatrix (NcmMatrix *cm
,const guint k1
,const guint k2
,const guint nrows
,const guint ncols
);
This function returns a submatrix NcmMatrix of the matrix cm
.
The upper-left element of the submatrix is the element (k1
,k2
) of the original matrix.
The submatrix has nrows
rows and ncols
columns.
NcmVector * ncm_matrix_get_col (NcmMatrix *cm
,const guint col
);
This function returns the elements of the col
column of the matrix cm
into a NcmVector.
NcmVector * ncm_matrix_get_row (NcmMatrix *cm
,const guint row
);
This function returns the elements of the row
row of the matrix cm
into a NcmVector.
NcmVector *
ncm_matrix_as_vector (NcmMatrix *cm
);
Creates a vector containing the row-wise concatenation
of the matrix cm
. It requires a matrix with tda==ncols.
void ncm_matrix_set_from_variant (NcmMatrix *cm
,GVariant *var
);
This function sets the values of cm
using the variant var
.
GVariant *
ncm_matrix_get_variant (NcmMatrix *cm
);
This function gets a variant of values taken from cm
.
GVariant *
ncm_matrix_peek_variant (NcmMatrix *cm
);
This function gets a variant of values taken from cm
using the same memory.
The matrix cm
should not be modified during the variant existance.
void ncm_matrix_set_from_data (NcmMatrix *cm
,gdouble *data
);
This function sets the valuus of cm
using data
. Data
must have the same size as NcmMatrix.
void ncm_matrix_set_from_array (NcmMatrix *cm
,GArray *a
);
This function sets the valuus of cm
using data
. Data
must have the same size as NcmMatrix.
const NcmMatrix *
ncm_matrix_new_gsl_const (gsl_matrix *gm
);
This function converts gm
into a constant NcmMatrix.
[skip]
gdouble ncm_matrix_get_colmajor (const NcmMatrix *cm
,const guint i
,const guint j
);
Gets the (i
,j
)-th component of cm
assuming
a column-major order.
All column-major methods should be used carefully, they are inconsistent with most other methods and are used mainly to interface with Fortran sub-routines.
const gdouble * ncm_matrix_const_ptr (const NcmMatrix *cm
,const guint i
,const guint j
);
GArray *
ncm_matrix_get_array (NcmMatrix *cm
);
This function returns the array of cv
. It is only applied if the matrix cm
was created with ncm_matrix_new_array()
.
GArray *
ncm_matrix_dup_array (NcmMatrix *cm
);
This function returns an GArray containing a copy of its elements..
void ncm_matrix_set (NcmMatrix *cm
,const guint i
,const guint j
,const gdouble val
);
This function sets the value of the (i
,j
)-th element of the matrix cm
to val
.
void ncm_matrix_set_colmajor (NcmMatrix *cm
,const guint i
,const guint j
,gdouble val
);
This function sets the value of the (i
,j
)-th element of the matrix cm
to val
considering it being in the column-major order.
All column-major methods should be used carefully, they are inconsistent with most other methods and are used mainly to interface with Fortran sub-routines.
void ncm_matrix_addto (NcmMatrix *cm
,const guint i
,const guint j
,const gdouble val
);
This function adds the value val
to the (i
,j
)-th element of the matrix cm
.
void
ncm_matrix_transpose (NcmMatrix *cm
);
This function replaces the matrix cm
by its transpose by copying the elements of the matrix in-place.
The matrix must be square for this operation to be possible.
void
ncm_matrix_set_identity (NcmMatrix *cm
);
This function sets the elements of the matrix cm
to the corresponding elements of the identity matrix,
i.e. a unit diagonal with all off-diagonal elements zero. This applies to both square and rectangular matrices.
void
ncm_matrix_set_zero (NcmMatrix *cm
);
This function sets all the elements of the matrix cm
to zero.
void ncm_matrix_set_all (NcmMatrix *cm
,const gdouble val
);
This function sets all the elements of the matrix cm
to val
.
void ncm_matrix_add (NcmMatrix *cm1
,const NcmMatrix *cm2
);
This function adds the elements of the matrices cm1
and cm2
.
The two matrices must have the same size.
void ncm_matrix_sub (NcmMatrix *cm1
,const NcmMatrix *cm2
);
This function subtracts the elements of the matrices cm1
and cm2
.
The two matrices must have the same size.
void ncm_matrix_mul_elements (NcmMatrix *cm1
,const NcmMatrix *cm2
);
This function multiplies the elements of the matrices cm1
and cm2
.
The two matrices must have the same size.
void ncm_matrix_div_elements (NcmMatrix *cm1
,const NcmMatrix *cm2
);
This function divides the elements of the matrices cm1
and cm2
.
The two matrices must have the same size.
void ncm_matrix_scale (NcmMatrix *cm
,const gdouble val
);
This function multiplies the elements of the matrix cm
by the constant factor val
.
The result is stored in cm
.
void ncm_matrix_add_constant (NcmMatrix *cm
,const gdouble val
);
This function adds the the constant factor val
to the elements of the matrix cm
.
The result is stored in cm
.
void ncm_matrix_mul_row (NcmMatrix *cm
,const guint row_i
,const gdouble val
);
This function multiplies row row_i
elements by val
.
void ncm_matrix_mul_col (NcmMatrix *cm
,const guint col_i
,const gdouble val
);
This function multiplies column col_i
elements by val
.
void ncm_matrix_get_diag (NcmMatrix *cm
,NcmVector *diag
);
This function copies de diagonal elements of the matrix cm
to the vector diag
.
void ncm_matrix_set_diag (NcmMatrix *cm
,NcmVector *diag
);
This function copies de the elements of the vector diag
to the diagonal elements of the matrix cm
.
void ncm_matrix_memcpy (NcmMatrix *cm1
,const NcmMatrix *cm2
);
This function copies the elements of the matrix cm2
into the matrix cm1
.
The two matrices must have the same size.
void ncm_matrix_memcpy_to_colmajor (NcmMatrix *cm1
,const NcmMatrix *cm2
);
This function copies the elements of the matrix cm2
into the matrix cm1
.
The two matrices must have the same size. The elements are written in cm1
in column-major order
order.
All column-major methods should be used carefully, they are inconsistent with most other methods and are used mainly to interface with Fortran sub-routines.
void ncm_matrix_set_col (NcmMatrix *cm
,const guint n
,const NcmVector *cv
);
This function copies the elements of the vector cv
into the n
-th column of the matrix cm
.
The length of the vector must be the same as the length of the column.
void ncm_matrix_set_row (NcmMatrix *cm
,const guint n
,const NcmVector *cv
);
This function copies the elements of the vector cv
into the n
-th row of the matrix cm
.
The length of the vector must be the same as the length of the row.
gdouble ncm_matrix_fast_get (NcmMatrix *cm
,const guint ij
);
This function returns the value of the cm
[i
,j
] element by direct access of its base data. Where ij
= i $\times$ tda
$+$ j.
If the matrix was created with ncm_matrix_new()
or ncm_matrix_new0()
then tda
= ncols
.
void ncm_matrix_fast_set (NcmMatrix *cm
,const guint ij
,const gdouble val
);
This function sets the value of the cm
[i
,j
] element to val
by direct access of its base data. Where ij
= i $\times$ tda
$+$ j.
If the matrix was created with ncm_matrix_new()
or ncm_matrix_new0()
then tda
= ncols
.
gsl_matrix *
ncm_matrix_gsl (NcmMatrix *cm
);
This function returns a pointer to the gsl_matrix associated to the matrix cm
.
[skip]
const gsl_matrix *
ncm_matrix_const_gsl (const NcmMatrix *cm
);
This function returns a constant pointer to the gsl_matrix associated to the matrix cm
.
[skip]
guint
ncm_matrix_col_len (const NcmMatrix *cm
);
This function returns the number of elements in a column of the matrix cm
. The columns length.
guint
ncm_matrix_row_len (const NcmMatrix *cm
);
This function returns the number of elements in a row of the matrix cm
. The rows length.
guint
ncm_matrix_nrows (const NcmMatrix *cm
);
This function returns the number of elements in a row of the matrix cm
.
guint
ncm_matrix_ncols (const NcmMatrix *cm
);
This function returns the number of elements in a column of the matrix cm
.
guint
ncm_matrix_size (const NcmMatrix *cm
);
Calculates the total size of the matrix, ncols
$\times$ nrows
.
guint
ncm_matrix_tda (const NcmMatrix *cm
);
This functions returns the matrix cm
tda
value.
gdouble *
ncm_matrix_data (NcmMatrix *cm
);
This function returns a pointer to the matrix cm
base data.
const gdouble *
ncm_matrix_const_data (const NcmMatrix *cm
);
This function returns a constant pointer to the matrix cm
base data.
NcmMatrix *
ncm_matrix_dup (const NcmMatrix *cm
);
Duplicates cm
setting the same values of the original propertities.
void ncm_matrix_substitute (NcmMatrix **cm
,NcmMatrix *nm
,gboolean check_size
);
Substitute the matrix *cm
by nm
, first it unref *cm
if it is not NULL.
If check_size
is TRUE then check if the two matrix have the same size.
void ncm_matrix_add_mul (NcmMatrix *cm1
,const gdouble a
,NcmMatrix *cm2
);
This function performs the operation cm1
= a
$\times$ cm2
$+$ cm1
.
gdouble ncm_matrix_cmp (const NcmMatrix *cm1
,const NcmMatrix *cm2
,const gdouble scale
);
This function performes a comparison, component-wise, of the two matrices, given by $\left| \right. ($cm1
$-$ cm2
$)/($scale
$+$ cm2
$)\left. \right|$ and returns its maximum value.
gdouble ncm_matrix_cmp_diag (const NcmMatrix *cm1
,const NcmMatrix *cm2
,const gdouble scale
);
This function is similar to ncm_matrix_cmp()
, but now only the diagonal elements are compared.
void
ncm_matrix_free (NcmMatrix *cm
);
Atomically decrements the reference count of cm
by one. If the reference count drops to 0,
all memory allocated by cm
is released.
void
ncm_matrix_clear (NcmMatrix **cm
);
Atomically decrements the reference count of cm
by one. If the reference count drops to 0,
all memory allocated by cm
is released. The pointer is set to NULL.
void
ncm_matrix_const_free (const NcmMatrix *cm
);
Atomically decrements the reference count of cv
by one. If the reference count drops to 0,
all memory allocated by cv
is released.
void ncm_matrix_copy_triangle (NcmMatrix *cm
,gchar UL
);
If UL
== 'U' copy the upper triangle over the lower.
If UL
== 'L' copy the lower triangle over the upper.
void ncm_matrix_dsymm (NcmMatrix *cm
,gchar UL
,const gdouble alpha
,NcmMatrix *A
,NcmMatrix *B
,const gdouble beta
);
This function performes the following operation:
if UL
== 'U': $\left( \mathsf{cm} \leftarrow \alpha \mathbf{A} \mathbf{B} + \beta \, \mathsf{cm} \right)$;
if UL
== 'L': $\left( \mathsf{cm} \leftarrow \alpha \mathbf{B} \mathbf{A} + \beta \, \mathsf{cm} \right)$.
Where $\mathbf{A} = \mathbf{A}^\intercal$.
void ncm_matrix_dgemm (NcmMatrix *cm
,gchar TransA
,gchar TransB
,const gdouble alpha
,NcmMatrix *A
,NcmMatrix *B
,const gdouble beta
);
Calculates $C = \alpha\mathrm{op}(A)\mathrm{op}(B) + \beta C$.
gint ncm_matrix_cholesky_decomp (NcmMatrix *cm
,gchar UL
);
Calculates in-place the Cholesky decomposition for a symmetric positive definite matrix.
gint ncm_matrix_cholesky_inverse (NcmMatrix *cm
,gchar UL
);
Calculates inplace the inverse of cm
that has been previously decomposed by
the Cholesky decomposition ncm_matrix_cholesky_decomp()
.
gdouble
ncm_matrix_cholesky_lndet (NcmMatrix *cm
);
Calculates determinant of a symmetric positive definite matrix,
that was previously decomposed using ncm_matrix_cholesky_decomp()
.
gint ncm_matrix_cholesky_solve (NcmMatrix *cm
,NcmVector *b
,gchar UL
);
Calculates in-place the Cholesky decomposition for a symmetric positive
definite matrix and solve the system $A x = B$ where $A=$cm
and $B$=b
.
gint ncm_matrix_cholesky_solve2 (NcmMatrix *cm
,NcmVector *b
,gchar UL
);
Using a previously computed Cholesky decomposition in cm
, through
ncm_matrix_cholesky_decomp()
, solves the system $A x = B$ where
$A=$cm
and $B$=b
.
gint ncm_matrix_nearPD (NcmMatrix *cm
,gchar UL
,gboolean cholesky_decomp
,const guint maxiter
);
Assuming that cm
is a symmetric matrix with data on UL
side, computes the nearest positive definite matrix
in the Frobenius norm. See Higham (2002).
The iterations stop when the Cholesky decomposition is valid.
void ncm_matrix_sym_exp_cholesky (NcmMatrix *cm
,gchar UL
,NcmMatrix *exp_cm_dec
);
Assuming that cm
is a symmetric matrix with data on UL
side, computes the matrix exponential of cm
and its cholesky
decomposition.
cm |
$M$ a NcmMatrix |
|
UL |
char indicating 'U'pper or 'L'ower matrix |
|
exp_cm_dec |
on exit this matrix contain the upper triangular matrix $U$ where $\exp(M) = U^\intercal U$ |
void ncm_matrix_sym_posdef_log (NcmMatrix *cm
,gchar UL
,NcmMatrix *ln_cm
);
Assuming that cm
is a symmetric matrix with data on UL
side, computes the matrix logarithm of cm
.
cm |
$M$ a NcmMatrix |
|
UL |
char indicating 'U'pper or 'L'ower matrix |
|
ln_cm |
on exit this matrix contain the upper triangular matrix $U$ where $\exp(M) = U^\intercal U$ |
void ncm_matrix_triang_to_sym (NcmMatrix *cm
,gchar UL
,gboolean zero
,NcmMatrix *sym
);
Assuming that cm
is a triangular square matrix with data on UL
side, computes the symmetric matrix $M^\intercal \times M$ if
cm
is upper triangular or $M\times M^\intercal$ if it is
lower triangular.
If zero
is TRUE it first sets to zero all elements above/below
the diagonal for UL == 'L'/'U'. It should be TRUE whenever cm
has non-zero values at the other side.
void ncm_matrix_square_to_sym (NcmMatrix *cm
,gchar NT
,gchar UL
,NcmMatrix *sym
);
Computes the symmetric matrix $M^\intercal \times M$ if
NT
== 'T' or $M\times M^\intercal$ if NT
== 'N'. The result
is stored in the upper/lower triangle if UL
='U'/'L'
void ncm_matrix_update_vector (NcmMatrix *cm
,gchar NT
,const gdouble alpha
,NcmVector *v
,const gdouble beta
,NcmVector *u
);
Computes the matrix - vector product $u = \alpha M v + \beta u$
if NT
== 'N' or $u = \alpha M^\intercal v + u$ if NT
== 'T'
and stores the result in u
.
void ncm_matrix_sym_update_vector (NcmMatrix *cm
,gchar UL
,const gdouble alpha
,NcmVector *v
,const gdouble beta
,NcmVector *u
);
Computes the matrix - vector product $u = \alpha M v + \beta u$
if NT
== 'N' or $u = M^\intercal v$ if NT
== 'T'
and stores the result in u
. This function assumes
that $M$ is symmetric and it´s stored in the Upper/Lower
triangle if UL
== 'U'/'L'.
void ncm_matrix_log_vals (NcmMatrix *cm
,gchar *prefix
,gchar *format
);
Prints to the log the values of cm
.
void ncm_matrix_fill_rand_cor (NcmMatrix *cm
,const gdouble cor_level
,NcmRNG *rng
);
Overwrite cm
with a random correlation matrix, the
parameter cor_level
controls the correlation between
entries the lower cor_level
more correlated the entries
are.
void ncm_matrix_fill_rand_cov (NcmMatrix *cm
,const gdouble sigma_min
,const gdouble sigma_max
,const gdouble cor_level
,NcmRNG *rng
);
Overwrite cm
with a random covariance matrix, the
parameter cor_level
controls the correlation between
entries the lower cor_level
more correlated the entries
are.
void ncm_matrix_fill_rand_cov2 (NcmMatrix *cm
,NcmVector *mu
,const gdouble reltol_min
,const gdouble reltol_max
,const gdouble cor_level
,NcmRNG *rng
);
Overwrite cm
with a random covariance matrix, the
parameter cor_level
controls the correlation between
entries the lower cor_level
more correlated the entries
are.
void ncm_matrix_cov2cor (const NcmMatrix *cov
,NcmMatrix *cor
);
Convert a covariance matrix cov
to a correlation
matrix cor
. The matrices cor
and cov
can be the same
object.
This enumerator is only used internally. Only by developers.