15 std::vector<std::vector<float>>
matMul(std::vector<std::vector<float>>& firstMatrix, std::vector<std::vector<float>>& secondMatrix) {
16 std::vector<std::vector<float>> res;
18 if(firstMatrix[0].
size() != secondMatrix.size()) {
19 throw std::runtime_error(
"Number of column of the first matrix should match with the number of rows of the second matrix ");
25 for(
size_t i = 0; i < firstMatrix.size(); ++i) {
26 std::vector<float> col_vec(secondMatrix[0].
size(), 0);
27 res.push_back(col_vec);
31 for(
size_t i = 0; i < firstMatrix.size(); ++i) {
32 for(
size_t j = 0; j < secondMatrix[0].size(); ++j) {
33 for(
size_t k = 0; k < firstMatrix[0].size(); ++k) {
34 res[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
46 static void getCofactor(std::vector<std::vector<float>>& A, std::vector<std::vector<float>>& temp,
size_t p,
size_t q,
size_t n) {
50 for(
size_t row = 0; row < n; ++row) {
51 for(
size_t col = 0; col < n; ++col) {
54 if(row != p && col != q) {
55 temp[i][j++] = A[row][col];
70 static float determinant(std::vector<std::vector<float>>& A,
size_t n) {
74 if(n == 1)
return A[0][0];
76 std::vector<std::vector<float>> temp(n, std::vector<float>(n, 0));
80 for(
size_t f = 0; f < n; ++f) {
93 static void adjoint(std::vector<std::vector<float>>& A, std::vector<std::vector<float>>& adj) {
101 std::vector<std::vector<float>> temp(A.size(), std::vector<float>(A.size(), 0));
103 for(
size_t i = 0; i < A.size(); ++i) {
104 for(
size_t j = 0; j < A.size(); ++j) {
110 sign = ((i + j) % 2 == 0) ? 1 : -1;
114 adj[j][i] = (sign) * (
determinant(temp, A.size() - 1));
121 bool matInv(std::vector<std::vector<float>>& A, std::vector<std::vector<float>>& inverse) {
123 if(A[0].
size() != A.size()) {
124 throw std::runtime_error(
"Not a Square Matrix ");
134 std::vector<std::vector<float>> adj(A.size(), std::vector<float>(A.size(), 0));
137 std::vector<float> temp;
139 for(
size_t i = 0; i < A.size(); ++i) {
140 for(
size_t j = 0; j < A.size(); ++j) {
141 temp.push_back(adj[i][j] / det);
143 inverse.push_back(temp);