#ifndef _matrix_n_ #define _matrix_n_ #include "iostream" #include #include #include "./utils/vector.h" namespace utils{ //####################################### //# MATRIX TYPE # //####################################### template struct Matrix{ utils::vector m; using vector_type = typename decltype(std::declval().v)::value_type; void fill(uint64_t rows, uint64_t cols, const vector_type num){ m.clear(); for (uint64_t i = 0; i < rows; i++){ T temp_vec; for (uint64_t j = 0; j < cols; j++){ temp_vec.v.push_back(num); } m.push_back(temp_vec); } } void disturbance(const double min, const double max){ uint64_t rows = m.size(); uint64_t cols = m[0].v.size(); std::mt19937_64 rng{}; rng.seed( std::random_device{}()); for (uint64_t i = 0; i < rows; i++){ for (uint64_t j = 0; j < cols; j++){ m[i].v[j] *= std::uniform_real_distribution<>{min, max}(rng); } } } void fill_RNG(const uint64_t rows, const uint64_t cols, const double min, const double max){ m.clear(); std::mt19937_64 rng{}; rng.seed( std::random_device{}()); for (uint64_t i = 0; i < rows; i++){ T temp_vec; for (uint64_t j = 0; j < cols; j++){ temp_vec.v.push_back(std::uniform_real_distribution<>{min, max}(rng)); } m.push_back(temp_vec); } } inline friend std::ostream& operator << (std::ostream& out, const Matrix& mat){ out << "["; for (uint64_t i = 0; i < mat.m.size(); i++){ out << "["; for (uint64_t j = 0; j < mat.m[i].v.size(); j++){ if (j % mat.m[i].v.size() == mat.m[i].v.size() -1 && i == mat.m.size()-1){ out << mat.m[i].v[j] << "]"; } else if ((j % mat.m[i].v.size() == mat.m[i].v.size() -1)){ out << mat.m[i].v[j] << "]," << std::endl; } else{ out << mat.m[i].v[j] << ", "; } } } out << "]"; return out; } void transpose(){ std::vector temp_m = m; m.clear(); uint64_t rows = temp_m.size(); uint64_t cols = temp_m[0].v.size(); for (uint64_t i = 0; i < cols; i++){ T temp_vec; for (uint64_t j = 0; j < rows; j++){ temp_vec.v.push_back(temp_m[j].v[i]); } m.push_back(temp_vec); } } }; typedef Matrix Mi; typedef Matrix Mf; typedef Matrix Md; //####################################### //# UTILITY FUNCTIONS FOR MATRIX # //####################################### Md matrix_dot(const Md& M, const Md& N); Md matrix_add_vector(const Md& M, const Vd& vect, bool axis); Md matrix_add_matrix(const Md& M, const Md& N); Md matrix_sub_vector(const Md& M, const Vd& vect, bool axis); Md matrix_max_cap(const Md& M, const double value); Md matrix_min_cap(const Md& M, const double value); Md matrix_clip(const Md& M, const double min_value, const double max_value); Vd matrix_get_max(const Md& M, bool axis); Vd matrix_get_min(const Md& M, bool axis); Vd matrix_get_sum(const Md& M, bool axis); Md matrix_normalize(const Md& M, bool axis); Md matrix_exp(const Md& M); Vd matrix_argmax(const Md& M); Md matrix_transpose(const Md& M); double matrix_determinant(const Md& M); Md matrix_cofactor(const Md& M); Md matrix_inverse(const Md& M); Md matrix_scalar_element_wise_division(const Md& M, const double& C); Md matrix_element_wise_division(const Md& M, const Md& N); Md matrix_sign(const Md& M); double random(const double& min, const double& max); } #endif // _numerics_n_