Vector and Matrix Implementation in C++
#include <iostream>
#include <stdlib.h>
#include <cmath>
using namespace std;
class Vector
{
public:
int rows{0};
float *vct;
std::string multi = "dot";
// Allow init from blank slate
explicit Vector() {}
explicit Vector(int length, std::string multiply = "dot")
{
this->rows = length;
float *t_vct = new float[length];
int i;
for (i = 0; i < length; i++)
{
t_vct[i] = 0;
}
this->vct = t_vct;
// Sets default multiplication method to cross or dot
this->multi = multiply;
}
// Allow init from an array
explicit Vector(float *arr, int length, std::string multiply = "dot")
{
this->rows = length;
float *t_vct = new float[length];
int i;
for (i = 0; i < length; i++)
{
t_vct[i] = arr[i];
}
this->vct = t_vct;
// Sets default multiplication method to cross or dot
this->multi = multiply;
}
float magnitude()
{
float mag = 0;
int i;
for (i = 0; i < this->rows; i++)
{
mag += pow(this->vct[i], 2);
}
return sqrt(mag);
}
// Allow adding of two vectors
Vector operator+(const Vector &vct)
{
if (this->rows != vct.rows)
{
throw std::invalid_argument("trying to add two vectors of different dimensions");
}
// Init blank vector with same length as other two
Vector temp_vct{this->rows};
int i;
for (i = 0; i < this->rows; i++)
{
temp_vct.vct[i] = this->vct[i] + vct.vct[i];
}
return temp_vct;
}
// Allow subtraction of two vectors
Vector operator-(const Vector &vct)
{
if (this->rows != vct.rows)
{
throw std::invalid_argument("trying to subtract two vectors of different dimensions");
}
// Init blank vector with same length as other two
Vector temp_vct{this->rows};
int i;
for (i = 0; i < this->rows; i++)
{
temp_vct.vct[i] = this->vct[i] - vct.vct[i];
}
return temp_vct;
}
// Allow multiplication of two vectors
float operator*(const Vector &vct)
{
if (this->rows != vct.rows)
{
throw std::invalid_argument("trying to multiply two vectors of different dimensions");
}
float rtval;
if (this->multi == "dot")
{
rtval = 0;
int i;
for (i = 0; i < this->rows; i++)
{
rtval += this->vct[i] * vct.vct[i];
}
}
else
{
throw std::invalid_argument("unknown multiplication type");
}
return rtval;
}
// Add std::cout support for vector type
friend auto operator<<(std::ostream &os, Vector const &v) -> std::ostream &
{
std::string rtstring = "[";
int i;
for (i = 0; i < v.rows; i++)
{
if (i != 0)
{
rtstring += ", ";
}
rtstring += std::to_string(v.vct[i]);
}
rtstring += "]";
return os << rtstring;
}
float operator[](int i) const
{
return this->vct[i];
}
float &operator[](int i)
{
return this->vct[i];
}
};
class Matrix
{
public:
Vector *mtx;
int rows{0};
int cols{0};
explicit Matrix(int rows, int cols)
{
this->rows = rows;
this->cols = cols;
Vector *t_mtx = new Vector[cols];
int i;
for (i = 0; i < cols; i++)
{
t_mtx[i] = Vector{rows};
}
this->mtx = t_mtx;
}
explicit Matrix(Vector *arr, int rows, int cols)
{
this->rows = rows;
this->cols = cols;
Vector *t_mtx = new Vector[cols];
int i;
for (i = 0; i < cols; i++)
{
t_mtx[i] = arr[i];
}
this->mtx = t_mtx;
}
Matrix operator+(const Matrix &mtx)
{
if (this->rows != mtx.rows || this->cols != mtx.cols)
{
throw std::invalid_argument("trying to add two matrices of different dimensions");
}
Matrix t_mtx{this->rows, this->cols};
int i, j;
for (i = 0; i < this->cols; i++)
{
for (j = 0; j < this->rows; j++)
{
t_mtx[i][j] = this->mtx[i][j] + mtx[i][j];
}
}
return t_mtx;
}
Vector operator[](int i) const
{
return this->mtx[i];
}
Vector &operator[](int i)
{
return this->mtx[i];
}
friend auto operator<<(std::ostream &os, Matrix const &m) -> std::ostream &
{
std::string rtstring = "[\n";
int i, j;
for (i = 0; i < m.cols; i++)
{
rtstring.append("[");
for (j = 0; j < m.rows; j++)
{
if (j != 0)
{
rtstring.append(", ");
}
rtstring.append(std::to_string(m.mtx[i][j]));
}
rtstring.append("]\n");
}
rtstring += "]";
return os << rtstring;
}
};
int main()
{
Vector vct{5};
float x[] = {1, 2, 3, 4, 5};
float y[] = {5, 32, 1, -4, 3.5};
Vector vct1{x, 5};
Vector vct2{y, 5};
// vct1 = vct1 + vct2;
float dotp = vct1 * vct2;
std::cout << "Hello World" << std::endl;
std::cout << vct1 << std::endl;
std::cout << dotp << std::endl
<< std::endl;
Vector VectorL[] = {vct1, vct2};
Vector VectorR[] = {vct2, vct1};
Matrix m{VectorL, 5, 2};
Matrix m2{VectorR, 5, 2};
std::cout << m.rows << std::endl;
std::cout << m.cols << std::endl;
std::cout << m + m2;
return 0;
}