MyraMath
csvread.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_DENSE_CSVREAD_H
7 #define MYRAMATH_DENSE_CSVREAD_H
8 
15 #include <myramath/MYRAMATH_EXPORT.h>
17 
18 #include <myramath/expression/vector.h>
21 
22 #include <vector>
23 #include <fstream>
24 #include <sstream>
25 
26 namespace myra {
27 
28 // Forward declarations.
29 template<class Number> class Matrix;
30 template<class Number> class Vector;
31 
33 template<class Number> Matrix<Number> csvread_Matrix(const char* filename)
34  {
35  std::ifstream in(filename);
36  // Size and numeric values.
37  int I = 0;
38  std::vector<int> J;
39  std::vector<Number> v;
40  // For each row..
41  std::string rowstr;
42  while (std::getline(in,rowstr))
43  {
44  std::stringstream row(rowstr);
45  int j = 0;
46  // For each cell on this row..
47  std::string cellstr;
48  while(std::getline(row,cellstr,','))
49  {
50  std::stringstream cell(cellstr);
51  Number n;
52  cell >> n;
53  v.push_back(n);
54  ++j;
55  }
56  J.push_back(j);
57  ++I;
58  }
59  // Verify every row had the same number of columns.
60  for (int i = 1; i < I; ++i)
61  if (J[i] != J[0])
62  throw eprintf("csvread_Matrix(), size mismatch between rows J[%d] != J[0] [%d != %d]",i,J[i],J[0]);
63  // Cast into an expression, reshape, transpose, and populate output Matrix.
64  return Matrix<Number>::evaluate( transpose(reshape(expr(v),J[0],I) ) );
65  }
66 
68 template<class Number> Vector<Number> csvread_Vector(const char* filename)
69  {
70  std::ifstream in(filename);
71  std::vector<Number> v;
72  Number n;
73  while (in >> n)
74  v.push_back(n);
75  in.close();
76  return Vector<Number>::evaluate(expr(v));
77  }
78 
80 template<class T> std::vector<T> csvread_vector(const char* filename)
81  {
82  std::ifstream in(filename);
83  std::vector<T> v;
84  T t;
85  while (in >> t)
86  v.push_back(t);
87  in.close();
88  return v;
89  }
90 
91 } // namespace
92 
93 #endif
Vector< Number > csvread_Vector(const char *filename)
Loads a Vector from a .csv file.
Definition: csvread.h:68
Reshapes an Expression (for instance, reinterpret an arity-1 Expression of size 25 into an arity-2 Ex...
Returns a std::runtime_error() whose message has been populated using printf()-style formatting...
Tabulates an IxJ matrix. Allows random access, has column major layout to be compatible with BLAS/LAP...
Definition: bdsqr.h:20
Matrix< Number > csvread_Matrix(const char *filename)
Loads a Matrix from a .csv file.
Definition: csvread.h:33
Definition: syntax.dox:1
Returns the transpose of an arity-2 Expression.
Various utility functions/classes related to scalar Number types.
Tabulates a vector of length N, allows random access.
Definition: conjugate.h:21
static Matrix< Number > evaluate(const Expression< 2, Number > &e)
Generates a Matrix by evaluating an arity-2 Expression of Number.
Definition: Matrix.cpp:378
std::vector< T > csvread_vector(const char *filename)
Loads a std::vector from a .csv file.
Definition: csvread.h:80
static Vector< Number > evaluate(const Expression< 1, Number > &e)
Generates a Vector by evaluating an arity-1 Expression of Number.
Definition: Vector.cpp:313