Geogram  Version 1.9.1
A programming library of geometric algorithms
nl_matrix.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #include "nl_private.h"
41 
42 #ifndef OPENNL_MATRIX_H
43 #define OPENNL_MATRIX_H
44 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 /******************************************************************************/
55 /* Abstract matrix interface */
56 
57  struct NLMatrixStruct;
58  typedef struct NLMatrixStruct* NLMatrix;
59 
63  typedef void(*NLDestroyMatrixFunc)(NLMatrix M);
64 
68  typedef void(*NLMultMatrixVectorFunc)(NLMatrix M, const double* x, double* y);
69 
70 #define NL_MATRIX_SPARSE_DYNAMIC 0x1001
71 #define NL_MATRIX_CRS 0x1002
72 #define NL_MATRIX_SUPERLU_EXT 0x1003
73 #define NL_MATRIX_CHOLMOD_EXT 0x1004
74 #define NL_MATRIX_FUNCTION 0x1005
75 #define NL_MATRIX_OTHER 0x1006
76 
80  struct NLMatrixStruct {
85 
90 
99 
104 
109  };
110 
116  NLAPI void NLAPIENTRY nlDeleteMatrix(NLMatrix M);
117 
124  NLAPI void NLAPIENTRY nlMultMatrixVector(
125  NLMatrix M, const double* x, double* y
126  );
127 
128 /******************************************************************************/
129 /* Dynamic arrays for sparse row/columns */
130 
135  typedef struct {
140 
145  } NLCoeff;
146 
151  typedef struct {
156 
162 
168  } NLRowColumn;
169 
176  NLAPI void NLAPIENTRY nlRowColumnConstruct(NLRowColumn* c);
177 
186  NLAPI void NLAPIENTRY nlRowColumnDestroy(NLRowColumn* c);
187 
198  NLAPI void NLAPIENTRY nlRowColumnGrow(NLRowColumn* c);
199 
213  NLAPI void NLAPIENTRY nlRowColumnAdd(
214  NLRowColumn* c, NLuint index, NLdouble value
215  );
216 
227  NLAPI void NLAPIENTRY nlRowColumnAppend(
228  NLRowColumn* c, NLuint index, NLdouble value
229  );
230 
238  NLAPI void NLAPIENTRY nlRowColumnZero(NLRowColumn* c);
239 
247  NLAPI void NLAPIENTRY nlRowColumnClear(NLRowColumn* c);
248 
255  NLAPI void NLAPIENTRY nlRowColumnSort(NLRowColumn* c);
256 
257 /******************************************************************************/
258 /* Compressed Row Storage */
259 
265 #ifdef GARGANTUA
266  typedef NLulong NLuint_big;
267 #else
269 #endif
270 
271 
278  typedef struct {
283 
288 
295 
300 
305 
311 
316 
321 
326 
332 
338  } NLCRSMatrix;
339 
350  NLAPI void NLAPIENTRY nlCRSMatrixConstruct(
351  NLCRSMatrix* M, NLuint m, NLuint n, NLuint_big nnz, NLuint nslices
352  );
353 
361  NLAPI void NLAPIENTRY nlCRSMatrixConstructSymmetric(
362  NLCRSMatrix* M, NLuint n, NLuint_big nnz
363  );
364 
365 
375  NLAPI void NLAPIENTRY nlCRSMatrixConstructPattern(
376  NLCRSMatrix* M, NLuint m, NLuint n
377  );
378 
389  NLCRSMatrix* M, NLuint n
390  );
391 
397  NLAPI void NLAPIENTRY nlCRSMatrixPatternSetRowLength(
398  NLCRSMatrix* M, NLuint i, NLuint n
399  );
400 
401 
407  NLAPI void NLAPIENTRY nlCRSMatrixPatternCompile(NLCRSMatrix* M);
408 
420  NLAPI NLulong NLAPIENTRY nlCRSMatrixAdd(
421  NLCRSMatrix* M, NLuint i, NLuint j, NLdouble value
422  );
423 
437  NLAPI void NLAPIENTRY nlCRSMatrixAddAt(
438  NLCRSMatrix* M, NLuint i, NLuint j, NLdouble value, NLulong index
439  );
440 
454  NLCRSMatrix* M, NLuint i, NLuint j, NLdouble value, NLuint row_offset
455  );
456 
465  NLAPI NLboolean NLAPIENTRY nlCRSMatrixLoad(
466  NLCRSMatrix* M, const char* filename
467  );
468 
477  NLAPI NLboolean NLAPIENTRY nlCRSMatrixSave(
478  NLCRSMatrix* M, const char* filename
479  );
480 
488  NLAPI NLuint_big NLAPIENTRY nlCRSMatrixNNZ(NLCRSMatrix* M);
489 
490 /******************************************************************************/
491 /* SparseMatrix data structure */
492 
497 #define NL_MATRIX_STORE_ROWS 1
498 
503 #define NL_MATRIX_STORE_COLUMNS 2
504 
510 #define NL_MATRIX_STORE_SYMMETRIC 4
511 
512  typedef struct {
517 
522 
529 
534 
539 
540 
545 
550 
555 
561 
567 
572 
578 
584 
585  } NLSparseMatrix;
586 
587 
598  NLAPI NLMatrix NLAPIENTRY nlSparseMatrixNew(
599  NLuint m, NLuint n, NLenum storage
600  );
601 
611  NLAPI void NLAPIENTRY nlSparseMatrixConstruct(
612  NLSparseMatrix* M, NLuint m, NLuint n, NLenum storage
613  );
614 
622  NLAPI void NLAPIENTRY nlSparseMatrixDestroy(NLSparseMatrix* M);
623 
631  NLAPI void NLAPIENTRY nlSparseMatrixMult(
632  NLSparseMatrix* A, const NLdouble* x, NLdouble* y
633  );
634 
645  NLAPI void NLAPIENTRY nlSparseMatrixAdd(
646  NLSparseMatrix* M, NLuint i, NLuint j, NLdouble value
647  );
648 
659  NLAPI void NLAPIENTRY nlSparseMatrixAddMatrix(
660  NLSparseMatrix* M, double mul, const NLMatrix N
661  );
662 
669  NLAPI void NLAPIENTRY nlSparseMatrixZero( NLSparseMatrix* M);
670 
677  NLAPI void NLAPIENTRY nlSparseMatrixClear( NLSparseMatrix* M);
678 
687 
693  NLAPI void NLAPIENTRY nlSparseMatrixSort( NLSparseMatrix* M);
694 
699  NLAPI void NLAPIENTRY nlSparseMatrixAddRow( NLSparseMatrix* M);
700 
705  NLAPI void NLAPIENTRY nlSparseMatrixAddColumn( NLSparseMatrix* M);
706 
715  NLAPI void NLAPIENTRY nlSparseMatrixMAddRow(
716  NLSparseMatrix* M, NLuint i1, double s, NLuint i2
717  );
718 
727  NLAPI void NLAPIENTRY nlSparseMatrixScaleRow(
728  NLSparseMatrix* M, NLuint i, double s
729  );
730 
738  NLAPI void NLAPIENTRY nlSparseMatrixZeroRow(
739  NLSparseMatrix* M, NLuint i
740  );
741 
742 
743 /******************************************************************************/
744 
754 
765  NLSparseMatrix* M
766  );
767 
768 
776  NLAPI void NLAPIENTRY nlMatrixCompress(NLMatrix* M);
777 
784  NLAPI NLuint_big NLAPIENTRY nlMatrixNNZ(NLMatrix M);
785 
800  NLAPI NLMatrix NLAPIENTRY nlMatrixFactorize(NLMatrix M, NLenum solver);
801 
802 /******************************************************************************/
803 
807  typedef void(*NLMatrixFunc)(const double* x, double* y);
808 
817  NLuint m, NLuint n, NLMatrixFunc func
818  );
819 
827 
828 /******************************************************************************/
829 
847  NLMatrix M, NLboolean product_owns_M,
848  NLMatrix N, NLboolean product_owns_N
849  );
850 
851 /******************************************************************************/
852 
853 #ifdef __cplusplus
854 }
855 #endif
856 
857 #endif
uint32_t NLuint
A 4-bytes unsigned integer.
Definition: nl.h:263
uint64_t NLulong
A 8-bytes unsigned integer.
Definition: nl.h:273
double NLdouble
A double-precision floating-point number.
Definition: nl.h:288
struct NLMatrixStruct * NLMatrix
Opaque handle to a matrix.
Definition: nl.h:1496
unsigned int NLenum
A symbolic constant.
Definition: nl.h:215
unsigned char NLboolean
A truth value (NL_TRUE or NL_FALSE).
Definition: nl.h:221
NLAPI void NLAPIENTRY nlCRSMatrixPatternCompile(NLCRSMatrix *M)
Intializes a NLCRSMatrix from the pattern (row lengths).
NLAPI NLMatrix NLAPIENTRY nlMatrixNewFromFunction(NLuint m, NLuint n, NLMatrixFunc func)
Creates a matrix implemented by a matrix-vector function.
NLAPI void NLAPIENTRY nlSparseMatrixAddMatrix(NLSparseMatrix *M, double mul, const NLMatrix N)
Adds a matrix to another sparse matrix.
NLAPI NLMatrix NLAPIENTRY nlMatrixNewFromProduct(NLMatrix M, NLboolean product_owns_M, NLMatrix N, NLboolean product_owns_N)
Creates a matrix that represents the product of two matrices.
NLAPI NLMatrix NLAPIENTRY nlMatrixFactorize(NLMatrix M, NLenum solver)
Factorizes a matrix.
NLAPI void NLAPIENTRY nlCRSMatrixPatternSetRowLength(NLCRSMatrix *M, NLuint i, NLuint n)
Specifies the number of non-zero entries in the row of a matrix that was constructed by nlCRSMatrixCo...
NLAPI void NLAPIENTRY nlDeleteMatrix(NLMatrix M)
Deletes a matrix.
NLAPI NLuint_big NLAPIENTRY nlMatrixNNZ(NLMatrix M)
Gets the number of non-zero entries in a matrix.
NLAPI void NLAPIENTRY nlSparseMatrixAddColumn(NLSparseMatrix *M)
Adds a new empty column to a sparse matrix.
NLAPI void NLAPIENTRY nlSparseMatrixMAddRow(NLSparseMatrix *M, NLuint i1, double s, NLuint i2)
Adds a row of a sparse matrix to another row. (row[i1] += s * row[i2]).
NLAPI void NLAPIENTRY nlSparseMatrixAddRow(NLSparseMatrix *M)
Adds a new empty row to a sparse matrix.
NLAPI void NLAPIENTRY nlSparseMatrixScaleRow(NLSparseMatrix *M, NLuint i, double s)
Scales a row of a sparse matrix. (row[i] *= s).
void(* NLMatrixFunc)(const double *x, double *y)
Function pointer type for matrix-vector products.
Definition: nl_matrix.h:807
NLAPI NLMatrixFunc NLAPIENTRY nlMatrixGetFunction(NLMatrix M)
Gets the function pointer that implements matrix x vector product.
NLAPI void NLAPIENTRY nlMultMatrixVector(NLMatrix M, const double *x, double *y)
Computes a matrix x vector product.
void(* NLMultMatrixVectorFunc)(NLMatrix M, const double *x, double *y)
Function pointer type for matrix x vector product.
Definition: nl_matrix.h:68
void(* NLDestroyMatrixFunc)(NLMatrix M)
Function pointer type for matrix destructors.
Definition: nl_matrix.h:63
NLuint NLuint_big
Type for internal indices.
Definition: nl_matrix.h:268
NLAPI void NLAPIENTRY nlSparseMatrixZeroRow(NLSparseMatrix *M, NLuint i)
Zeroes a row of a sparse matrix. (row[i] = 0).
Some macros and functions used internally by OpenNL.
A compact self-contained storage for sparse matrices.
Definition: nl_matrix.h:278
NLAPI void NLAPIENTRY nlCRSMatrixConstructSymmetric(NLCRSMatrix *M, NLuint n, NLuint_big nnz)
Constructs a new NLCRSMatrix with symmetric storage.
NLenum type
Matrix type.
Definition: nl_matrix.h:294
NLMultMatrixVectorFunc mult_func
Matrix x vector product.
Definition: nl_matrix.h:304
NLAPI NLMatrix NLAPIENTRY nlCRSMatrixNewFromSparseMatrix(NLSparseMatrix *M)
Creates a compressed row storage matrix from a dynamic sparse matrix.
NLuint_big * rowptr
row pointers, size = m+1
Definition: nl_matrix.h:315
NLAPI NLuint_big NLAPIENTRY nlCRSMatrixNNZ(NLCRSMatrix *M)
Gets the number of non-zero coefficient in an NLCRSMatrix.
NLuint m
number of rows
Definition: nl_matrix.h:282
NLAPI void NLAPIENTRY nlCRSMatrixConstructPatternSymmetric(NLCRSMatrix *M, NLuint n)
Constructs a new NLCRSMatrix with symmetric storage.
NLAPI void NLAPIENTRY nlCRSMatrixConstruct(NLCRSMatrix *M, NLuint m, NLuint n, NLuint_big nnz, NLuint nslices)
Constructs a new NLCRSMatrix.
NLuint nslices
number of slices, used by parallel spMv
Definition: nl_matrix.h:325
NLAPI NLboolean NLAPIENTRY nlCRSMatrixSave(NLCRSMatrix *M, const char *filename)
Saves a NLCRSMatrix into a file.
NLuint n
number of columns
Definition: nl_matrix.h:287
NLAPI NLMatrix NLAPIENTRY nlCRSMatrixNewFromSparseMatrixSymmetric(NLSparseMatrix *M)
Creates a compressed row storage matrix from a dynamic sparse matrix.
NLdouble * val
array of coefficient values, size = NNZ (number of non-zero coefficients)
Definition: nl_matrix.h:310
NLuint * sliceptr
slice pointers, size = nslices + 1, used by parallel spMv
Definition: nl_matrix.h:331
NLAPI void NLAPIENTRY nlCRSMatrixConstructPattern(NLCRSMatrix *M, NLuint m, NLuint n)
Constructs a new NLCRSMatrix with only the pattern.
NLAPI NLboolean NLAPIENTRY nlCRSMatrixLoad(NLCRSMatrix *M, const char *filename)
Loads a NLCRSMatrix from a file.
NLuint * colind
column indices, size = NNZ
Definition: nl_matrix.h:320
NLDestroyMatrixFunc destroy_func
destructor
Definition: nl_matrix.h:299
NLboolean symmetric_storage
NL_TRUE if symmetric storage is used, NL_FALSE otherwise.
Definition: nl_matrix.h:337
Represents a coefficient in a sparse matrix.
Definition: nl_matrix.h:135
NLuint index
index of the coefficient.
Definition: nl_matrix.h:139
NLdouble value
value of the coefficient.
Definition: nl_matrix.h:144
The base class for abstract matrices.
Definition: nl_matrix.h:80
NLMultMatrixVectorFunc mult_func
Matrix x vector product.
Definition: nl_matrix.h:108
NLuint n
number of columns
Definition: nl_matrix.h:89
NLenum type
Matrix type.
Definition: nl_matrix.h:98
NLDestroyMatrixFunc destroy_func
Destructor.
Definition: nl_matrix.h:103
NLuint m
number of rows
Definition: nl_matrix.h:84
Represents a row or a column of a sparse matrix.
Definition: nl_matrix.h:151
NLAPI void NLAPIENTRY nlRowColumnAdd(NLRowColumn *c, NLuint index, NLdouble value)
Adds a coefficient to an NLRowColumn.
NLAPI void NLAPIENTRY nlRowColumnDestroy(NLRowColumn *c)
Destroys a NLRowColumn.
NLAPI void NLAPIENTRY nlRowColumnSort(NLRowColumn *c)
Sorts the coefficients of an NLRowColumn by increasing index.
NLAPI void NLAPIENTRY nlRowColumnGrow(NLRowColumn *c)
Allocates additional storage for the coefficients of an NLRowColumn.
NLAPI void NLAPIENTRY nlRowColumnZero(NLRowColumn *c)
Zeroes an NLRowColumn.
NLAPI void NLAPIENTRY nlRowColumnConstruct(NLRowColumn *c)
Constructs a new NLRowColumn.
NLAPI void NLAPIENTRY nlRowColumnClear(NLRowColumn *c)
Zeroes an NLRowColumn and deallocates the memory used by the NLRowColumn.
NLCoeff * coeff
the array of coefficients, with enough space to store capacity coefficients.
Definition: nl_matrix.h:167
NLuint size
number of coefficients.
Definition: nl_matrix.h:155
NLAPI void NLAPIENTRY nlRowColumnAppend(NLRowColumn *c, NLuint index, NLdouble value)
Appends a coefficient to an NLRowColumn .
NLuint capacity
number of coefficients that can be stored without reallocating memory.
Definition: nl_matrix.h:161
NLuint diag_size
number of elements in the diagonal
Definition: nl_matrix.h:544
NLuint row_capacity
Number of row descriptors allocated in the row array.
Definition: nl_matrix.h:577
NLRowColumn * row
the rows if (storage & NL_MATRIX_STORE_ROWS), size = m, NULL otherwise
Definition: nl_matrix.h:560
NLAPI void NLAPIENTRY nlSparseMatrixDestroy(NLSparseMatrix *M)
Destroys an NLSparseMatrix.
NLuint column_capacity
Number of column descriptors allocated in the column array.
Definition: nl_matrix.h:583
NLAPI void NLAPIENTRY nlSparseMatrixConstruct(NLSparseMatrix *M, NLuint m, NLuint n, NLenum storage)
Constructs a new NLSparseMatrix.
NLAPI void NLAPIENTRY nlCRSMatrixAddAt(NLCRSMatrix *M, NLuint i, NLuint j, NLdouble value, NLulong index)
Adds a coefficient to an NLSparseMatrix at a known location.
NLAPI NLulong NLAPIENTRY nlCRSMatrixAdd(NLCRSMatrix *M, NLuint i, NLuint j, NLdouble value)
Adds a coefficient to an NLSparseMatrix.
NLRowColumn * column
the columns if (storage & NL_MATRIX_STORE_COLUMNS), size = n, NULL otherwise
Definition: nl_matrix.h:566
NLAPI void NLAPIENTRY nlSparseMatrixMult(NLSparseMatrix *A, const NLdouble *x, NLdouble *y)
Computes a matrix-vector product.
NLDestroyMatrixFunc destroy_func
destructor
Definition: nl_matrix.h:533
NLenum storage
indicates what is stored in this matrix
Definition: nl_matrix.h:554
NLuint m
number of rows
Definition: nl_matrix.h:516
NLMultMatrixVectorFunc mult_func
Matrix x vector product.
Definition: nl_matrix.h:538
NLAPI void NLAPIENTRY nlSparseMatrixSort(NLSparseMatrix *M)
Sorts the coefficients in an NLSParseMatrix.
NLuint diag_capacity
Number of elements allocated to store the diagonal.
Definition: nl_matrix.h:549
NLAPI void NLAPIENTRY nlCRSMatrixSetCoefficientAtRowOffset(NLCRSMatrix *M, NLuint i, NLuint j, NLdouble value, NLuint row_offset)
Sets a coefficient to an NLSparseMatrix at a known row offset.
NLAPI NLuint_big NLAPIENTRY nlSparseMatrixNNZ(NLSparseMatrix *M)
Gets the number of non-zero coefficient in an NLSparseMatrix.
NLAPI void NLAPIENTRY nlSparseMatrixClear(NLSparseMatrix *M)
Clears an NLSparseMatrix.
NLuint n
number of columns
Definition: nl_matrix.h:521
NLAPI void NLAPIENTRY nlSparseMatrixZero(NLSparseMatrix *M)
Zeroes an NLSparseMatrix.
NLenum type
Matrix type.
Definition: nl_matrix.h:528
NLAPI void NLAPIENTRY nlSparseMatrixAdd(NLSparseMatrix *M, NLuint i, NLuint j, NLdouble value)
Adds a coefficient to an NLSparseMatrix.
NLAPI NLMatrix NLAPIENTRY nlSparseMatrixNew(NLuint m, NLuint n, NLenum storage)
Constructs a new NLSparseMatrix.
NLdouble * diag
the diagonal elements, size = diag_size
Definition: nl_matrix.h:571