1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """ Functions.py - Utility functions for the multimodal solver.
23
24 In order to solve a complex matricial system of ordinary differential
25 equations, it must be convert to a real system of equation. Two functions
26 are used to convert back and forth from the matrix notation to the
27 vectorial form: flat2matrix and matrix2flat.
28
29 """
30
31 import numpy
32
34 """ Convert a flat vector (obtained with matrix2flat) to a matrix of
35 complex numbers
36
37 @param flat_matrix: a flat vector representation of a matrix
38 @param N: the size of the square matrix
39 """
40 [flat_matrix_real, flat_matrix_imag] = numpy.split(flat_matrix,2)
41 matrix = flat_matrix_real.reshape(N,N) + 1j*flat_matrix_imag.reshape(N,N)
42 return matrix
43
45 """ Transform a matrix of complex numbers in a vector of real number
46 consisting of a flatten version of the original.
47
48 Example
49
50 >>> import numpy
51 >>> import wiat
52 >>> mat = numpy.array([[complex(1.,1.), complex(2.,2.)],
53 ... [complex(3.,3.), complex(4.,4.)]])
54 >>> fmat = wiat.MM.Functions.matrix2flat(mat)
55 >>> print mat
56 [[ 1.+1.j 2.+2.j]
57 [ 3.+3.j 4.+4.j]]
58 >>> print fmat
59 [ 1. 2. 3. 4. 1. 2. 3. 4.]
60
61 """
62 flat_matrix = numpy.concatenate((numpy.real(matrix.flatten()),
63 numpy.imag(matrix.flatten())))
64 return flat_matrix
65