-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathexpression.h
More file actions
178 lines (152 loc) · 5.1 KB
/
Copy pathexpression.h
File metadata and controls
178 lines (152 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* This file is a part of TiledArray.
* Copyright (C) 2020 Virginia Tech
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef TA_PYTHON_EXPRESSION_H
#define TA_PYTHON_EXPRESSION_H
#include "python.h"
#include <tiledarray.h>
#include <vector>
#include <string>
namespace TiledArray {
namespace python {
namespace expression {
template<class Array>
struct Expression {
struct Term {
std::shared_ptr<Array> array;
std::string index;
double factor = 1;
auto evaluate() const {
const auto &array = *this->array;
return factor*(array(index));
}
};
explicit Expression(std::vector<Term> terms)
: terms(terms)
{
}
Expression add(const Expression &e) const {
auto r = this->terms;
for (const auto &t : e.terms) {
r.push_back({t.array, t.index, t.factor});
}
return Expression{r};
}
Expression sub(const Expression &e) const {
auto r = this->terms;
for (const auto &t : e.terms) {
r.push_back({t.array, t.index, -t.factor});
}
return Expression{r};
}
Expression mul(double f) const {
auto r = this->terms;
for (auto &t : r) {
t.factor *= f;
}
return Expression{r};
}
Expression div(double f) const {
auto r = this->terms;
for (auto &t : r) {
t.factor /= f;
}
return Expression{r};
}
template<size_t ... Idx>
auto operator[](std::integer_sequence<size_t,Idx...>) const {
return (terms.at(Idx).evaluate() + ...);
}
public:
const std::vector<Term> terms;
};
template<size_t N, class Array, size_t ... Idx>
auto index(const Expression<Array> &e, std::integer_sequence<size_t,Idx...> idx) {
using Index = std::variant< std::make_integer_sequence<size_t,1+Idx>... >;
if (N == e.terms.size()) {
return Index(std::make_integer_sequence<size_t,N>());
}
if constexpr (N < TA_PYTHON_MAX_EXPRESSION) {
return index<N+1>(e, idx);
}
throw std::domain_error(
"Expression exceeds TA_PYTHON_MAX_EXPRESSION=" + std::to_string(TA_PYTHON_MAX_EXPRESSION)
);
}
template<class Array>
auto index(const Expression<Array> &e) {
return index<1>(e, std::make_integer_sequence<size_t,TA_PYTHON_MAX_EXPRESSION>());
}
template<class F, class Array>
auto evaluate(F &&f, const Expression<Array> &a) {
auto visitor = [&](auto &&A) {
return f(a[A]);
};
return std::visit(visitor, index(a));
}
template<class F, class Array>
auto evaluate(F &&f, const Expression<Array> &a, const Expression<Array> &b) {
auto visitor = [&](auto &&A, auto &&B) {
return f(a[A], b[B]);
};
return std::visit(visitor, index(a), index(b));
}
#define TA_PYTHON_EXPRESSION_REDUCE(EXPRESSION, OP) \
[](const EXPRESSION &e) { \
auto op = [](auto &&e) { return e.OP(); }; \
return evaluate(op, e).get(); \
}
#define TA_PYTHON_EXPRESSION_REDUCE2(EXPRESSION, OP) \
[](const EXPRESSION &a, const EXPRESSION &b) { \
auto op = [](auto &&a, auto &&b) { return a.OP(b); }; \
return evaluate(op, a, b).get(); \
}
template<class Array>
inline Expression<Array> getitem(std::shared_ptr<Array> array, std::string idx) {
return Expression<Array>({{array, idx}});
}
template<class Array>
inline void setitem(Array &array, std::string idx, const Expression<Array> &e) {
auto op = [&](auto &&e) {
array(idx) = e;
};
evaluate(op, e);
}
template<class Array>
void make_array_expression_class(py::module m, const char *name) {
using Expression = Expression<Array>;
py::class_<Expression>(m, name)
.def("__add__", &Expression::add)
.def("__sub__", &Expression::sub)
.def("__mul__", &Expression::mul)
.def("__rmul__", &Expression::mul)
.def("__truediv__", &Expression::div)
.def("min", TA_PYTHON_EXPRESSION_REDUCE(Expression ,min))
.def("max", TA_PYTHON_EXPRESSION_REDUCE(Expression, max))
.def("norm", TA_PYTHON_EXPRESSION_REDUCE(Expression, norm))
.def("dot", TA_PYTHON_EXPRESSION_REDUCE2(Expression, dot))
;
}
inline void __init__(py::module m) {
make_array_expression_class< TArray<double> >(m, "Expression");
make_array_expression_class< TSpArray<double> >(m, "SparseExpression");
}
}
}
}
#endif // TA_PYTHON_EXPRESSION_H