v0.14.0
single.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <ostream>
3 template <char i> class Index
4 {
5 public:
6  Index(){};
7 };
8 
9 class Tensor1;
10 
11 template <class A, char i> class Tensor1_Expr
12 {
13  A *iter;
14 
15 public:
16  Tensor1_Expr(A *a) : iter(a) {}
17  double &operator()(const int N) { return (*iter)(N); }
18  double operator()(const int N) const { return (*iter)(N); }
19 
20  const A operator=(const Tensor1_Expr<Tensor1, 'i'> &result)
21  {
22  cout << "equaling" << endl;
23  iter->data0 = result(0);
24  iter->data1 = result(1);
25  iter->data2 = result(2);
26  return *iter;
27  }
28 
29  // template<class B>
30  // const A operator=(const Tensor1_Expr<B,'i'> &result)
31  // {
32  // cout << "equaling" << endl;
33  // iter->data0=result(0);
34  // iter->data1=result(1);
35  // iter->data2=result(2);
36  // return iter;
37  // }
38 };
39 
40 class Tensor1
41 {
42 public:
43  double data0, data1, data2;
44 
45 public:
46  Tensor1(double d0, double d1, double d2) : data0(d0), data1(d1), data2(d2) {}
47  double &operator()(const int N)
48  {
49  return N == 0 ? data0 : (N == 1 ? data1 : data2);
50  }
51 
52  double operator()(const int N) const
53  {
54  return N == 0 ? data0 : (N == 1 ? data1 : data2);
55  }
56 
57  template <char i> Tensor1_Expr<Tensor1, i> operator()(const Index<i> index)
58  {
59  return Tensor1_Expr<Tensor1, i>(this);
60  }
61 
62  friend ostream &operator<<(ostream &s, const Tensor1 &a);
63 };
64 
65 ostream &operator<<(ostream &s, const Tensor1 &a)
66 {
67  return s << a.data0 << " " << a.data1 << " " << a.data2 << " ";
68 }
69 
70 int main()
71 {
72  Tensor1 y(0, 1, 2);
73  Tensor1 x(2, 3, 4);
74  const Index<'i'> i;
75 
76  y(i) = x(i);
77 
78  cout << y << endl;
79 }
Index::Index
Index()
Definition: single.cpp:6
Tensor1_Expr::Tensor1_Expr
Tensor1_Expr(A *a)
Definition: single.cpp:16
Tensor1::data1
double data1
Definition: single.cpp:43
Tensor1::operator()
double operator()(const int N) const
Definition: single.cpp:52
Tensor1_Expr::iter
A * iter
Definition: single.cpp:13
Tensor1::operator<<
friend ostream & operator<<(ostream &s, const Tensor1 &a)
Definition: single.cpp:65
A
constexpr AssemblyType A
Definition: operators_tests.cpp:30
Index
Definition: single.cpp:3
Tensor1::operator()
Tensor1_Expr< Tensor1, i > operator()(const Index< i > index)
Definition: single.cpp:57
Tensor1_Expr::operator=
const A operator=(const Tensor1_Expr< Tensor1, 'i'> &result)
Definition: single.cpp:20
main
int main()
Definition: single.cpp:70
Tensor1::Tensor1
Tensor1(double d0, double d1, double d2)
Definition: single.cpp:46
a
constexpr double a
Definition: approx_sphere.cpp:30
Tensor1::data0
double data0
Definition: single.cpp:43
Tensor1_Expr::operator()
double & operator()(const int N)
Definition: single.cpp:17
Tensor1::data2
double data2
Definition: single.cpp:43
i
FTensor::Index< 'i', SPACE_DIM > i
Definition: hcurl_divergence_operator_2d.cpp:27
N
const int N
Definition: speed_test.cpp:3
Tensor1_Expr
Definition: single.cpp:11
Tensor1
Definition: single.cpp:40
Tensor1::operator()
double & operator()(const int N)
Definition: single.cpp:47
operator<<
ostream & operator<<(ostream &s, const Tensor1 &a)
Definition: single.cpp:65
Tensor1_Expr::operator()
double operator()(const int N) const
Definition: single.cpp:18