[8901] | 1 | /// \ingroup newmat
|
---|
| 2 | ///@{
|
---|
| 3 |
|
---|
| 4 | /// \file newmatap.h
|
---|
| 5 | /// Definition file for advanced matrix functions.
|
---|
| 6 |
|
---|
| 7 | // Copyright (C) 1991,2,3,4,8: R B Davies
|
---|
| 8 |
|
---|
| 9 | #ifndef NEWMATAP_LIB
|
---|
| 10 | #define NEWMATAP_LIB 0
|
---|
| 11 |
|
---|
| 12 | #include "newmat.h"
|
---|
| 13 |
|
---|
| 14 | #ifdef use_namespace
|
---|
| 15 | namespace NEWMAT {
|
---|
| 16 | #endif
|
---|
| 17 |
|
---|
| 18 |
|
---|
| 19 | // ************************** applications *****************************/
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | void QRZT(Matrix&, LowerTriangularMatrix&);
|
---|
| 23 |
|
---|
| 24 | void QRZT(const Matrix&, Matrix&, Matrix&);
|
---|
| 25 |
|
---|
| 26 | void QRZ(Matrix&, UpperTriangularMatrix&);
|
---|
| 27 |
|
---|
| 28 | void QRZ(const Matrix&, Matrix&, Matrix&);
|
---|
| 29 |
|
---|
| 30 | inline void QRZT(Matrix& X, Matrix& Y, LowerTriangularMatrix& L, Matrix& M)
|
---|
| 31 | { QRZT(X, L); QRZT(X, Y, M); }
|
---|
| 32 |
|
---|
| 33 | inline void QRZ(Matrix& X, Matrix& Y, UpperTriangularMatrix& U, Matrix& M)
|
---|
| 34 | { QRZ(X, U); QRZ(X, Y, M); }
|
---|
| 35 |
|
---|
| 36 | inline void HHDecompose(Matrix& X, LowerTriangularMatrix& L)
|
---|
| 37 | { QRZT(X,L); }
|
---|
| 38 |
|
---|
| 39 | inline void HHDecompose(const Matrix& X, Matrix& Y, Matrix& M)
|
---|
| 40 | { QRZT(X, Y, M); }
|
---|
| 41 |
|
---|
| 42 | void updateQRZT(Matrix& X, LowerTriangularMatrix& L);
|
---|
| 43 |
|
---|
| 44 | void updateQRZ(Matrix& X, UpperTriangularMatrix& U);
|
---|
| 45 |
|
---|
| 46 | void updateQRZ(const Matrix& X, Matrix& MX, Matrix& MU);
|
---|
| 47 |
|
---|
| 48 | void updateQRZ(UpperTriangularMatrix& X, UpperTriangularMatrix& U);
|
---|
| 49 |
|
---|
| 50 | void updateQRZ(const UpperTriangularMatrix& X, Matrix& MX, Matrix& MU);
|
---|
| 51 |
|
---|
| 52 | inline void UpdateQRZT(Matrix& X, LowerTriangularMatrix& L)
|
---|
| 53 | { updateQRZT(X, L); }
|
---|
| 54 |
|
---|
| 55 | inline void UpdateQRZ(Matrix& X, UpperTriangularMatrix& U)
|
---|
| 56 | { updateQRZ(X, U); }
|
---|
| 57 |
|
---|
| 58 | inline void UpdateQRZ(UpperTriangularMatrix& X, UpperTriangularMatrix& U)
|
---|
| 59 | { updateQRZ(X, U); }
|
---|
| 60 |
|
---|
| 61 | inline void UpdateQRZ(const UpperTriangularMatrix& X, Matrix& MX, Matrix& MU)
|
---|
| 62 | { updateQRZ(X, MX, MU); }
|
---|
| 63 |
|
---|
| 64 | inline void UpdateQRZ(const Matrix& X, Matrix& MX, Matrix& MU)
|
---|
| 65 | { updateQRZ(X, MX, MU); }
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | // Matrix A's first n columns are orthonormal
|
---|
| 69 | // so A.Columns(1,n).t() * A.Columns(1,n) is the identity matrix.
|
---|
| 70 | // Fill out the remaining columns of A to make them orthonormal
|
---|
| 71 | // so A.t() * A is the identity matrix
|
---|
| 72 | void extend_orthonormal(Matrix& A, int n);
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | ReturnMatrix Cholesky(const SymmetricMatrix&);
|
---|
| 76 |
|
---|
| 77 | ReturnMatrix Cholesky(const SymmetricBandMatrix&);
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | // produces the Cholesky decomposition of A + x.t() * x
|
---|
| 81 | // where A = chol.t() * chol and x is a RowVector
|
---|
| 82 | void update_Cholesky(UpperTriangularMatrix& chol, RowVector x);
|
---|
| 83 | inline void UpdateCholesky(UpperTriangularMatrix& chol, const RowVector& x)
|
---|
| 84 | { update_Cholesky(chol, x); }
|
---|
| 85 |
|
---|
| 86 | // produces the Cholesky decomposition of A - x.t() * x
|
---|
| 87 | // where A = chol.t() * chol and x is a RowVector
|
---|
| 88 | void downdate_Cholesky(UpperTriangularMatrix &chol, RowVector x);
|
---|
| 89 | inline void DowndateCholesky(UpperTriangularMatrix &chol, const RowVector& x)
|
---|
| 90 | { downdate_Cholesky(chol, x); }
|
---|
| 91 |
|
---|
| 92 | // a RIGHT circular shift of the rows and columns from
|
---|
| 93 | // 1,...,k-1,k,k+1,...l,l+1,...,p to
|
---|
| 94 | // 1,...,k-1,l,k,k+1,...l-1,l+1,...p
|
---|
| 95 | void right_circular_update_Cholesky(UpperTriangularMatrix &chol, int k, int l);
|
---|
| 96 | inline void RightCircularUpdateCholesky(UpperTriangularMatrix &chol,
|
---|
| 97 | int k, int l) { right_circular_update_Cholesky(chol, k, l); }
|
---|
| 98 |
|
---|
| 99 | // a LEFT circular shift of the rows and columns from
|
---|
| 100 | // 1,...,k-1,k,k+1,...l,l+1,...,p to
|
---|
| 101 | // 1,...,k-1,k+1,...l,k,l+1,...,p to
|
---|
| 102 | void left_circular_update_Cholesky(UpperTriangularMatrix &chol, int k, int l);
|
---|
| 103 | inline void LeftCircularUpdateCholesky(UpperTriangularMatrix &chol,
|
---|
| 104 | int k, int l) { left_circular_update_Cholesky(chol, k, l); }
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 | void SVD(const Matrix&, DiagonalMatrix&, Matrix&, Matrix&,
|
---|
| 108 | bool=true, bool=true);
|
---|
| 109 |
|
---|
| 110 | void SVD(const Matrix&, DiagonalMatrix&);
|
---|
| 111 |
|
---|
| 112 | inline void SVD(const Matrix& A, DiagonalMatrix& D, Matrix& U,
|
---|
| 113 | bool withU = true) { SVD(A, D, U, U, withU, false); }
|
---|
| 114 |
|
---|
| 115 | void SortSV(DiagonalMatrix& D, Matrix& U, bool ascending = false);
|
---|
| 116 |
|
---|
| 117 | void SortSV(DiagonalMatrix& D, Matrix& U, Matrix& V, bool ascending = false);
|
---|
| 118 |
|
---|
| 119 | void Jacobi(const SymmetricMatrix&, DiagonalMatrix&);
|
---|
| 120 |
|
---|
| 121 | void Jacobi(const SymmetricMatrix&, DiagonalMatrix&, SymmetricMatrix&);
|
---|
| 122 |
|
---|
| 123 | void Jacobi(const SymmetricMatrix&, DiagonalMatrix&, Matrix&);
|
---|
| 124 |
|
---|
| 125 | void Jacobi(const SymmetricMatrix&, DiagonalMatrix&, SymmetricMatrix&,
|
---|
| 126 | Matrix&, bool=true);
|
---|
| 127 |
|
---|
| 128 | void eigenvalues(const SymmetricMatrix&, DiagonalMatrix&);
|
---|
| 129 |
|
---|
| 130 | void eigenvalues(const SymmetricMatrix&, DiagonalMatrix&, SymmetricMatrix&);
|
---|
| 131 |
|
---|
| 132 | void eigenvalues(const SymmetricMatrix&, DiagonalMatrix&, Matrix&);
|
---|
| 133 |
|
---|
| 134 | inline void EigenValues(const SymmetricMatrix& A, DiagonalMatrix& D)
|
---|
| 135 | { eigenvalues(A, D); }
|
---|
| 136 |
|
---|
| 137 | inline void EigenValues(const SymmetricMatrix& A, DiagonalMatrix& D,
|
---|
| 138 | SymmetricMatrix& S) { eigenvalues(A, D, S); }
|
---|
| 139 |
|
---|
| 140 | inline void EigenValues(const SymmetricMatrix& A, DiagonalMatrix& D, Matrix& V)
|
---|
| 141 | { eigenvalues(A, D, V); }
|
---|
| 142 |
|
---|
| 143 | class SymmetricEigenAnalysis
|
---|
| 144 | // not implemented yet
|
---|
| 145 | {
|
---|
| 146 | public:
|
---|
| 147 | SymmetricEigenAnalysis(const SymmetricMatrix&);
|
---|
| 148 | private:
|
---|
| 149 | DiagonalMatrix diag;
|
---|
| 150 | DiagonalMatrix offdiag;
|
---|
| 151 | SymmetricMatrix backtransform;
|
---|
| 152 | FREE_CHECK(SymmetricEigenAnalysis)
|
---|
| 153 | };
|
---|
| 154 |
|
---|
| 155 | void sort_ascending(GeneralMatrix&);
|
---|
| 156 |
|
---|
| 157 | void sort_descending(GeneralMatrix&);
|
---|
| 158 |
|
---|
| 159 | inline void SortAscending(GeneralMatrix& gm) { sort_ascending(gm); }
|
---|
| 160 |
|
---|
| 161 | inline void SortDescending(GeneralMatrix& gm) { sort_descending(gm); }
|
---|
| 162 |
|
---|
| 163 | /// Decide which fft method to use and carry out new fft function
|
---|
| 164 | class FFT_Controller
|
---|
| 165 | {
|
---|
| 166 | public:
|
---|
| 167 | static bool OnlyOldFFT;
|
---|
| 168 | static bool ar_1d_ft (int PTS, Real* X, Real *Y);
|
---|
| 169 | static bool CanFactor(int PTS);
|
---|
| 170 | };
|
---|
| 171 |
|
---|
| 172 | void FFT(const ColumnVector&, const ColumnVector&,
|
---|
| 173 | ColumnVector&, ColumnVector&);
|
---|
| 174 |
|
---|
| 175 | void FFTI(const ColumnVector&, const ColumnVector&,
|
---|
| 176 | ColumnVector&, ColumnVector&);
|
---|
| 177 |
|
---|
| 178 | void RealFFT(const ColumnVector&, ColumnVector&, ColumnVector&);
|
---|
| 179 |
|
---|
| 180 | void RealFFTI(const ColumnVector&, const ColumnVector&, ColumnVector&);
|
---|
| 181 |
|
---|
| 182 | void DCT_II(const ColumnVector&, ColumnVector&);
|
---|
| 183 |
|
---|
| 184 | void DCT_II_inverse(const ColumnVector&, ColumnVector&);
|
---|
| 185 |
|
---|
| 186 | void DST_II(const ColumnVector&, ColumnVector&);
|
---|
| 187 |
|
---|
| 188 | void DST_II_inverse(const ColumnVector&, ColumnVector&);
|
---|
| 189 |
|
---|
| 190 | void DCT(const ColumnVector&, ColumnVector&);
|
---|
| 191 |
|
---|
| 192 | void DCT_inverse(const ColumnVector&, ColumnVector&);
|
---|
| 193 |
|
---|
| 194 | void DST(const ColumnVector&, ColumnVector&);
|
---|
| 195 |
|
---|
| 196 | void DST_inverse(const ColumnVector&, ColumnVector&);
|
---|
| 197 |
|
---|
| 198 | void FFT2(const Matrix& U, const Matrix& V, Matrix& X, Matrix& Y);
|
---|
| 199 |
|
---|
| 200 | void FFT2I(const Matrix& U, const Matrix& V, Matrix& X, Matrix& Y);
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | // This class is used by the new FFT program
|
---|
| 204 |
|
---|
| 205 | // Suppose an integer is expressed as a sequence of digits with each
|
---|
| 206 | // digit having a different radix.
|
---|
| 207 | // This class supposes we are counting with this multi-radix number
|
---|
| 208 | // but also keeps track of the number with the digits (and radices)
|
---|
| 209 | // reversed.
|
---|
| 210 | // The integer starts at zero
|
---|
| 211 | // operator++() increases it by 1
|
---|
| 212 | // Counter gives the number of increments
|
---|
| 213 | // Reverse() gives the value with the digits in reverse order
|
---|
| 214 | // Swap is true if reverse is less than counter
|
---|
| 215 | // Finish is true when we have done a complete cycle and are back at zero
|
---|
| 216 |
|
---|
| 217 | class MultiRadixCounter
|
---|
| 218 | {
|
---|
| 219 | const SimpleIntArray& Radix;
|
---|
| 220 | // radix of each digit
|
---|
| 221 | // n-1 highest order, 0 lowest order
|
---|
| 222 | SimpleIntArray& Value; // value of each digit
|
---|
| 223 | const int n; // number of digits
|
---|
| 224 | int reverse; // value when order of digits is reversed
|
---|
| 225 | int product; // product of radices
|
---|
| 226 | int counter; // counter
|
---|
| 227 | bool finish; // true when we have gone over whole range
|
---|
| 228 | public:
|
---|
| 229 | MultiRadixCounter(int nx, const SimpleIntArray& rx,
|
---|
| 230 | SimpleIntArray& vx);
|
---|
| 231 | void operator++(); // increment the multi-radix counter
|
---|
| 232 | bool Swap() const { return reverse < counter; }
|
---|
| 233 | bool Finish() const { return finish; }
|
---|
| 234 | int Reverse() const { return reverse; }
|
---|
| 235 | int Counter() const { return counter; }
|
---|
| 236 | };
|
---|
| 237 |
|
---|
| 238 | // multiplication by Helmert matrix
|
---|
| 239 | ReturnMatrix Helmert(int n, bool full=false);
|
---|
| 240 | ReturnMatrix Helmert(const ColumnVector& X, bool full=false);
|
---|
| 241 | ReturnMatrix Helmert(int n, int j, bool full=false);
|
---|
| 242 | ReturnMatrix Helmert_transpose(const ColumnVector& Y, bool full=false);
|
---|
| 243 | Real Helmert_transpose(const ColumnVector& Y, int j, bool full=false);
|
---|
| 244 | ReturnMatrix Helmert(const Matrix& X, bool full=false);
|
---|
| 245 | ReturnMatrix Helmert_transpose(const Matrix& Y, bool full=false);
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 |
|
---|
| 250 | #ifdef use_namespace
|
---|
| 251 | }
|
---|
| 252 | #endif
|
---|
| 253 |
|
---|
| 254 |
|
---|
| 255 |
|
---|
| 256 | #endif
|
---|
| 257 |
|
---|
| 258 | // body file: cholesky.cpp
|
---|
| 259 | // body file: evalue.cpp
|
---|
| 260 | // body file: fft.cpp
|
---|
| 261 | // body file: hholder.cpp
|
---|
| 262 | // body file: jacobi.cpp
|
---|
| 263 | // body file: newfft.cpp
|
---|
| 264 | // body file: sort.cpp
|
---|
| 265 | // body file: svd.cpp
|
---|
| 266 | // body file: nm_misc.cpp
|
---|
| 267 |
|
---|
| 268 |
|
---|
| 269 |
|
---|
| 270 | ///@}
|
---|
| 271 |
|
---|
| 272 |
|
---|