v0.7.2
Tensor1_value.hpp
Go to the documentation of this file.
1 /* The general version, not for pointers. */
2 
3 #include <iostream>
4 #pragma once
5 
6 namespace FTensor
7 {
8  template <class T, int Tensor_Dim>
9  class Tensor1
10  {
11  T data[Tensor_Dim];
12  public:
13  /* Initializations for varying numbers of elements, with each one
14  defined for a particular Tensor_Dim. To initialize a different
15  dimension, just add the appropriate constructor and call to
16  the Tensor1_constructor constructor. */
17  Tensor1(T d0)
18  {
20  }
21  Tensor1(T d0, T d1)
22  {
24  }
25  Tensor1(T d0, T d1, T d2)
26  {
28  }
29  Tensor1(T d0, T d1, T d2, T d3)
30  {
32  }
33 
34  Tensor1() {}
35 
36  /* There are two operator(int)'s, one for non-consts that lets you
37  change the value, and one for consts that doesn't. */
38 
39  T & operator()(const int N)
40  {
41 #ifdef FTENSOR_DEBUG
42  if(N>=Tensor_Dim || N<0)
43  {
44  std::stringstream s;
45  s << "Bad index in Tensor1<T," << Tensor_Dim
46  << ">.operator(" << N << ")" << std::endl;
47  throw std::runtime_error(s.str());
48  }
49 #endif
50  return data[N];
51  }
52  T operator()(const int N) const
53  {
54 #ifdef FTENSOR_DEBUG
55  if(N>=Tensor_Dim || N<0)
56  {
57  std::stringstream s;
58  s << "Bad index in Tensor1<T," << Tensor_Dim
59  << ">.operator(" << N << ") const" << std::endl;
60  throw std::runtime_error(s.str());
61  }
62 #endif
63  return data[N];
64  }
65 
66  /* These operator()'s are the first part in constructing template
67  expressions. They can be used to slice off lower dimensional
68  parts. They are not entirely safe, since you can accidently use a
69  higher dimension than what is really allowed (like Dim=5). */
70 
71  template<char i, int Dim>
72  typename std::enable_if<(Tensor_Dim >= Dim),
73  Tensor1_Expr<Tensor1<T,Tensor_Dim>,T,Dim,i> >::type
74  operator()(const Index<i,Dim> &)
75  {
76  return Tensor1_Expr<Tensor1<T,Tensor_Dim>,T,Dim,i>(*this);
77  }
78 
79  template<char i, int Dim>
80  typename std::enable_if<(Tensor_Dim >= Dim),
81  Tensor1_Expr<const Tensor1<T,Tensor_Dim>,T,Dim,i> >::type
82  operator()(const Index<i,Dim> &) const
83  {
84  return Tensor1_Expr<const Tensor1<T,Tensor_Dim>,T,Dim,i>(*this);
85  }
86 
87  /* Convenience functions */
88 
90  {
91  const Index<'a',Tensor_Dim> a;
92  (*this)(a) /= l2();
93  return *this;
94  }
95 
96  T l2() const
97  {
98  return sqrt(l2_squared(Number<Tensor_Dim>()));
99  }
100 
101  template <int Current_Dim>
103  {
104  return data[Current_Dim-1]*data[Current_Dim-1]
106  }
107  T l2_squared(const Number<1> &) const
108  {
109  return data[0]*data[0];
110  }
111  };
112 }
113 /// JSON compatible output
114 
115 template <class T, int Tensor_Dim>
116 std::ostream & operator<<(std::ostream &os,
118 {
119  os << '[';
120  for(int i=0; i+1<Tensor_Dim; ++i)
121  { os << t(i) << ','; }
122  if(Tensor_Dim>0)
123  { os << t(Tensor_Dim-1); }
124  os << ']';
125  return os;
126 }
127 
128 template <class T, int Tensor_Dim>
129 std::istream & operator>>(std::istream &is,
131 {
132  char c;
133  is >> c;
134  for(int i=0; i+1<Tensor_Dim; ++i)
135  { is >> t(i) >> c; }
136  if(Tensor_Dim>0)
137  { is >> t(Tensor_Dim-1); }
138  is >> c;
139  return is;
140 }
std::ostream & operator<<(std::ostream &os, const FTensor::Tensor1< T, Tensor_Dim > &t)
JSON compatible output.
std::istream & operator>>(std::istream &is, FTensor::Tensor1< T, Tensor_Dim > &t)
Tensor1(T d0, T d1, T d2)
JSON compatible output.
T data[Tensor_Dim]
T l2_squared(const Number< Current_Dim > &) const
T l2_squared(const Number< 1 > &) const
Tensor1(T d0, T d1, T d2, T d3)
T & operator()(const int N)
T operator()(const int N) const
Tensor1(T d0, T d1)
Tensor1< T, Tensor_Dim > normalize()
const int N
Definition: speed_test.cpp:3