ready for parralization
This commit is contained in:
+146
-217
@@ -3,249 +3,178 @@
|
||||
|
||||
#include "./utils/vector.h"
|
||||
|
||||
#include <iomanip>
|
||||
|
||||
namespace utils{
|
||||
//#######################################
|
||||
//# MATRIX TYPE #
|
||||
//# Backed by utils::Vector<T> #
|
||||
//#######################################
|
||||
template <typename T>
|
||||
struct Matrix{
|
||||
utils::Vector<T> m;
|
||||
|
||||
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]
|
||||
|
||||
using vector_type = typename decltype(std::declval<T>().v)::value_type;
|
||||
class Matrix{
|
||||
public:
|
||||
Matrix() : rows_(0), cols_(0), data_() {} // Default constructor
|
||||
|
||||
// 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
|
||||
Matrix(uint64_t rows, uint64_t cols, const T& value = T())
|
||||
: rows_(rows), cols_(cols), data_(rows * cols, value) {}
|
||||
|
||||
|
||||
//# MATRIX: basic properties #
|
||||
uint64_t rows() const noexcept {return rows_;}
|
||||
uint64_t cols() const noexcept {return cols_;}
|
||||
|
||||
//# MATRIX: element access (fast; unchecked) #
|
||||
T& operator()(uint64_t i, uint64_t j) { return data_[i * cols_ + j]; }
|
||||
const T& operator()(uint64_t i, uint64_t j) const { return data_[i * cols_ + j]; }
|
||||
|
||||
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;
|
||||
//# MATRIX: data access #
|
||||
T* data() noexcept { return data_.data(); }
|
||||
const T* data() const noexcept { return data_.data(); }
|
||||
|
||||
for (uint64_t j = 0; j < cols; j++){
|
||||
temp_vec.v.push_back(num);
|
||||
}
|
||||
m.push_back(temp_vec);
|
||||
}
|
||||
//# MATRIX: equal operator #
|
||||
bool operator==(const Matrix<T>& A) const {
|
||||
if (rows_ != A.rows_ || cols_ != A.cols_) return false;
|
||||
for (uint64_t i = 0; i < rows_; ++i)
|
||||
for (uint64_t j = 0; j < cols_; ++j)
|
||||
if (data_[i*cols_ + j] != A(i,j))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
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{};
|
||||
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);
|
||||
}
|
||||
bool operator!=(const Matrix<T>& A) const {
|
||||
return !(*this == A);
|
||||
}
|
||||
|
||||
|
||||
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 print() const{
|
||||
std::cout << *this << std::endl;
|
||||
}
|
||||
|
||||
void inplace_transpose(){
|
||||
utils::Vector<T> 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);
|
||||
}
|
||||
}
|
||||
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];
|
||||
bool nearly_equal(const Matrix<T>& A, T tol = static_cast<T>(1e-9)) const {
|
||||
if (rows_ != A.rows_ || cols_ != A.cols_) return false;
|
||||
for (uint64_t i = 0; i < rows_; ++i)
|
||||
for (uint64_t j = 0; j < cols_; ++j) {
|
||||
T a = (*this)(i,j);
|
||||
T b = A(i,j);
|
||||
if (std::is_floating_point<T>::value) {
|
||||
if (std::fabs(a - b) > tol) return false;
|
||||
} else {
|
||||
if (a != b) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//# MATRIX: row helpers (copy out) #
|
||||
// Read whole row as an owning Vector<T>
|
||||
// utils::Vf v = M.get_row(2);
|
||||
Vector<T> get_row(const uint64_t row) const {
|
||||
if (row >= rows_) {
|
||||
throw std::out_of_range("Matrix::get_row -> row index");
|
||||
}
|
||||
utils::Vector<T> result(cols_, T{});
|
||||
for (uint64_t i = 0; i < cols_; ++i){
|
||||
result[i] = data_[row * cols_ + i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//# MATRIX: row helpers (copy in) #
|
||||
// Assign a whole Vector<T> to a row
|
||||
// M.set_row(2) = v;
|
||||
void set_row(const uint64_t row, const Vector<T>& vector){
|
||||
if (row >= rows_) {
|
||||
throw std::out_of_range("Matrix::set_row -> row index");
|
||||
}
|
||||
if (vector.size() != cols_){
|
||||
throw std::runtime_error("Matrix::set_row -> size mismatch");
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < cols_; ++i){
|
||||
data_[row * cols_ + i] = vector[i];
|
||||
}
|
||||
}
|
||||
|
||||
void inplace_matmult(const Matrix<T>& Mat){
|
||||
//# MATRIX: col helpers (copy out) #
|
||||
// Read whole col as an owning Vector<T>
|
||||
// utils::Vf v = M.get_col(2);
|
||||
Vector<T> get_col(const uint64_t col) const {
|
||||
if (col >= cols_) {
|
||||
throw std::out_of_range("Matrix::get_col -> col index");
|
||||
}
|
||||
utils::Vector<T> result(rows_, T{});
|
||||
for (uint64_t i = 0; i < rows_; ++i){
|
||||
result[i] = data_[i * cols_ + col];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
if (m.v[0].size() != Mat.m.v.size()){
|
||||
throw std::runtime_error("utill:Matrix.matmult - Dimentions does not fit");
|
||||
}
|
||||
//# MATRIX: col helpers (copy in) #
|
||||
// Assign a whole Vector<T> to a col
|
||||
// M.set_col(2) = v;
|
||||
void set_col(const uint64_t col, const Vector<T>& vector){
|
||||
if (col >= cols_) {
|
||||
throw std::out_of_range("Matrix::set_col -> col index");
|
||||
}
|
||||
if (vector.size() != rows_){
|
||||
throw std::runtime_error("Matrix::set_col -> size mismatch");
|
||||
}
|
||||
for (uint64_t i = 0; i < rows_; ++i){
|
||||
data_[i * cols_ + col] = vector[i];
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
void swap_rows(uint64_t a, uint64_t b){
|
||||
if (a >= rows_ || b >= rows_) {
|
||||
throw std::out_of_range("Matrix::swap_rows -> row index");
|
||||
}
|
||||
if (a == b){
|
||||
return;
|
||||
}
|
||||
for (uint64_t i = 0; i < cols_; ++i){
|
||||
T tmp = data_[a * cols_ + i];
|
||||
data_[a * cols_ + i] = data_[b * cols_ + i];
|
||||
data_[b * cols_ + i] = tmp;
|
||||
}
|
||||
}
|
||||
void swap_cols(uint64_t a, uint64_t b){
|
||||
if (a >= cols_ || b >= cols_) {
|
||||
throw std::out_of_range("Matrix::swap_cols -> col index");
|
||||
}
|
||||
if (a == b){
|
||||
return;
|
||||
}
|
||||
for (uint64_t i = 0; i < rows_; ++i){
|
||||
T tmp = data_[i * cols_ + a];
|
||||
data_[i * cols_ + a] = data_[i * cols_ + b];
|
||||
data_[i * cols_ + b] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
// Create a temporary result matrix
|
||||
Matrix<T> temp_m(rows, cols, 0);
|
||||
inline friend std::ostream& operator<<(std::ostream& out, const Matrix& M) {
|
||||
out << "[";
|
||||
for (uint64_t i = 0; i < M.rows_; ++i) {
|
||||
out << "[";
|
||||
for (uint64_t j = 0; j < M.cols_; ++j) {
|
||||
out << std::setw(4) << std::setprecision(3) << std::fixed << M(i, j);
|
||||
if (j + 1 < M.cols_) out << ", ";
|
||||
}
|
||||
out << "]";
|
||||
if (i + 1 < M.rows_) out << ",\n ";
|
||||
}
|
||||
out << "]";
|
||||
return out;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
void print() const {
|
||||
std::cout << *this << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t rows_, cols_;
|
||||
std::vector<T> data_;
|
||||
|
||||
};
|
||||
typedef Matrix<Vi> Mi;
|
||||
typedef Matrix<Vf> Mf;
|
||||
typedef Matrix<Vd> Md;
|
||||
};
|
||||
typedef Matrix<int> Mi;
|
||||
typedef Matrix<float> Mf;
|
||||
typedef Matrix<double> Md;
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // _numerics_n_
|
||||
#endif // _matrix_n_
|
||||
Reference in New Issue
Block a user