1 // matrix_sparse_usage.pycmd
3 // Copyright (c) 2014 - Michele De Stefano (micdestefano@users.sourceforge.net)
5 // Distributed under the MIT License (See accompanying file LICENSE)
9 * \example matrix_sparse_usage.pycmd
11 * Shows the usage of the matrix_sparse_usage extension from the Python prompt.
14 mds_utils::python::ublas::matrix_sparse.hpp functionalities usage example from the Python prompt:
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)
24 matrix([[ 0. , 1.1, 0. ],
30 [4,3]((0,1.1,0),(0,0,0),(0,0,3.3),(2.2,0,0))
31 >>> mu.get_matrix_int(M)
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)
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)
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)
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)
52 matrix([[ 0. , 1.1, 0. ],
56 >>> mu.get_csc_matrix(A)
58 [4,3]((0,1.1,0),(0,0,0),(0,0,3.3),(2.2,0,0))
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()
68 <Swig Object of type 'boost::numeric::ublas::compressed_matrix< double > *' at 0x2878330>
71 >>> mu.get_matrix_no_conversion(M)
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)
76 <Swig Object of type 'boost::numeric::ublas::compressed_matrix< double > *' at 0x2260b10>
77 >>> B = mu.create_csr_matrix()
79 <class 'scipy.sparse.csr.csr_matrix'>
81 <4x3 sparse matrix of type '<type 'numpy.float64'>'
82 with 3 stored elements in Compressed Sparse Row format>
84 matrix([[ 0. , 1.1, 0. ],
88 >>> C = mu.create_csc_matrix_fcplx()
90 <class 'scipy.sparse.csc.csc_matrix'>
92 <4x3 sparse matrix of type '<type 'numpy.complex64'>'
93 with 3 stored elements in Compressed Sparse Column format>
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)