Class Power

Inheritance Relationships

Base Type

Class Documentation

class Power : public sequant::Expr

Represents base^exponent where base is a scalar (Constant or Variable) and exponent is a rational number.

Public Types

using exponent_type = rational

Public Functions

Power() = delete
virtual ~Power() = default
Power(const Power&) = default
Power(Power&&) = default
Power &operator=(const Power&) = default
Power &operator=(Power&&) = default
inline Power(ExprPtr base, exponent_type exponent)
Parameters:
  • base[in] the base expression; must be a Constant or Variable.

  • exponent[in] rational exponent

template<typename L>
inline Power(L &&label, exponent_type exponent)
template<typename V>
inline Power(V &&value, exponent_type exponent)
inline const ExprPtr &base() const
Returns:

the base expression

inline const exponent_type &exponent() const
Returns:

the rational exponent

inline bool conjugated() const

Note

Conjugation is tracked as a flag because, in general, conj(base^exponent) != conj(base)^exponent

Returns:

whether this Power has been complex-conjugated via adjoint()

inline void conjugate()

toggles the conjugation flag

inline virtual bool is_zero() const override

Note

Construction rejects all undefined 0^n cases; 0^0 is legal and treated as 1.

Returns:

true if the base is zero and the exponent is positive

inline virtual type_id_type type_id() const override

Computes and returns the derived type identifier

Note

this function must be overridden in the derived class

Returns:

the hash value for this Expr

inline virtual bool is_scalar() const override

Reports if this is a pure scalar (number-like) expression.

Note

This is distinct from is_cnumber()

Warning

this returns false for all leaves by default, hence must be overridden for scalar leaf types.

Returns:

true if this is a scalar

inline virtual ExprPtr clone() const override

Note

- must be overridden in the derived class.

  • the default implementation throws an exception

Returns:

a clone of this object, i.e. an object that is equal to this

inline virtual void adjoint() override

adjoint of Power: flips the conjugation flag.

inline virtual Expr &operator*=(const Expr &that) override

Combines exponents when effective bases match:

  • b^e1 *= b^e2b^(e1+e2) when this and that share the same Power-level conjugation flag.

  • b^e *= Bb^(e+1) for a bare scalar B equal to this Power’s effective base. For a Variable base the labels must match and the effective conjugation parities align. For a Constant base only the fully unconjugated case combines.

Throws:

Exception – if that is not combinable.

Public Static Functions

static inline void flatten(ExprPtr &expr)

Attempts to flatten a Power, mutating expr in place. Folds when expr holds a Power and any of:

  • the exponent is 1 (then b^1 = b and conjugate if needed);

  • the exponent is 0 (then b^0 = 1 for any base);

  • the base is the Constant 1 (then 1^k = 1 for any rational k);

  • the base is a Constant and the exponent is a real integer;

  • the base is a Constant and the exponent has the form m/2 and the base is a non-negative real rational p/q with both p and q perfect squares (e.g. 4^{1/2} -> 2, (1/4)^{1/2} -> 1/2). On success, expr is rebound to the folded expression; otherwise it is left unchanged.

Note

Only square-root exponents are folded (that is the only case needed in practice right now). Extending to general n-th roots only requires replacing the integer-square-root step with an integer n-th-root.