A trait representing an object that is in some sense numerical and can be evaluated mathematically. Valuable objects are either Eager, in which case they are concrete mathematical values, or Lazy in which case they are symbolic expressions that require further evaluation (through the materialize method).
The Valuable trait extends multiple traits, enabling it to support rendering, numeric operations, normalization, exactitude, and type safety. Instances of Valuable can be either eager or lazy in computation and can represent various structures or numbers.
Converts the current Lazy instance into an Eager instance by forcing the resolution or evaluation of its underlying value.
Converts the current Lazy instance into an Eager instance by forcing the resolution or evaluation of its underlying value.
The materialize method ensures that the computation of the value represented by this Lazy instance is completed and encapsulated within an appropriate Eager representation. This is particularly useful in scenarios where deferred computation needs to be explicitly resolved prior to further processing.
Attributes
Returns
the materialized Eager instance representing the resolved value
Obtains the product of this Valuable instance and another Valuable instance. The behavior of the method is determined by whether the instances are of type Lazy or Eager. If the provided instances do not conform to the expected types, an AlgebraException is thrown. CONSIDER supporting sum also.
Obtains the product of this Valuable instance and another Valuable instance. The behavior of the method is determined by whether the instances are of type Lazy or Eager. If the provided instances do not conform to the expected types, an AlgebraException is thrown. CONSIDER supporting sum also.
CONSIDER using lazify
Value parameters
other
the other Valuable instance to compute the product with
Attributes
Returns
a Valuable instance representing the product of this and the other instance
Note
throws AlgebraException if the computation involves unsupported Valuable types
Determines whether this object is exact, i.e., is not any kind of approximation.
Determines whether this object is exact, i.e., is not any kind of approximation.
CONSIDER it may be possible that there are non-approximatable entities that are not exact either.
The method returns true if the value is represented exactly in IEEE 754 format, or Two's complement or the exact value must be represented symbolically. Examples of the first type include the number one (represented here as either WholeNumber(1) or Succ(NatZero). Examples of the second type include 1.5 or the square root of two (represented here as InversePower(2,2)).
When we are forced to approximate a value, for example when we want to print the decimal value of 𝛗, the Golden Ratio, then we mark such a representation as approximate, i.e., this method returns false.
Attributes
Returns
a Boolean indicating whether the entity is exact (true) or is an approximation (false).
If this is exact, it returns the exact value as a Double. Otherwise, it returns None. NOTE: do NOT implement this method to return a Double for a fuzzy Real--only for exact numbers.
If this is exact, it returns the exact value as a Double. Otherwise, it returns None. NOTE: do NOT implement this method to return a Double for a fuzzy Real--only for exact numbers.
Attributes
Returns
Some(x) where x is a Double if this is exact, else None.
Normalizes this T to its simplest equivalent form where T is the supertype of all possible results. This may change the type (e.g., RationalNumber → WholeNumber, Complex(5,0) → WholeNumber(5)).
Normalizes this T to its simplest equivalent form where T is the supertype of all possible results. This may change the type (e.g., RationalNumber → WholeNumber, Complex(5,0) → WholeNumber(5)).
For Expression types, this will attempt to simplify and materialize if the result is exact. For Eager types, this will reduce to the simplest type representation.
Attributes
Returns
the simplest representation of this value that is a subtype of T.
Method to render this Valuable for presentation to the user. NOTE to implementors: never ever invoke this.toString in your implementation lest you get a StackOverflow.
Method to render this Valuable for presentation to the user. NOTE to implementors: never ever invoke this.toString in your implementation lest you get a StackOverflow.
Determines the sign of the Structure value represented by this instance. Returns an integer indicating whether the value is positive, negative, or zero.
Determines the sign of the Structure value represented by this instance. Returns an integer indicating whether the value is positive, negative, or zero.
Attributes
Returns
1 if the value is positive, -1 if the value is negative, and 0 if the value is zero
Casts this Valuable instance into a Structure type if it already is a Structure. If the instance is not a Structure, an AlgebraException is thrown. CONSIDER returning Option[Structure] instead.
Casts this Valuable instance into a Structure type if it already is a Structure. If the instance is not a Structure, an AlgebraException is thrown. CONSIDER returning Option[Structure] instead.