33std::array<std::size_t, static_cast<std::size_t>(
Expr::Op::End)> Expr::_stats = { };
35void Expr::Symbol::describe(std::stringstream& out)
const {
39std::size_t Expr::Symbol::size()
const {
43Expr::Constant::Constant():
46Expr::Constant::Constant(
double x):
49void Expr::Constant::describe(std::stringstream& out)
const {
50 out << std::to_string(value);
53std::size_t Expr::Constant::size()
const {
57Expr::Binary::Binary(Expr lhs, Op op, Expr rhs):
58 _lhs{std::make_shared<Expr>(lhs)},
60 _rhs{std::make_shared<Expr>(rhs)} { }
62void Expr::Binary::describe(std::stringstream& out)
const {
64 case Op::Add: out <<
"("; _lhs->describe(out); out <<
") + ("; _rhs->describe(out); out <<
")";
break;
65 case Op::Sub: out <<
"("; _lhs->describe(out); out <<
") - ("; _rhs->describe(out); out <<
")";
break;
66 case Op::Mul: out <<
"("; _lhs->describe(out); out <<
") * ("; _rhs->describe(out); out <<
")";
break;
67 case Op::Div: out <<
"("; _lhs->describe(out); out <<
") / ("; _rhs->describe(out); out <<
")";
break;
68 case Op::Pow: out <<
"Pow("; _lhs->describe(out); out <<
","; _rhs->describe(out); out <<
")";
break;
69 default:
throw std::invalid_argument(
"Unsupported binary operation");
73std::size_t Expr::Binary::size()
const {
74 return 1 + _lhs->size() + _rhs->size();
77Expr::Unary::Unary(Op op, Expr arg):
79 _arg{std::make_shared<Expr>(arg)} { }
81void Expr::Unary::describe(std::stringstream& out)
const {
83 case Op::Sqrt: out <<
"sqrt("; _arg->describe(out); out <<
")";
break;
84 case Op::Abs: out <<
"Abs("; _arg->describe(out); out <<
")";
break;
85 case Op::Exp: out <<
"exp("; _arg->describe(out); out <<
")";
break;
86 default:
throw std::invalid_argument(
"Unsupported unary operation");
90std::size_t Expr::Unary::size()
const {
91 return 1 + _arg->size();
95 _payload(Constant{}) { }
98 _payload(rhs._payload) { }
101 _payload(rhs._payload) { }
104 _payload(Constant{v}) { }
107 _payload(Symbol{name}) { }
110 _payload(Binary(lhs, op, rhs)) { }
113 _payload(Unary(op, rhs)) { }
116 _payload = rhs._payload;
122 _payload = Binary(*
this,
Op::Add, rhs);
128 _payload = Binary(*
this,
Op::Sub, rhs);
134 _payload = Binary(*
this,
Op::Mul, rhs);
140 _payload = Binary(*
this,
Op::Div, rhs);
145 std::visit([&out](
auto& x) {
151 std::stringstream out;
157 if (
auto symbol = std::get_if<Symbol>(&_payload)) {
158 return (*symbol).name == name;
165 std::visit([&
size](
auto& x) {
192 return Expr(-1.) * rhs;
196 throw std::domain_error(
"Invalid operator for Expr type: '%'");
200 throw std::domain_error(
"Invalid operator for Expr type: '=='");
204 throw std::domain_error(
"Invalid operator for Expr type: '!='");
208 throw std::domain_error(
"Invalid operator for Expr type: '>'");
212 throw std::domain_error(
"Invalid operator for Expr type: '<'");
216 throw std::domain_error(
"Invalid operator for Expr type: '>='");
220 throw std::domain_error(
"Invalid operator for Expr type: '<='");
246 throw std::domain_error(
"Invalid operator for Expr type: 'max'");
250 throw std::domain_error(
"Invalid operator for Expr type: 'min'");
Basic value-substitute enabling extraction of expression trees for code generation.
std::string describe() const
Returns the serialized expression tree.
Expr & operator=(const Expr &rhs)
Expr & operator*=(Expr rhs)
static void increment(Op op)
Increment FLOP counter.
std::size_t size() const
Returns expanded size of tree.
Expr & operator-=(Expr rhs)
Expr & operator/=(Expr rhs)
bool isSymbol(std::string name) const
Helper function to quickly check if Expr is a specific symbol.
Expr & operator+=(Expr rhs)
Expr pow(Expr base, Expr exp)
Top level namespace for all of OpenLB.
bool operator>=(const Expr &rhs, const Expr &lhs)
Expr operator/(Expr lhs, Expr rhs)
bool operator>(const Expr &rhs, const Expr &lhs)
bool operator<=(const Expr &rhs, const Expr &lhs)
Expr operator*(Expr lhs, Expr rhs)
Expr operator%(Expr lhs, int rhs)
Expr operator+(Expr lhs, Expr rhs)
bool operator!=(const Expr &rhs, const Expr &lhs)
bool operator==(const Expr &rhs, const Expr &lhs)
Expr operator-(Expr lhs, Expr rhs)
bool operator<(const Expr &rhs, const Expr &lhs)
LB initialisation routine – header file.