27#if defined(HAVE_LIBBLAS) && defined(HAVE_LIBLAPACK)
36#define LAPACK_COMPLEX_CUSTOM
37#define lapack_complex_float _Fcomplex
38#define lapack_complex_double _Dcomplex
42#if defined(HAVE_CBLAS_ATLAS_H)
43#include <cblas-atlas.h>
52static int egcmp(
const void *
pa,
const void *pb);
70 if (rows < 1 || cols < 1 || ldim < rows) {
71 G_warning(
_(
"Matrix dimensions out of range"));
122 if (rows < 1 || cols < 1 || ldim < 0) {
123 G_warning(
_(
"Matrix dimensions out of range"));
133 A->
vals = (
double *)
G_calloc(ldim * cols,
sizeof(
double));
154 G_warning(
_(
"Matrix is not initialised fully."));
159 G_warning(
_(
"Unable to allocate space for matrix copy"));
164 (
size_t)A->
cols * A->
ldim *
sizeof(
double));
222 G_warning(
_(
"Input matrix is uninitialized"));
235 for (i = 0; i <
m; i++) {
236 for (
j = 0;
j < n;
j++) {
285 G_warning(
_(
"First scalar multiplier must be non-zero"));
291 G_warning(
_(
"One or both input matrices uninitialised"));
298 if (!((
mt1->is_init) && (
mt2->is_init))) {
299 G_warning(
_(
"One or both input matrices uninitialised"));
310 G_warning(
_(
"Unable to allocate space for matrix sum"));
316 for (i = 0; i <
mt3->rows; i++) {
317 for (
j = 0;
j <
mt3->cols;
j++) {
326 for (i = 0; i <
mt3->rows; i++) {
327 for (
j = 0;
j <
mt3->cols;
j++) {
356 if (!((
mt1->is_init) || (
mt2->is_init))) {
357 G_warning(
_(
"One or both input matrices uninitialised"));
361 if (
mt1->cols !=
mt2->rows) {
367 G_warning(
_(
"Unable to allocate space for matrix product"));
406 if (
mt->cols % 2 == 0)
469 if (
mt1->is_init == 0 ||
bmat->is_init == 0) {
470 G_warning(
_(
"Input: one or both data matrices uninitialised"));
474 if (
mt1->rows !=
mt1->cols ||
mt1->rows < 1) {
475 G_warning(
_(
"Principal matrix is not properly dimensioned"));
479 if (
bmat->cols < 1) {
480 G_warning(
_(
"Input: you must have at least one array to solve"));
486 G_warning(
_(
"Could not allocate space for solution matrix"));
492 G_warning(
_(
"Could not allocate space for working matrix"));
500 G_warning(
_(
"Could not allocate space for working matrix"));
544 (
size_t)
wmat->cols *
wmat->ldim *
sizeof(
double));
553 _(
"Matrix (or submatrix is singular). Solution undetermined"));
563 G_warning(
_(
"Procedure not yet available for selected matrix type"));
589 if (
mt->rows !=
mt->cols) {
590 G_warning(
_(
"Matrix is not square. Cannot determine inverse"));
595 G_warning(
_(
"Unable to allocate space for matrix"));
600 for (i = 0; i <
mt0->rows - 1; i++) {
601 mt0->vals[i + i *
mt0->ldim] = 1.0;
603 for (
j = i + 1;
j <
mt0->cols;
j++) {
604 mt0->vals[i +
j *
mt0->ldim] =
mt0->vals[
j + i *
mt0->ldim] = 0.0;
608 mt0->vals[
mt0->rows - 1 + (
mt0->rows - 1) *
mt0->ldim] = 1.0;
658 char buf[2048],
numbuf[64];
660 for (i = 0; i <
mt->rows; i++) {
663 for (
j = 0;
j <
mt->cols;
j++) {
696 G_warning(
_(
"Element array has not been allocated"));
701 G_warning(
_(
"Specified element is outside array bounds"));
751 G_warning(
_(
"Specified matrix column index is outside range"));
761 G_warning(
_(
"Could not allocate space for vector structure"));
765 for (i = 0; i <
mt->rows; i++)
790 G_warning(
_(
"Specified matrix row index is outside range"));
800 G_warning(
_(
"Could not allocate space for vector structure"));
804 for (i = 0; i <
mt->cols; i++)
828 G_warning(
_(
"Specified row index is outside range"));
833 G_warning(
_(
"Specified column index is outside range"));
896 unsigned int i,
m, n,
j;
901 if (A->
cols !=
b->cols) {
902 G_warning(
_(
"Input matrix and vector have differing dimensions1"));
907 G_warning(
_(
"Output vector is uninitialized"));
919 for (i = 0; i <
m; i++) {
923 for (
j = 0;
j < n;
j++) {
951 if ((cells < 1) || (
vt ==
RVEC && ldim < 1) ||
952 (
vt ==
CVEC && ldim < cells) || ldim < 0) {
953 G_warning(
"Vector dimensions out of range.");
1018 if (!
out->is_init) {
1019 G_warning(
_(
"Output vector is uninitialized"));
1024 G_warning(
_(
"Vectors are not of the same type"));
1029 G_warning(
_(
"Output vector is of incorrect type"));
1040 G_warning(
_(
"Vectors have differing dimensions"));
1046 G_warning(
_(
"Output vector has incorrect dimension"));
1055 for (i = 0; i < v1->
cols; i++)
1061 for (i = 0; i < v1->
rows; i++)
1089 if ((cells < 1) || (
vt ==
RVEC && ldim < 1) ||
1090 (
vt ==
CVEC && ldim < cells) || ldim < 0) {
1091 G_warning(
_(
"Vector dimensions out of range"));
1145 incr = (
int)
vc->ldim;
1190 ncells = (
int)
vc->cols;
1191 incr = (
int)
vc->ldim;
1198 ncells = (
int)
vc->rows;
1209 while (ncells > 0) {
1237 return (
double)
xval;
1252 double result = 0.0;
1261 idx = (
vc->v_indx > 0) ?
vc->v_indx : 0;
1264 for (i = 0; i <
vc->cols; i++)
1268 for (i = 0; i <
vc->rows; i++)
1293 if (!
out->is_init) {
1294 G_warning(
_(
"Output vector is uninitialized"));
1299 G_warning(
_(
"Vectors are not of the same type"));
1304 G_warning(
_(
"Output vector is not the same type as others"));
1315 G_warning(
_(
"Vectors have differing dimensions"));
1321 G_warning(
_(
"Output vector has incorrect dimension"));
1330 for (i = 0; i < v1->
cols; i++)
1336 for (i = 0; i < v1->
rows; i++)
1362 if (!
vc1->is_init) {
1363 G_warning(
_(
"Vector structure is not initialised"));
1397 G_warning(
"Copy method must be specified: [DO,NO]_COMPACT.\n");
1423 G_warning(
"Structure type is not vector.");
1437 G_warning(
"Copy method must be specified: [DO,NO]_COMPACT.\n");
1475 if (!
G_getl(buff,
sizeof(buff), fp))
1481 if (
sscanf(buff,
"Matrix: %d by %d", &rows, &cols) != 2) {
1488 for (i = 0; i < rows; i++) {
1489 if (
fscanf(fp,
"row%d:", &row) != 1 || row != i) {
1493 for (
j = 0;
j < cols;
j++) {
1494 if (
fscanf(fp,
"%lf:", &val) != 1) {
1525 for (i = 0; i < rows; i++)
1526 for (
j = 0;
j < cols;
j++)
1579 for (i = 0; i <
m->cols; i++) {
1580 for (
j = 0;
j <
m->rows;
j++)
1592 for (i = 0; i <
m->cols; i++) {
1593 for (
j = 0;
j <
m->rows;
j++)
1606static int egcmp(
const void *
pa,
const void *pb)
1608 double a = *(
double *
const)
pa;
1609 double b = *(
double *
const)pb;
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void G_message(const char *,...) __attribute__((format(printf
int G_getl(char *, int, FILE *)
Gets a line of text from a file.
size_t G_strlcpy(char *, const char *, size_t)
Safe string copy function.
vec_struct * G_matvect_get_column(mat_struct *mt, int col)
Retrieve a column of the matrix to a vector structure.
mat_struct * G_matrix_copy(const mat_struct *A)
Copy a matrix.
vec_struct * G_vector_sub(vec_struct *v1, vec_struct *v2, vec_struct *out)
Subtract two vectors.
double G_vector_norm_maxval(vec_struct *vc, int vflag)
Calculates maximum value.
mat_struct * G__matrix_add(mat_struct *mt1, mat_struct *mt2, const double c1, const double c2)
vec_struct * G_vector_product(vec_struct *v1, vec_struct *v2, vec_struct *out)
Calculates the vector product.
mat_struct * G_matrix_subtract(mat_struct *mt1, mat_struct *mt2)
Subtract two matrices.
int G_matvect_retrieve_matrix(vec_struct *vc)
Revert a vector to matrix.
double G_vector_norm_euclid(vec_struct *vc)
Calculates euclidean norm.
vec_struct * G_vector_init(int cells, int ldim, vtype vt)
Initialize a vector structure.
mat_struct * G_matrix_inverse(mat_struct *mt)
Returns the matrix inverse.
double G_matrix_get_element(mat_struct *mt, int rowval, int colval)
Retrieve value of the (i,j)th element.
mat_struct * G_matrix_scale(mat_struct *mt1, const double c)
Scale a matrix by a scalar value.
int G_matrix_stdin(mat_struct *out)
Read a matrix from standard input.
mat_struct * G_matrix_resize(mat_struct *in, int rows, int cols)
Resizes a matrix.
void G_matrix_print(mat_struct *mt)
Print out a matrix.
mat_struct * G_matrix_scalar_mul(double scalar, mat_struct *matrix, mat_struct *out)
Calculates the scalar-matrix multiplication.
vec_struct * G_matvect_product(mat_struct *A, vec_struct *b, vec_struct *out)
Calculates the matrix-vector product.
mat_struct * G_matrix_add(mat_struct *mt1, mat_struct *mt2)
Adds two matrices.
int G_matvect_extract_vector(mat_struct *mt, vtype vt, int indx)
Convert matrix to vector.
int G_matrix_LU_solve(const mat_struct *mt1, mat_struct **xmat0, const mat_struct *bmat, mat_type mtype)
Solve a general system A.X = B.
mat_struct * G_matrix_init(int rows, int cols, int ldim)
Initialize a matrix structure.
int suppress_empty_translation_unit_compiler_warning
double G_vector_norm1(vec_struct *vc)
Calculates the 1-norm of a vector.
int G_matrix_read(FILE *fp, mat_struct *out)
Read a matrix from a file stream.
vec_struct * G_matvect_get_row(mat_struct *mt, int row)
Retrieve a row of the matrix to a vector structure.
int G_matrix_eigen_sort(vec_struct *d, mat_struct *m)
Sort eigenvectors according to eigenvalues.
vec_struct * G_vector_copy(const vec_struct *vc1, int comp_flag)
Returns a vector copied from vc1. Underlying structure is preserved unless DO_COMPACT flag.
void G_vector_free(vec_struct *v)
Free an allocated vector structure.
void G_matrix_free(mat_struct *mt)
Free up allocated matrix.
mat_struct * G_matrix_product(mat_struct *mt1, mat_struct *mt2)
Returns product of two matrices.
int G_vector_set(vec_struct *A, int cells, int ldim, vtype vt, int vindx)
mat_struct * G_matrix_transpose(mat_struct *mt)
Transpose a matrix.
int G_matrix_set_element(mat_struct *mt, int rowval, int colval, double val)
int G_matrix_set(mat_struct *A, int rows, int cols, int ldim)
Set parameters for an initialized matrix.
int G_matrix_zero(mat_struct *A)
Clears (or resets) the matrix values to 0.
Wrapper headers for BLAS/LAPACK.