This commit is contained in:
2025-08-29 18:02:31 +02:00
parent f3d4ef1741
commit 795617f3fd
8 changed files with 230 additions and 111 deletions
-2
View File
@@ -1,2 +0,0 @@
*.psd filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
-34
View File
@@ -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
Executable
BIN
View File
Binary file not shown.
+169 -57
View File
@@ -14,11 +14,23 @@ namespace utils{
//####################################### //#######################################
template <typename T> template <typename T>
struct Matrix{ struct Matrix{
utils::vector<T> m; utils::Vector<T> m;
using vector_type = typename decltype(std::declval<T>().v)::value_type; using vector_type = typename decltype(std::declval<T>().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(); m.clear();
for (uint64_t i = 0; i < rows; i++){ for (uint64_t i = 0; i < rows; i++){
T temp_vec; T temp_vec;
@@ -30,22 +42,7 @@ namespace utils{
} }
} }
void disturbance(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){
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(); m.clear();
std::mt19937_64 rng{}; std::mt19937_64 rng{};
@@ -60,6 +57,7 @@ namespace utils{
} }
} }
inline friend std::ostream& operator << (std::ostream& out, const Matrix& mat){ inline friend std::ostream& operator << (std::ostream& out, const Matrix& mat){
out << "["; out << "[";
for (uint64_t i = 0; i < mat.m.size(); i++){ for (uint64_t i = 0; i < mat.m.size(); i++){
@@ -79,9 +77,12 @@ namespace utils{
out << "]"; out << "]";
return out; return out;
} }
void print() const{
std::cout << *this << std::endl;
}
void transpose(){ void inplace_transpose(){
std::vector<T> temp_m = m; utils::Vector<T> temp_m = m;
m.clear(); m.clear();
uint64_t rows = temp_m.size(); uint64_t rows = temp_m.size();
@@ -95,47 +96,158 @@ namespace utils{
m.push_back(temp_vec); m.push_back(temp_vec);
} }
} }
}; Matrix<T> transpose()const{
Matrix<T> copy = *this;
copy.inplace_transpose();
return copy;
}
void inplace_inverse(std::string method = "Gauss-Jordan"){
//Matrix<T> temp_m = *this; // Copies the m into temp_m correctly (Before: utils::Vector<T> temp_m = m;)
if (method == "Gauss-Jordan"){
Matrix<T> 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<T> inverse(std::string method = "Gauss-Jordan")const{
Matrix<T> copy = *this;
copy.inplace_inverse(method);
return copy;
}
utils::Vector<vector_type> vecmult(const utils::Vector<vector_type>& 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<vector_type> 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<T>& 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<T> 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<T> matmult(const Matrix<T>& Mat)const{
Matrix<T> copy = *this;
copy.inplace_matmult(Mat);
return copy;
}
};
typedef Matrix<Vi> Mi; typedef Matrix<Vi> Mi;
typedef Matrix<Vf> Mf; typedef Matrix<Vf> Mf;
typedef Matrix<Vd> Md; typedef Matrix<Vd> 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);
} }
+5
View File
@@ -0,0 +1,5 @@
// "./utils/utils.h"
#pragma once
#include "./utils/vector.h"
#include "./utils/matrix.h"
+31
View File
@@ -12,8 +12,23 @@ namespace utils{
//####################################### //#######################################
template<typename T> template<typename T>
struct Vector{ struct Vector{
using value_type = T;
std::vector<T> v; std::vector<T> 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){ void fill(const uint64_t size, const T num){
v.clear(); v.clear();
for (uint64_t i = 0; i < size; i++) for (uint64_t i = 0; i < size; i++)
@@ -93,6 +108,22 @@ namespace utils{
void print() const{ void print() const{
std::cout << *this << std::endl; 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<T>(start + delta * i));
}
//v.push_back(static_cast<T>(stop));
}
}
}; };
typedef Vector<int> Vi; typedef Vector<int> Vi;
BIN
View File
Binary file not shown.
+25 -18
View File
@@ -1,4 +1,4 @@
#include "./utils/vector.h" #include "./utils/utils.h"
#include <vector> #include <vector>
#include <iostream> #include <iostream>
@@ -8,26 +8,33 @@
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
{ {
utils::Vd vect; utils::Vd vect1, vect2;
vect.fill_RNG(3, 0, 42); vect1.linspace(1, 3.14159, 11);
vect.print(); vect1.print();
//std::cout << vect[1] << std::endl;
utils::Md matr; /*
/*matrix2.fill(2, 2, 0); utils::Md matr1, matr2, matr3;
matrix2.m[0].v[0] = 3; matr1.fill_RNG(3,3);
matrix2.m[0].v[1] = 3.5; 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; std::cout << "---------" << std::endl;
matrix2.m[1].v[1] = 3.6; utils::Md matr4(2,3,2);
vect2 = matr4.vecmult(vect1);
matr4.inplace_transpose();
std::cout << matrix2 << std::endl << std::endl; matr4.transpose();
numeric::Md det;
det = numeric::matrix_inverse(matrix2);
std::cout << det << std::endl << std::endl;
*/
//result.print();
*/
return 0; return 0;