22 #ifndef OPERATOR_H_INCLUDED 23 #define OPERATOR_H_INCLUDED 48 template<
typename ...Args>
50 :T(std::forward<Args>(args)...), empty(
true){}
54 bool getCommutable()
const override;
56 bool commutesWith(
Expr_info other,
int sign = -1)
const override;
58 bool isAnOperator()
const override;
60 bool isEmpty()
const override;
62 void setEmpty(
bool t_empty)
override;
64 void setOperandPrivate(
const Expr& operand,
bool leaveEmpty)
override;
66 Expr applyOperator(
const Expr& expr,
bool leaveEmpty=
false)
const override;
68 std::optional<Expr> expand(
bool full =
false,
69 bool inplace =
false)
const override;
71 std::optional<Expr> expand_if(
72 std::function<
bool(
Expr const&)>
const&f,
74 bool inplace =
false)
const override;
79 #ifndef TEMPLATE_OPERATOR_DEFINED 80 #define TEMPLATE_OPERATOR_DEFINED 86 return T::getCommutable()
87 and (not empty and this->getOperand()->getCommutable());
93 return T::commutesWith(other, sign)
95 and (empty or
Commutation(this->getOperand().
get(), other) == CSL_0)
96 and (not empty or not this->operatorAppliesOn(other)));
120 this->setOperand(expr);
121 empty = empty and leaveEmpty;
124 bool pullLeft(csl::vector_expr& argument,
size_t pos,
size_t& begin);
125 bool pullRight(csl::vector_expr& argument,
size_t& pos,
size_t& end);
126 void getParts(
const csl::vector_expr& argument,
size_t begin,
size_t end,
136 csl::vector_expr foo(0);
139 size_t posDerivative;
140 size_t endDerivative;
141 switch(expr->getType()) {
143 foo = expr->getVectorArgument();
145 for (
auto iter=foo.begin(); iter!=foo.end(); ++iter) {
146 *iter = foo2->applyOperator(*iter,leaveEmpty);
153 if (this->getOperand() != CSL_1 and this->getOperand() !=
CSL_UNDEF) {
155 foo = this->getOperand()->getVectorArgument();
157 foo.push_back(this->getOperand());
159 foo.insert(foo.end(),expr->begin(),
162 endDerivative = foo.size();
163 for (
size_t i = 0; i != endDerivative; ++i) {
164 if (not this->operatorAppliesOn(foo[i].
get())) {
165 if (not pullLeft(foo,i,posDerivative))
166 pullRight(foo,i,endDerivative);
169 if (posDerivative != 0 or endDerivative != foo.size()) {
170 Expr left, mid, right;
171 getParts(foo, posDerivative, endDerivative, left, mid, right);
175 res->setOperandPrivate(mid, leaveEmpty);
176 if (*left != CSL_1 or *right != CSL_1)
182 res->setOperandPrivate(
prod_s(foo),leaveEmpty);
187 if (this->operatorAppliesOn(expr.get())) {
189 res->setOperandPrivate(res->getOperand()*expr,leaveEmpty);
193 if (empty and not leaveEmpty)
209 std::optional<Expr> op = this->getOperand()->expand(full, inplace);
212 Expr copyExpr = this->copy();
213 copyExpr->setOperand(CSL_1);
214 copyExpr->setEmpty(
true);
216 return copyExpr->applyOperator(op.value());
219 return T::expand(full, inplace);
224 std::function<
bool(
Expr const&)>
const& f,
229 std::optional<Expr> op = this->getOperand()->expand_if(f, full, inplace);
232 Expr copyExpr = this->copy();
233 copyExpr->setOperand(CSL_1);
234 copyExpr->setEmpty(
true);
236 return copyExpr->applyOperator(op.value());
239 return T::expand_if(f, full, inplace);
Expr prod_s(const Expr &leftOperand, const Expr &rightOperand, bool explicitProd=0)
Returns the product of the two operands, applying basic simplifications.
Definition: operations.cpp:2246
const csl::Expr CSL_UNDEF
Constant that is useful in return of non-defined calculations as for example Commutator(): [A...
Definition: literal.h:617
Namespace for csl library.
Definition: abreviation.h:34
Expr Copy(const Abstract *expr)
See Copy(const Expr& expr).
Definition: utils.cpp:98
Root class of the inheritance tree of abstracts.
Definition: abstract.h:76
Expr Refreshed(const Abstract *expr)
See Refreshed(const Expr& expr).
Definition: utils.cpp:129
Base classes for all exprs in the program.
Linear operator O(a*X+b*Y) = a*O(X) + b*O(Y)
Definition: operator.h:38
Expr Commutation(const Expr &A, const Expr &B, int sign=-1)
Returns the result of the (anit-)commutation of A and B. In most cases it returns CSL_0...
Definition: commutation.cpp:27
Expression type/.
Definition: abstract.h:1573