Package wiat :: Package MM :: Module Functions
[hide private]
[frames] | no frames]

Source Code for Module wiat.MM.Functions

 1  # Copyright 2008 Antoine Lefebvre 
 2  # 
 3  # This file is part of "The Wind Instrument Acoustic Toolkit". 
 4  # 
 5  # "The Wind Instrument Acoustic Toolkit" is free software: 
 6  # you can redistribute it and/or modify it under the terms of 
 7  # the GNU General Public License as published by the 
 8  # Free Software Foundation, either version 3 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # "The Wind Instrument Acoustic Toolkit" is distributed in the hope 
12  # that it will be useful, but WITHOUT ANY WARRANTY; without even 
13  # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
14  # See the GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with "The Wind Instrument Acoustic Toolkit". 
18  # If not, see <http://www.gnu.org/licenses/>. 
19  # 
20  # All rights reserved. 
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   
33 -def flat2matrix(flat_matrix,N):
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
44 -def matrix2flat(matrix):
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