File: test_extract.py

package info (click to toggle)
python-scipy 0.10.1%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 42,232 kB
  • sloc: cpp: 224,773; ansic: 103,496; python: 85,210; fortran: 79,130; makefile: 272; sh: 43
file content (44 lines) | stat: -rw-r--r-- 1,497 bytes parent folder | download
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
38
39
40
41
42
43
44
"""test sparse matrix construction functions"""

from numpy.testing import TestCase, assert_equal
from scipy.sparse import csr_matrix

import numpy as np
from scipy.sparse import extract


class TestExtract(TestCase):
    def setUp(self):
        cases = []

        cases.append( csr_matrix( [[1,2]] ) )
        cases.append( csr_matrix( [[1,0]] ) )
        cases.append( csr_matrix( [[0,0]] ) )
        cases.append( csr_matrix( [[1],[2]] ) )
        cases.append( csr_matrix( [[1],[0]] ) )
        cases.append( csr_matrix( [[0],[0]] ) )
        cases.append( csr_matrix( [[1,2],[3,4]] ) )
        cases.append( csr_matrix( [[0,1],[0,0]] ) )
        cases.append( csr_matrix( [[0,0],[1,0]] ) )
        cases.append( csr_matrix( [[0,0],[0,0]] ) )
        cases.append( csr_matrix( [[1,2,0,0,3],[4,5,0,6,7],[0,0,8,9,0]] ) )
        cases.append( csr_matrix( [[1,2,0,0,3],[4,5,0,6,7],[0,0,8,9,0]] ).T )

        self.cases = cases

    def find(self):
        for A in self.cases:
            I,J,V = extract.find(A)
            assert_equal( A.toarray(), csr_matrix(((I,J),V), shape=A.shape) )

    def test_tril(self):
        for A in self.cases:
            B = A.toarray()
            for k in [-3,-2,-1,0,1,2,3]:
                assert_equal( extract.tril(A,k=k).toarray(), np.tril(B,k=k))

    def test_triu(self):
        for A in self.cases:
            B = A.toarray()
            for k in [-3,-2,-1,0,1,2,3]:
                assert_equal( extract.triu(A,k=k).toarray(), np.triu(B,k=k))