| 1 | /* Eigen_and_Matrix.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2019 David Weenink, 2015,2017 Paul Boersma |
| 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 2002 |
| 21 | djmw 20020813 GPL header |
| 22 | */ |
| 23 | |
| 24 | #include "Eigen_and_Matrix.h" |
| 25 | #include "Matrix_extensions.h" |
| 26 | #include "NUM2.h" |
| 27 | |
| 28 | // TODO: two step proces 1. Extract 2. Reshape ?? |
| 29 | autoMatrix Eigen_extractEigenvector (Eigen me, integer index, integer numberOfRows, integer numberOfColumns) { |
| 30 | try { |
| 31 | if (numberOfRows == 0 && numberOfColumns == 0) { |
| 32 | numberOfRows = 1; |
| 33 | numberOfColumns = my dimension; |
| 34 | } |
| 35 | Melder_require (numberOfRows * numberOfColumns == my dimension, |
| 36 | U"The number of rows times the number of columns should be equal to the dimension of the eigenvectors."); |
| 37 | autoMatrix thee = Matrix_createSimple (numberOfRows, numberOfColumns); |
| 38 | for (integer irow = 1; irow <= numberOfRows; irow ++) |
| 39 | thy z.row (irow) <<= my eigenvectors [index].part ((irow - 1) * numberOfColumns + 1, irow * numberOfColumns); |
| 40 | return thee; |
| 41 | } catch (MelderError) { |
| 42 | Melder_throw (me, U"No eigenvector extracted."); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | autoMatrix Eigen_Matrix_to_Matrix_projectRows (Eigen me, Matrix thee, integer numberOfDimensionsToKeep) { |
| 47 | try { |
| 48 | if (numberOfDimensionsToKeep <= 0 || numberOfDimensionsToKeep > my numberOfEigenvalues) { |
| 49 | numberOfDimensionsToKeep = my numberOfEigenvalues; |
| 50 | } |
| 51 | Melder_require (thy nx == my dimension, |
| 52 | U"The number of columns (", thy nx, U") should equal the size of the eigenvectors (", my dimension, U")."); |
| 53 | |
| 54 | autoMatrix him = Matrix_create (0.5, 0.5 + numberOfDimensionsToKeep, numberOfDimensionsToKeep, 1.0, 1.0, thy ymin, thy ymax, thy ny, thy dy, thy y1); |
| 55 | mul_MAT_out (his z.get(), thy z.get(), my eigenvectors.horizontalBand (1, numberOfDimensionsToKeep).transpose()); |
| 56 | return him; |
| 57 | } catch (MelderError) { |
| 58 | Melder_throw (U"Projection Matrix from ", me, U" and ", thee, U" not created."); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | autoMatrix Eigen_Matrix_to_Matrix_projectColumns (Eigen me, Matrix thee, integer numberOfDimensionsToKeep) { |
| 63 | try { |
| 64 | if (numberOfDimensionsToKeep <= 0 || numberOfDimensionsToKeep > my numberOfEigenvalues) |
| 65 | numberOfDimensionsToKeep = my numberOfEigenvalues; |
| 66 | |
| 67 | Melder_require (thy nx == my dimension, |
| 68 | U"The number of rows (", thy ny, U") should equal the size of the eigenvectors (", my dimension, U")."); |
| 69 | |
| 70 | autoMatrix him = Matrix_create (thy xmin, thy xmax, thy nx, thy dx, thy x1, 0.5, 0.5 + numberOfDimensionsToKeep, numberOfDimensionsToKeep, 1.0, 1.0); |
| 71 | MATprojectColumnsOnEigenspace_preallocated (his z.get(), thy z.get(), my eigenvectors.horizontalBand (1, numberOfDimensionsToKeep)); |
| 72 | return him; |
| 73 | } catch (MelderError) { |
| 74 | Melder_throw (U"Projection Matrix from ", me, U" and ", thee, U" not created."); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* End of file Eigen_and_Matrix.cpp */ |