The main goal of this library is to offer a light-weight tool which mimics NUMPY like operation on C++ vector. For simple addition and subtraction, we always need to write for-loops in C++. It would be nice to do these operation like NUMPY. This library also contains some useful math operations, e.g. matmul, cross_product, dot_product, transpose etc.
Copy header file in your source directory and include into your cpp file.
This library contains various operator overloading to avoid writing explicit for-loop,
Example-1: Vector addition
#include "vmath.h"
// Without vmath
for (int i=0; i<n;++i){
A[i] = B[i] + C;
}
// With vmath
A = B + C;
Example-2: Overloading console output "<<"
//Without vmath
for (int i=0;i<n;++i)
std::cout<< A[i]<< " ";
std::endl;
//With vmath
std::cout<< A << std::endl;
Following operators can be overloaded using vmath.h
- Addition: +, +=
- Subtraction: -, -=
- Multiplication: *, *=
- Division: /, /=
// Get a vector/matrix initialized with 1 and size of n;
std::vector<float> vec = vmath::ones<float>(n);
vmath::matrix<float> mat = vmath::ones<float>(n1,n2);
// Get vector/matrix with randomly initialized values (0.0 to 1.0)
std::vector<float> vec= vmath::random<float>(n);
vmath::matrix<float> mat= vmath::random<float>(n1,n2);
// Get a vector/matrix initialized with 0 and size of n;
std::vector<float> vec = vmath::zeros<float>(n);
vmath::matrix<float> mat = vmath::zeros<float>(n1,n2);
vector<float> u = {1,2,3};
vector<int> v = {3,2,1};
//cross_product
std::cout << vmath::cross_product(v,u) <<std::endl;
//dot_product
std::cout << vmath::dot_product(v,u) <<std::endl;
//sum
std::cout << vmath::sum(u) << std::endl;
//argmin
std::cout << vmath::argmin(v) <<std::endl;
//argmax
std::cout << vmath::argmax(v) <<std::endl;
// Defining matrix
vmath::matrix<float> mat0=vmath::random<float>(5000,300);
vmath::matrix<float> mat1=vmath::random<float>(300,600);
// Multiply
vmath::matrix<float> reslt= vmath::matmul(mat0,mat1);
// Transpose
vmath::matrix<float> reslt= vmath::transpose(mat0);