|
1 | 1 | 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 |
2 | 7 |
|
3 | 8 | import numpy as np |
4 | 9 | cimport numpy as cnp |
@@ -101,9 +106,9 @@ cdef extern from "umfpack.h": |
101 | 106 | int umfpack_di_get_lunz(int * lnz, int * unz, int * n_row, int * n_col, |
102 | 107 | int * nz_udiag, void * numeric) |
103 | 108 |
|
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, |
107 | 112 | int * do_recip, double * Rs, |
108 | 113 | void * numeric) |
109 | 114 |
|
@@ -410,6 +415,92 @@ cdef class UmfpackSolver: |
410 | 415 |
|
411 | 416 | return (lnz, unz, n_row, n_col, nz_udiag) |
412 | 417 |
|
| 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 | + |
413 | 504 | #################################################################################################################### |
414 | 505 | # REPORTING ROUTINES |
415 | 506 | #################################################################################################################### |
|
0 commit comments