Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions include/matrix_operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class MatrixOp {
public:
Matrix init(std::vector<std::vector<double>>);
Matrix init(std::vector<std::vector<std::string>>);
Matrix init(double);
Matrix init(std::string);
Matrix init(std::vector<double>);
Matrix init(std::vector<std::string>);
Matrix concatenate(Matrix, Matrix, std::string);
Matrix matmul(Matrix, Matrix);
Matrix zeros(int, int);
Expand Down Expand Up @@ -73,6 +77,44 @@ Matrix MatrixOp::init(std::vector<std::vector<std::string>> vec) {
return result;
}

/// Method to initialize values of a Matrix object using a double
Matrix MatrixOp::init(double d) {
Matrix result;
std::vector<std::vector<double>> vec_d(1);
vec_d[0].push_back(d);
result.double_mat = vec_d;
result.to_string();
return result;
}

/// Method to initialize values of a Matrix object using a string
Matrix MatrixOp::init(std::string s) {
Matrix result;
std::vector<std::vector<std::string>> vec_s(1);
vec_s[0].push_back(s);
result.str_mat = vec_s;
result.to_double();
return result;
}

/// Method to initialize values of a Matrix object using a double vector
Matrix MatrixOp::init(std::vector<double> inner_d) {
Matrix result;
std::vector<std::vector<double>> vec_d(1, inner_d);
result.double_mat = vec_d;
result.to_string();
return result;
}

/// Method to initialize values of a Matrix object using a string vector
Matrix MatrixOp::init(std::vector<std::string> inner_s) {
Matrix result;
std::vector<std::vector<std::string>> vec_s(1, inner_s);
result.str_mat = vec_s;
result.to_double();
return result;
}

/// Method to concatenate/join two Matrix objects
Matrix MatrixOp::concatenate(Matrix mat1, Matrix mat2, std::string dim) {
if (dim == "column") {
Expand Down
73 changes: 73 additions & 0 deletions tests/initialization_tests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,77 @@ TEST(MatrixInitTest, CreatesMatrixFromCSV) {
EXPECT_EQ(mat, test_with);
}

TEST(MatrixInitTest, CreatesMatrixObjectFromString1DVector) {
std::vector<std::string> vec;
Matrix mat;

EXPECT_EQ(typeid(mat).name(), typeid(matrix.init(vec)).name());
}

TEST(MatrixInitTest, CreatesMatrixObjectFromDouble1DVector) {
std::vector<double> vec;
Matrix mat;

EXPECT_EQ(typeid(mat).name(), typeid(matrix.init(vec)).name());
}

TEST(MatrixInitTest, CreatesMatrixObjectFromString) {
Matrix mat;

EXPECT_EQ(typeid(mat).name(), typeid(matrix.init("")).name());
}

TEST(MatrixInitTest, CreatesMatrixObjectFromDouble) {
Matrix mat;

EXPECT_EQ(typeid(mat).name(), typeid(matrix.init(0)).name());
}

TEST(MatrixInitTest, InitializesMatrixFromString1DVector) {
std::vector<std::string> vec;
vec.push_back("1");
vec.push_back("2");
vec.push_back("3");

Matrix mat = matrix.init(vec);

Matrix test_with = matrix.genfromtxt("test_dataset.csv", ',');
test_with = test_with.slice(0, 1, 0, test_with.col_length());

EXPECT_EQ(mat, test_with);
}

TEST(MatrixInitTest, InitializesMatrixFromDouble1DVector) {
std::vector<double> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);

Matrix mat = matrix.init(vec);

Matrix test_with = matrix.genfromtxt("test_dataset.csv", ',');
test_with = test_with.slice(0, 1, 0, test_with.col_length());
test_with.to_double();

EXPECT_EQ(mat, test_with);
}

TEST(MatrixInitTest, InitializesMatrixFromString) {
Matrix mat = matrix.init("1");

Matrix test_with = matrix.genfromtxt("test_dataset.csv", ',');
test_with = test_with.slice(0, 1, 0, 1);

EXPECT_EQ(mat, test_with);
}

TEST(MatrixInitTest, InitializesMatrixFromDouble) {
Matrix mat = matrix.init(1);

Matrix test_with = matrix.genfromtxt("test_dataset.csv", ',');
test_with = test_with.slice(0, 1, 0, 1);
test_with.to_double();

EXPECT_EQ(mat, test_with);
}
} // namespace