2 #include <symengine/matrices/matrix_expr.h>
3 #include <symengine/matrices/trace.h>
11 hash_t seed = SYMENGINE_TRACE;
12 hash_combine<Basic>(seed, *arg_);
18 return (is_a<Trace>(o) && arg_->__eq__(*down_cast<const Trace &>(o).arg_));
23 SYMENGINE_ASSERT(is_a<Trace>(o));
25 return arg_->compare(*down_cast<const Trace &>(o).arg_);
36 RCP<const Basic> trace_;
40 throw DomainError(
"Trace is only valid for square matrices");
46 void bvisit(
const Basic &x){};
50 auto arg = rcp_static_cast<const MatrixExpr>(x.
rcp_from_this());
51 trace_ = make_rcp<const Trace>(arg);
61 tribool sq = is_square(x);
64 }
else if (is_false(sq)) {
67 auto arg = rcp_static_cast<const MatrixExpr>(x.
rcp_from_this());
68 trace_ = make_rcp<const Trace>(arg);
74 trace_ =
add(x.get_container());
79 if (x.nrows() != x.ncols()) {
83 for (
size_t i = 0; i < x.nrows(); i++) {
84 diag.push_back(x.get(i, i));
92 RCP<const Basic> sum = zero;
93 for (
auto &e : x.get_terms()) {
95 sum =
add(sum, trace_);
107 RCP<const Basic> trace(
const RCP<const MatrixExpr> &arg)
110 return visitor.apply(*arg);
The base class for SymEngine.
The lowest unit of symbolic representation.
RCP< T > rcp_from_this()
Get RCP<T> pointer to self (it will cast the pointer to T)
hash_t __hash__() const override
bool __eq__(const Basic &o) const override
Test equality.
int compare(const Basic &o) const override
vec_basic get_args() const override
Returns the list of arguments.
Main namespace for SymEngine package.
RCP< const Basic > add(const RCP< const Basic > &a, const RCP< const Basic > &b)
Adds two objects (safely).