Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion SeQuant/core/expressions/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class Constant : public Expr {
template <typename U>
requires(!is_constant_v<U> && !is_an_expr_v<std::remove_reference_t<U>> &&
!Expr::is_shared_ptr_of_expr_or_derived<
std::remove_reference_t<U>>::value)
std::remove_reference_t<U>>::value &&
std::constructible_from<scalar_type, U>)
explicit Constant(U &&value) : value_(std::forward<U>(value)) {}

/// @tparam T the result type; default to the type of value_
Expand Down
37 changes: 37 additions & 0 deletions SeQuant/core/expressions/expr_operators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <SeQuant/core/expressions/expr_ptr.hpp>
#include <SeQuant/core/expressions/product.hpp>
#include <SeQuant/core/expressions/sum.hpp>
#include <SeQuant/core/expressions/variable.hpp>
#include <SeQuant/core/utility/macros.hpp>

#include <concepts>
Expand Down Expand Up @@ -149,6 +150,42 @@ inline ExprPtr operator/(const ExprPtr &lhs, const Constant &rhs) {
return lhs * ex<Constant>(1.0 / rhs.value());
}

template <typename T>
requires(std::constructible_from<Variable, T>)
ExprPtr operator+(T &&lhs, const ExprPtr &rhs) {
return ex<Variable>(std::forward<T>(lhs)) + rhs;
}

template <typename T>
requires(std::constructible_from<Variable, T>)
ExprPtr operator+(const ExprPtr &lhs, T &&rhs) {
return lhs + ex<Variable>(std::forward<T>(rhs));
}

template <typename T>
requires(std::constructible_from<Variable, T>)
ExprPtr operator-(T &&lhs, const ExprPtr &rhs) {
return ex<Variable>(std::forward<T>(lhs)) - rhs;
}

template <typename T>
requires(std::constructible_from<Variable, T>)
ExprPtr operator-(const ExprPtr &lhs, T &&rhs) {
return lhs - ex<Variable>(std::forward<T>(rhs));
}

template <typename T>
requires(std::constructible_from<Variable, T>)
ExprPtr operator*(T &&lhs, const ExprPtr &rhs) {
return ex<Variable>(std::forward<T>(lhs)) * rhs;
}

template <typename T>
requires(std::constructible_from<Variable, T>)
ExprPtr operator*(const ExprPtr &lhs, T &&rhs) {
return lhs * ex<Variable>(std::forward<T>(rhs));
}

} // namespace sequant

#endif // SEQUANT_EXPRESSIONS_OPERATORS_HPP
10 changes: 9 additions & 1 deletion SeQuant/core/expressions/variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <SeQuant/core/expressions/labeled.hpp>
#include <SeQuant/core/hash.hpp>
#include <SeQuant/core/utility/macros.hpp>
#include <SeQuant/core/utility/string.hpp>

#include <string>
#include <string_view>
Expand All @@ -21,11 +22,18 @@ class Variable : public Expr, public MutatableLabeled {
Variable(Variable &&) = default;
Variable &operator=(const Variable &) = default;
Variable &operator=(Variable &&) = default;
template <typename U, typename = std::enable_if_t<!is_variable_v<U>>>
template <typename U>
requires(!is_variable_v<U> && !is_an_expr_v<std::remove_reference_t<U>> &&
!Expr::is_shared_ptr_of_expr_or_derived<
std::remove_reference_t<U>>::value &&
std::constructible_from<std::wstring, U>)
explicit Variable(U &&label) : label_(std::forward<U>(label)) {}

Variable(std::wstring label) : label_(std::move(label)), conjugated_(false) {}

Variable(const std::string &label)
: label_(sequant::toUtf16(label)), conjugated_(false) {}

/// @return variable label
/// @warning conjugation does not change it
std::wstring_view label() const override;
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,29 @@ TEST_CASE("expr", "[elements]") {
REQUIRE(res == ex<Constant>(rational(1, 5)));
}

SECTION("Overloads with Variables") {
auto One = ex<Constant>(1);
auto Two = ex<Constant>(2);

ExprPtr res1 = One + L"x";
simplify(res1);
REQUIRE(res1 == simplify(One + ex<Variable>(L"x")));
REQUIRE(simplify(L"x" + One) == res1);

ExprPtr res2 = res1 - "x";
simplify(res2);
REQUIRE(res2 == One);

ExprPtr res3 = L"x" - One;
simplify(res3);
REQUIRE(res3 == ex<Variable>(L"x") - One);

ExprPtr res4 = Two * "y";
simplify(res4);
REQUIRE(res4 == simplify(Two * ex<Variable>("y")));
REQUIRE(simplify("y" * Two) == res4);
}

SECTION("Divide by Constant") {
ex1 = ex<Constant>(5);

Expand Down