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