openGPMP
Open Source Mathematics Package
mtx2.cpp
Go to the documentation of this file.
1 
23 #include <cassert>
24 #include <fstream>
25 #include <iostream>
26 #include <sstream>
27 #include <stdio.h>
28 #include <vector>
29 
30 #include <openGPMP/linalg.hpp>
31 
32 using namespace gpmp;
33 
34 void print_csv() {
35  std::ifstream csvFile("data/school_scores.csv");
36  std::string line;
37  std::vector<std::vector<double>> data;
38  while (std::getline(csvFile, line)) {
39  std::vector<double> row;
40  std::stringstream lineStream(line);
41  std::string cell;
42  while (std::getline(lineStream, cell, ',')) {
43  row.push_back(std::stod(cell));
44  std::cout << line;
45  }
46  data.push_back(row);
47  }
48  csvFile.close();
49  size_t rows = data.size();
50  size_t cols = data[0].size();
51  gpmp::Matrix<double> mat(rows, cols);
52 
53  mat.print_mtx();
54 }
55 
56 int main() {
57  // declaring an object for the Vectors class is permitted
58  gpmp::Vectors v;
59  std::cout << "MATRIX/VECTOR OPERATIONS EXAMPLE\n" << std::endl;
60 
61  int x = v.add(1, 3);
62 
63  std::cout << "Sum = " << x << "\n\n";
64 
65  gpmp::Matrix<int> mat(3, 4);
66  mat.print_mtx();
67 
68  std::tuple<Matrix<double>, Matrix<double>> matrices =
69  std::make_tuple(Matrix<double>(5, 3), Matrix<double>(6, 4));
70 
71  std::get<0>(matrices).print_mtx();
72  std::get<1>(matrices).print_mtx();
73 
74  std::cout << "PRINT CSV AS MATRIX \n";
75  // print_csv();
76 
77  // declaring matrix with random negative decimals
78  std::cout << "Creating 2x2 matrix of random negative floats\n";
79  auto matrix_neg = gpmp::mtx<double>::randn(2, 2);
80  matrix_neg.print_shape();
81  matrix_neg.print_mtx();
82 
83  // declaring matrix with random positive decimals
84  std::cout << "Creating 2x2 matrix of random positive floats\n";
85  auto matrix_pos = gpmp::mtx<double>::rand(2, 2);
86  matrix_pos.print_shape();
87  matrix_pos.print_mtx();
88 
89  std::cout << "Transpose the MATRIX \n";
90  (matrix_pos.transpose()).print_mtx();
91  // multiply each element of matrix_pos by a number
92  std::cout << "Multiply each element of the matrix by a number"
93  << "\n";
94  std::cout << "By 2\n";
95  (matrix_pos.scalar_mult(2.f)).print_mtx();
96  std::cout << "By 3\n";
97  (matrix_pos.scalar_mult(3.f)).print_mtx();
98 
99  // multiply each element of matrix_pos by itself
100  std::cout << "Multiply each element of the matrix by itself"
101  << "\n";
102  (matrix_pos.hadamard(matrix_pos)).print_mtx();
103 
104  // declare a matrix of zeros with 3 x 5 dimensions
105  std::cout << "Creating 3x5 matrix of 0's"
106  << "\n";
107  auto matrix_zero = gpmp::Matrix<double>(3, 5);
108  matrix_zero.print_shape();
109  matrix_zero.print_mtx();
110 
111  // another method to declare a matrix of zeros with 8 x 9
112  // dimensions
113  std::cout << "Creating 8x9 matrix of 0's"
114  << "\n";
115  auto matrix_zero_2 = gpmp::mtx<int>::zeros(8, 9);
116  matrix_zero_2.print_shape();
117  matrix_zero_2.print_mtx();
118 
119  // declare matrix of ones with 8 x 9 dimensions
120  std::cout << "Creating 8x9 matrix of 1's"
121  << "\n";
122  auto matrix_one = gpmp::mtx<int>::ones(8, 9);
123  matrix_one.print_shape();
124  matrix_one.print_mtx();
125 
126  return 0;
127 }
User API for OpenGPMP LINEAR ALGEBRA MODULE.
void print_csv()
Definition: mtx2.cpp:34
int main()
Definition: mtx2.cpp:56
The source C++ openGPMP namespace.