1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-09 21:50:45 +01:00

Conversion of Grid tensors to std::vector made more elegant, also pair syntax changed to (x y) to avoid issues with JSON/XML

This commit is contained in:
Antonin Portelli 2018-03-07 15:12:18 +00:00
parent 485c5db0fe
commit 90dbe03e17
2 changed files with 90 additions and 57 deletions

View File

@ -34,74 +34,73 @@ Author: Guido Cossu <guido.cossu@ed.ac.uk>
#include <Grid/tensors/Tensors.h> #include <Grid/tensors/Tensors.h>
namespace Grid { namespace Grid {
// Vector IO utilities /////////////////////////////////////////////////////// // Grid scalar tensors to nested std::vectors //////////////////////////////////
// helper function to read space-separated values
template <typename T> template <typename T>
std::vector<T> strToVec(const std::string s) struct TensorToVec
{ {
std::istringstream sstr(s); typedef T type;
T buf; };
std::vector<T> v;
template <typename T>
while(!sstr.eof()) struct TensorToVec<iScalar<T>>
{
sstr >> buf;
v.push_back(buf);
}
return v;
}
// output to streams for vectors
template < class T >
inline std::ostream & operator<<(std::ostream &os, const std::vector<T> &v)
{ {
os << "["; typedef typename TensorToVec<T>::type type;
for (auto &x: v) };
{
os << x << " "; template <typename T, int N>
} struct TensorToVec<iVector<T, N>>
if (v.size() > 0)
{
os << "\b";
}
os << "]";
return os;
}
// convert Grid scalar tensors to std::vectors
template <typename T, typename V>
void tensorToVec(V &v, const T& t)
{ {
v = t; typedef typename std::vector<typename TensorToVec<T>::type> type;
};
template <typename T, int N>
struct TensorToVec<iMatrix<T, N>>
{
typedef typename std::vector<std::vector<typename TensorToVec<T>::type>> type;
};
template <typename T>
typename TensorToVec<T>::type tensorToVec(const T &t)
{
return t;
} }
template <typename T, typename V> template <typename T>
void tensorToVec(V &v, const iScalar<T>& t) typename TensorToVec<iScalar<T>>::type tensorToVec(const iScalar<T>& t)
{ {
tensorToVec(v, t._internal); return tensorToVec(t._internal);
} }
template <typename T, typename V, int N> template <typename T, int N>
void tensorToVec(std::vector<V> &v, const iVector<T, N>& t) typename TensorToVec<iVector<T, N>>::type tensorToVec(const iVector<T, N>& t)
{ {
typename TensorToVec<iVector<T, N>>::type v;
v.resize(N); v.resize(N);
for (unsigned int i = 0; i < N; i++) for (unsigned int i = 0; i < N; i++)
{ {
tensorToVec(v[i], t._internal[i]); v[i] = tensorToVec(t._internal[i]);
} }
return v;
} }
template <typename T, typename V, int N> template <typename T, int N>
void tensorToVec(std::vector<std::vector<V>> &v, const iMatrix<T, N>& t) typename TensorToVec<iMatrix<T, N>>::type tensorToVec(const iMatrix<T, N>& t)
{ {
v.resize(N, std::vector<V>(N)); typename TensorToVec<iMatrix<T, N>>::type v;
v.resize(N);
for (unsigned int i = 0; i < N; i++) for (unsigned int i = 0; i < N; i++)
for (unsigned int j = 0; j < N; j++)
{ {
tensorToVec(v[i][j], t._internal[i][j]); v[i].resize(N);
for (unsigned int j = 0; j < N; j++)
{
v[i][j] = tensorToVec(t._internal[i][j]);
}
} }
return v;
} }
// Vector element trait ////////////////////////////////////////////////////// // Vector element trait //////////////////////////////////////////////////////
@ -186,15 +185,15 @@ namespace Grid {
do do
{ {
is.get(c); is.get(c);
} while (c != '<' && !is.eof()); } while (c != '(' && !is.eof());
if (c == '<') if (c == '(')
{ {
int start = is.tellg(); int start = is.tellg();
do do
{ {
is.get(c); is.get(c);
} while (c != '>' && !is.eof()); } while (c != ')' && !is.eof());
if (c == '>') if (c == ')')
{ {
int end = is.tellg(); int end = is.tellg();
int psize = end - start - 1; int psize = end - start - 1;
@ -217,7 +216,43 @@ namespace Grid {
template <class T1, class T2> template <class T1, class T2>
inline std::ostream & operator<<(std::ostream &os, const std::pair<T1, T2> &p) inline std::ostream & operator<<(std::ostream &os, const std::pair<T1, T2> &p)
{ {
os << "<" << p.first << " " << p.second << ">"; os << "(" << p.first << " " << p.second << ")";
return os;
}
// Vector IO utilities ///////////////////////////////////////////////////////
// helper function to read space-separated values
template <typename T>
std::vector<T> strToVec(const std::string s)
{
std::istringstream sstr(s);
T buf;
std::vector<T> v;
while(!sstr.eof())
{
sstr >> buf;
v.push_back(buf);
}
return v;
}
// output to streams for vectors
template < class T >
inline std::ostream & operator<<(std::ostream &os, const std::vector<T> &v)
{
os << "[";
for (auto &x: v)
{
os << x << " ";
}
if (v.size() > 0)
{
os << "\b";
}
os << "]";
return os; return os;
} }

View File

@ -202,8 +202,6 @@ int main(int argc,char **argv)
GridSerialRNG rng; GridSerialRNG rng;
SpinColourMatrix scm; SpinColourMatrix scm;
SpinColourVector scv; SpinColourVector scv;
std::vector<std::vector<std::vector<std::vector<ComplexD>>>> scmv;
std::vector<std::vector<ComplexD>> scvv;
rng.SeedFixedIntegers(std::vector<int>({42,10,81,9})); rng.SeedFixedIntegers(std::vector<int>({42,10,81,9}));
random(rng, scm); random(rng, scm);
@ -211,8 +209,8 @@ int main(int argc,char **argv)
std::cout << "Test spin-color matrix: " << scm << std::endl; std::cout << "Test spin-color matrix: " << scm << std::endl;
std::cout << "Test spin-color vector: " << scv << std::endl; std::cout << "Test spin-color vector: " << scv << std::endl;
std::cout << "Converting to std::vector" << std::endl; std::cout << "Converting to std::vector" << std::endl;
tensorToVec(scmv, scm); auto scmv = tensorToVec(scm);
tensorToVec(scvv, scv); auto scvv = tensorToVec(scv);
std::cout << "Spin-color matrix: " << scmv << std::endl; std::cout << "Spin-color matrix: " << scmv << std::endl;
std::cout << "Spin-color vector: " << scvv << std::endl; std::cout << "Spin-color vector: " << scvv << std::endl;
} }