Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

from __future__ import print_function, division 

 

from sympy.core.compatibility import range 

from sympy import SparseMatrix 

 

 

def _doktocsr(dok): 

    """Converts a sparse matrix to Compressed Sparse Row (CSR) format. 

 

    Parameters 

    ========== 

 

    A : contains non-zero elements sorted by key (row, column) 

    JA : JA[i] is the column corresponding to A[i] 

    IA : IA[i] contains the index in A for the first non-zero element 

        of row[i]. Thus IA[i+1] - IA[i] gives number of non-zero 

        elements row[i]. The length of IA is always 1 more than the 

        number of rows in the matrix. 

    """ 

    row, JA, A = [list(i) for i in zip(*dok.row_list())] 

    IA = [0]*((row[0] if row else 0) + 1) 

    for i, r in enumerate(row): 

        IA.extend([i]*(r - row[i - 1]))  # if i = 0 nothing is extended 

    IA.extend([len(A)]*(dok.rows - len(IA) + 1)) 

    shape = [dok.rows, dok.cols] 

    return [A, JA, IA, shape] 

 

 

def _csrtodok(csr): 

    """Converts a CSR representation to DOK representation""" 

    smat = {} 

    A, JA, IA, shape = csr 

    for i in range(len(IA) - 1): 

        indices = slice(IA[i], IA[i + 1]) 

        for l, m in zip(A[indices], JA[indices]): 

            smat[i, m] = l 

    return SparseMatrix(*(shape + [smat]))