Skip to content

Commit

Permalink
updating Matrix and Vector operations in samples/cpp/ dir
Browse files Browse the repository at this point in the history
  • Loading branch information
akielaries committed Jan 8, 2023
1 parent 917ad98 commit 73e2e99
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 14 deletions.
20 changes: 14 additions & 6 deletions include/linalg/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ class Matrix {
std::vector<Type> data;
std::tuple<size_t, size_t> dim;

int num = rows * cols;
int num_elements = rows * cols;

/**
* @brief Matrix Class constructor initializing empty vector
*
* @param[in] rows : size of rows
* @param[in] cols : size of columns
*/
Matrix(size_t rows, size_t cols) {
Matrix(size_t rows, size_t cols) :
cols(cols), rows(rows), data({}) {
// initialize an empty vector for storage
data.resize(cols * rows, Type());
dim = {rows, cols};
dim = std::make_tuple(rows, cols);
}
Matrix() : cols(0), rows(0), data({}) {dim = {rows, cols};};

Expand Down Expand Up @@ -204,7 +205,7 @@ class Matrix {
counter++;
}
}
return (counter == num);
return (counter == num_elements);
}

/*
Expand Down Expand Up @@ -266,7 +267,7 @@ class Matrix {

// Compute mean of matrix by elements
Matrix mean() {
auto m = Type(num);
auto m = Type(num_elements);
return sum().scalar_mult(1 / m);
}

Expand Down Expand Up @@ -342,6 +343,13 @@ class Matrix {
return res;
}

void print_shape() {
std::cout << "Matrix Size([" <<
rows << ", " << cols <<
"])" << std::endl;
}


void print_mtx() {
for (size_t r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
Expand Down Expand Up @@ -388,7 +396,7 @@ struct mtx {

std::random_device rd{};
std::mt19937 gen{rd()};
T n(MTX.num);
T n(MTX.num_elements);
T stdev{1 / sqrt(n)};
std::normal_distribution<T> d{0, stdev};

Expand Down
34 changes: 26 additions & 8 deletions samples/cpp/linalg_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,44 @@
#include <stdio.h>
#include <vector>
#include <cassert>
#include <openMTPK/linalg.hpp>
//#include <openMTPK/linalg.hpp>
#include "../../include/linalg/vectors.hpp"
#include "../../include/linalg/matrix.hpp"


int main() {
mtpk::Vectors f;
std::cout << "VECTOR/MATRIX OPERATIONS EXAMPLE" << std::endl;
// declaring an object for the Vectors class is permitted
mtpk::Vectors v;
std::cout << "MATRIX/VECTOR OPERATIONS EXAMPLE" << std::endl;

int x = f.add(1, 3);
int x = v.add(1, 3);

std::cout << "Sum = " << x << std::endl;

// declaring matrix with random floats
std::cout << "Creating 2x2 matrix of random floats" << "\n";
auto MAT1 = mtpk::mtx<float>::randn(2, 2);
MAT1.print_shape();
MAT1.print_mtx();

//auto MTX = mtx<float>::randn(2, 2);
//MTX.print_mtx();
// declare a matrix of zeros with 3 x 5 dimensions
std::cout << "Creating 3x5 matrix of 0's" << "\n";
auto MAT2 = mtpk::Matrix<float>(3, 5);
MAT2.print_shape();
MAT2.print_mtx();

//(MTX-MTX).print_mtx();
// another method to declare a matrix of zeros with 8 x 9 dimensions
std::cout << "Creating 8x9 matrix of 0's" << "\n";
auto MAT3 = mtpk::mtx<int>::zeros(8, 9);
MAT3.print_shape();
MAT3.print_mtx();

// declare a matrix
// auto MAT4 = mtpk::Matrix<float>();

// MAT4.print_shape();
// MAT4.print_mtx();

return 0;

}

9 changes: 9 additions & 0 deletions samples/cpp/test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "matrix.hpp"

int main() {
auto M = mtpk::mtx<float>::randn(2, 2); // init randn matrix

M.print_shape();

}

Loading

0 comments on commit 73e2e99

Please sign in to comment.