philsupertramp/game-math
Loading...
Searching...
No Matches
backwardSub.h
Go to the documentation of this file.
1
16#pragma once
17#include "../utils.h"
18#include <iostream>
19#include <vector>
20
28 size_t m = R.rows();
29 size_t n = R.columns();
30 size_t v = b.rows();
31 size_t nv = b.columns();
32 if(v != m) {
33 // Error, cannot compute
34 std::cout << "Matrix vector dimension miss match, error!\n";
35 return Matrix<double>();
36 }
37 if(m != n) {
38 // Error, cannot compute
39 std::cout << "Matrix not squared. Dimension miss match, error!\n";
40 return Matrix<double>();
41 }
42
43 auto x = zeros(v, nv);
44
45 if(R(m - 1, n - 1) != 0) { x.SetRow(v - 1, b(v - 1) * (1 / R(m - 1, n - 1))); }
46
47 for(int j = (int)m - 2; j >= 0; j--) {
48 double s_k = 0.0f;
49 for(size_t k = j + 1; k < m; k++) { s_k += (R(j, k) * x(k, 0)); }
50 for(size_t i = 0; i < nv; ++i) { x(j, i) = (b(j, i) - s_k) / R(j, j); }
51 }
52
53 return x;
54}
Matrix< double > backwardSub(const Matrix< double > &R, const Matrix< double > &b)
Definition: backwardSub.h:27
Definition: Matrix.h:42
size_t rows() const
Definition: Matrix.h:193
size_t columns() const
Definition: Matrix.h:198
Matrix< double > zeros(size_t rows, size_t columns, size_t elements=1)