Skip to content

Commit 7a49c10

Browse files
author
counterclocker
committed
first draft of umfpack done
1 parent 5933190 commit 7a49c10

7 files changed

Lines changed: 1655 additions & 607 deletions

File tree

examples/umfpack.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,13 @@
3939
print "&" * 80
4040

4141
print solver.get_lunz()
42+
43+
(L, U, P, Q, D, do_recip, R) = solver.get_LU()
44+
45+
print L
46+
print U
47+
print P
48+
print Q
49+
print D
50+
print do_recip
51+
print R

setup.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ def get_path_option(config, section, option):
3939
suitesparse_include_dirs = get_path_option(cysparse_config, 'SUITESPARSE', 'include_dirs')
4040
suitesparse_library_dirs = get_path_option(cysparse_config, 'SUITESPARSE', 'library_dirs')
4141

42-
print "TOTO:"
43-
print suitesparse_include_dirs
44-
4542
########################################################################################################################
4643
# EXTENSIONS
4744
########################################################################################################################

sparse_lib/solvers/suitesparse/umfpack.c

Lines changed: 1532 additions & 584 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sparse_lib/solvers/suitesparse/umfpack.pyx

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
from sparse_lib.sparse.ll_mat cimport LLSparseMatrix
2+
from sparse_lib.sparse.csr_mat cimport CSRSparseMatrix, MakeCSRSparseMatrix
3+
from sparse_lib.sparse.csc_mat cimport CSCSparseMatrix, MakeCSCSparseMatrix
4+
5+
6+
from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free
27

38
import numpy as np
49
cimport numpy as cnp
@@ -101,9 +106,9 @@ cdef extern from "umfpack.h":
101106
int umfpack_di_get_lunz(int * lnz, int * unz, int * n_row, int * n_col,
102107
int * nz_udiag, void * numeric)
103108

104-
int umfpack_di_get_numeric(int * Lp, int * Lj, double * Lx, double * Lz,
105-
int * Up, int * Ui, double * Ux, double * Uz,
106-
int * P, int * Q, double * Dx, double * Dz,
109+
int umfpack_di_get_numeric(int * Lp, int * Lj, double * Lx,
110+
int * Up, int * Ui, double * Ux,
111+
int * P, int * Q, double * Dx,
107112
int * do_recip, double * Rs,
108113
void * numeric)
109114

@@ -410,6 +415,92 @@ cdef class UmfpackSolver:
410415

411416
return (lnz, unz, n_row, n_col, nz_udiag)
412417

418+
def get_LU(self):
419+
"""
420+
Return LU factorisation objects. If needed, the LU factorisation is triggered.
421+
422+
Returns:
423+
(L, U, P, Q, D, do_recip, R)
424+
425+
"""
426+
# TODO: use properties?? we can only get matrices, not set them...
427+
self.create_numeric()
428+
429+
cdef:
430+
int lnz
431+
int unz
432+
int n_row
433+
int n_col
434+
int nz_udiag
435+
436+
int _do_recip
437+
438+
(lnz, unz, n_row, n_col, nz_udiag) = self.get_lunz()
439+
440+
# L CSR matrix
441+
cdef int * Lp = <int *> PyMem_Malloc((n_row + 1) * sizeof(int))
442+
if not Lp:
443+
raise MemoryError()
444+
445+
cdef int * Lj = <int *> PyMem_Malloc(lnz * sizeof(int))
446+
if not Lj:
447+
raise MemoryError()
448+
449+
cdef double * Lx = <double *> PyMem_Malloc(lnz * sizeof(double))
450+
if not Lx:
451+
raise MemoryError()
452+
453+
# U CSC matrix
454+
cdef int * Up = <int *> PyMem_Malloc((n_col + 1) * sizeof(int))
455+
if not Up:
456+
raise MemoryError()
457+
458+
cdef int * Ui = <int *> PyMem_Malloc(unz * sizeof(int))
459+
if not Ui:
460+
raise MemoryError()
461+
462+
cdef double * Ux = <double *> PyMem_Malloc(unz * sizeof(double))
463+
if not Ux:
464+
raise MemoryError()
465+
466+
cdef cnp.ndarray[cnp.int_t, ndim=1, mode='c'] P
467+
cdef cnp.ndarray[cnp.int_t, ndim=1, mode='c'] Q
468+
cdef cnp.ndarray[cnp.double_t, ndim=1, mode='c'] D
469+
cdef cnp.ndarray[cnp.double_t, ndim=1, mode='c'] R
470+
471+
cdef cnp.npy_intp *dims_n_row = [n_row]
472+
cdef cnp.npy_intp *dims_n_col = [n_col]
473+
#cdef cnp.ndarray[cnp.int_t, ndim=1] result = \
474+
cdef cnp.npy_intp *dims_min = [min(n_row, n_col)]
475+
476+
P = cnp.PyArray_EMPTY(1, dims_n_row, cnp.NPY_INTP, 0)
477+
Q = cnp.PyArray_EMPTY(1, dims_n_col, cnp.NPY_INTP, 0)
478+
479+
D = cnp.PyArray_EMPTY(1, dims_min, cnp .NPY_DOUBLE, 0)
480+
481+
R = cnp.PyArray_EMPTY(1, dims_n_row, cnp .NPY_DOUBLE, 0)
482+
483+
484+
cdef int status =umfpack_di_get_numeric(Lp, Lj, Lx,
485+
Up, Ui, Ux,
486+
<int *> P.data, <int *> Q.data, <double *> D.data,
487+
&_do_recip, <double *> R.data,
488+
self.numeric)
489+
490+
if status != UMFPACK_OK:
491+
test_umfpack_result(status, "get_LU()")
492+
493+
cdef bint do_recip = _do_recip
494+
495+
cdef CSRSparseMatrix L
496+
cdef CSCSparseMatrix U
497+
498+
cdef int size = min(n_row,n_col)
499+
L = MakeCSRSparseMatrix(nrow=size, ncol=size, nnz=lnz, ind=Lp, col=Lj, val=Lx)
500+
U = MakeCSCSparseMatrix(nrow=size, ncol=size, nnz=unz, ind=Up, row=Ui, val=Ux)
501+
502+
return (L, U, P, Q, D, do_recip, R)
503+
413504
####################################################################################################################
414505
# REPORTING ROUTINES
415506
####################################################################################################################

sparse_lib/sparse/csr_mat.c

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sparse_lib/sparse/csr_mat.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ cdef class CSRSparseMatrix(ImmutableSparseMatrix):
2525

2626
cdef _order_column_indices(self)
2727

28-
cdef MakeCSRSparseMatrix(int nrow, int ncol, int nnz, int * ind, int * col, double * val)
28+
cdef MakeCSRSparseMatrix(int nrow, int ncol, int nnz, int * ind, int * col, double * val)

sparse_lib/sparse/csr_mat.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ cdef MakeCSRSparseMatrix(int nrow, int ncol, int nnz, int * ind, int * col, doub
263263
col (int *): C-array with column indices.
264264
val (double *): C-array with values.
265265
"""
266+
267+
266268
csr_mat = CSRSparseMatrix(nrow=nrow, ncol=ncol, nnz=nnz)
267269

268270
csr_mat.val = val

0 commit comments

Comments
 (0)