| 1 | /* TableOfReal_and_Permutation.cpp |
| 2 | * |
| 3 | * Copyright (C) 2005-2022 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | djmw 20050708 |
| 21 | */ |
| 22 | |
| 23 | #include "Index.h" |
| 24 | #include "TableOfReal_and_Permutation.h" |
| 25 | #include "TableOfReal_extensions.h" |
| 26 | #include "NUM2.h" |
| 27 | |
| 28 | autoTableOfReal TableOfReal_Permutation_permuteRows (TableOfReal me, Permutation thee) { |
| 29 | try { |
| 30 | Melder_require (my numberOfRows == thy numberOfElements, |
| 31 | U"The number of rows in the table and the number of elements in the Permutation should be equal."); |
| 32 | autoTableOfReal him = TableOfReal_create (my numberOfRows, my numberOfColumns); |
| 33 | |
| 34 | for (integer irow = 1; irow <= thy numberOfElements; irow ++) |
| 35 | TableOfReal_copyOneRowWithLabel (me, him.get(), thy p [irow], irow); |
| 36 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 37 | TableOfReal_setColumnLabel (him.get(), icol, my columnLabels [icol].get()); |
| 38 | return him; |
| 39 | } catch (MelderError) { |
| 40 | Melder_throw (me, U": rows not permuted."); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | autoTableOfReal TableOfReal_Permutation_permuteColumns (TableOfReal me, Permutation thee) { |
| 45 | try { |
| 46 | Melder_require (my numberOfColumns == thy numberOfElements, |
| 47 | U"The number of columns in the table and the number of elements in the Permutation should be equal."); |
| 48 | autoTableOfReal him = TableOfReal_create (my numberOfRows, my numberOfColumns); |
| 49 | |
| 50 | for (integer icol = 1; icol <= thy numberOfElements; icol ++) { |
| 51 | const integer fromCol = thy p [icol]; |
| 52 | his columnLabels [icol] = Melder_dup (my columnLabels [fromCol].get()); |
| 53 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 54 | his data [irow] [icol] = my data [irow][fromCol]; |
| 55 | } |
| 56 | for (integer irow = 1; irow <= my numberOfColumns; irow ++) |
| 57 | TableOfReal_setRowLabel (him.get(), irow, my rowLabels [irow].get()); |
| 58 | return him; |
| 59 | } catch (MelderError) { |
| 60 | Melder_throw (me, U": columns not permuted."); |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | autoPermutation TableOfReal_to_Permutation_sortRowLabels (TableOfReal me) { |
| 65 | try { |
| 66 | autoPermutation thee = Permutation_create (my numberOfRows, true); |
| 67 | INTVECindex_inout (thy p.get(), my rowLabels.get()); |
| 68 | return thee; |
| 69 | } catch (MelderError) { |
| 70 | Melder_throw (me, U": no Permutation created."); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* End of file TableOfReal_and_Permutation.cpp */ |