Package pypower :: Module get_reorder
[hide private]
[frames] | no frames]

Source Code for Module pypower.get_reorder

 1  # Copyright (C) 2009-2011 Power System Engineering Research Center (PSERC) 
 2  # Copyright (C) 2011 Richard Lincoln 
 3  # 
 4  # PYPOWER is free software: you can redistribute it and/or modify 
 5  # it under the terms of the GNU General Public License as published 
 6  # by the Free Software Foundation, either version 3 of the License, 
 7  # or (at your option) any later version. 
 8  # 
 9  # PYPOWER is distributed in the hope that it will be useful, 
10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
12  # GNU General Public License for more details. 
13  # 
14  # You should have received a copy of the GNU General Public License 
15  # along with PYPOWER. If not, see <http://www.gnu.org/licenses/>. 
16   
17  """Indexes a matrix dimension. 
18  """ 
19   
20  from numpy import ndim 
21   
22 -def get_reorder(A, idx, dim=0):
23 """Returns A with one of its dimensions indexed:: 24 25 B = get_reorder(A, idx, dim) 26 27 Returns A[:, ..., :, idx, :, ..., :], where dim determines 28 in which dimension to place the idx. 29 30 @author: Ray Zimmerman (PSERC Cornell) 31 @author: Richard Lincoln 32 """ 33 ndims = ndim(A) 34 if ndims == 1: 35 B = A[idx].copy() 36 elif ndims == 2: 37 if dim == 0: 38 B = A[idx, :].copy() 39 elif dim == 1: 40 B = A[:, idx].copy() 41 else: 42 raise ValueError, 'dim (%d) may be 0 or 1' % dim 43 else: 44 raise ValueError, 'number of dimensions (%d) may be 1 or 2' % dim 45 46 return B
47