Geogram Version 1.8.5
A programming library of geometric algorithms
Loading...
Searching...
No Matches
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
51extern "C" {
52#endif
53
54/******************************************************************************/
55/* Abstract matrix interface */
56
57struct NLMatrixStruct;
58typedef struct NLMatrixStruct* NLMatrix;
59
63typedef void(*NLDestroyMatrixFunc)(NLMatrix M);
64
68typedef 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
110
116NLAPI void NLAPIENTRY nlDeleteMatrix(NLMatrix M);
117
124NLAPI void NLAPIENTRY nlMultMatrixVector(
125 NLMatrix M, const double* x, double* y
126);
127
128/******************************************************************************/
129/* Dynamic arrays for sparse row/columns */
130
135typedef struct {
140
145} NLCoeff;
146
151typedef struct {
156
162
169
176NLAPI void NLAPIENTRY nlRowColumnConstruct(NLRowColumn* c);
177
186NLAPI void NLAPIENTRY nlRowColumnDestroy(NLRowColumn* c);
187
198NLAPI void NLAPIENTRY nlRowColumnGrow(NLRowColumn* c);
199
213NLAPI void NLAPIENTRY nlRowColumnAdd(
214 NLRowColumn* c, NLuint index, NLdouble value
215);
216
227NLAPI void NLAPIENTRY nlRowColumnAppend(
228 NLRowColumn* c, NLuint index, NLdouble value
229);
230
238NLAPI void NLAPIENTRY nlRowColumnZero(NLRowColumn* c);
239
247NLAPI void NLAPIENTRY nlRowColumnClear(NLRowColumn* c);
248
255NLAPI 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
339
350NLAPI void NLAPIENTRY nlCRSMatrixConstruct(
351 NLCRSMatrix* M, NLuint m, NLuint n, NLuint_big nnz, NLuint nslices
352);
353
361NLAPI void NLAPIENTRY nlCRSMatrixConstructSymmetric(
362 NLCRSMatrix* M, NLuint n, NLuint_big nnz
363);
364
365
375NLAPI void NLAPIENTRY nlCRSMatrixConstructPattern(
376 NLCRSMatrix* M, NLuint m, NLuint n
377);
378
389 NLCRSMatrix* M, NLuint n
390);
391
397NLAPI void NLAPIENTRY nlCRSMatrixPatternSetRowLength(
398 NLCRSMatrix* M, NLuint i, NLuint n
399);
400
401
407NLAPI void NLAPIENTRY nlCRSMatrixPatternCompile(NLCRSMatrix* M);
408
419NLAPI void NLAPIENTRY nlCRSMatrixAdd(
420 NLCRSMatrix* M, NLuint i, NLuint j, NLdouble value
421);
422
423
432NLAPI NLboolean NLAPIENTRY nlCRSMatrixLoad(
433 NLCRSMatrix* M, const char* filename
434);
435
444NLAPI NLboolean NLAPIENTRY nlCRSMatrixSave(
445 NLCRSMatrix* M, const char* filename
446);
447
456
457/******************************************************************************/
458/* SparseMatrix data structure */
459
464#define NL_MATRIX_STORE_ROWS 1
465
470#define NL_MATRIX_STORE_COLUMNS 2
471
477#define NL_MATRIX_STORE_SYMMETRIC 4
478
553
554
566 NLuint m, NLuint n, NLenum storage
567);
568
578NLAPI void NLAPIENTRY nlSparseMatrixConstruct(
579 NLSparseMatrix* M, NLuint m, NLuint n, NLenum storage
580);
581
589NLAPI void NLAPIENTRY nlSparseMatrixDestroy(NLSparseMatrix* M);
590
598NLAPI void NLAPIENTRY nlSparseMatrixMult(
599 NLSparseMatrix* A, const NLdouble* x, NLdouble* y
600);
601
612NLAPI void NLAPIENTRY nlSparseMatrixAdd(
613 NLSparseMatrix* M, NLuint i, NLuint j, NLdouble value
614);
615
626NLAPI void NLAPIENTRY nlSparseMatrixAddMatrix(
627 NLSparseMatrix* M, double mul, const NLMatrix N
628);
629
636NLAPI void NLAPIENTRY nlSparseMatrixZero( NLSparseMatrix* M);
637
644NLAPI void NLAPIENTRY nlSparseMatrixClear( NLSparseMatrix* M);
645
654
660NLAPI void NLAPIENTRY nlSparseMatrixSort( NLSparseMatrix* M);
661
666NLAPI void NLAPIENTRY nlSparseMatrixAddRow( NLSparseMatrix* M);
667
672NLAPI void NLAPIENTRY nlSparseMatrixAddColumn( NLSparseMatrix* M);
673
682NLAPI void NLAPIENTRY nlSparseMatrixMAddRow(
683 NLSparseMatrix* M, NLuint i1, double s, NLuint i2
684);
685
694NLAPI void NLAPIENTRY nlSparseMatrixScaleRow(
695 NLSparseMatrix* M, NLuint i, double s
696);
697
705NLAPI void NLAPIENTRY nlSparseMatrixZeroRow(
707);
708
709
710/******************************************************************************/
711
721
733);
734
735
743NLAPI void NLAPIENTRY nlMatrixCompress(NLMatrix* M);
744
751NLAPI NLuint_big NLAPIENTRY nlMatrixNNZ(NLMatrix M);
752
767NLAPI NLMatrix NLAPIENTRY nlMatrixFactorize(NLMatrix M, NLenum solver);
768
769/******************************************************************************/
770
774 typedef void(*NLMatrixFunc)(const double* x, double* y);
775
784 NLuint m, NLuint n, NLMatrixFunc func
785);
786
794
795/******************************************************************************/
796
814 NLMatrix M, NLboolean product_owns_M,
815 NLMatrix N, NLboolean product_owns_N
816);
817
818/******************************************************************************/
819
820#ifdef __cplusplus
821}
822#endif
823
824#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
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:774
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:511
NLuint row_capacity
Number of row descriptors allocated in the row array.
Definition nl_matrix.h:544
NLRowColumn * row
the rows if (storage & NL_MATRIX_STORE_ROWS), size = m, NULL otherwise
Definition nl_matrix.h:527
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:550
NLAPI void NLAPIENTRY nlSparseMatrixConstruct(NLSparseMatrix *M, NLuint m, NLuint n, NLenum storage)
Constructs a new NLSparseMatrix.
NLRowColumn * column
the columns if (storage & NL_MATRIX_STORE_COLUMNS), size = n, NULL otherwise
Definition nl_matrix.h:533
NLAPI void NLAPIENTRY nlCRSMatrixAdd(NLCRSMatrix *M, NLuint i, NLuint j, NLdouble value)
Adds a coefficient to an NLSparseMatrix.
NLAPI void NLAPIENTRY nlSparseMatrixMult(NLSparseMatrix *A, const NLdouble *x, NLdouble *y)
Computes a matrix-vector product.
NLDestroyMatrixFunc destroy_func
destructor
Definition nl_matrix.h:500
NLenum storage
indicates what is stored in this matrix
Definition nl_matrix.h:521
NLuint m
number of rows
Definition nl_matrix.h:483
NLMultMatrixVectorFunc mult_func
Matrix x vector product.
Definition nl_matrix.h:505
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:516
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:488
NLAPI void NLAPIENTRY nlSparseMatrixZero(NLSparseMatrix *M)
Zeroes an NLSparseMatrix.
NLenum type
Matrix type.
Definition nl_matrix.h:495
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:538