CholeskyDecomposition.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Matrix.h>
  9. #include <Mathematics/GMatrix.h>
  10. namespace WwiseGTE
  11. {
  12. // Implementation for size known at compile time.
  13. template <typename Real, int N = 0>
  14. class CholeskyDecomposition
  15. {
  16. public:
  17. // Ensure that N > 0 at compile time.
  18. CholeskyDecomposition()
  19. {
  20. static_assert(N > 0, "Invalid size in CholeskyDecomposition constructor.");
  21. }
  22. // Disallow copies and moves.
  23. CholeskyDecomposition(CholeskyDecomposition const&) = delete;
  24. CholeskyDecomposition& operator=(CholeskyDecomposition const&) = delete;
  25. CholeskyDecomposition(CholeskyDecomposition&&) = delete;
  26. CholeskyDecomposition& operator=(CholeskyDecomposition&&) = delete;
  27. // On input, A is symmetric. Only the lower-triangular portion is
  28. // modified. On output, the lower-triangular portion is L where
  29. // A = L * L^T.
  30. bool Factor(Matrix<N, N, Real>& A)
  31. {
  32. for (int c = 0; c < N; ++c)
  33. {
  34. if (A(c, c) <= (Real)0)
  35. {
  36. return false;
  37. }
  38. A(c, c) = std::sqrt(A(c, c));
  39. for (int r = c + 1; r < N; ++r)
  40. {
  41. A(r, c) /= A(c, c);
  42. }
  43. for (int k = c + 1; k < N; ++k)
  44. {
  45. for (int r = k; r < N; ++r)
  46. {
  47. A(r, k) -= A(r, c) * A(k, c);
  48. }
  49. }
  50. }
  51. return true;
  52. }
  53. // Solve L*Y = B, where L is lower triangular and invertible. The
  54. // input value of Y is B. On output, Y is the solution.
  55. void SolveLower(Matrix<N, N, Real> const& L, Vector<N, Real>& Y)
  56. {
  57. for (int r = 0; r < N; ++r)
  58. {
  59. for (int c = 0; c < r; ++c)
  60. {
  61. Y[r] -= L(r, c) * Y[c];
  62. }
  63. Y[r] /= L(r, r);
  64. }
  65. }
  66. // Solve L^T*X = Y, where L is lower triangular (L^T is upper
  67. // triangular) and invertible. The input value of X is Y. On
  68. // output, X is the solution.
  69. void SolveUpper(Matrix<N, N, Real> const& L, Vector<N, Real>& X)
  70. {
  71. for (int r = N - 1; r >= 0; --r)
  72. {
  73. for (int c = r + 1; c < N; ++c)
  74. {
  75. X[r] -= L(c, r) * X[c];
  76. }
  77. X[r] /= L(r, r);
  78. }
  79. }
  80. };
  81. // Implementation for size known only at run time.
  82. template <typename Real>
  83. class CholeskyDecomposition<Real, 0>
  84. {
  85. public:
  86. int const N;
  87. // Ensure that N > 0 at run time.
  88. CholeskyDecomposition(int n)
  89. :
  90. N(n)
  91. {
  92. }
  93. // Disallow copies and moves. This is required to avoid compiler
  94. // complaints about the 'int const N' member.
  95. CholeskyDecomposition(CholeskyDecomposition const&) = delete;
  96. CholeskyDecomposition& operator=(CholeskyDecomposition const&) = delete;
  97. CholeskyDecomposition(CholeskyDecomposition&&) = delete;
  98. CholeskyDecomposition& operator=(CholeskyDecomposition&&) = delete;
  99. // On input, A is symmetric. Only the lower-triangular portion is
  100. // modified. On output, the lower-triangular portion is L where
  101. // A = L * L^T.
  102. bool Factor(GMatrix<Real>& A)
  103. {
  104. if (A.GetNumRows() == N && A.GetNumCols() == N)
  105. {
  106. for (int c = 0; c < N; ++c)
  107. {
  108. if (A(c, c) <= (Real)0)
  109. {
  110. return false;
  111. }
  112. A(c, c) = std::sqrt(A(c, c));
  113. for (int r = c + 1; r < N; ++r)
  114. {
  115. A(r, c) /= A(c, c);
  116. }
  117. for (int k = c + 1; k < N; ++k)
  118. {
  119. for (int r = k; r < N; ++r)
  120. {
  121. A(r, k) -= A(r, c) * A(k, c);
  122. }
  123. }
  124. }
  125. return true;
  126. }
  127. LogError("Matrix must be square.");
  128. }
  129. // Solve L*Y = B, where L is lower triangular and invertible. The
  130. // input value of Y is B. On output, Y is the solution.
  131. void SolveLower(GMatrix<Real> const& L, GVector<Real>& Y)
  132. {
  133. if (L.GetNumRows() == N && L.GetNumCols() == N && Y.GetSize() == N)
  134. {
  135. for (int r = 0; r < N; ++r)
  136. {
  137. for (int c = 0; c < r; ++c)
  138. {
  139. Y[r] -= L(r, c) * Y[c];
  140. }
  141. Y[r] /= L(r, r);
  142. }
  143. return;
  144. }
  145. LogError("Invalid size.");
  146. }
  147. // Solve L^T*X = Y, where L is lower triangular (L^T is upper
  148. // triangular) and invertible. The input value of X is Y. On
  149. // output, X is the solution.
  150. void SolveUpper(GMatrix<Real> const& L, GVector<Real>& X)
  151. {
  152. if (L.GetNumRows() == N && L.GetNumCols() == N && X.GetSize() == N)
  153. {
  154. for (int r = N - 1; r >= 0; --r)
  155. {
  156. for (int c = r + 1; c < N; ++c)
  157. {
  158. X[r] -= L(c, r) * X[c];
  159. }
  160. X[r] /= L(r, r);
  161. }
  162. }
  163. else
  164. {
  165. LogError("Invalid size.");
  166. }
  167. }
  168. };
  169. // Implementation for sizes known at compile time.
  170. template <typename Real, int BlockSize = 0, int NumBlocks = 0>
  171. class BlockCholeskyDecomposition
  172. {
  173. public:
  174. // Let B represent the block size and N represent the number of
  175. // blocks. The matrix A is (N*B)-by-(N*B) but partitioned into an
  176. // N-by-N matrix of blocks, each block of size B-by-B. The value
  177. // N*B is NumDimensions.
  178. enum
  179. {
  180. NumDimensions = NumBlocks * BlockSize
  181. };
  182. typedef std::array<Vector<BlockSize, Real>, NumBlocks> BlockVector;
  183. typedef std::array<std::array<Matrix<BlockSize, BlockSize, Real>, NumBlocks>, NumBlocks> BlockMatrix;
  184. // Ensure that BlockSize > 0 and NumBlocks > 0 at compile time.
  185. BlockCholeskyDecomposition()
  186. {
  187. static_assert(BlockSize > 0 && NumBlocks > 0, "Invalid size in BlockCholeskyDecomposition constructor.");
  188. }
  189. // Disallow copies and moves.
  190. BlockCholeskyDecomposition(BlockCholeskyDecomposition const&) = delete;
  191. BlockCholeskyDecomposition& operator=(BlockCholeskyDecomposition const&) = delete;
  192. BlockCholeskyDecomposition(BlockCholeskyDecomposition&&) = delete;
  193. BlockCholeskyDecomposition& operator=(BlockCholeskyDecomposition&&) = delete;
  194. // Treating the matrix as a 2D table of scalars with NUM_DIMENSIONS
  195. // rows and NUM_DIMENSIONS columns, look up the correct block that
  196. // stores the requested element and return a reference.
  197. Real Get(BlockMatrix const& M, int row, int col)
  198. {
  199. int b0 = col / BlockSize, b1 = row / BlockSize;
  200. int i0 = col % BlockSize, i1 = row % BlockSize;
  201. auto const& block = M[b1][b0];
  202. return block(i1, i0);
  203. }
  204. void Set(BlockMatrix& M, int row, int col, Real value)
  205. {
  206. int b0 = col / BlockSize, b1 = row / BlockSize;
  207. int i0 = col % BlockSize, i1 = row % BlockSize;
  208. auto& block = M[b1][b0];
  209. block(i1, i0) = value;
  210. }
  211. bool Factor(BlockMatrix& A)
  212. {
  213. for (int c = 0; c < NumBlocks; ++c)
  214. {
  215. if (!mDecomposer.Factor(A[c][c]))
  216. {
  217. return false;
  218. }
  219. for (int r = c + 1; r < NumBlocks; ++r)
  220. {
  221. LowerTriangularSolver(r, c, A);
  222. }
  223. for (int k = c + 1; k < NumBlocks; ++k)
  224. {
  225. for (int r = k; r < NumBlocks; ++r)
  226. {
  227. SubtractiveUpdate(r, k, c, A);
  228. }
  229. }
  230. }
  231. return true;
  232. }
  233. // Solve L*Y = B, where L is an invertible lower-triangular block
  234. // matrix whose diagonal blocks are lower-triangular matrices.
  235. // The input B is a block vector of commensurate size. The input
  236. // value of Y is B. On output, Y is the solution.
  237. void SolveLower(BlockMatrix const& L, BlockVector& Y)
  238. {
  239. for (int r = 0; r < NumBlocks; ++r)
  240. {
  241. auto& Yr = Y[r];
  242. for (int c = 0; c < r; ++c)
  243. {
  244. auto const& Lrc = L[r][c];
  245. auto const& Yc = Y[c];
  246. for (int i = 0; i < BlockSize; ++i)
  247. {
  248. for (int j = 0; j < BlockSize; ++j)
  249. {
  250. Yr[i] -= Lrc(i, j) * Yc[j];
  251. }
  252. }
  253. }
  254. mDecomposer.SolveLower(L[r][r], Yr);
  255. }
  256. }
  257. // Solve L^T*X = Y, where L is an invertible lower-triangular block
  258. // matrix (L^T is an upper-triangular block matrix) whose diagonal
  259. // blocks are lower-triangular matrices. The input value of X is Y.
  260. // On output, X is the solution.
  261. void SolveUpper(BlockMatrix const& L, BlockVector& X)
  262. {
  263. for (int r = NumBlocks - 1; r >= 0; --r)
  264. {
  265. auto& Xr = X[r];
  266. for (int c = r + 1; c < NumBlocks; ++c)
  267. {
  268. auto const& Lcr = L[c][r];
  269. auto const& Xc = X[c];
  270. for (int i = 0; i < BlockSize; ++i)
  271. {
  272. for (int j = 0; j < BlockSize; ++j)
  273. {
  274. Xr[i] -= Lcr(j, i) * Xc[j];
  275. }
  276. }
  277. }
  278. mDecomposer.SolveUpper(L[r][r], Xr);
  279. }
  280. }
  281. private:
  282. // Solve G(c,c)*G(r,c)^T = A(r,c)^T for G(r,c). The matrices
  283. // G(c,c) and A(r,c) are known quantities, and G(c,c) occupies
  284. // the lower triangular portion of A(c,c). The solver stores
  285. // its results in-place, so A(r,c) stores the G(r,c) result.
  286. void LowerTriangularSolver(int r, int c, BlockMatrix& A)
  287. {
  288. auto const& Acc = A[c][c];
  289. auto& Arc = A[r][c];
  290. for (int j = 0; j < BlockSize; ++j)
  291. {
  292. for (int i = 0; i < j; ++i)
  293. {
  294. Real Lji = Acc(j, i);
  295. for (int k = 0; k < BlockSize; ++k)
  296. {
  297. Arc(k, j) -= Lji * Arc(k, i);
  298. }
  299. }
  300. Real Ljj = Acc(j, j);
  301. for (int k = 0; k < BlockSize; ++k)
  302. {
  303. Arc(k, j) /= Ljj;
  304. }
  305. }
  306. }
  307. void SubtractiveUpdate(int r, int k, int c, BlockMatrix& A)
  308. {
  309. auto const& Arc = A[r][c];
  310. auto const& Akc = A[k][c];
  311. auto& Ark = A[r][k];
  312. for (int j = 0; j < BlockSize; ++j)
  313. {
  314. for (int i = 0; i < BlockSize; ++i)
  315. {
  316. for (int m = 0; m < BlockSize; ++m)
  317. {
  318. Ark(j, i) -= Arc(j, m) * Akc(i, m);
  319. }
  320. }
  321. }
  322. }
  323. CholeskyDecomposition<Real, BlockSize> mDecomposer;
  324. };
  325. // Implementation for sizes known only at run time.
  326. template <typename Real>
  327. class BlockCholeskyDecomposition<Real, 0, 0>
  328. {
  329. public:
  330. // Let B represent the block size and N represent the number of
  331. // blocks. The matrix A is (N*B)-by-(N*B) but partitioned into an
  332. // N-by-N matrix of blocks, each block of size B-by-B. The value
  333. // N*B is NumDimensions.
  334. int const BlockSize;
  335. int const NumBlocks;
  336. int const NumDimensions;
  337. // The number of elements in a BlockVector object must be NumBlocks
  338. // and each GVector element has BlockSize components.
  339. typedef std::vector<GVector<Real>> BlockVector;
  340. // The BlockMatrix is an array of NumBlocks-by-NumBlocks matrices.
  341. // Each block matrix is stored in row-major order. The BlockMatrix
  342. // elements themselves are stored in row-major order. The block
  343. // matrix element M = BlockMatrix[col + NumBlocks * row] is of size
  344. // BlockSize-by-BlockSize (in row-major order) and is in the (row,col)
  345. // location of the full matrix of blocks.
  346. typedef std::vector<GMatrix<Real>> BlockMatrix;
  347. // Ensure that BlockSize > 0 and NumDimensions > 0 at run time.
  348. BlockCholeskyDecomposition(int blockSize, int numBlocks)
  349. :
  350. BlockSize(blockSize),
  351. NumBlocks(numBlocks),
  352. NumDimensions(numBlocks * blockSize),
  353. mDecomposer(blockSize)
  354. {
  355. LogAssert(blockSize > 0 && numBlocks > 0, "Invalid input.");
  356. }
  357. // Disallow copies and moves. This is required to avoid compiler
  358. // complaints about the 'int const' members.
  359. BlockCholeskyDecomposition(BlockCholeskyDecomposition const&) = delete;
  360. BlockCholeskyDecomposition& operator=(BlockCholeskyDecomposition const&) = delete;
  361. BlockCholeskyDecomposition(BlockCholeskyDecomposition&&) = delete;
  362. BlockCholeskyDecomposition& operator=(BlockCholeskyDecomposition&&) = delete;
  363. // Treating the matrix as a 2D table of scalars with NumDimensions
  364. // rows and NumDimensions columns, look up the correct block that
  365. // stores the requested element and return a reference.
  366. Real Get(BlockMatrix const& M, int row, int col)
  367. {
  368. int b0 = col / BlockSize, b1 = row / BlockSize;
  369. int i0 = col % BlockSize, i1 = row % BlockSize;
  370. auto const& block = M[GetIndex(b1, b0)];
  371. return block(i1, i0);
  372. }
  373. void Set(BlockMatrix& M, int row, int col, Real value)
  374. {
  375. int b0 = col / BlockSize, b1 = row / BlockSize;
  376. int i0 = col % BlockSize, i1 = row % BlockSize;
  377. auto& block = M[GetIndex(b1, b0)];
  378. block(i1, i0) = value;
  379. }
  380. bool Factor(BlockMatrix& A)
  381. {
  382. for (int c = 0; c < NumBlocks; ++c)
  383. {
  384. if (!mDecomposer.Factor(A[GetIndex(c, c)]))
  385. {
  386. return false;
  387. }
  388. for (int r = c + 1; r < NumBlocks; ++r)
  389. {
  390. LowerTriangularSolver(r, c, A);
  391. }
  392. for (int k = c + 1; k < NumBlocks; ++k)
  393. {
  394. for (int r = k; r < NumBlocks; ++r)
  395. {
  396. SubtractiveUpdate(r, k, c, A);
  397. }
  398. }
  399. }
  400. return true;
  401. }
  402. // Solve L*Y = B, where L is an invertible lower-triangular block
  403. // matrix whose diagonal blocks are lower-triangular matrices.
  404. // The input B is a block vector of commensurate size. The input
  405. // value of Y is B. On output, Y is the solution.
  406. void SolveLower(BlockMatrix const& L, BlockVector& Y)
  407. {
  408. for (int r = 0; r < NumBlocks; ++r)
  409. {
  410. auto& Yr = Y[r];
  411. for (int c = 0; c < r; ++c)
  412. {
  413. auto const& Lrc = L[GetIndex(r, c)];
  414. auto const& Yc = Y[c];
  415. for (int i = 0; i < NumBlocks; ++i)
  416. {
  417. for (int j = 0; j < NumBlocks; ++j)
  418. {
  419. Yr[i] -= Lrc[GetIndex(i, j)] * Yc[j];
  420. }
  421. }
  422. }
  423. mDecomposer.SolveLower(L[GetIndex(r, r)], Yr);
  424. }
  425. }
  426. // Solve L^T*X = Y, where L is an invertible lower-triangular block
  427. // matrix (L^T is an upper-triangular block matrix) whose diagonal
  428. // blocks are lower-triangular matrices. The input value of X is Y.
  429. // On output, X is the solution.
  430. void SolveUpper(BlockMatrix const& L, BlockVector& X)
  431. {
  432. for (int r = NumBlocks - 1; r >= 0; --r)
  433. {
  434. auto& Xr = X[r];
  435. for (int c = r + 1; c < NumBlocks; ++c)
  436. {
  437. auto const& Lcr = L[GetIndex(c, r)];
  438. auto const& Xc = X[c];
  439. for (int i = 0; i < BlockSize; ++i)
  440. {
  441. for (int j = 0; j < BlockSize; ++j)
  442. {
  443. Xr[i] -= Lcr[GetIndex(j, i)] * Xc[j];
  444. }
  445. }
  446. }
  447. mDecomposer.SolveUpper(L[GetIndex(r, r)], Xr);
  448. }
  449. }
  450. private:
  451. // Compute the 1-dimensional index of the block matrix in a
  452. // 2-dimensional BlockMatrix object.
  453. inline int GetIndex(int row, int col) const
  454. {
  455. return col + row * NumBlocks;
  456. }
  457. // Solve G(c,c)*G(r,c)^T = A(r,c)^T for G(r,c). The matrices
  458. // G(c,c) and A(r,c) are known quantities, and G(c,c) occupies
  459. // the lower triangular portion of A(c,c). The solver stores
  460. // its results in-place, so A(r,c) stores the G(r,c) result.
  461. void LowerTriangularSolver(int r, int c, BlockMatrix& A)
  462. {
  463. auto const& Acc = A[GetIndex(c, c)];
  464. auto& Arc = A[GetIndex(r, c)];
  465. for (int j = 0; j < BlockSize; ++j)
  466. {
  467. for (int i = 0; i < j; ++i)
  468. {
  469. Real Lji = Acc[GetIndex(j, i)];
  470. for (int k = 0; k < BlockSize; ++k)
  471. {
  472. Arc[GetIndex(k, j)] -= Lji * Arc[GetIndex(k, i)];
  473. }
  474. }
  475. Real Ljj = Acc[GetIndex(j, j)];
  476. for (int k = 0; k < BlockSize; ++k)
  477. {
  478. Arc[GetIndex(k, j)] /= Ljj;
  479. }
  480. }
  481. }
  482. void SubtractiveUpdate(int r, int k, int c, BlockMatrix& A)
  483. {
  484. auto const& Arc = A[GetIndex(r, c)];
  485. auto const& Akc = A[GetIndex(k, c)];
  486. auto& Ark = A[GetIndex(r, k)];
  487. for (int j = 0; j < BlockSize; ++j)
  488. {
  489. for (int i = 0; i < BlockSize; ++i)
  490. {
  491. for (int m = 0; m < BlockSize; ++m)
  492. {
  493. Ark[GetIndex(j, i)] -= Arc[GetIndex(j, m)] * Akc[GetIndex(i, m)];
  494. }
  495. }
  496. }
  497. }
  498. // The decomposer has size BlockSize.
  499. CholeskyDecomposition<Real> mDecomposer;
  500. };
  501. }