Michele De Stefano's C++ Utilities
vector_usage.pycmd
1 // vector_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 vector_usage.pycmd
10  *
11  * Shows the usage of the vector_usage extension from the Python prompt.
12  */
13 
14 vector.hpp functionalities usage example from the Python prompt:
15 
16 >>> import vector_usage as vu
17 >>> v = vu.create_npy_array()
18 >>> v
19 array([ 1.1, 2.2, 3.3, 4.4, 5.5, 6.6])
20 >>> v.flags
21  C_CONTIGUOUS : True
22  F_CONTIGUOUS : True
23  OWNDATA : True
24  WRITEABLE : True
25  ALIGNED : True
26  UPDATEIFCOPY : False
27 >>> v.shape
28 (6,)
29 >>> v2 = vu.create_ublas_vector()
30 >>> v2.__class__
31 <type 'SwigPyObject'>
32 >>> v2
33 <Swig Object of type 'boost::numeric::ublas::vector< double > *' at 0x7f44f0225ed0>
34 >>> vu.get_vec_no_conversion(v2)
35 Vector got:
36 [6](1.1,2.2,3.3,4.4,5.5,6.6)
37 >>> vu.destroy_ublas_vector(v2)
38 >>> cv = vu.create_npy_array_cplx()
39 >>> cv
40 array([ 1.1+1.1j, 2.2+2.2j, 3.3+3.3j, 4.4+4.4j, 5.5+5.5j, 6.6+6.6j])
41 >>> cv.flags
42  C_CONTIGUOUS : True
43  F_CONTIGUOUS : True
44  OWNDATA : True
45  WRITEABLE : True
46  ALIGNED : True
47  UPDATEIFCOPY : False
48 >>> cv.shape
49 (6,)
50 >>> cv.dtype
51 dtype('complex128')
52 >>> sv = vu.create_npy_array_short()
53 >>> sv
54 array([-1, 2, -3, 4, 5, -6], dtype=int16)
55 >>> vu.get_vec(v)
56 Vector got:
57 [6](1.1,2.2,3.3,4.4,5.5,6.6)
58 >>> vu.get_vec(cv)
59 vector_usage.py:107: ComplexWarning: Casting complex values to real discards the imaginary part
60  return _vector_usage.get_vec(*args)
61 Vector got:
62 [6](1.1,2.2,3.3,4.4,5.5,6.6)
63 >>> cv
64 array([ 1.1+1.1j, 2.2+2.2j, 3.3+3.3j, 4.4+4.4j, 5.5+5.5j, 6.6+6.6j])
65 >>> vu.get_vec_cplx(cv)
66 Vector got:
67 [6]((1.1,1.1),(2.2,2.2),(3.3,3.3),(4.4,4.4),(5.5,5.5),(6.6,6.6))
68 >>> vu.get_vec_fcplx(v)
69 Vector got:
70 [6]((1.1,0),(2.2,0),(3.3,0),(4.4,0),(5.5,0),(6.6,0))
71 >>> v
72 array([ 1.1, 2.2, 3.3, 4.4, 5.5, 6.6])
73 >>> vu.get_vec_cast(cv)
74 vector_usage.py:147: ComplexWarning: Casting complex values to real discards the imaginary part
75  return _vector_usage.get_vec_cast(*args)
76 Vector got:
77 [6](1,2,3,4,5,6)
78 >>> vu.get_vec_cast(v)
79 Vector got:
80 [6](1,2,3,4,5,6)
81 >>> import numpy as np
82 >>> v = np.array([ 1.1, 2.2, 3.3, 4.4, 5.5, 6.6],dtype=np.complex64)*(1.1+1.1j)
83 >>> v
84 array([ 1.21000004+1.21000004j, 2.42000008+2.42000008j,
85  3.63000011+3.63000011j, 4.84000015+4.84000015j,
86  6.05000019+6.05000019j, 7.26000023+7.26000023j], dtype=complex64)
87 >>> vu.get_vec_fcplx(v)
88 Vector got:
89 [6]((1.21,1.21),(2.42,2.42),(3.63,3.63),(4.84,4.84),(6.05,6.05),(7.26,7.26))