VeraGridEngine.Utils.Sparse package

Submodules

VeraGridEngine.Utils.Sparse.csc module

VeraGridEngine.Utils.Sparse.csc.Csc0(m: int, n: int)[source]
class VeraGridEngine.Utils.Sparse.csc.CscMat(arg1, shape=None, dtype=None, copy=False)[source]

Bases: csc_matrix

Matrix in compressed-column or triplet form.

dot(o) CscMat[source]

Dot product :param o: CscMat instance :return: CscMat instance

islands()[source]

Find islands in the matrix :return: list of islands

m
n
VeraGridEngine.Utils.Sparse.csc.csc_stack_2d_ff(mats, m_rows=1, m_cols=1, row_major=True)[source]

Assemble matrix from a list of matrices representing a β€œsuper matrix”

|mat11 | mat12 | mat13 | |mat21 | mat22 | mat23 |

if row-major turns into:

mats = [mat11, mat12, mat13, mat21, mat22, mat23]

else: (it is column major)

mats = [mat11, mat21, mat12, mat22, mat31, mat32]

m_rows = 2 m_cols = 3

Parameters:
  • mats – array of CSC matrices arranged in row-major or column-major order into a list

  • m_rows – number of rows of the mats structure

  • m_cols – number of cols of the mats structure

  • row_major – mats is sorted in row major, else it is sorted in column major

Returns:

Final assembled matrix in CSC format

VeraGridEngine.Utils.Sparse.csc.csc_stack_2d_ff_old(mats, m_rows=1, m_cols=1)[source]

Assemble matrix from a list of matrices representing a β€œsuper matrix”

|mat11 | mat12 | mat13 | |mat21 | mat22 | mat23 |

turns into:

mats = [mat11, mat12, mat13, mat21, mat22, mat23] m_rows = 2 m_cols = 3

Parameters:
  • mats – array of CSC matrices arranged in row-major order into a list

  • m_rows – number of rows of the mats structure

  • m_cols – number of cols of the mats structure

Returns:

Final assembled matrix

VeraGridEngine.Utils.Sparse.csc.dense_to_csc(mat: ndarray[tuple[Any, ...], dtype[float64]] | ndarray[tuple[int, int], dtype[float64]], threshold: float) csc_matrix[source]

Extract the sparse matrix from a dense matrix where abs values are below a threshold :param mat: dense matrix :param threshold: threshold :return: CSC sparse matrix

VeraGridEngine.Utils.Sparse.csc.diagc(n, value) csc_matrix[source]

Create constant value diagonal matrix :param n: size :param value: value :return:

VeraGridEngine.Utils.Sparse.csc.diags(array: ndarray) csc_matrix[source]

Convert array to CSC diagonal matrix :param array: :return:

VeraGridEngine.Utils.Sparse.csc.pack_3_by_4(A11: CscMat, A12: CscMat, A21: CscMat)[source]

Stack 3 CSC matrices in a 2 by 2 structure stack csc sparse float matrices like this: | A11 | A12 | | A21 | 0 | :param A11: Upper left matrix :param A12: Upper right matrix :param A21: Lower left matrix :return: Stitched matrix

VeraGridEngine.Utils.Sparse.csc.pack_4_by_4(A11: CscMat, A12: CscMat, A21: CscMat, A22: CscMat)[source]

Stack 4 CSC matrices in a 2 by 2 structure stack csc sparse float matrices like this: | A11 | A12 | | A21 | A22 | :param A11: Upper left matrix :param A12: Upper right matrix :param A21: Lower left matrix :param A22: Lower right matrix :return: Stitched matrix

VeraGridEngine.Utils.Sparse.csc.pack_4_by_4_scipy(A11: csc_matrix, A12: csc_matrix, A21: csc_matrix, A22: csc_matrix) csc_matrix[source]

Stack 4 CSC matrices in a 2 by 2 structure stack csc sparse float matrices like this: | A11 | A12 | | A21 | A22 | :param A11: Upper left matrix :param A12: Upper right matrix :param A21: Lower left matrix :param A22: Lower right matrix :return: Stitched matrix

VeraGridEngine.Utils.Sparse.csc.scipy_to_mat(scipy_mat: csc_matrix)[source]

Build CsCMat from csc_matrix :param scipy_mat: :return: CscMat

VeraGridEngine.Utils.Sparse.csc.sp_slice(mat: csc_matrix, rows, cols)[source]
/*
  • This function performs the trivial slicing of the CSC sparse matrix A

  • Steps:

    • Slice the columns with β€œsp_submat_c(A, cols)”

    • convert to CSR with .t() {transpose}

    • Slice the rows with sp_submat_c(B, rows), because it is a CSR now

    • Convert the result back to CSC with the final .t()

  • */

Parameters:
  • mat

  • rows

  • cols

Returns:

VeraGridEngine.Utils.Sparse.csc.sp_slice_cols(mat: csc_matrix, cols: ndarray)[source]

Slice columns :param mat: Matrix to slice :param cols: vector of columns :return: New sliced matrix

VeraGridEngine.Utils.Sparse.csc.sp_slice_rows(mat: csc_matrix, rows: ndarray)[source]

Slice rows :param mat: :param rows: :return: CSC matrix

VeraGridEngine.Utils.Sparse.csc.sp_transpose(mat: csc_matrix)[source]

Actual CSC transpose unlike scipy’s :param mat: CSC matrix :return: CSC transposed matrix

VeraGridEngine.Utils.Sparse.csc2 module

class VeraGridEngine.Utils.Sparse.csc2.CSC(*args, **kwargs)[source]

Bases: CSC

numba CSC matrix struct

class_type = jitclass.CSC#7beb32d82cd0<n_rows:int32,n_cols:int32,nnz:int32,data:array(float64, 1d, A),indices:array(int32, 1d, A),indptr:array(int32, 1d, A),format:unicode_type>
class VeraGridEngine.Utils.Sparse.csc2.CxCSC(*args, **kwargs)[source]

Bases: CxCSC

numba CSC matrix struct

class_type = jitclass.CxCSC#7beb32da03d0<n_rows:int32,n_cols:int32,nnz:int32,data:array(complex128, 1d, A),indices:array(int32, 1d, A),indptr:array(int32, 1d, A),format:unicode_type>
VeraGridEngine.Utils.Sparse.csc2.csc_add_cx2(Am, An, Aindptr, Aindices, Adata, Bn, Bindptr, Bindices, Bdata)[source]

C = A + B

@param A: column-compressed matrix @param B: column-compressed matrix @return: C=alpha*A + beta*B, null on error (Cm, Cn, Cp, Ci, Cx)

VeraGridEngine.Utils.Sparse.csc2.csc_add_cx3(A: CxCSC, B: CxCSC) CxCSC[source]
VeraGridEngine.Utils.Sparse.csc2.csc_add_ff(A: CSC, B: CSC, alpha=1.0, beta=1.0) CSC[source]

C = alpha*A + beta*B

@param A: column-compressed matrix @param B: column-compressed matrix @param alpha: scalar alpha @param beta: scalar beta @return: C=alpha*A + beta*B, null on error (Cm, Cn, Cp, Ci, Cx)

VeraGridEngine.Utils.Sparse.csc2.csc_add_ff2(Am, An, Aindptr, Aindices, Adata, Bn, Bindptr, Bindices, Bdata)[source]

C = A + B

@param A: column-compressed matrix @param B: column-compressed matrix @return: C=alpha*A + beta*B, null on error (Cm, Cn, Cp, Ci, Cx)

VeraGridEngine.Utils.Sparse.csc2.csc_cumsum_i(p, c, n)[source]

p [0..n] = cumulative sum of c [0..n-1], and then copy p [0..n-1] into c

@param p: size n+1, cumulative sum of c @param c: size n, overwritten with p [0..n-1] on output @param n: length of c @return: sum (c), null on error

VeraGridEngine.Utils.Sparse.csc2.csc_matvec_cx(A: CxCSC, x: ndarray) ndarray[source]
Parameters:
  • A

  • x

Returns:

VeraGridEngine.Utils.Sparse.csc2.csc_matvec_ff(A: CSC, x: ndarray) ndarray[source]
Parameters:
  • A

  • x

Returns:

VeraGridEngine.Utils.Sparse.csc2.csc_multiply_cx(A: CxCSC, B: CSC) CxCSC[source]

Sparse matrix multiplication, C = A*B where A and B are CSC sparse matrices :param A: :param B: :return:

VeraGridEngine.Utils.Sparse.csc2.csc_multiply_ff(A: CSC, B: CSC) CSC[source]

Sparse matrix multiplication, C = A*B where A and B are CSC sparse matrices :param A: :param B: :return:

VeraGridEngine.Utils.Sparse.csc2.csc_multiply_ff2(Am, An, Ap, Ai, Ax, Bm, Bn, Bp, Bi, Bx)[source]

Sparse matrix multiplication, C = A*B where A and B are CSC sparse matrices :param Am: number of rows in A :param An: number of columns in A :param Ap: column pointers of A :param Ai: indices of A :param Ax: data of A :param Bm: number of rows in B :param Bn: number of columns in B :param Bp: column pointers of B :param Bi: indices of B :param Bx: data of B :return: Cm, Cn, Cp, Ci, Cx, Cnzmax

VeraGridEngine.Utils.Sparse.csc2.csc_scatter_cx(Ap, Ai, Ax, j, beta, w, x, mark, Ci, nz)[source]

Scatters and sums a sparse vector A(:,j) into a dense vector, x = x + beta * A(:,j) :param Ap: :param Ai: :param Ax: :param j: the column of A to use :param beta: scalar multiplied by A(:,j) :param w: size m, node i is marked if w[i] = mark :param x: size m, ignored if null :param mark: mark value of w :param Ci: pattern of x accumulated in C.i :param nz: pattern of x placed in C starting at C.i[nz] :return: new value of nz, -1 on error, x and w are modified

VeraGridEngine.Utils.Sparse.csc2.csc_scatter_f(Ap, Ai, Ax, j, beta, w, x, mark, Ci, nz)[source]

Scatters and sums a sparse vector A(:,j) into a dense vector, x = x + beta * A(:,j) :param Ap: :param Ai: :param Ax: :param j: the column of A to use :param beta: scalar multiplied by A(:,j) :param w: size m, node i is marked if w[i] = mark :param x: size m, ignored if null :param mark: mark value of w :param Ci: pattern of x accumulated in C.i :param nz: pattern of x placed in C starting at C.i[nz] :return: new value of nz, -1 on error, x and w are modified

VeraGridEngine.Utils.Sparse.csc2.csc_spalloc_cx(m, n, nzmax)[source]

Allocate a sparse matrix (triplet form or compressed-column form).

@param m: number of rows @param n: number of columns @param nzmax: maximum number of entries @return: m, n, Aindptr, Aindices, Adata, Anzmax

VeraGridEngine.Utils.Sparse.csc2.csc_spalloc_f(m, n, nzmax)[source]

Allocate a sparse matrix (triplet form or compressed-column form).

@param m: number of rows @param n: number of columns @param nzmax: maximum number of entries @return: m, n, Aindptr, Aindices, Adata, Anzmax

VeraGridEngine.Utils.Sparse.csc2.csc_stack_2d_ff(mats: List[CSC], n_rows: int = 1, n_cols: int = 1) CSC[source]

Assemble matrix from a list of matrices representing a β€œsuper matrix”

|mat11 | mat12 | mat13 | |mat21 | mat22 | mat23 |

turns into:

[mat11, mat12, mat13, mat21, mat22, mat23] m_rows = 2 m_cols = 3

Parameters:
  • mats – list of CSC matrices arranged in row-major order (i.e. [mat11, mat12, mat13, mat21, mat22, mat23]

  • n_rows – number of rows of the mats structure

  • n_cols – number of cols of the mats structure

Returns:

Final assembled matrix in CSC format

VeraGridEngine.Utils.Sparse.csc2.diagc(m: int, value: float = 1.0) CSC[source]

Get diagonal sparse matrix from value :param m: size of the matrix :param value: value to set :return: CSC matrix

VeraGridEngine.Utils.Sparse.csc2.diags(array: ndarray[tuple[Any, ...], dtype[float64]]) CSC[source]

Get diagonal sparse matrix from array :param array: :return:

VeraGridEngine.Utils.Sparse.csc2.extend(A: CSC, last_col: ndarray[tuple[Any, ...], dtype[float64]], last_row: ndarray[tuple[Any, ...], dtype[float64]], corner_val: float) CSC[source]
B = | A last_col |
last_row val |
Parameters:
  • A – Original matrix

  • last_col – last column to be added to A

  • last_row – last row to be added to A | last_col

  • corner_val – The botton-right corner value

Returns:

Extended matrix

VeraGridEngine.Utils.Sparse.csc2.mat_to_scipy(csc: CSC | CxCSC) csc_matrix[source]

CSC or CxCSC Matrix to Scipy :param csc: :return:

VeraGridEngine.Utils.Sparse.csc2.pack_3_by_4(A: CSC, B: CSC, C: CSC) CSC[source]

Stack 3 CSC matrices in a 2 by 2 structure stack csc sparse float matrices like this: | A | B | | C | 0 | :param A: Upper left matrix :param B: Upper right matrix :param C: Lower left matrix :return: Stitched matrix

VeraGridEngine.Utils.Sparse.csc2.pack_4_by_4(A: CSC, B: CSC, C: CSC, D: CSC) CSC[source]

Stack 4 CSC matrices in a 2 by 2 structure stack csc sparse float matrices like this: | A | B | | C | D | :param A: Upper left matrix :param B: Upper right matrix :param C: Lower left matrix :param D: Lower right matrix :return: Stitched matrix

VeraGridEngine.Utils.Sparse.csc2.scipy_to_cxmat(mat: csc_matrix) CxCSC[source]

Scipy CSC matrix to CxCSC marix :param mat: Scipy CSC matrix :return: CxCSC marix

VeraGridEngine.Utils.Sparse.csc2.scipy_to_mat(mat: csc_matrix) CSC[source]

Scipy CSC matrix to CSC marix :param mat: Scipy CSC matrix :return: CSC marix

VeraGridEngine.Utils.Sparse.csc2.sp_slice(A: CSC, rows: ndarray[tuple[Any, ...], dtype[int64]], cols: ndarray[tuple[Any, ...], dtype[int64]])[source]
/*
  • This function performs the trivial slicing of the CSC sparse matrix A

  • Steps:

    • Slice the columns with β€œsp_submat_c(A, cols)”

    • convert to CSR with .t() {transpose}

    • Slice the rows with sp_submat_c(B, rows), because it is a CSR now

    • Convert the result back to CSC with the final .t()

  • */

Parameters:
  • A

  • rows

  • cols

Returns:

VeraGridEngine.Utils.Sparse.csc2.sp_slice_cols(A: CSC, cols: ndarray[tuple[Any, ...], dtype[int64]]) CSC[source]

Slice columns :param A: Matrix to slice :param cols: vector of columns :return: New sliced matrix

VeraGridEngine.Utils.Sparse.csc2.sp_slice_rows(mat: CSC, rows: ndarray) CSC[source]

Slice rows :param mat: :param rows: :return: CSC matrix

VeraGridEngine.Utils.Sparse.csc2.sp_transpose(A: CSC) CSC[source]

Actual CSC transpose unlike scipy’s :param A: CSC matrix :return: CSC transposed matrix

VeraGridEngine.Utils.Sparse.csc2.spfactor(A: CSC) None | SuperLU[source]

Sparse factorization with SuperLU :param A: CSC matrix :return: SuperLU factorization object

VeraGridEngine.Utils.Sparse.csc2.spsolve_csc(A: CSC, x: ndarray[tuple[Any, ...], dtype[float64]]) ndarray[tuple[Any, ...], dtype[float64]][source]

Sparse solution :param A: CSC matrix :param x: vector :return: solution

VeraGridEngine.Utils.Sparse.csc_numba module

VeraGridEngine.Utils.Sparse.csc_numba.binary_find(N, x, array)[source]

Binary search :param N: size of the array :param x: value :param array: array :return: position where it is found. -1 if it is not found

VeraGridEngine.Utils.Sparse.csc_numba.coo_to_csc(m, n, Ti, Tj, Tx, nnz)[source]

C = compressed-column form of a triplet matrix T. The columns of T are not sorted, and duplicate entries may be present in T.

Parameters:
  • m – row number

  • n – column number

  • Ti – array of row indices

  • Tj – array of column indices

  • Tx – array of data

  • nnz – non-zero entries, if there are no duplicate entries, nnz = len(Tx)

Returns:

Cm, Cn, Cp, Ci, Cx

VeraGridEngine.Utils.Sparse.csc_numba.csc_add_ff(Am, An, Aindptr, Aindices, Adata, Bm, Bn, Bindptr, Bindices, Bdata, alpha, beta)[source]

C = alpha*A + beta*B

@param A: column-compressed matrix @param B: column-compressed matrix @param alpha: scalar alpha @param beta: scalar beta @return: C=alpha*A + beta*B, null on error (Cm, Cn, Cp, Ci, Cx)

VeraGridEngine.Utils.Sparse.csc_numba.csc_cumsum_i(p, c, n)[source]

p [0..n] = cumulative sum of c [0..n-1], and then copy p [0..n-1] into c

@param p: size n+1, cumulative sum of c @param c: size n, overwritten with p [0..n-1] on output @param n: length of c @return: sum (c), null on error

VeraGridEngine.Utils.Sparse.csc_numba.csc_diagonal(m, value=1.0)[source]

Build CSC diagonal matrix of the given value :param m: size :param value: value :return: CSC matrix

VeraGridEngine.Utils.Sparse.csc_numba.csc_diagonal_from_array(array)[source]
Parameters:
  • m

  • array

Returns:

VeraGridEngine.Utils.Sparse.csc_numba.csc_diagonal_from_complex_array(array)[source]
Parameters:
  • m

  • array

Returns:

VeraGridEngine.Utils.Sparse.csc_numba.csc_diagonal_from_number(m: int, value: float)[source]
Parameters:
  • m

  • value

Returns:

VeraGridEngine.Utils.Sparse.csc_numba.csc_mat_vec_ff(m, n, Ap, Ai, Ax, x)[source]

Sparse matrix times dense column vector, y = A * x. :param m: number of rows :param n: number of columns :param Ap: pointers :param Ai: indices :param Ax: data :param x: vector x (n) :return: vector y (m)

VeraGridEngine.Utils.Sparse.csc_numba.csc_multiply_ff(Am, An, Ap, Ai, Ax, Bm, Bn, Bp, Bi, Bx)[source]

Sparse matrix multiplication, C = A*B where A and B are CSC sparse matrices :param Am: number of rows in A :param An: number of columns in A :param Ap: column pointers of A :param Ai: indices of A :param Ax: data of A :param Bm: number of rows in B :param Bn: number of columns in B :param Bp: column pointers of B :param Bi: indices of B :param Bx: data of B :return: Cm, Cn, Cp, Ci, Cx, Cnzmax

VeraGridEngine.Utils.Sparse.csc_numba.csc_norm(n, Ap, Ax)[source]

Computes the 1-norm of a sparse matrix = max (sum (abs (A))), largest column sum.

@param A: column-compressed matrix @return: the 1-norm if successful, -1 on error

VeraGridEngine.Utils.Sparse.csc_numba.csc_scatter_f(Ap, Ai, Ax, j, beta, w, x, mark, Ci, nz)[source]

Scatters and sums a sparse vector A(:,j) into a dense vector, x = x + beta * A(:,j) :param Ap: :param Ai: :param Ax: :param j: the column of A to use :param beta: scalar multiplied by A(:,j) :param w: size m, node i is marked if w[i] = mark :param x: size m, ignored if null :param mark: mark value of w :param Ci: pattern of x accumulated in C.i :param nz: pattern of x placed in C starting at C.i[nz] :return: new value of nz, -1 on error, x and w are modified

VeraGridEngine.Utils.Sparse.csc_numba.csc_scatter_ff(Aindptr, Aindices, Adata, j, beta, w, x, mark, Ci, nz)[source]

Scatters and sums a sparse vector A(:,j) into a dense vector, x = x + beta * A(:,j) :param Aindptr: :param Aindices: :param Adata: :param j: the column of A to use :param beta: scalar multiplied by A(:,j) :param w: size m, node i is marked if w[i] = mark :param x: size m, ignored if null :param mark: mark value of w :param Ci: pattern of x accumulated in C.i :param nz: pattern of x placed in C starting at C.i[nz] :return: new value of nz, -1 on error, x and w are modified

VeraGridEngine.Utils.Sparse.csc_numba.csc_spalloc_f(m, n, nzmax)[source]

Allocate a sparse matrix (triplet form or compressed-column form).

@param m: number of rows @param n: number of columns @param nzmax: maximum number of entries @return: m, n, Aindptr, Aindices, Adata, Anzmax

VeraGridEngine.Utils.Sparse.csc_numba.csc_sprealloc_f(An, Aindptr, Aindices, Adata, nzmax)[source]

Change the max # of entries a sparse matrix can hold. :param An: number of columns :param Aindptr: csc column pointers :param Aindices: csc row indices :param Adata: csc data :param nzmax:new maximum number of entries :return: indices, data, nzmax

VeraGridEngine.Utils.Sparse.csc_numba.csc_stack_2d_ff_col_major(mats_data, mats_indptr, mats_indices, mats_cols, mats_rows, m_rows=1, m_cols=1)[source]

Assemble matrix from a list of matrices representing a β€œsuper matrix”

|mat11 | mat12 | mat13 | |mat21 | mat22 | mat23 |

turns into:

mats = [mat11, mat12, mat13, mat21, mat22, mat23] m_rows = 2 m_cols = 3

Parameters:
  • mats_data – array of numpy arrays with the data of each CSC matrix

  • mats_indptr – array of numpy arrays with the indptr of each CSC matrix

  • mats_indices – array of numpy arrays with the indices of each CSC matrix

  • mats_cols – array with the number of columns of each CSC matrix

  • mats_rows – array with the number of rows of each CSC matrix

  • m_rows – number of rows of the mats structure

  • m_cols – number of cols of the mats structure

Returns:

Final assembled matrix

VeraGridEngine.Utils.Sparse.csc_numba.csc_stack_2d_ff_row_major(mats_data, mats_indptr, mats_indices, mats_cols, mats_rows, m_rows=1, m_cols=1)[source]

Assemble matrix from a list of matrices representing a β€œsuper matrix”

|mat11 | mat12 | mat13 | |mat21 | mat22 | mat23 |

turns into:

mats = [mat11, mat12, mat13, mat21, mat22, mat23] m_rows = 2 m_cols = 3

Parameters:
  • mats_data – array of numpy arrays with the data of each CSC matrix

  • mats_indptr – array of numpy arrays with the indptr of each CSC matrix

  • mats_indices – array of numpy arrays with the indices of each CSC matrix

  • mats_cols – array with the number of columns of each CSC matrix

  • mats_rows – array with the number of rows of each CSC matrix

  • m_rows – number of rows of the mats structure

  • m_cols – number of cols of the mats structure

Returns:

Final assembled matrix

VeraGridEngine.Utils.Sparse.csc_numba.csc_stack_3_by_4_ff(am, an, Ai, Ap, Ax, bm, bn, Bi, Bp, Bx, cm, cn, Ci, Cp, Cx)[source]

stack csc sparse float matrices like this: | A | B | | C | 0 |

Parameters:
  • am

  • an

  • Ai

  • Ap

  • Ax

  • bm

  • bn

  • Bi

  • Bp

  • Bx

  • cm

  • cn

  • Ci

  • Cp

  • Cx

Returns:

VeraGridEngine.Utils.Sparse.csc_numba.csc_stack_4_by_4_ff(am, an, Ai, Ap, Ax, bm, bn, Bi, Bp, Bx, cm, cn, Ci, Cp, Cx, dm, dn, Di, Dp, Dx)[source]

stack csc sparse float matrices like this: | A | B | | C | D |

Parameters:
  • am

  • an

  • Ai

  • Ap

  • Ax

  • bm

  • bn

  • Bi

  • Bp

  • Bx

  • cm

  • cn

  • Ci

  • Cp

  • Cx

  • dm

  • dn

  • Di

  • Dp

  • Dx

Returns:

VeraGridEngine.Utils.Sparse.csc_numba.csc_sub_matrix(Am, Annz, Ap, Ai, Ax, rows, cols)[source]

CSC matrix sub-matrix view :param Am: number of rows :param Annz: number of non-zero entries :param Ap: Column pointers :param Ai: Row indices :param Ax: Data :param rows: array of selected rows: must be sorted! to use the binary search :param cols: array of columns: should be sorted :return:

VeraGridEngine.Utils.Sparse.csc_numba.csc_sub_matrix_cols(Am, Anz, Ap, Ai, Ax, cols)[source]

Get SCS arbitrary sub-matrix with all the rows :param Am: number of rows :param Anz: number of non-zero entries :param Ap: Column pointers :param Ai: Row indices :param Ax: Data :param cols: column indices to keep :return: CSC sub-matrix (n, new_col_ptr, new_row_ind, new_val)

VeraGridEngine.Utils.Sparse.csc_numba.csc_sub_matrix_old(Am, Anz, Ap, Ai, Ax, rows, cols)[source]

Get SCS arbitrary sub-matrix :param Am: number of rows :param Anz: number of non-zero entries :param Ap: Column pointers :param Ai: Row indices :param Ax: Data :param rows: row indices to keep :param cols: column indices to keep :return: CSC sub-matrix (n, new_col_ptr, new_row_ind, new_val)

VeraGridEngine.Utils.Sparse.csc_numba.csc_sub_matrix_rows(An, Anz, Ap, Ai, Ax, rows)[source]

Get SCS arbitrary sub-matrix :param An: number of rows :param Anz: number of non-zero entries :param Ap: Column pointers :param Ai: Row indices :param Ax: Data :param rows: row indices to keep :return: CSC sub-matrix (n, new_col_ptr, new_row_ind, new_val)

VeraGridEngine.Utils.Sparse.csc_numba.csc_to_csr(m, n, Ap, Ai, Ax, Bp, Bi, Bx)[source]

Convert a CSC Matrix into a CSR Matrix :param m: number of rows :param n: number of columns :param Ap: indptr of the CSC matrix :param Ai: indices of the CSC matrix :param Ax: data of the CSC matrix :param Bp: indptr of the CSR matrix (to compute, size β€˜m+1’, has to be initialized to zeros) :param Bi: indices of the CSR matrix (to compute, size nnz) :param Bx: data of the CSR matrix (to compute, size nnz)

VeraGridEngine.Utils.Sparse.csc_numba.csc_to_dense(m, n, indptr, indices, data)[source]

Convert csc matrix to dense :param m: :param n: :param indptr: :param indices: :param data: :return: 2d numpy array

VeraGridEngine.Utils.Sparse.csc_numba.csc_transpose(m, n, Ap, Ai, Ax)[source]

Transpose matrix :param m: A.m :param n: A.n :param Ap: A.indptr :param Ai: A.indices :param Ax: A.data :return: Cm, Cn, Cp, Ci, Cx

VeraGridEngine.Utils.Sparse.csc_numba.dense_to_csc_numba(mat: ndarray[tuple[Any, ...], dtype[float64]] | ndarray[tuple[int, int], dtype[float64]], threshold: float) Tuple[ndarray[tuple[Any, ...], dtype[float64]], ndarray[tuple[Any, ...], dtype[int64]], ndarray[tuple[Any, ...], dtype[int64]]][source]

Extract the sparse matrix from a dense matrix where abs values are below a threshold :param mat: dense matrix :param threshold: threshold :return: data, indices, indptr

VeraGridEngine.Utils.Sparse.csc_numba.diag_positions(n, Ap, Ai)[source]

get the positions of the diagonal in the CSC data scheme :param n: number of rows and columns (must be square) :param Ap: pointers :param Ai: indices :return: vector pos (n)

VeraGridEngine.Utils.Sparse.csc_numba.find_islands(node_number, indptr, indices)[source]

Method to get the islands of a graph This is the non-recursive version :return: islands list where each element is a list of the node indices of the island

VeraGridEngine.Utils.Sparse.csc_numba.get_sparse_array_numba(arr: ndarray[tuple[Any, ...], dtype[float64]], threshold: float) Tuple[ndarray[tuple[Any, ...], dtype[float64]], ndarray[tuple[Any, ...], dtype[int64]]][source]

Extract the sparse array from a dense array where abs values are below a threshold :param arr: dense matrix :param threshold: threshold :return: data, indices

VeraGridEngine.Utils.Sparse.csc_numba.ialloc(n)[source]
VeraGridEngine.Utils.Sparse.csc_numba.sp_submat_c_numba(nrows, ptrs, indices, values, cols)[source]

slice CSC columns :param nrows: number of rows of the matrix :param ptrs: row pointers :param indices: column indices :param values: data :param cols: vector of columns to slice :return: new_indices, new_col_ptr, new_val, nrows, ncols

VeraGridEngine.Utils.Sparse.csc_numba.xalloc(n)[source]

VeraGridEngine.Utils.Sparse.sparse_array module

class VeraGridEngine.Utils.Sparse.sparse_array.SparseArray[source]

Bases: object

at(idx: int) Any[source]

Get the array at a position :param idx: index :return: Numeric value

clear()[source]

Clear the sparse array :return:

copy() SparseArray[source]

Get a deep copy of this object :return: A new SparseArray copy of this object

create(size: int, default_value: type | DeviceType | GenericAlias, data: Dict[int, int | float | bool | complex] | None = None) SparseArray[source]

Build sparse from definition :param size: size :param default_value: default value :param data: data map

create_from_array(array: ndarray[tuple[Any, ...], dtype[float64]], default_value: type | DeviceType | GenericAlias) SparseArray[source]

Build sparse from array :param array: NumericVec :param default_value: default value of the array

create_from_dict(default_value: type | DeviceType | GenericAlias, size: int, map_data: Dict[int, int | float | bool | complex]) SparseArray[source]

Create this array from dict data :param default_value: :param size: :param map_data: :return:

property default_value

Default value getter :return: numeric value

property dtype: bool | int | float | DeviceType

Get the declared type :return: type

fill(value: Any)[source]

Fill the sparse array with the same value :param value: any value

get_map() Dict[int, int | float | bool | complex][source]

Return the dictionary hosting the sparse data :return: Dict[int, Numeric]

get_sparse_representation()[source]

Get the sparse representation of the sparse data :return:

get_sparsity() float[source]

Get the sparsity of this profile :return: Sparsity metric

info()[source]

Return dictionary with information about the profile object and its content :return:

insert(i: int, x: int | float | bool | complex)[source]

Insert an element in the data dictionary :param i: :param x: :return:

resample(indices: ndarray[tuple[Any, ...], dtype[int64]])[source]

Resample this sparse array in-place :param indices: array of integer indices (not repeated)

resize(n: int)[source]

Resize the array :param n:number of elements.

If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them)

set_data(d: Dict[int, Any])[source]
set_sparse_data_from_data(indptr, data)[source]
Parameters:
  • indptr

  • data

Returns:

size() int[source]

Get the size :return: integer

slice(indices: ndarray[tuple[Any, ...], dtype[int64]]) SparseArray[source]

Get a resampled copy of this sparse array :param indices: array of integer indices (not repeated)

toarray() ndarray[tuple[Any, ...], dtype[float64]][source]

Get numpy vector from this sparse structure :return: NumericVec

class VeraGridEngine.Utils.Sparse.sparse_array.SparseObjectArray(n: int)[source]

Bases: object

SparseArray

at(idx: int) Any[source]

Get the array at a position :param idx: index :return: Numeric value

copy() SparseObjectArray[source]

Get a deep copy of this object :return: A new SparseObjectArray copy of this object

get_map() Dict[int, object][source]

Return the dictionary hosting the sparse data :return: Dict[int, Numeric]

get_sparsity() float[source]

Get the sparsity of this profile :return: Sparsity metric

info()[source]

Return dictionary with information about the profile object and its content :return:

insert(i: int, x: object)[source]

Insert an element in the data dictionary :param i: :param x: :return:

resample(indices: ndarray[tuple[Any, ...], dtype[int64]])[source]

Resample this sparse array in-place :param indices: array of integer indices (not repeated)

resize(n: int)[source]

Resize the array :param n:number of elements.

If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them)

size() int[source]

Get the size :return: integer

slice(indices: ndarray[tuple[Any, ...], dtype[int64]]) SparseObjectArray[source]

Get a resampled copy of this sparse array :param indices: array of integer indices (not repeated)

VeraGridEngine.Utils.Sparse.sparse_array.check_type(dtype: type | DeviceType | GenericAlias, value: Any) bool[source]

Checks that the type of value is the declared type in the profile :param dtype: expected type :param value: Any value :return:

VeraGridEngine.Utils.Sparse.utils module

VeraGridEngine.Utils.Sparse.utils.dense_to_str(mat: ndarray)[source]

Turn dense 2D numpy array into a string :param mat: 2D numpy array :return: string

VeraGridEngine.Utils.Sparse.utils.slice_to_range(sl: slice, n)[source]

Turn a slice into a range :param sl: slice object :param n: total number of items :return: range object, if the slice is not supported an exception is raised

Module contents