diff --git a/mlpp/lin_alg/lin_alg_old.cpp b/mlpp/lin_alg/lin_alg_old.cpp index db25d21..d62662d 100644 --- a/mlpp/lin_alg/lin_alg_old.cpp +++ b/mlpp/lin_alg/lin_alg_old.cpp @@ -42,25 +42,6 @@ std::vector> MLPPLinAlgOld::gaussianNoise(int n, int m) { return A; } -Ref MLPPLinAlgOld::gaussian_noise(int n, int m) { - std::random_device rd; - std::default_random_engine generator(rd()); - std::normal_distribution distribution(0, 1); // Standard normal distribution. Mean of 0, std of 1. - - Ref A; - A.instance(); - A->resize(Size2i(m, n)); - - int a_data_size = A->data_size(); - real_t *a_ptr = A->ptrw(); - - for (int i = 0; i < a_data_size; ++i) { - a_ptr[i] = distribution(generator); - } - - return A; -} - std::vector> MLPPLinAlgOld::addition(std::vector> A, std::vector> B) { std::vector> C; C.resize(A.size()); @@ -108,83 +89,6 @@ std::vector> MLPPLinAlgOld::matmult(std::vector MLPPLinAlgOld::additionm(const Ref &A, const Ref &B) { - ERR_FAIL_COND_V(!A.is_valid() || !B.is_valid(), Ref()); - Size2i a_size = A->size(); - ERR_FAIL_COND_V(a_size != B->size(), Ref()); - - Ref C; - C.instance(); - C->resize(a_size); - - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = C->ptrw(); - - int data_size = A->data_size(); - - for (int i = 0; i < data_size; ++i) { - c_ptr[i] = a_ptr[i] + b_ptr[i]; - } - - return C; -} -Ref MLPPLinAlgOld::subtractionm(const Ref &A, const Ref &B) { - ERR_FAIL_COND_V(!A.is_valid() || !B.is_valid(), Ref()); - Size2i a_size = A->size(); - ERR_FAIL_COND_V(a_size != B->size(), Ref()); - - Ref C; - C.instance(); - C->resize(a_size); - - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = C->ptrw(); - - int data_size = A->data_size(); - - for (int i = 0; i < data_size; ++i) { - c_ptr[i] = a_ptr[i] - b_ptr[i]; - } - - return C; -} -Ref MLPPLinAlgOld::matmultm(const Ref &A, const Ref &B) { - ERR_FAIL_COND_V(!A.is_valid() || !B.is_valid(), Ref()); - - Size2i a_size = A->size(); - Size2i b_size = B->size(); - - ERR_FAIL_COND_V(a_size.x != b_size.y, Ref()); - - Ref C; - C.instance(); - C->resize(Size2i(b_size.x, a_size.y)); - C->fill(0); - - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = C->ptrw(); - - for (int i = 0; i < a_size.y; i++) { - for (int k = 0; k < b_size.y; k++) { - int ind_i_k = A->calculate_index(i, k); - - for (int j = 0; j < b_size.x; j++) { - int ind_i_j = C->calculate_index(i, j); - int ind_k_j = B->calculate_index(k, j); - - c_ptr[ind_i_j] += a_ptr[ind_i_k] * b_ptr[ind_k_j]; - - //C->set_element(i, j, C->get_element(i, j) + A->get_element(i, k) * B->get_element(k, j - } - } - } - - return C; -} - std::vector> MLPPLinAlgOld::hadamard_product(std::vector> A, std::vector> B) { std::vector> C; C.resize(A.size()); @@ -243,97 +147,6 @@ std::vector> MLPPLinAlgOld::elementWiseDivision(std::vector< return C; } -Ref MLPPLinAlgOld::hadamard_productm(const Ref &A, const Ref &B) { - ERR_FAIL_COND_V(!A.is_valid() || !B.is_valid(), Ref()); - Size2i a_size = A->size(); - ERR_FAIL_COND_V(a_size != B->size(), Ref()); - - Ref C; - C.instance(); - C->resize(a_size); - - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = C->ptrw(); - - for (int i = 0; i < a_size.y; i++) { - for (int j = 0; j < a_size.x; j++) { - int ind_i_j = A->calculate_index(i, j); - c_ptr[ind_i_j] = a_ptr[ind_i_j] * b_ptr[ind_i_j]; - } - } - - return C; -} -Ref MLPPLinAlgOld::kronecker_productm(const Ref &A, const Ref &B) { - // [1,1,1,1] [1,2,3,4,5] - // [1,1,1,1] [1,2,3,4,5] - // [1,2,3,4,5] - - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - - // Resulting matrix: A.size() * B.size() - // A[0].size() * B[0].size() - - ERR_FAIL_COND_V(!A.is_valid() || !B.is_valid(), Ref()); - Size2i a_size = A->size(); - Size2i b_size = B->size(); - - Ref C; - C.instance(); - C->resize(Size2i(b_size.x * a_size.x, b_size.y * a_size.y)); - - const real_t *a_ptr = A->ptr(); - - Ref row_tmp; - row_tmp.instance(); - row_tmp->resize(b_size.x); - - for (int i = 0; i < a_size.y; ++i) { - for (int j = 0; j < b_size.y; ++j) { - B->get_row_into_mlpp_vector(j, row_tmp); - - Vector> row; - for (int k = 0; k < a_size.x; ++k) { - row.push_back(scalar_multiplynv(a_ptr[A->calculate_index(i, k)], row_tmp)); - } - - Ref flattened_row = flattenv(row); - - C->set_row_mlpp_vector(i * b_size.y + j, flattened_row); - } - } - - return C; -} -Ref MLPPLinAlgOld::element_wise_divisionm(const Ref &A, const Ref &B) { - ERR_FAIL_COND_V(!A.is_valid() || !B.is_valid(), Ref()); - Size2i a_size = A->size(); - ERR_FAIL_COND_V(a_size != B->size(), Ref()); - - Ref C; - C.instance(); - C->resize(a_size); - - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = C->ptrw(); - - for (int i = 0; i < a_size.y; i++) { - for (int j = 0; j < a_size.x; j++) { - int ind_i_j = A->calculate_index(i, j); - c_ptr[ind_i_j] = a_ptr[ind_i_j] / b_ptr[ind_i_j]; - } - } - - return C; -} - std::vector> MLPPLinAlgOld::transpose(std::vector> A) { std::vector> AT; AT.resize(A[0].size()); @@ -367,52 +180,6 @@ std::vector> MLPPLinAlgOld::scalarAdd(real_t scalar, std::ve return A; } -Ref MLPPLinAlgOld::transposem(const Ref &A) { - Size2i a_size = A->size(); - - Ref AT; - AT.instance(); - AT->resize(Size2i(a_size.y, a_size.x)); - - const real_t *a_ptr = A->ptr(); - real_t *at_ptr = AT->ptrw(); - - for (int i = 0; i < a_size.y; ++i) { - for (int j = 0; j < a_size.x; ++j) { - at_ptr[AT->calculate_index(j, i)] = a_ptr[A->calculate_index(i, j)]; - } - } - - return AT; -} -Ref MLPPLinAlgOld::scalar_multiplym(real_t scalar, const Ref &A) { - Ref AN = A->duplicate(); - Size2i a_size = AN->size(); - real_t *an_ptr = AN->ptrw(); - - for (int i = 0; i < a_size.y; ++i) { - for (int j = 0; j < a_size.x; ++j) { - an_ptr[AN->calculate_index(i, j)] *= scalar; - } - } - - return AN; -} - -Ref MLPPLinAlgOld::scalar_addm(real_t scalar, const Ref &A) { - Ref AN = A->duplicate(); - Size2i a_size = AN->size(); - real_t *an_ptr = AN->ptrw(); - - for (int i = 0; i < a_size.y; ++i) { - for (int j = 0; j < a_size.x; ++j) { - an_ptr[AN->calculate_index(i, j)] += scalar; - } - } - - return AN; -} - std::vector> MLPPLinAlgOld::log(std::vector> A) { std::vector> B; B.resize(A.size()); @@ -486,131 +253,6 @@ std::vector> MLPPLinAlgOld::cbrt(std::vector MLPPLinAlgOld::logm(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); - - Ref out; - out.instance(); - - int data_size = A->data_size(); - out->resize(A->size()); - - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < data_size; ++i) { - out_ptr[i] = Math::log(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::log10m(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); - - Ref out; - out.instance(); - - int data_size = A->data_size(); - out->resize(A->size()); - - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < data_size; ++i) { - out_ptr[i] = std::log10(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::expm(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); - - Ref out; - out.instance(); - - int data_size = A->data_size(); - out->resize(A->size()); - - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < data_size; ++i) { - out_ptr[i] = Math::exp(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::erfm(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); - - Ref out; - out.instance(); - - int data_size = A->data_size(); - out->resize(A->size()); - - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < data_size; ++i) { - out_ptr[i] = std::erf(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::exponentiatem(const Ref &A, real_t p) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); - - Ref out; - out.instance(); - - int data_size = A->data_size(); - out->resize(A->size()); - - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < data_size; ++i) { - out_ptr[i] = Math::pow(a_ptr[i], p); - } - - return out; -} -Ref MLPPLinAlgOld::sqrtm(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); - - Ref out; - out.instance(); - - int data_size = A->data_size(); - out->resize(A->size()); - - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < data_size; ++i) { - out_ptr[i] = Math::sqrt(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::cbrtm(const Ref &A) { - return exponentiatem(A, real_t(1) / real_t(3)); -} - -std::vector> MLPPLinAlgOld::matrixPower(std::vector> A, int n) { - std::vector> B = identity(A.size()); - if (n == 0) { - return identity(A.size()); - } else if (n < 0) { - A = inverse(A); - } - for (int i = 0; i < std::abs(n); i++) { - B = matmult(B, A); - } - return B; -} - std::vector> MLPPLinAlgOld::abs(std::vector> A) { std::vector> B; B.resize(A.size()); @@ -625,25 +267,6 @@ std::vector> MLPPLinAlgOld::abs(std::vector MLPPLinAlgOld::absm(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); - - Ref out; - out.instance(); - - int data_size = A->data_size(); - out->resize(A->size()); - - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < data_size; ++i) { - out_ptr[i] = ABS(a_ptr[i]); - } - - return out; -} - real_t MLPPLinAlgOld::det(std::vector> A, int d) { real_t deter = 0; std::vector> B; @@ -679,43 +302,6 @@ real_t MLPPLinAlgOld::det(std::vector> A, int d) { return deter; } -real_t MLPPLinAlgOld::detm(const Ref &A, int d) { - ERR_FAIL_COND_V(!A.is_valid(), 0); - - real_t deter = 0; - Ref B; - B.instance(); - B->resize(Size2i(d, d)); - B->fill(0); - - /* This is the base case in which the input is a 2x2 square matrix. - Recursion is performed unless and until we reach this base case, - such that we recieve a scalar as the result. */ - if (d == 2) { - return A->get_element(0, 0) * A->get_element(1, 1) - A->get_element(0, 1) * A->get_element(1, 0); - } else { - for (int i = 0; i < d; i++) { - int sub_i = 0; - for (int j = 1; j < d; j++) { - int sub_j = 0; - for (int k = 0; k < d; k++) { - if (k == i) { - continue; - } - - B->set_element(sub_i, sub_j, A->get_element(j, k)); - sub_j++; - } - sub_i++; - } - - deter += Math::pow(static_cast(-1), static_cast(i)) * A->get_element(0, i) * detm(B, d - 1); - } - } - - return deter; -} - real_t MLPPLinAlgOld::trace(std::vector> A) { real_t trace = 0; for (uint32_t i = 0; i < A.size(); i++) { @@ -791,76 +377,6 @@ std::vector> MLPPLinAlgOld::pinverse(std::vector MLPPLinAlgOld::cofactorm(const Ref &A, int n, int i, int j) { - Ref cof; - cof.instance(); - cof->resize(A->size()); - - int sub_i = 0; - int sub_j = 0; - - for (int row = 0; row < n; row++) { - for (int col = 0; col < n; col++) { - if (row != i && col != j) { - cof->set_element(sub_i, sub_j++, A->get_element(row, col)); - - if (sub_j == n - 1) { - sub_j = 0; - sub_i++; - } - } - } - } - - return cof; -} -Ref MLPPLinAlgOld::adjointm(const Ref &A) { - Ref adj; - - ERR_FAIL_COND_V(!A.is_valid(), adj); - - Size2i a_size = A->size(); - - ERR_FAIL_COND_V(a_size.x != a_size.y, adj); - - //Resizing the initial adjoint matrix - - adj.instance(); - adj->resize(a_size); - - // Checking for the case where the given N x N matrix is a scalar - if (a_size.y == 1) { - adj->set_element(0, 0, 1); - return adj; - } - - if (a_size.y == 2) { - adj->set_element(0, 0, A->get_element(1, 1)); - adj->set_element(1, 1, A->get_element(0, 0)); - - adj->set_element(0, 1, -A->get_element(0, 1)); - adj->set_element(1, 0, -A->get_element(1, 0)); - - return adj; - } - - for (int i = 0; i < a_size.y; i++) { - for (int j = 0; j < a_size.x; j++) { - Ref cof = cofactorm(A, a_size.y, i, j); - // 1 if even, -1 if odd - int sign = (i + j) % 2 == 0 ? 1 : -1; - adj->set_element(j, i, sign * detm(cof, int(a_size.y) - 1)); - } - } - return adj; -} -Ref MLPPLinAlgOld::inversem(const Ref &A) { - return scalar_multiplym(1 / detm(A, int(A->size().y)), adjointm(A)); -} -Ref MLPPLinAlgOld::pinversem(const Ref &A) { - return matmultm(inversem(matmultm(transposem(A), A)), transposem(A)); -} - std::vector> MLPPLinAlgOld::zeromat(int n, int m) { std::vector> zeromat; zeromat.resize(n); @@ -874,34 +390,6 @@ std::vector> MLPPLinAlgOld::onemat(int n, int m) { return full(n, m, 1); } -Ref MLPPLinAlgOld::zeromatm(int n, int m) { - Ref mat; - mat.instance(); - - mat->resize(Size2i(m, n)); - mat->fill(0); - - return mat; -} -Ref MLPPLinAlgOld::onematm(int n, int m) { - Ref mat; - mat.instance(); - - mat->resize(Size2i(m, n)); - mat->fill(1); - - return mat; -} -Ref MLPPLinAlgOld::fullm(int n, int m, int k) { - Ref mat; - mat.instance(); - - mat->resize(Size2i(m, n)); - mat->fill(k); - - return mat; -} - std::vector> MLPPLinAlgOld::full(int n, int m, int k) { std::vector> full; full.resize(n); @@ -944,43 +432,6 @@ std::vector> MLPPLinAlgOld::cos(std::vector MLPPLinAlgOld::sinm(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); - - Ref out; - out.instance(); - - int data_size = A->data_size(); - out->resize(A->size()); - - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < data_size; ++i) { - out_ptr[i] = Math::sin(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::cosm(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); - - Ref out; - out.instance(); - - int data_size = A->data_size(); - out->resize(A->size()); - - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < data_size; ++i) { - out_ptr[i] = Math::cos(a_ptr[i]); - } - - return out; -} - std::vector MLPPLinAlgOld::max(std::vector a, std::vector b) { std::vector c; c.resize(a.size()); @@ -1044,21 +495,6 @@ std::vector> MLPPLinAlgOld::identity(real_t d) { return identityMat; } -Ref MLPPLinAlgOld::identitym(int d) { - Ref identity_mat; - identity_mat.instance(); - identity_mat->resize(Size2i(d, d)); - identity_mat->fill(0); - - real_t *im_ptr = identity_mat->ptrw(); - - for (int i = 0; i < d; i++) { - im_ptr[identity_mat->calculate_index(i, i)] = 1; - } - - return identity_mat; -} - std::vector> MLPPLinAlgOld::cov(std::vector> A) { MLPPStat stat; std::vector> covMat; @@ -1074,37 +510,6 @@ std::vector> MLPPLinAlgOld::cov(std::vector MLPPLinAlgOld::covm(const Ref &A) { - MLPPStat stat; - - Ref cov_mat; - cov_mat.instance(); - - Size2i a_size = A->size(); - - cov_mat->resize(a_size); - - Ref a_i_row_tmp; - a_i_row_tmp.instance(); - a_i_row_tmp->resize(a_size.x); - - Ref a_j_row_tmp; - a_j_row_tmp.instance(); - a_j_row_tmp->resize(a_size.x); - - for (int i = 0; i < a_size.y; ++i) { - A->get_row_into_mlpp_vector(i, a_i_row_tmp); - - for (int j = 0; j < a_size.x; ++j) { - A->get_row_into_mlpp_vector(j, a_j_row_tmp); - - cov_mat->set_element(i, j, stat.covariancev(a_i_row_tmp, a_j_row_tmp)); - } - } - - return cov_mat; -} - std::tuple>, std::vector>> MLPPLinAlgOld::eig(std::vector> A) { /* A (the entered parameter) in most use cases will be X'X, XX', etc. and must be symmetric. @@ -1350,146 +755,6 @@ MLPPLinAlgOld::EigenResultOld MLPPLinAlgOld::eigen_old(std::vector A) { - EigenResult res; - - ERR_FAIL_COND_V(!A.is_valid(), res); - - /* - A (the entered parameter) in most use cases will be X'X, XX', etc. and must be symmetric. - That simply means that 1) X' = X and 2) X is a square matrix. This function that computes the - eigenvalues of a matrix is utilizing Jacobi's method. - */ - - real_t diagonal = true; // Perform the iterative Jacobi algorithm unless and until we reach a diagonal matrix which yields us the eigenvals. - - HashMap val_to_vec; - Ref a_new; - Ref eigenvectors = identitym(A->size().y); - Size2i a_size = A->size(); - - do { - real_t a_ij = A->get_element(0, 1); - real_t sub_i = 0; - real_t sub_j = 1; - for (int i = 0; i < a_size.y; ++i) { - for (int j = 0; j < a_size.x; ++j) { - real_t ca_ij = A->get_element(i, j); - real_t abs_ca_ij = ABS(ca_ij); - - if (i != j && abs_ca_ij > a_ij) { - a_ij = ca_ij; - sub_i = i; - sub_j = j; - } else if (i != j && abs_ca_ij == a_ij) { - if (i < sub_i) { - a_ij = ca_ij; - sub_i = i; - sub_j = j; - } - } - } - } - - real_t a_ii = A->get_element(sub_i, sub_i); - real_t a_jj = A->get_element(sub_j, sub_j); - //real_t a_ji = A->get_element(sub_j, sub_i); - real_t theta; - - if (a_ii == a_jj) { - theta = M_PI / 4; - } else { - theta = 0.5 * atan(2 * a_ij / (a_ii - a_jj)); - } - - Ref P = identitym(A->size().y); - P->set_element(sub_i, sub_j, -Math::sin(theta)); - P->set_element(sub_i, sub_i, Math::cos(theta)); - P->set_element(sub_j, sub_j, Math::cos(theta)); - P->set_element(sub_j, sub_i, Math::sin(theta)); - - a_new = matmultm(matmultm(inversem(P), A), P); - - Size2i a_new_size = a_new->size(); - - for (int i = 0; i < a_new_size.y; ++i) { - for (int j = 0; j < a_new_size.x; ++j) { - if (i != j && Math::is_zero_approx(Math::round(a_new->get_element(i, j)))) { - a_new->set_element(i, j, 0); - } - } - } - - bool non_zero = false; - for (int i = 0; i < a_new_size.y; ++i) { - for (int j = 0; j < a_new_size.x; ++j) { - if (i != j && Math::is_zero_approx(Math::round(a_new->get_element(i, j)))) { - non_zero = true; - } - } - } - - if (non_zero) { - diagonal = false; - } else { - diagonal = true; - } - - if (a_new->is_equal_approx(A)) { - diagonal = true; - for (int i = 0; i < a_new_size.y; ++i) { - for (int j = 0; j < a_new_size.x; ++j) { - if (i != j) { - a_new->set_element(i, j, 0); - } - } - } - } - - eigenvectors = matmultm(eigenvectors, P); - A = a_new; - - } while (!diagonal); - - Ref a_new_prior = a_new->duplicate(); - - Size2i a_new_size = a_new->size(); - - // Bubble Sort. Should change this later. - for (int i = 0; i < a_new_size.y - 1; ++i) { - for (int j = 0; j < a_new_size.x - 1 - i; ++j) { - if (a_new->get_element(j, j) < a_new->get_element(j + 1, j + 1)) { - real_t temp = a_new->get_element(j + 1, j + 1); - a_new->set_element(j + 1, j + 1, a_new->get_element(j, j)); - a_new->set_element(j, j, temp); - } - } - } - - for (int i = 0; i < a_new_size.y; ++i) { - for (int j = 0; j < a_new_size.x; ++j) { - if (a_new->get_element(i, i) == a_new_prior->get_element(j, j)) { - val_to_vec[i] = j; - } - } - } - - Ref eigen_temp = eigenvectors->duplicate(); - - Size2i eigenvectors_size = eigenvectors->size(); - - for (int i = 0; i < eigenvectors_size.y; ++i) { - for (int j = 0; j < eigenvectors_size.x; ++j) { - eigenvectors->set_element(i, j, eigen_temp->get_element(i, val_to_vec[j])); - } - } - - res.eigen_vectors = eigenvectors; - res.eigen_values = a_new; - - return res; -} - MLPPLinAlgOld::SVDResultOld MLPPLinAlgOld::SVD(std::vector> A) { EigenResultOld left_eigen = eigen_old(matmult(A, transpose(A))); EigenResultOld right_eigen = eigen_old(matmult(transpose(A), A)); @@ -1510,34 +775,6 @@ MLPPLinAlgOld::SVDResultOld MLPPLinAlgOld::SVD(std::vector> return res; } -MLPPLinAlgOld::SVDResult MLPPLinAlgOld::svd(const Ref &A) { - SVDResult res; - - ERR_FAIL_COND_V(!A.is_valid(), res); - - Size2i a_size = A->size(); - - EigenResult left_eigen = eigen(matmultm(A, transposem(A))); - EigenResult right_eigen = eigen(matmultm(transposem(A), A)); - - Ref singularvals = sqrtm(left_eigen.eigen_values); - Ref sigma = zeromatm(a_size.y, a_size.x); - - Size2i singularvals_size = singularvals->size(); - - for (int i = 0; i < singularvals_size.y; ++i) { - for (int j = 0; j < singularvals_size.x; ++j) { - sigma->set_element(i, j, singularvals->get_element(i, j)); - } - } - - res.U = left_eigen.eigen_vectors; - res.S = sigma; - res.Vt = right_eigen.eigen_vectors; - - return res; -} - std::vector MLPPLinAlgOld::vectorProjection(std::vector a, std::vector b) { real_t product = dot(a, b) / dot(a, a); return scalarMultiply(product, a); // Projection of vector a onto b. Denotated as proj_a(b). @@ -1647,35 +884,6 @@ std::vector MLPPLinAlgOld::flatten(std::vector> A) { return a; } -Ref MLPPLinAlgOld::flattenv(const Vector> &A) { - Ref a; - a.instance(); - - int vsize = 0; - for (int i = 0; i < A.size(); ++i) { - vsize += A[i]->size(); - } - - a->resize(vsize); - - int a_index = 0; - real_t *a_ptr = a->ptrw(); - - for (int i = 0; i < A.size(); ++i) { - const Ref &r = A[i]; - - int r_size = r->size(); - const real_t *r_ptr = r->ptr(); - - for (int j = 0; j < r_size; ++j) { - a_ptr[a_index] = r_ptr[j]; - ++a_index; - } - } - - return a; -} - std::vector MLPPLinAlgOld::solve(std::vector> A, std::vector b) { return mat_vec_mult(inverse(A), b); } @@ -1749,26 +957,6 @@ std::vector> MLPPLinAlgOld::outerProduct(std::vector return C; } -Ref MLPPLinAlgOld::outer_product(const Ref &a, const Ref &b) { - Ref C; - C.instance(); - Size2i size = Size2i(b->size(), a->size()); - C->resize(size); - - const real_t *a_ptr = a->ptr(); - const real_t *b_ptr = b->ptr(); - - for (int i = 0; i < size.y; ++i) { - real_t curr_a = a_ptr[i]; - - for (int j = 0; j < size.x; ++j) { - C->set_element(i, j, curr_a * b_ptr[j]); - } - } - - return C; -} - std::vector MLPPLinAlgOld::hadamard_product(std::vector a, std::vector b) { std::vector c; c.resize(a.size()); @@ -1780,48 +968,6 @@ std::vector MLPPLinAlgOld::hadamard_product(std::vector a, std:: return c; } -Ref MLPPLinAlgOld::hadamard_productnv(const Ref &a, const Ref &b) { - ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - - ERR_FAIL_COND_V(size != b->size(), Ref()); - - out->resize(size); - - const real_t *a_ptr = a->ptr(); - const real_t *b_ptr = b->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = a_ptr[i] * b_ptr[i]; - } - - return out; -} -void MLPPLinAlgOld::hadamard_productv(const Ref &a, const Ref &b, Ref out) { - ERR_FAIL_COND(!a.is_valid() || !b.is_valid() || !out.is_valid()); - - int size = a->size(); - - ERR_FAIL_COND(size != b->size()); - - if (unlikely(out->size() != size)) { - out->resize(size); - } - - const real_t *a_ptr = a->ptr(); - const real_t *b_ptr = b->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = a_ptr[i] * b_ptr[i]; - } -} - std::vector MLPPLinAlgOld::elementWiseDivision(std::vector a, std::vector b) { std::vector c; c.resize(a.size()); @@ -1832,29 +978,6 @@ std::vector MLPPLinAlgOld::elementWiseDivision(std::vector a, st return c; } -Ref MLPPLinAlgOld::element_wise_division(const Ref &a, const Ref &b) { - ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - - ERR_FAIL_COND_V(size != b->size(), Ref()); - - out->resize(size); - - const real_t *a_ptr = a->ptr(); - const real_t *b_ptr = b->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = a_ptr[i] / b_ptr[i]; - } - - return out; -} - std::vector MLPPLinAlgOld::scalarMultiply(real_t scalar, std::vector a) { for (uint32_t i = 0; i < a.size(); i++) { a[i] *= scalar; @@ -1862,42 +985,6 @@ std::vector MLPPLinAlgOld::scalarMultiply(real_t scalar, std::vector MLPPLinAlgOld::scalar_multiplynv(real_t scalar, const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - - out->resize(size); - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = a_ptr[i] * scalar; - } - - return out; -} -void MLPPLinAlgOld::scalar_multiplyv(real_t scalar, const Ref &a, Ref out) { - ERR_FAIL_COND(!a.is_valid() || !out.is_valid()); - - int size = a->size(); - - if (unlikely(out->size() != size)) { - out->resize(size); - } - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = a_ptr[i] * scalar; - } -} - std::vector MLPPLinAlgOld::scalarAdd(real_t scalar, std::vector a) { for (uint32_t i = 0; i < a.size(); i++) { a[i] += scalar; @@ -1905,42 +992,6 @@ std::vector MLPPLinAlgOld::scalarAdd(real_t scalar, std::vector return a; } -Ref MLPPLinAlgOld::scalar_addnv(real_t scalar, const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - - out->resize(size); - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = a_ptr[i] + scalar; - } - - return out; -} -void MLPPLinAlgOld::scalar_addv(real_t scalar, const Ref &a, Ref out) { - ERR_FAIL_COND(!a.is_valid() || !out.is_valid()); - - int size = a->size(); - - if (unlikely(out->size() != size)) { - out->resize(size); - } - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = a_ptr[i] + scalar; - } -} - std::vector MLPPLinAlgOld::addition(std::vector a, std::vector b) { std::vector c; c.resize(a.size()); @@ -1950,47 +1001,6 @@ std::vector MLPPLinAlgOld::addition(std::vector a, std::vector MLPPLinAlgOld::additionnv(const Ref &a, const Ref &b) { - ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref()); - - int size = a->size(); - - ERR_FAIL_COND_V(size != b->size(), Ref()); - - Ref out; - out.instance(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); - const real_t *b_ptr = b->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = a_ptr[i] + b_ptr[i]; - } - - return out; -} -void MLPPLinAlgOld::additionv(const Ref &a, const Ref &b, Ref out) { - ERR_FAIL_COND(!a.is_valid() || !b.is_valid() || !out.is_valid()); - - int size = a->size(); - - ERR_FAIL_COND(size != b->size()); - - if (unlikely(out->size() != size)) { - out->resize(size); - } - - const real_t *a_ptr = a->ptr(); - const real_t *b_ptr = b->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = a_ptr[i] + b_ptr[i]; - } -} - std::vector MLPPLinAlgOld::subtraction(std::vector a, std::vector b) { std::vector c; c.resize(a.size()); @@ -2000,52 +1010,6 @@ std::vector MLPPLinAlgOld::subtraction(std::vector a, std::vecto return c; } -Ref MLPPLinAlgOld::subtractionnv(const Ref &a, const Ref &b) { - ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref()); - - int size = a->size(); - - ERR_FAIL_COND_V(size != b->size(), Ref()); - - Ref out; - out.instance(); - - if (unlikely(size == 0)) { - return out; - } - - out->resize(size); - - const real_t *a_ptr = a->ptr(); - const real_t *b_ptr = b->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = a_ptr[i] - b_ptr[i]; - } - - return out; -} -void MLPPLinAlgOld::subtractionv(const Ref &a, const Ref &b, Ref out) { - ERR_FAIL_COND(!a.is_valid() || !b.is_valid() || !out.is_valid()); - - int size = a->size(); - - ERR_FAIL_COND(size != b->size()); - - if (unlikely(out->size() != size)) { - out->resize(size); - } - - const real_t *a_ptr = a->ptr(); - const real_t *b_ptr = b->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = a_ptr[i] - b_ptr[i]; - } -} - std::vector MLPPLinAlgOld::subtractMatrixRows(std::vector a, std::vector> B) { for (uint32_t i = 0; i < B.size(); i++) { a = subtraction(a, B[i]); @@ -2053,25 +1017,6 @@ std::vector MLPPLinAlgOld::subtractMatrixRows(std::vector a, std return a; } -Ref MLPPLinAlgOld::subtract_matrix_rows(const Ref &a, const Ref &B) { - Ref c = a->duplicate(); - - Size2i b_size = B->size(); - - ERR_FAIL_COND_V(b_size.x != c->size(), c); - - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = c->ptrw(); - - for (int i = 0; i < b_size.y; ++i) { - for (int j = 0; j < b_size.x; ++j) { - c_ptr[j] -= b_ptr[B->calculate_index(i, j)]; - } - } - - return c; -} - std::vector MLPPLinAlgOld::log(std::vector a) { std::vector b; b.resize(a.size()); @@ -2125,141 +1070,6 @@ std::vector MLPPLinAlgOld::cbrt(std::vector a) { return exponentiate(a, real_t(1) / real_t(3)); } -Ref MLPPLinAlgOld::logv(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = Math::log(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::log10v(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = std::log10(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::expv(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = Math::exp(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::erfv(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = std::erf(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::exponentiatev(const Ref &a, real_t p) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = Math::pow(a_ptr[i], p); - } - - return out; -} -Ref MLPPLinAlgOld::sqrtv(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = Math::sqrt(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::cbrtv(const Ref &a) { - return exponentiatev(a, static_cast(1) / static_cast(3)); -} - -real_t MLPPLinAlgOld::dot(std::vector a, std::vector b) { - real_t c = 0; - for (uint32_t i = 0; i < a.size(); i++) { - c += a[i] * b[i]; - } - return c; -} - -real_t MLPPLinAlgOld::dotv(const Ref &a, const Ref &b) { - int a_size = a->size(); - - ERR_FAIL_COND_V(a_size != b->size(), 0); - - const real_t *a_ptr = a->ptr(); - const real_t *b_ptr = b->ptr(); - - real_t c = 0; - for (int i = 0; i < a_size; ++i) { - c += a_ptr[i] * b_ptr[i]; - } - return c; -} - std::vector MLPPLinAlgOld::cross(std::vector a, std::vector b) { // Cross products exist in R^7 also. Though, I will limit it to R^3 as Wolfram does this. std::vector> mat = { onevec(3), a, b }; @@ -2298,25 +1108,6 @@ std::vector> MLPPLinAlgOld::diag(std::vector a) { return B; } -Ref MLPPLinAlgOld::diagm(const Ref &a) { - int a_size = a->size(); - - Ref B; - B.instance(); - - B->resize(Size2i(a_size, a_size)); - B->fill(0); - - const real_t *a_ptr = a->ptr(); - real_t *b_ptr = B->ptrw(); - - for (int i = 0; i < a_size; ++i) { - b_ptr[B->calculate_index(i, i)] = a_ptr[i]; - } - - return B; -} - std::vector MLPPLinAlgOld::full(int n, int k) { std::vector full; full.resize(n); @@ -2326,53 +1117,6 @@ std::vector MLPPLinAlgOld::full(int n, int k) { return full; } -Ref MLPPLinAlgOld::absv(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = ABS(a_ptr[i]); - } - - return out; -} - -Ref MLPPLinAlgOld::zerovecv(int n) { - Ref vec; - vec.instance(); - - vec->resize(n); - vec->fill(0); - - return vec; -} -Ref MLPPLinAlgOld::onevecv(int n) { - Ref vec; - vec.instance(); - - vec->resize(n); - vec->fill(1); - - return vec; -} -Ref MLPPLinAlgOld::fullv(int n, int k) { - Ref vec; - vec.instance(); - - vec->resize(n); - vec->fill(k); - - return vec; -} - std::vector MLPPLinAlgOld::sin(std::vector a) { std::vector b; b.resize(a.size()); @@ -2391,43 +1135,6 @@ std::vector MLPPLinAlgOld::cos(std::vector a) { return b; } -Ref MLPPLinAlgOld::sinv(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = Math::sin(a_ptr[i]); - } - - return out; -} -Ref MLPPLinAlgOld::cosv(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); - - Ref out; - out.instance(); - - int size = a->size(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); - - for (int i = 0; i < size; ++i) { - out_ptr[i] = Math::cos(a_ptr[i]); - } - - return out; -} - std::vector> MLPPLinAlgOld::rotate(std::vector> A, real_t theta, int axis) { std::vector> rotationMatrix = { { std::cos(theta), -std::sin(theta) }, { std::sin(theta), std::cos(theta) } }; if (axis == 0) { @@ -2491,43 +1198,6 @@ real_t MLPPLinAlgOld::euclideanDistance(std::vector a, std::vector &a, const Ref &b) { - ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), 0); - - int a_size = a->size(); - - ERR_FAIL_COND_V(a_size != b->size(), 0); - - const real_t *aa = a->ptr(); - const real_t *ba = b->ptr(); - - real_t dist = 0; - - for (int i = 0; i < a_size; i++) { - dist += (aa[i] - ba[i]) * (aa[i] - ba[i]); - } - - return Math::sqrt(dist); -} -real_t MLPPLinAlgOld::euclidean_distance_squared(const Ref &a, const Ref &b) { - ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), 0); - - int a_size = a->size(); - - ERR_FAIL_COND_V(a_size != b->size(), 0); - - const real_t *aa = a->ptr(); - const real_t *ba = b->ptr(); - - real_t dist = 0; - - for (int i = 0; i < a_size; i++) { - dist += (aa[i] - ba[i]) * (aa[i] - ba[i]); - } - - return dist; -} - real_t MLPPLinAlgOld::norm_2(std::vector a) { return std::sqrt(norm_sq(a)); } @@ -2539,18 +1209,6 @@ real_t MLPPLinAlgOld::norm_sq(std::vector a) { } return n_sq; } -real_t MLPPLinAlgOld::norm_sqv(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), 0); - - int size = a->size(); - const real_t *a_ptr = a->ptr(); - - real_t n_sq = 0; - for (int i = 0; i < size; ++i) { - n_sq += a_ptr[i] * a_ptr[i]; - } - return n_sq; -} real_t MLPPLinAlgOld::sum_elements(std::vector a) { real_t sum = 0; @@ -2560,18 +1218,6 @@ real_t MLPPLinAlgOld::sum_elements(std::vector a) { return sum; } -real_t MLPPLinAlgOld::sum_elementsv(const Ref &a) { - int a_size = a->size(); - - const real_t *a_ptr = a->ptr(); - - real_t sum = 0; - for (int i = 0; i < a_size; ++i) { - sum += a_ptr[i]; - } - return sum; -} - real_t MLPPLinAlgOld::cosineSimilarity(std::vector a, std::vector b) { return dot(a, b) / (norm_2(a) * norm_2(b)); } @@ -2604,59 +1250,6 @@ std::vector MLPPLinAlgOld::mat_vec_mult(std::vector> return c; } -Ref MLPPLinAlgOld::mat_vec_addv(const Ref &A, const Ref &b) { - ERR_FAIL_COND_V(!A.is_valid() || !b.is_valid(), Ref()); - - Size2i a_size = A->size(); - - ERR_FAIL_COND_V(a_size.x != b->size(), Ref()); - - Ref ret; - ret.instance(); - ret->resize(a_size); - - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = b->ptr(); - real_t *ret_ptr = ret->ptrw(); - - for (int i = 0; i < a_size.y; ++i) { - for (int j = 0; j < a_size.x; ++j) { - int mat_index = A->calculate_index(i, j); - - ret_ptr[mat_index] = a_ptr[mat_index] + b_ptr[j]; - } - } - - return ret; -} -Ref MLPPLinAlgOld::mat_vec_multv(const Ref &A, const Ref &b) { - ERR_FAIL_COND_V(!A.is_valid() || !b.is_valid(), Ref()); - - Size2i a_size = A->size(); - int b_size = b->size(); - - ERR_FAIL_COND_V(a_size.x < b->size(), Ref()); - - Ref c; - c.instance(); - c->resize(a_size.y); - c->fill(0); - - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = b->ptr(); - real_t *c_ptr = c->ptrw(); - - for (int i = 0; i < a_size.y; ++i) { - for (int k = 0; k < b_size; ++k) { - int mat_index = A->calculate_index(i, k); - - c_ptr[i] += a_ptr[mat_index] * b_ptr[k]; - } - } - - return c; -} - std::vector>> MLPPLinAlgOld::addition(std::vector>> A, std::vector>> B) { for (uint32_t i = 0; i < A.size(); i++) { A[i] = addition(A[i], B[i]); @@ -2731,19 +1324,6 @@ std::vector>> MLPPLinAlgOld::scalarAdd(real_t sc return A; } -Vector> MLPPLinAlgOld::scalar_multiply_vm(real_t scalar, Vector> A) { - for (int i = 0; i < A.size(); i++) { - A.write[i] = scalar_multiplym(scalar, A[i]); - } - return A; -} -Vector> MLPPLinAlgOld::scalar_add_vm(real_t scalar, Vector> A) { - for (int i = 0; i < A.size(); i++) { - A.write[i] = scalar_addm(scalar, A[i]); - } - return A; -} - std::vector>> MLPPLinAlgOld::resize(std::vector>> A, std::vector>> B) { A.resize(B.size()); for (uint32_t i = 0; i < B.size(); i++) { @@ -2803,6 +1383,3 @@ std::vector>> MLPPLinAlgOld::vector_wise_tensor_ } return C; } - -void MLPPLinAlgOld::_bind_methods() { -} diff --git a/mlpp/lin_alg/lin_alg_old.h b/mlpp/lin_alg/lin_alg_old.h index d933a48..4ec91b8 100644 --- a/mlpp/lin_alg/lin_alg_old.h +++ b/mlpp/lin_alg/lin_alg_old.h @@ -12,17 +12,10 @@ #include "core/math/math_defs.h" -#include "core/object/reference.h" - -#include "../lin_alg/mlpp_matrix.h" -#include "../lin_alg/mlpp_vector.h" - #include #include -class MLPPLinAlgOld : public Reference { - GDCLASS(MLPPLinAlgOld, Reference); - +class MLPPLinAlgOld { public: // MATRIX FUNCTIONS @@ -31,32 +24,19 @@ public: bool linearIndependenceChecker(std::vector> A); std::vector> gaussianNoise(int n, int m); - Ref gaussian_noise(int n, int m); std::vector> addition(std::vector> A, std::vector> B); std::vector> subtraction(std::vector> A, std::vector> B); std::vector> matmult(std::vector> A, std::vector> B); - Ref additionm(const Ref &A, const Ref &B); - Ref subtractionm(const Ref &A, const Ref &B); - Ref matmultm(const Ref &A, const Ref &B); - std::vector> hadamard_product(std::vector> A, std::vector> B); std::vector> kronecker_product(std::vector> A, std::vector> B); std::vector> elementWiseDivision(std::vector> A, std::vector> B); - Ref hadamard_productm(const Ref &A, const Ref &B); - Ref kronecker_productm(const Ref &A, const Ref &B); - Ref element_wise_divisionm(const Ref &A, const Ref &B); - std::vector> transpose(std::vector> A); std::vector> scalarMultiply(real_t scalar, std::vector> A); std::vector> scalarAdd(real_t scalar, std::vector> A); - Ref transposem(const Ref &A); - Ref scalar_multiplym(real_t scalar, const Ref &A); - Ref scalar_addm(real_t scalar, const Ref &A); - std::vector> log(std::vector> A); std::vector> log10(std::vector> A); std::vector> exp(std::vector> A); @@ -65,22 +45,11 @@ public: std::vector> sqrt(std::vector> A); std::vector> cbrt(std::vector> A); - Ref logm(const Ref &A); - Ref log10m(const Ref &A); - Ref expm(const Ref &A); - Ref erfm(const Ref &A); - Ref exponentiatem(const Ref &A, real_t p); - Ref sqrtm(const Ref &A); - Ref cbrtm(const Ref &A); - std::vector> matrixPower(std::vector> A, int n); std::vector> abs(std::vector> A); - Ref absm(const Ref &A); - real_t det(std::vector> A, int d); - real_t detm(const Ref &A, int d); real_t trace(std::vector> A); @@ -89,25 +58,13 @@ public: std::vector> inverse(std::vector> A); std::vector> pinverse(std::vector> A); - Ref cofactorm(const Ref &A, int n, int i, int j); - Ref adjointm(const Ref &A); - Ref inversem(const Ref &A); - Ref pinversem(const Ref &A); - std::vector> zeromat(int n, int m); std::vector> onemat(int n, int m); std::vector> full(int n, int m, int k); - Ref zeromatm(int n, int m); - Ref onematm(int n, int m); - Ref fullm(int n, int m, int k); - std::vector> sin(std::vector> A); std::vector> cos(std::vector> A); - Ref sinm(const Ref &A); - Ref cosm(const Ref &A); - std::vector> rotate(std::vector> A, real_t theta, int axis = -1); std::vector> max(std::vector> A, std::vector> B); @@ -119,10 +76,8 @@ public: real_t norm_2(std::vector> A); std::vector> identity(real_t d); - Ref identitym(int d); std::vector> cov(std::vector> A); - Ref covm(const Ref &A); std::tuple>, std::vector>> eig(std::vector> A); @@ -133,13 +88,6 @@ public: EigenResultOld eigen_old(std::vector> A); - struct EigenResult { - Ref eigen_vectors; - Ref eigen_values; - }; - - EigenResult eigen(Ref A); - struct SVDResultOld { std::vector> U; std::vector> S; @@ -148,14 +96,6 @@ public: SVDResultOld SVD(std::vector> A); - struct SVDResult { - Ref U; - Ref S; - Ref Vt; - }; - - SVDResult svd(const Ref &A); - std::vector vectorProjection(std::vector a, std::vector b); std::vector> gramSchmidtProcess(std::vector> A); @@ -181,7 +121,6 @@ public: real_t sum_elements(std::vector> A); std::vector flatten(std::vector> A); - Ref flattenv(const Vector> &A); std::vector solve(std::vector> A, std::vector b); @@ -196,33 +135,19 @@ public: // VECTOR FUNCTIONS std::vector> outerProduct(std::vector a, std::vector b); // This multiplies a, bT - Ref outer_product(const Ref &a, const Ref &b); // This multiplies a, bT - std::vector hadamard_product(std::vector a, std::vector b); - Ref hadamard_productnv(const Ref &a, const Ref &b); - void hadamard_productv(const Ref &a, const Ref &b, Ref out); std::vector elementWiseDivision(std::vector a, std::vector b); - Ref element_wise_division(const Ref &a, const Ref &b); std::vector scalarMultiply(real_t scalar, std::vector a); - Ref scalar_multiplynv(real_t scalar, const Ref &a); - void scalar_multiplyv(real_t scalar, const Ref &a, Ref out); std::vector scalarAdd(real_t scalar, std::vector a); - Ref scalar_addnv(real_t scalar, const Ref &a); - void scalar_addv(real_t scalar, const Ref &a, Ref out); std::vector addition(std::vector a, std::vector b); - Ref additionnv(const Ref &a, const Ref &b); - void additionv(const Ref &a, const Ref &b, Ref out); std::vector subtraction(std::vector a, std::vector b); - Ref subtractionnv(const Ref &a, const Ref &b); - void subtractionv(const Ref &a, const Ref &b, Ref out); std::vector subtractMatrixRows(std::vector a, std::vector> B); - Ref subtract_matrix_rows(const Ref &a, const Ref &B); std::vector log(std::vector a); std::vector log10(std::vector a); @@ -232,16 +157,7 @@ public: std::vector sqrt(std::vector a); std::vector cbrt(std::vector a); - Ref logv(const Ref &a); - Ref log10v(const Ref &a); - Ref expv(const Ref &a); - Ref erfv(const Ref &a); - Ref exponentiatev(const Ref &a, real_t p); - Ref sqrtv(const Ref &a); - Ref cbrtv(const Ref &a); - real_t dot(std::vector a, std::vector b); - real_t dotv(const Ref &a, const Ref &b); std::vector cross(std::vector a, std::vector b); @@ -251,21 +167,11 @@ public: std::vector onevec(int n); std::vector full(int n, int k); - Ref absv(const Ref &a); - - Ref zerovecv(int n); - Ref onevecv(int n); - Ref fullv(int n, int k); - std::vector> diag(std::vector a); - Ref diagm(const Ref &a); std::vector sin(std::vector a); std::vector cos(std::vector a); - Ref sinv(const Ref &a); - Ref cosv(const Ref &a); - std::vector max(std::vector a, std::vector b); real_t max(std::vector a); @@ -275,16 +181,12 @@ public: std::vector round(std::vector a); real_t euclideanDistance(std::vector a, std::vector b); - real_t euclidean_distance(const Ref &a, const Ref &b); - real_t euclidean_distance_squared(const Ref &a, const Ref &b); real_t norm_2(std::vector a); real_t norm_sq(std::vector a); - real_t norm_sqv(const Ref &a); real_t sum_elements(std::vector a); - real_t sum_elementsv(const Ref &a); real_t cosineSimilarity(std::vector a, std::vector b); @@ -294,9 +196,6 @@ public: std::vector> mat_vec_add(std::vector> A, std::vector b); std::vector mat_vec_mult(std::vector> A, std::vector b); - Ref mat_vec_addv(const Ref &A, const Ref &b); - Ref mat_vec_multv(const Ref &A, const Ref &b); - // TENSOR FUNCTIONS std::vector>> addition(std::vector>> A, std::vector>> B); @@ -315,9 +214,6 @@ public: std::vector>> scalarMultiply(real_t scalar, std::vector>> A); std::vector>> scalarAdd(real_t scalar, std::vector>> A); - Vector> scalar_multiply_vm(real_t scalar, Vector> A); - Vector> scalar_add_vm(real_t scalar, Vector> A); - std::vector>> resize(std::vector>> A, std::vector>> B); std::vector>> hadamard_product(std::vector>> A, std::vector>> B); @@ -329,9 +225,6 @@ public: real_t norm_2(std::vector>> A); std::vector>> vector_wise_tensor_product(std::vector>> A, std::vector> B); - -protected: - static void _bind_methods(); }; #endif /* LinAlg_hpp */ \ No newline at end of file