Skip to content
Open
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
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -306,16 +306,28 @@ set(SeQuant_symb_src
SeQuant/core/context.hpp
SeQuant/core/expressions/abstract_tensor.cpp
SeQuant/core/expressions/abstract_tensor.hpp
SeQuant/core/expressions/constant.cpp
SeQuant/core/expressions/constant.hpp
SeQuant/core/expressions/expr.cpp
SeQuant/core/expressions/expr.hpp
SeQuant/core/expressions/expr_algorithms.cpp
SeQuant/core/expressions/expr_algorithms.hpp
SeQuant/core/expressions/expr_operators.hpp
SeQuant/core/expressions/expr_ptr.cpp
SeQuant/core/expressions/expr_ptr.hpp
SeQuant/core/expressions/expr_range.hpp
SeQuant/core/expressions/power.cpp
SeQuant/core/expressions/power.hpp
SeQuant/core/expressions/product.cpp
SeQuant/core/expressions/product.hpp
SeQuant/core/expressions/result_expr.cpp
SeQuant/core/expressions/result_expr.hpp
SeQuant/core/expressions/sum.cpp
SeQuant/core/expressions/sum.hpp
SeQuant/core/expressions/tensor.cpp
SeQuant/core/expressions/tensor.hpp
SeQuant/core/expressions/variable.cpp
SeQuant/core/expressions/variable.hpp
SeQuant/core/hash.cpp
SeQuant/core/hash.hpp
SeQuant/core/hugenholtz.hpp
Expand Down
77 changes: 77 additions & 0 deletions SeQuant/core/expressions/constant.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <SeQuant/core/expressions/constant.hpp>
#include <SeQuant/core/expressions/expr_ptr.hpp>
#include <SeQuant/core/io/latex/latex.hpp>
#include <SeQuant/core/utility/exception.hpp>
#include <SeQuant/core/utility/macros.hpp>

namespace sequant {

std::wstring Constant::to_latex() const {
return L"{" + io::latex::to_string(value()) + L"}";
}

Expr::type_id_type Constant::type_id() const { return get_type_id<Constant>(); }

bool Constant::is_scalar() const { return true; }

ExprPtr Constant::clone() const { return ex<Constant>(this->value()); }

void Constant::adjoint() {
value_ = conj(value_);
reset_hash_value();
}

Constant &Constant::operator*=(const Expr &that) {
if (that.is<Constant>()) {
value_ *= that.as<Constant>().value();
} else {
throw Exception("Constant::operator*=(that): not valid for that");
}

reset_hash_value();

return *this;
}

Constant &Constant::operator+=(const Expr &that) {
if (that.is<Constant>()) {
value_ += that.as<Constant>().value();
} else {
throw Exception("Constant::operator+=(that): not valid for that");
}

reset_hash_value();

return *this;
}

Constant &Constant::operator-=(const Expr &that) {
if (that.is<Constant>()) {
value_ -= that.as<Constant>().value();
} else {
throw Exception("Constant::operator-=(that): not valid for that");
}

reset_hash_value();

return *this;
}

bool Constant::is_zero(scalar_type v) { return v.is_zero(); }

bool Constant::is_zero() const { return is_zero(this->value()); }

Expr::hash_type Constant::memoizing_hash() const {
if (!hash_value_) {
hash_value_ = hash::value(value_);
} else {
SEQUANT_ASSERT(*hash_value_ == hash::value(value_));
}
return *hash_value_;
}

bool Constant::static_equal(const Expr &that) const {
return value() == static_cast<const Constant &>(that).value();
}

} // namespace sequant
59 changes: 14 additions & 45 deletions SeQuant/core/expressions/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include <SeQuant/core/complex.hpp>
#include <SeQuant/core/expressions/expr.hpp>
#include <SeQuant/core/expressions/expr_ptr.hpp>
#include <SeQuant/core/io/latex/latex.hpp>
#include <SeQuant/core/rational.hpp>
#include <SeQuant/core/utility/macros.hpp>

Expand All @@ -14,6 +12,8 @@

namespace sequant {

class ExprPtr;

// implementation details of Constant; prefer sequant::detail over an unnamed
// namespace in a header (see CppCoreGuidelines SF.21)
namespace detail {
Expand Down Expand Up @@ -67,68 +67,37 @@ class Constant : public Expr {
throw Exception("Constant::value<T>: cannot convert value to type T");
}

std::wstring to_latex() const override {
return L"{" + io::latex::to_string(value()) + L"}";
}
std::wstring to_latex() const override;

type_id_type type_id() const override { return get_type_id<Constant>(); }
type_id_type type_id() const override;

bool is_scalar() const override { return true; }
bool is_scalar() const override;

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

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

virtual Expr &operator*=(const Expr &that) override {
if (that.is<Constant>()) {
value_ *= that.as<Constant>().value();
} else {
throw Exception("Constant::operator*=(that): not valid for that");
}
return *this;
}
Constant &operator*=(const Expr &that);

virtual Expr &operator+=(const Expr &that) override {
if (that.is<Constant>()) {
value_ += that.as<Constant>().value();
} else {
throw Exception("Constant::operator+=(that): not valid for that");
}
return *this;
}
Constant &operator+=(const Expr &that);

virtual Expr &operator-=(const Expr &that) override {
if (that.is<Constant>()) {
value_ -= that.as<Constant>().value();
} else {
throw Exception("Constant::operator-=(that): not valid for that");
}
return *this;
}
Constant &operator-=(const Expr &that);

/// @param[in] v a scalar
/// @return true if this is zero
static bool is_zero(scalar_type v) { return v.is_zero(); }
static bool is_zero(scalar_type v);

/// @return `Constant::is_zero(this->value())`
bool is_zero() const final { return is_zero(this->value()); }
bool is_zero() const final;

private:
scalar_type value_;

hash_type memoizing_hash() const override {
if (!hash_value_) {
hash_value_ = hash::value(value_);
} else {
SEQUANT_ASSERT(*hash_value_ == hash::value(value_));
}
return *hash_value_;
}
hash_type memoizing_hash() const override;

bool static_equal(const Expr &that) const override;

bool static_equal(const Expr &that) const override {
return value() == static_cast<const Constant &>(that).value();
}
}; // class Constant

} // namespace sequant
Expand Down
Loading
Loading