27 #if defined(HAVE_LIBBLAS) && defined(HAVE_LIBLAPACK)
35 #if defined(HAVE_CBLAS_ATLAS_H)
36 #include <cblas-atlas.h>
45 static int egcmp(
const void *pa,
const void *pb);
64 if (rows < 1 || cols < 1 || ldim < rows) {
65 G_warning(
_(
"Matrix dimensions out of range"));
70 tmp_arry->
rows = rows;
71 tmp_arry->
cols = cols;
72 tmp_arry->
ldim = ldim;
76 tmp_arry->
vals = (
double *)
G_calloc(ldim * cols,
sizeof(
double));
96 memset(A->
vals, 0, (A->
ldim * A->
cols) *
sizeof(
double));
118 if (rows < 1 || cols < 1 || ldim < 0) {
119 G_warning(
_(
"Matrix dimensions out of range"));
129 A->
vals = (
double *)
G_calloc(ldim * cols,
sizeof(
double));
151 G_warning(
_(
"Matrix is not initialised fully."));
156 G_warning(
_(
"Unable to allocate space for matrix copy"));
161 (
size_t)A->
cols * A->
ldim *
sizeof(
double));
220 if (matrix ==
NULL) {
221 G_warning(
_(
"Input matrix is uninitialized"));
234 for (i = 0; i < m; i++) {
235 for (j = 0; j < n; j++) {
286 G_warning(
_(
"First scalar multiplier must be non-zero"));
292 G_warning(
_(
"One or both input matrices uninitialised"));
300 G_warning(
_(
"One or both input matrices uninitialised"));
311 G_warning(
_(
"Unable to allocate space for matrix sum"));
317 for (i = 0; i < mt3->
rows; i++) {
318 for (j = 0; j < mt3->
cols; j++) {
327 for (i = 0; i < mt3->
rows; i++) {
328 for (j = 0; j < mt3->
cols; j++) {
355 double unity = 1., zero = 0.;
356 int rows, cols, interdim, lda, ldb;
359 G_warning(
_(
"One or both input matrices uninitialised"));
369 G_warning(
_(
"Unable to allocate space for matrix product"));
375 rows = (int)mt1->
rows;
376 interdim = (
int)mt1->
cols;
377 cols = (int)mt2->
cols;
379 lda = (
int)mt1->
ldim;
380 ldb = (int)mt2->
ldim;
382 cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, rows, cols, interdim,
383 unity, mt1->
vals, lda, mt2->
vals, ldb, zero, mt3->
vals, lda);
405 double *dbo, *dbt, *dbx, *dby;
409 if (mt->
cols % 2 == 0)
421 for (cnt = 0; cnt < mt->
cols; cnt++) {
425 for (cnt2 = 0; cnt2 < ldo - 1; cnt2++) {
433 if (cnt < mt->cols - 1) {
473 G_warning(
_(
"Input: one or both data matrices uninitialised"));
478 G_warning(
_(
"Principal matrix is not properly dimensioned"));
482 if (bmat->
cols < 1) {
483 G_warning(
_(
"Input: you must have at least one array to solve"));
489 G_warning(
_(
"Could not allocate space for solution matrix"));
495 G_warning(
_(
"Could not allocate space for working matrix"));
503 G_warning(
_(
"Could not allocate space for working matrix"));
512 int num_eqns, nrhs, lda, ldb;
517 num_eqns = (int)mt1->
rows;
518 nrhs = (
int)wmat->
cols;
519 lda = (int)mt1->
ldim;
520 ldb = (
int)wmat->
ldim;
523 res_info = LAPACKE_dgesv(LAPACK_COL_MAJOR, num_eqns, nrhs, mtx->
vals,
524 lda, perm, wmat->
vals, ldb);
547 (
size_t)wmat->
cols * wmat->
ldim *
sizeof(
double));
556 _(
"Matrix (or submatrix is singular). Solution undetermined"));
559 else if (res_info < 0) {
566 G_warning(
_(
"Procedure not yet available for selected matrix type"));
594 G_warning(
_(
"Matrix is not square. Cannot determine inverse"));
599 G_warning(
_(
"Unable to allocate space for matrix"));
604 for (i = 0; i < mt0->
rows - 1; i++) {
607 for (j = i + 1; j < mt0->
cols; j++) {
664 char buf[2048], numbuf[64];
666 for (i = 0; i < mt->
rows; i++) {
669 for (j = 0; j < mt->
cols; j++) {
670 snprintf(numbuf,
sizeof(numbuf),
"%14.6f",
673 if (j < mt->cols - 1)
680 fprintf(stderr,
"\n");
703 G_warning(
_(
"Element array has not been allocated"));
707 if (rowval >= mt->
rows || colval >= mt->
cols || rowval < 0 || colval < 0) {
708 G_warning(
_(
"Specified element is outside array bounds"));
712 mt->
vals[rowval + colval * mt->
ldim] = (double)val;
739 return (val = (
double)mt->
vals[rowval + colval * mt->
ldim]);
759 if (col < 0 || col >= mt->
cols) {
760 G_warning(
_(
"Specified matrix column index is outside range"));
770 G_warning(
_(
"Could not allocate space for vector structure"));
774 for (i = 0; i < mt->
rows; i++)
799 if (row < 0 || row >= mt->
cols) {
800 G_warning(
_(
"Specified matrix row index is outside range"));
810 G_warning(
_(
"Could not allocate space for vector structure"));
814 for (i = 0; i < mt->
cols; i++)
838 if (vt ==
RVEC && indx >= mt->
rows) {
839 G_warning(
_(
"Specified row index is outside range"));
843 else if (vt ==
CVEC && indx >= mt->
cols) {
844 G_warning(
_(
"Specified column index is outside range"));
909 unsigned int i, m, n, j;
914 if (A->
cols !=
b->cols) {
915 G_warning(
_(
"Input matrix and vector have differing dimensions1"));
920 G_warning(
_(
"Output vector is uninitialized"));
932 for (i = 0; i < m; i++) {
936 for (j = 0; j < n; j++) {
965 if ((cells < 1) || (vt ==
RVEC && ldim < 1) ||
966 (vt ==
CVEC && ldim < cells) || ldim < 0) {
967 G_warning(
"Vector dimensions out of range.");
975 tmp_arry->
cols = cells;
976 tmp_arry->
ldim = ldim;
980 else if (vt ==
CVEC) {
981 tmp_arry->
rows = cells;
983 tmp_arry->
ldim = ldim;
1031 int idx1, idx2, idx0;
1035 G_warning(
_(
"Output vector is uninitialized"));
1040 G_warning(
_(
"Vectors are not of the same type"));
1045 G_warning(
_(
"Output vector is of incorrect type"));
1056 G_warning(
_(
"Vectors have differing dimensions"));
1062 G_warning(
_(
"Output vector has incorrect dimension"));
1071 for (i = 0; i < v1->
cols; i++)
1077 for (i = 0; i < v1->
rows; i++)
1106 if ((cells < 1) || (vt ==
RVEC && ldim < 1) ||
1107 (vt ==
CVEC && ldim < cells) || ldim < 0) {
1108 G_warning(
_(
"Vector dimensions out of range"));
1112 if ((vt ==
RVEC && vindx >= A->
cols) || (vt ==
CVEC && vindx >= A->
rows)) {
1162 Nval = (int)vc->
cols;
1163 incr = (
int)vc->
ldim;
1170 Nval = (int)vc->
rows;
1179 return cblas_dnrm2(Nval, startpt, incr);
1201 double xval, *startpt, *curpt;
1209 ncells = (int)vc->
cols;
1210 incr = (
int)vc->
ldim;
1217 ncells = (int)vc->
rows;
1228 while (ncells > 0) {
1229 if (curpt != startpt) {
1245 cellval = (double)(*curpt);
1246 if (hypot(cellval, cellval) > (double)xval)
1256 return (
double)xval;
1272 double result = 0.0;
1284 for (i = 0; i < vc->
cols; i++)
1288 for (i = 0; i < vc->
rows; i++)
1310 int idx1, idx2, idx0;
1314 G_warning(
_(
"Output vector is uninitialized"));
1319 G_warning(
_(
"Vectors are not of the same type"));
1324 G_warning(
_(
"Output vector is not the same type as others"));
1335 G_warning(
_(
"Vectors have differing dimensions"));
1341 G_warning(
_(
"Output vector has incorrect dimension"));
1350 for (i = 0; i < v1->
cols; i++)
1356 for (i = 0; i < v1->
rows; i++)
1380 double *startpt1, *startpt2, *curpt1, *curpt2;
1384 G_warning(
_(
"Vector structure is not initialised"));
1418 G_warning(
"Copy method must be specified: [DO,NO]_COMPACT.\n");
1426 startpt1 = tmp_arry->
vals;
1435 startpt1 = tmp_arry->
vals;
1444 G_warning(
"Structure type is not vector.");
1449 startpt1 = tmp_arry->
vals;
1450 startpt2 = vc1->
vals;
1458 G_warning(
"Copy method must be specified: [DO,NO]_COMPACT.\n");
1463 memcpy(curpt1, curpt2,
sizeof(
double));
1497 if (!
G_getl(buff,
sizeof(buff), fp))
1503 if (sscanf(buff,
"Matrix: %d by %d", &rows, &cols) != 2) {
1510 for (i = 0; i < rows; i++) {
1511 if (fscanf(fp,
"row%d:", &row) != 1 || row != i) {
1515 for (j = 0; j < cols; j++) {
1516 if (fscanf(fp,
"%lf:", &val) != 1) {
1548 for (i = 0; i < rows; i++)
1549 for (j = 0; j < cols; j++)
1553 int old_size = in->
rows * in->
cols;
1554 int new_size = rows * cols;
1556 if (new_size > old_size)
1557 for (p = old_size; p < new_size; p++)
1604 for (i = 0; i < m->
cols; i++) {
1605 for (j = 0; j < m->
rows; j++)
1614 qsort(tmp.
vals, tmp.
cols, tmp.
ldim *
sizeof(
double), egcmp);
1617 for (i = 0; i < m->
cols; i++) {
1618 for (j = 0; j < m->
rows; j++)
1631 static int egcmp(
const void *pa,
const void *pb)
1633 double a = *(
double *
const)pa;
1634 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_product(mat_struct *A, vec_struct *b, vec_struct *out)
Calculates the matrix-vector product.
double G_vector_norm_maxval(vec_struct *vc, int vflag)
Calculates maximum value.
vec_struct * G_matvect_get_column(mat_struct *mt, int col)
Retrieve a column of the matrix to a vector structure.
vec_struct * G_vector_sub(vec_struct *v1, vec_struct *v2, vec_struct *out)
Subtract two vectors.
int G_matvect_retrieve_matrix(vec_struct *vc)
Revert a vector to matrix.
double G_vector_norm_euclid(vec_struct *vc)
Calculates euclidean norm.
mat_struct * G_matrix_subtract(mat_struct *mt1, mat_struct *mt2)
Subtract two matricies.
mat_struct * G_matrix_add(mat_struct *mt1, mat_struct *mt2)
Adds two matricies.
double G_matrix_get_element(mat_struct *mt, int rowval, int colval)
Retrieve value of the (i,j)th element.
mat_struct * G__matrix_add(mat_struct *mt1, mat_struct *mt2, const double c1, const double c2)
mat_struct * G_matrix_inverse(mat_struct *mt)
Returns the matrix inverse.
int G_matrix_stdin(mat_struct *out)
void G_matrix_print(mat_struct *mt)
Print out a matrix.
mat_struct * G_matrix_copy(const mat_struct *A)
Copy a matrix.
mat_struct * G_matrix_resize(mat_struct *in, int rows, int cols)
Resizes a matrix.
int G_matvect_extract_vector(mat_struct *mt, vtype vt, int indx)
Convert matrix to vector.
vec_struct * G_vector_init(int cells, int ldim, vtype vt)
Initialize a vector structure.
mat_struct * G_matrix_product(mat_struct *mt1, mat_struct *mt2)
Returns product of two matricies.
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.
vec_struct * G_vector_product(vec_struct *v1, vec_struct *v2, vec_struct *out)
Calculates the vector product.
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.
int G_matrix_eigen_sort(vec_struct *d, mat_struct *m)
Sort eigenvectors according to eigenvalues.
mat_struct * G_matrix_scale(mat_struct *mt1, const double c)
Scale a matrix by a scalar value.
void G_vector_free(vec_struct *v)
Free an allocated vector structure.
vec_struct * G_matvect_get_row(mat_struct *mt, int row)
Retrieve a row of the matrix to a vector structure.
void G_matrix_free(mat_struct *mt)
Free up allocated matrix.
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.
int G_vector_set(vec_struct *A, int cells, int ldim, vtype vt, int vindx)
int G_matrix_set_element(mat_struct *mt, int rowval, int colval, double val)
mat_struct * G_matrix_transpose(mat_struct *mt)
Transpose a matrix.
mat_struct * G_matrix_scalar_mul(double scalar, mat_struct *matrix, mat_struct *out)
Calculates the scalar-matrix multiplication.
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.