Commit 6bf99bd1 authored by Lucas Serrano's avatar Lucas Serrano
Browse files

Adding Vectors

parent d84a5e30
......@@ -21,6 +21,9 @@ constexpr int nearest_power_of_two(int min_value, int current_value) {
template <typename T, int NRows, int NCols, int MaxVecSize>
class BaseMatrix;
template <typename T, int NCols, int MaxVecSize>
class Vector;
/*
template <typename T, int MatrixSize, int MaxVecSize>
class IdentityMatrix;
......@@ -158,6 +161,19 @@ static inline void matrix_mul_m_mt(BaseMatrix<T, l, m, MVS> &a, BaseMatrix<T, n,
}
}
template <typename T, int l, int m, int MVS>
static inline void matrix_mul_m_v(BaseMatrix<T, l, m, MVS> &a, Vector<T, m, MVS> &b, Vector<T, l, MVS> &c) {
static constexpr int VecSize = BaseMatrix<T, l, m, MVS>::VecSize;
using pack_t = bs::pack<T, VecSize>;
pack_t vector(&b.array[0]);
for (int i=0; i < l; i++) {
pack_t row(&a.array[VecSize*i]);
c.array[i] = bs::dot(row, vector);
}
}
template <typename T, int NumberRows, int NumberCols, int MaximumVectorSize>
class Inverse {
using M = BaseMatrix<T, NumberRows, NumberCols, MaximumVectorSize>;
......@@ -305,6 +321,7 @@ class BaseMatrix {
template <typename U, int l, int m, int n, int MVS> friend void matrix_mul_m_m(BaseMatrix<U, l, m, MVS> &a, BaseMatrix<U, m, n, MVS> &b, BaseMatrix<U, l, n, MVS> &c);
template <typename U, int l, int m, int n, int MVS> friend void matrix_mul_mt_m(BaseMatrix<U, m, l, MVS> &a, BaseMatrix<U, m, n, MVS> &b, BaseMatrix<U, l, n, MVS> &c);
template <typename U, int l, int m, int n, int MVS> friend void matrix_mul_m_mt(BaseMatrix<U, l, m, MVS> &a, BaseMatrix<U, n, m, MVS> &b, BaseMatrix<U, l, n, MVS> &c);
template <typename U, int l, int m, int MVS> friend void matrix_mul_m_v(BaseMatrix<U, l, m, MVS> &a, Vector<U, m, MVS> &b, Vector<U, l, MVS> &c);
friend class Inverse<T, NRows, NCols, MaxVecSize>;
friend std::ostream & operator<<<T, NRows, NCols, MaxVecSize>(std::ostream &os, const matrix_t &mat);
......@@ -312,3 +329,13 @@ class BaseMatrix {
alignas(sizeof(T)*VecSize) T array[NRows*VecSize] = {0};
};
template <typename T, int NumberCols, int MaximumVectorSize>
class Vector: public BaseMatrix<T, 1, NumberCols, MaximumVectorSize> {
public:
using BaseMatrix<T, 1, NumberCols, MaximumVectorSize>::BaseMatrix;
using BaseMatrix<T, 1, NumberCols, MaximumVectorSize>::operator=;
private:
template <typename U, int l, int m, int MVS> friend void matrix_mul_m_v(BaseMatrix<U, l, m, MVS> &a, Vector<U, m, MVS> &b, Vector<U, l, MVS> &c);
};
// Unitary tests for BaseMatrix class
#include <array>
#include <gtest/gtest.h>
#include "../fast_matrix.hpp"
TEST(VectorInstanciation, NoArguments) {
const std::array<float, 5> null_vector = {0, 0, 0, 0, 0};
Vector<float, 5, 8> no_args_vector;
ASSERT_EQ(no_args_vector.dump_array(), null_vector);
}
TEST(VectorInstanciation, ArrayArguments) {
const std::array<float, 6> float_array = {1, 2, 3, 4, 5, 6};
Vector<float, 6, 8> float_vector(&float_array[0]);
ASSERT_EQ(float_vector.dump_array(), float_array);
const std::array<double, 4> double_array = {1, 2, 3, 4};
Vector<double, 4, 8> double_vector(&double_array[0]);
ASSERT_EQ(double_vector.dump_array(), double_array);
}
TEST(VectorAssignment, ArrayRValue) {
const std::array<float, 6> array = {1, 2, 3, 4, 5, 6};
Vector<float, 6, 8> float_vector;
float_vector = &array[0];
ASSERT_EQ(float_vector.dump_array(), array);
}
TEST(VectorAssignment, VectorRValue) {
const std::array<float, 6> array_right_vector = {1, 2, 3, 4, 5, 6};
Vector<float, 6, 8> left_vector;
Vector<float, 6, 8> right_vector(&array_right_vector[0]);
left_vector = right_vector;
ASSERT_EQ(left_vector.dump_array(), right_vector.dump_array());
}
TEST(MatrixVectorProduct, SimpleProduct) {
const std::array<float, 6> matrix_array = {
1, -1, 2,
0, -3, 1,
};
const std::array<float, 3> vector_array = {2, 1, 0};
const std::array<float, 2> result_array = {1, -3};
BaseMatrix<float, 2, 3, 8> matrix(&matrix_array[0]);
Vector<float, 3, 8> vector(&vector_array[0]);
Vector<float, 2, 8> result_vector;
matrix_mul_m_v(matrix, vector, result_vector);
ASSERT_EQ(result_vector.dump_array(), result_array);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment