Michele De Stefano's C++ Utilities
matrix_sparse_usage.pycmd
1 // matrix_sparse_usage.pycmd
2 //
3 // Copyright (c) 2014 - Michele De Stefano (micdestefano@users.sourceforge.net)
4 //
5 // Distributed under the MIT License (See accompanying file LICENSE)
6 
7 
8 /**
9  * \example matrix_sparse_usage.pycmd
10  *
11  * Shows the usage of the matrix_sparse_usage extension from the Python prompt.
12  */
13 
14 mds_utils::python::ublas::matrix_sparse.hpp functionalities usage example from the Python prompt:
15 
16 >>> import matrix_sparse_usage as mu
17 >>> from scipy.sparse import csr_matrix,csc_matrix
18 >>> import numpy as np
19 >>> M = csr_matrix((4, 3))
20 >>> M[0,1] = 1.1; M[3,0] = 2.2; M[2,2] = 3.3
21 /usr/lib64/python2.7/site-packages/scipy/sparse/compressed.py:496: SparseEfficiencyWarning: changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
22  SparseEfficiencyWarning)
23 >>> M.todense()
24 matrix([[ 0. , 1.1, 0. ],
25  [ 0. , 0. , 0. ],
26  [ 0. , 0. , 3.3],
27  [ 2.2, 0. , 0. ]])
28 >>> mu.get_matrix(M)
29 Matrix got:
30 [4,3]((0,1.1,0),(0,0,0),(0,0,3.3),(2.2,0,0))
31 >>> mu.get_matrix_int(M)
32 Matrix got:
33 [4,3]((0,1,0),(0,0,0),(0,0,3),(2,0,0))
34 >>> M = csr_matrix((4, 3),dtype=np.complex128)
35 >>> M[0,1] = 1.1*(1.+1.j); M[3,0] = 2.2*(1.+1.j); M[2,2] = 3.3*(1.+1.j)
36 >>> M.todense()
37 matrix([[ 0.0+0.j , 1.1+1.1j, 0.0+0.j ],
38  [ 0.0+0.j , 0.0+0.j , 0.0+0.j ],
39  [ 0.0+0.j , 0.0+0.j , 3.3+3.3j],
40  [ 2.2+2.2j, 0.0+0.j , 0.0+0.j ]])
41 >>> mu.get_matrix_cplx(M)
42 Matrix got:
43 [4,3](((0,0),(1.1,1.1),(0,0)),((0,0),(0,0),(0,0)),((0,0),(0,0),(3.3,3.3)),((2.2,2.2),(0,0),(0,0)))
44 >>> mu.get_matrix_fcplx(M)
45 Matrix got:
46 [4,3](((0,0),(1.1,1.1),(0,0)),((0,0),(0,0),(0,0)),((0,0),(0,0),(3.3,3.3)),((2.2,2.2),(0,0),(0,0)))
47 >>> A = csc_matrix((4, 3))
48 >>> A[0,1] = 1.1; A[3,0] = 2.2; A[2,2] = 3.3
49 /usr/lib64/python2.7/site-packages/scipy/sparse/compressed.py:496: SparseEfficiencyWarning: changing the sparsity structure of a csc_matrix is expensive. lil_matrix is more efficient.
50  SparseEfficiencyWarning)
51 >>> A.todense()
52 matrix([[ 0. , 1.1, 0. ],
53  [ 0. , 0. , 0. ],
54  [ 0. , 0. , 3.3],
55  [ 2.2, 0. , 0. ]])
56 >>> mu.get_csc_matrix(A)
57 Matrix got:
58 [4,3]((0,1.1,0),(0,0,0),(0,0,3.3),(2.2,0,0))
59 >>> mu.get_matrix(A)
60 Traceback (most recent call last):
61  File "<stdin>", line 1, in <module>
62  File "matrix_sparse_usage.py", line 81, in get_matrix
63  return _matrix_sparse_usage.get_matrix(*args)
64 RuntimeError: Cannot get a uBLAS compressed matrix from the given parameter.
65 Check the data type and the storage layout (i.e. csr or csc).
66 >>> M = mu.create_ublas_compressed_matrix()
67 >>> M
68 <Swig Object of type 'boost::numeric::ublas::compressed_matrix< double > *' at 0x2878330>
69 >>> M.__class__
70 <type 'SwigPyObject'>
71 >>> mu.get_matrix_no_conversion(M)
72 Matrix got:
73 [4,3]((0,1.1,0),(0,0,0),(0,0,3.3),(2.2,0,0))
74 >>> mu.destroy_ublas_compressed_matrix(M)
75 >>> M
76 <Swig Object of type 'boost::numeric::ublas::compressed_matrix< double > *' at 0x2260b10>
77 >>> B = mu.create_csr_matrix()
78 >>> B.__class__
79 <class 'scipy.sparse.csr.csr_matrix'>
80 >>> B
81 <4x3 sparse matrix of type '<type 'numpy.float64'>'
82  with 3 stored elements in Compressed Sparse Row format>
83 >>> B.todense()
84 matrix([[ 0. , 1.1, 0. ],
85  [ 0. , 0. , 0. ],
86  [ 0. , 0. , 3.3],
87  [ 2.2, 0. , 0. ]])
88 >>> C = mu.create_csc_matrix_fcplx()
89 >>> C.__class__
90 <class 'scipy.sparse.csc.csc_matrix'>
91 >>> C
92 <4x3 sparse matrix of type '<type 'numpy.complex64'>'
93  with 3 stored elements in Compressed Sparse Column format>
94 >>> C.todense()
95 matrix([[ 0.00000000+0.j , 1.10000002+1.10000002j, 0.00000000+0.j ],
96  [ 0.00000000+0.j , 0.00000000+0.j , 0.00000000+0.j ],
97  [ 0.00000000+0.j , 0.00000000+0.j ,
98  3.29999995+3.29999995j],
99  [ 2.20000005+2.20000005j, 0.00000000+0.j , 0.00000000+0.j ]], dtype=complex64)