Michele De Stefano's C++ Utilities
ublas_pkg_usage.pycmd
1 // ublas_pkg_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 ublas_pkg_usage.pycmd
10  *
11  * Shows the usage of the ublas_pkg_usage extension from the Python prompt.
12  * Furthermore, it shows how to reuse classes wrapped by the
13  * mds_utils.ublas.ublas.py module.
14  *
15  * Before using the ublas_pkg_usage module, ensure that your
16  * PYTHONPATH environment variable contains the path to reach the
17  * mds_utils Python package. For example, if the mds_utils package has
18  * been built but not installed yet, it is typically found into the
19  * mds-utils/python/build/lib.linux-<arch>-<Python version> directory
20  * (for a Linux build) and you have to add this path to your PYTHONPATH
21  * variable. After that, you can import the ublas_pkg_usage module and
22  * try it.
23  */
24 
25 >>> import ublas_pkg_usage as ubpu
26 >>> v = ubpu.create_vector()
27 >>> v
28 <mds_utils.ublas.ublas.vector_d; proxy of <Swig Object of type 'boost::numeric::ublas::vector< double > *' at 0x7f9257478ea0> >
29 >>> print(v)
30 [3](1.1,2.2,3.3)
31 >>> v.__str__
32 <bound method vector_d.__str__ of <mds_utils.ublas.ublas.vector_d; proxy of <Swig Object of type 'boost::numeric::ublas::vector< double > *' at 0x7f9257478ea0> >>
33 >>> v.__str__()
34 '[3](1.1,2.2,3.3)'
35 >>> v.to_array()
36 array([ 1.1, 2.2, 3.3])
37 >>> [t for t in v]
38 [1.1, 2.2, 3.3]
39 >>> M = ubpu.create_compressed_matrix()
40 >>> M
41 <mds_utils.ublas.ublas.compressed_matrix_drow; proxy of <Swig Object of type 'boost::numeric::ublas::compressed_matrix< double,boost::numeric::ublas::row_major > *' at 0x22171e0> >
42 >>> print(M)
43 [4,3]((0,0,2.2),(1.1,0,0),(0,0,0),(0,3.3,0))
44 >>> sp_M = M.to_sparse_mtx()
45 >>> sp_M
46 <4x3 sparse matrix of type '<type 'numpy.float64'>'
47  with 3 stored elements in Compressed Sparse Row format>
48 >>> sp_M.todense()
49 matrix([[ 0. , 0. , 2.2],
50  [ 1.1, 0. , 0. ],
51  [ 0. , 0. , 0. ],
52  [ 0. , 3.3, 0. ]])
53 >>> M.to_dense()
54 array([[ 0. , 0. , 2.2],
55  [ 1.1, 0. , 0. ],
56  [ 0. , 0. , 0. ],
57  [ 0. , 3.3, 0. ]])
58 >>> v1 = ubpu.my_vec()
59 >>> import numpy as np
60 >>> v1.from_array(np.array([11.1,22.2,33.3]))
61 >>> print(v1)
62 [3](11.1,22.2,33.3)
63 >>> v1[0]
64 11.1
65 >>> v1[1]
66 22.2