Eigen_and_Matrix.cpp 3.3 KB

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