diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index a1ffb1c..0000000 --- a/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -*.psd filter=lfs diff=lfs merge=lfs -text -*.zip filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore deleted file mode 100644 index e257658..0000000 --- a/.gitignore +++ /dev/null @@ -1,34 +0,0 @@ -# ---> C++ -# Prerequisites -*.d - -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod -*.smod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app - diff --git a/bin/abc_lab b/bin/abc_lab new file mode 100755 index 0000000..b3f8948 Binary files /dev/null and b/bin/abc_lab differ diff --git a/include/utils/matrix.h b/include/utils/matrix.h index 6dd4be5..c6c73aa 100644 --- a/include/utils/matrix.h +++ b/include/utils/matrix.h @@ -14,11 +14,23 @@ namespace utils{ //####################################### template struct Matrix{ - utils::vector m; + 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){ + // Constructor to initialize matrix with rows × cols and a fill value + Matrix(uint64_t rows, uint64_t cols, typename T::value_type value = {}) { + fill(rows, cols, value); + } + + Matrix() = default; // Default constructor + + T& operator[](uint64_t idx) { return m[idx]; } // Makes it able to do matr[1][1] + const T& operator[](uint64_t idx) const { return m[idx]; } // Makes it able to do matr[1][1] + + + void fill(uint64_t rows, uint64_t cols, const vector_type num=0){ m.clear(); for (uint64_t i = 0; i < rows; i++){ T temp_vec; @@ -30,22 +42,7 @@ namespace utils{ } } - 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){ + void fill_RNG(const uint64_t rows, const uint64_t cols, const vector_type min = 0, const vector_type max = 1){ m.clear(); std::mt19937_64 rng{}; @@ -60,6 +57,7 @@ namespace utils{ } } + inline friend std::ostream& operator << (std::ostream& out, const Matrix& mat){ out << "["; for (uint64_t i = 0; i < mat.m.size(); i++){ @@ -79,9 +77,12 @@ namespace utils{ out << "]"; return out; } + void print() const{ + std::cout << *this << std::endl; + } - void transpose(){ - std::vector temp_m = m; + void inplace_transpose(){ + utils::Vector temp_m = m; m.clear(); uint64_t rows = temp_m.size(); @@ -95,47 +96,158 @@ namespace utils{ m.push_back(temp_vec); } } - }; + Matrix transpose()const{ + Matrix copy = *this; + copy.inplace_transpose(); + return copy; + } + + void inplace_inverse(std::string method = "Gauss-Jordan"){ + //Matrix temp_m = *this; // Copies the m into temp_m correctly (Before: utils::Vector temp_m = m;) + if (method == "Gauss-Jordan"){ + Matrix temp_m(m.v.size(),m[0].v.size(),0); + + //std::cout << temp_m.m.v[0].size() << std::endl; + //std::cout << m.v.size() << std::endl; + + uint64_t icol,irow,N=m.v.size(),M=temp_m.m.v[0].size(); + double big,dum,pivinv; + Vi indxc(N,0),indxr(N,0),ipiv(N,0); + + //for (uint64_t j = 0; j < N; ++j){ ipiv[j] = 0;} + for (uint64_t i = 0; i < N; i++){ + big=0.0; + for (uint64_t j = 0; j < N; j++){ + if (ipiv[j] != 1){ + for (uint64_t k = 0; k < N; k++){ + if (ipiv[k] == 0){ + if (abs(m[j].v[k]) >= big){ + big = abs(m[j].v[k]); + irow = j; + icol = k; + } + } + } + } + } + ipiv[icol]++; + if (irow != icol){ + for (uint64_t l = 0; l < N; l++){ // SWAP + double temp = m[irow].v[l]; + m[irow].v[l] = m[icol].v[l]; + m[icol].v[l] = temp; + } + for (uint64_t l = 0; l < M; l++){ // SWAP temp matrix + double temp = temp_m.m[irow].v[l]; + temp_m.m[irow].v[l] = temp_m.m[icol].v[l]; + temp_m.m[icol].v[l] = temp; + } + } + + indxr[i] = irow; + indxc[i] = icol; + if (m[icol].v[icol] == 0.0){ + throw std::runtime_error("utill:Matrix.Gauss-Jordan - Singular Matrix"); + } + pivinv= 1.0/m[icol].v[icol]; + m[icol].v[icol]=1.0; + for (uint64_t l = 0; l < N; l++){ + m[icol].v[l] *= pivinv; + } + for (uint64_t l = 0; l < M; l++){ + temp_m.m[icol].v[l] *= pivinv; + } + for (uint64_t ll = 0; ll < N; ll++){ + if (ll != icol){ + dum = m[ll].v[icol]; + m[ll].v[icol] = 0; + for (uint64_t l = 0; l < N; l++){ + m[ll].v[l] -= m[icol].v[l]*dum; + } + for (uint64_t l = 0; l < N; l++){ + temp_m.m[ll].v[l] -= temp_m.m[icol].v[l]*dum; + } + } + } + + } + //m = temp_m; + for (int64_t l = N-1; l >= 0; l--){ + if (indxr[l] != indxc[l]){ + for (uint64_t k = 0; k < N; k++){ + double temp = m[k].v[indxr[l]]; + m[k].v[indxr[l]] = m[k].v[indxc[l]]; + m[k].v[indxc[l]] = temp; + } + } + } + } + else{ + throw std::runtime_error("utill:Matrix." + method + " - Not implemented yet \r \nImplemented: 'Gauss-Jordan',"); + } + } + Matrix inverse(std::string method = "Gauss-Jordan")const{ + Matrix copy = *this; + copy.inplace_inverse(method); + return copy; + } + + utils::Vector vecmult(const utils::Vector& Vec)const{ + + if (m[0].size() != Vec.size()){ + throw std::runtime_error("utill:Matrix.vecmult - Dimentions does not fit"); + } + + // Create a temporary result vector + utils::Vector copy(Vec.size(), 0); + + + for (uint64_t i = 0; i < m.size(); ++i) { + for (uint64_t j = 0; j < m[0].size(); ++j) { + copy[i] += m[i][j] * Vec[j]; + } + } + return copy; + } + + + void inplace_matmult(const Matrix& Mat){ + + if (m.v[0].size() != Mat.m.v.size()){ + throw std::runtime_error("utill:Matrix.matmult - Dimentions does not fit"); + } + + // Dimensions of the result + uint64_t rows = m.v.size(); // rows in *this + uint64_t cols = Mat.m[0].v.size(); // columns in Mat + uint64_t inner = m.v[0].size(); // shared dimension + + // Create a temporary result matrix + Matrix temp_m(rows, cols, 0); + + // Perform matrix multiplication + for (uint64_t i = 0; i < rows; i++){ + for (uint64_t j = 0; j < cols; j++){ + for (uint64_t k = 0; k < inner; k++){ + temp_m.m[i].v[j] += m[i].v[k] * Mat.m[k].v[j]; + } + } + } + *this = temp_m; + } + Matrix matmult(const Matrix& Mat)const{ + Matrix copy = *this; + copy.inplace_matmult(Mat); + return copy; + } + + + }; 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); + } diff --git a/include/utils/utils.h b/include/utils/utils.h new file mode 100644 index 0000000..cfd11c5 --- /dev/null +++ b/include/utils/utils.h @@ -0,0 +1,5 @@ +// "./utils/utils.h" +#pragma once + +#include "./utils/vector.h" +#include "./utils/matrix.h" diff --git a/include/utils/vector.h b/include/utils/vector.h index 6107fc7..80ad505 100644 --- a/include/utils/vector.h +++ b/include/utils/vector.h @@ -12,8 +12,23 @@ namespace utils{ //####################################### template struct Vector{ + + using value_type = T; std::vector v; + Vector() = default; + + Vector(uint64_t size, T value = T()) { + v.resize(size, value); + } + + + void clear(){v.clear();} + void push_back(const T& val) { v.push_back(val); } + T& operator[](uint64_t idx) { return v[idx]; } + const T& operator[](uint64_t idx) const { return v[idx]; } + uint64_t size() const { return v.size(); } + void fill(const uint64_t size, const T num){ v.clear(); for (uint64_t i = 0; i < size; i++) @@ -93,6 +108,22 @@ namespace utils{ void print() const{ std::cout << *this << std::endl; } + + void linspace(const T start, const T stop, const T num){ + v.clear(); + + if (num > 1){ + + double delta = (stop - start)/(num - 1); + + for (uint64_t i = 0; i < num; i++){ + v.push_back(static_cast(start + delta * i)); + } + //v.push_back(static_cast(stop)); + } + + + } }; typedef Vector Vi; diff --git a/obj/main.o b/obj/main.o new file mode 100644 index 0000000..c3f0dab Binary files /dev/null and b/obj/main.o differ diff --git a/src/main.cpp b/src/main.cpp index aff6e42..dd0c4b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include "./utils/vector.h" +#include "./utils/utils.h" #include #include @@ -8,26 +8,33 @@ int main(int argc, char const *argv[]) { - utils::Vd vect; - vect.fill_RNG(3, 0, 42); - vect.print(); + utils::Vd vect1, vect2; + vect1.linspace(1, 3.14159, 11); + vect1.print(); + //std::cout << vect[1] << std::endl; - utils::Md matr; - /*matrix2.fill(2, 2, 0); - matrix2.m[0].v[0] = 3; - matrix2.m[0].v[1] = 3.5; + /* + utils::Md matr1, matr2, matr3; + matr1.fill_RNG(3,3); + matr2 = matr1.inverse("Gauss-Jordan"); + matr3 = matr1.matmult(matr2); + // + matr1.print(); + std::cout << "======" << std::endl; + matr2.print(); + std::cout << "======" << std::endl; + matr3.print(); + std::cout << "======" << std::endl; + matr1.matmult(matr2).print(); - matrix2.m[1].v[0] = 3.2; - matrix2.m[1].v[1] = 3.6; - - - std::cout << matrix2 << std::endl << std::endl; - numeric::Md det; - det = numeric::matrix_inverse(matrix2); - std::cout << det << std::endl << std::endl; - -*/ + std::cout << "---------" << std::endl; + utils::Md matr4(2,3,2); + vect2 = matr4.vecmult(vect1); + matr4.inplace_transpose(); + matr4.transpose(); + //result.print(); + */ return 0;