Skip to content

Commit 22363b2

Browse files
authored
Merge pull request #589 from Krzmbrzl/expr-impl-cleanup
Clean up expression implementations
2 parents 3168a65 + b2a5204 commit 22363b2

20 files changed

Lines changed: 1520 additions & 1287 deletions

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,28 @@ set(SeQuant_symb_src
306306
SeQuant/core/context.hpp
307307
SeQuant/core/expressions/abstract_tensor.cpp
308308
SeQuant/core/expressions/abstract_tensor.hpp
309+
SeQuant/core/expressions/constant.cpp
310+
SeQuant/core/expressions/constant.hpp
309311
SeQuant/core/expressions/expr.cpp
310312
SeQuant/core/expressions/expr.hpp
311313
SeQuant/core/expressions/expr_algorithms.cpp
312314
SeQuant/core/expressions/expr_algorithms.hpp
313315
SeQuant/core/expressions/expr_operators.hpp
316+
SeQuant/core/expressions/expr_ptr.cpp
317+
SeQuant/core/expressions/expr_ptr.hpp
314318
SeQuant/core/expressions/expr_range.hpp
319+
SeQuant/core/expressions/power.cpp
320+
SeQuant/core/expressions/power.hpp
321+
SeQuant/core/expressions/product.cpp
322+
SeQuant/core/expressions/product.hpp
315323
SeQuant/core/expressions/result_expr.cpp
316324
SeQuant/core/expressions/result_expr.hpp
325+
SeQuant/core/expressions/sum.cpp
326+
SeQuant/core/expressions/sum.hpp
317327
SeQuant/core/expressions/tensor.cpp
318328
SeQuant/core/expressions/tensor.hpp
329+
SeQuant/core/expressions/variable.cpp
330+
SeQuant/core/expressions/variable.hpp
319331
SeQuant/core/hash.cpp
320332
SeQuant/core/hash.hpp
321333
SeQuant/core/hugenholtz.hpp
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <SeQuant/core/expressions/constant.hpp>
2+
#include <SeQuant/core/expressions/expr_ptr.hpp>
3+
#include <SeQuant/core/hash.hpp>
4+
#include <SeQuant/core/io/latex/latex.hpp>
5+
#include <SeQuant/core/utility/exception.hpp>
6+
#include <SeQuant/core/utility/macros.hpp>
7+
8+
namespace sequant {
9+
10+
std::wstring Constant::to_latex() const {
11+
return L"{" + io::latex::to_string(value()) + L"}";
12+
}
13+
14+
Expr::type_id_type Constant::type_id() const { return get_type_id<Constant>(); }
15+
16+
bool Constant::is_scalar() const { return true; }
17+
18+
ExprPtr Constant::clone() const { return ex<Constant>(this->value()); }
19+
20+
void Constant::adjoint() {
21+
value_ = conj(value_);
22+
reset_hash_value();
23+
}
24+
25+
Constant &Constant::operator*=(const Expr &that) {
26+
if (that.is<Constant>()) {
27+
value_ *= that.as<Constant>().value();
28+
} else {
29+
throw Exception("Constant::operator*=(that): not valid for that");
30+
}
31+
32+
reset_hash_value();
33+
34+
return *this;
35+
}
36+
37+
Constant &Constant::operator+=(const Expr &that) {
38+
if (that.is<Constant>()) {
39+
value_ += that.as<Constant>().value();
40+
} else {
41+
throw Exception("Constant::operator+=(that): not valid for that");
42+
}
43+
44+
reset_hash_value();
45+
46+
return *this;
47+
}
48+
49+
Constant &Constant::operator-=(const Expr &that) {
50+
if (that.is<Constant>()) {
51+
value_ -= that.as<Constant>().value();
52+
} else {
53+
throw Exception("Constant::operator-=(that): not valid for that");
54+
}
55+
56+
reset_hash_value();
57+
58+
return *this;
59+
}
60+
61+
bool Constant::is_zero(scalar_type v) { return v.is_zero(); }
62+
63+
bool Constant::is_zero() const { return is_zero(this->value()); }
64+
65+
Expr::hash_type Constant::memoizing_hash() const {
66+
if (!hash_value_) {
67+
hash_value_ = hash::value(value_);
68+
} else {
69+
SEQUANT_ASSERT(*hash_value_ == hash::value(value_));
70+
}
71+
return *hash_value_;
72+
}
73+
74+
bool Constant::static_equal(const Expr &that) const {
75+
return value() == static_cast<const Constant &>(that).value();
76+
}
77+
78+
} // namespace sequant

SeQuant/core/expressions/constant.hpp

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#include <SeQuant/core/complex.hpp>
55
#include <SeQuant/core/expressions/expr.hpp>
6-
#include <SeQuant/core/expressions/expr_ptr.hpp>
7-
#include <SeQuant/core/io/latex/latex.hpp>
86
#include <SeQuant/core/rational.hpp>
97
#include <SeQuant/core/utility/macros.hpp>
108

@@ -14,6 +12,8 @@
1412

1513
namespace sequant {
1614

15+
class ExprPtr;
16+
1717
// implementation details of Constant; prefer sequant::detail over an unnamed
1818
// namespace in a header (see CppCoreGuidelines SF.21)
1919
namespace detail {
@@ -67,68 +67,37 @@ class Constant : public Expr {
6767
throw Exception("Constant::value<T>: cannot convert value to type T");
6868
}
6969

70-
std::wstring to_latex() const override {
71-
return L"{" + io::latex::to_string(value()) + L"}";
72-
}
70+
std::wstring to_latex() const override;
7371

74-
type_id_type type_id() const override { return get_type_id<Constant>(); }
72+
type_id_type type_id() const override;
7573

76-
bool is_scalar() const override { return true; }
74+
bool is_scalar() const override;
7775

78-
ExprPtr clone() const override { return ex<Constant>(this->value()); }
76+
ExprPtr clone() const override;
7977

8078
/// @brief adjoint of a Constant is its complex conjugate
8179
virtual void adjoint() override;
8280

83-
virtual Expr &operator*=(const Expr &that) override {
84-
if (that.is<Constant>()) {
85-
value_ *= that.as<Constant>().value();
86-
} else {
87-
throw Exception("Constant::operator*=(that): not valid for that");
88-
}
89-
return *this;
90-
}
81+
Constant &operator*=(const Expr &that);
9182

92-
virtual Expr &operator+=(const Expr &that) override {
93-
if (that.is<Constant>()) {
94-
value_ += that.as<Constant>().value();
95-
} else {
96-
throw Exception("Constant::operator+=(that): not valid for that");
97-
}
98-
return *this;
99-
}
83+
Constant &operator+=(const Expr &that);
10084

101-
virtual Expr &operator-=(const Expr &that) override {
102-
if (that.is<Constant>()) {
103-
value_ -= that.as<Constant>().value();
104-
} else {
105-
throw Exception("Constant::operator-=(that): not valid for that");
106-
}
107-
return *this;
108-
}
85+
Constant &operator-=(const Expr &that);
10986

11087
/// @param[in] v a scalar
11188
/// @return true if this is zero
112-
static bool is_zero(scalar_type v) { return v.is_zero(); }
89+
static bool is_zero(scalar_type v);
11390

11491
/// @return `Constant::is_zero(this->value())`
115-
bool is_zero() const final { return is_zero(this->value()); }
92+
bool is_zero() const final;
11693

11794
private:
11895
scalar_type value_;
11996

120-
hash_type memoizing_hash() const override {
121-
if (!hash_value_) {
122-
hash_value_ = hash::value(value_);
123-
} else {
124-
SEQUANT_ASSERT(*hash_value_ == hash::value(value_));
125-
}
126-
return *hash_value_;
127-
}
97+
hash_type memoizing_hash() const override;
98+
99+
bool static_equal(const Expr &that) const override;
128100

129-
bool static_equal(const Expr &that) const override {
130-
return value() == static_cast<const Constant &>(that).value();
131-
}
132101
}; // class Constant
133102

134103
} // namespace sequant

0 commit comments

Comments
 (0)