ValueExpression

com.phasmidsoftware.number.expression.expr.ValueExpression
See theValueExpression companion object
sealed abstract class ValueExpression(val value: Eager, val maybeName: Option[String] = ...) extends AtomicExpression

Represents an abstract expression for a Valuable that can optionally be associated with a name.

A ValueExpression provides methods for evaluating its qualifications within a given context, rendering its representation, and determining equality or hashing. It extends the AtomicExpression trait, allowing it to be used wherever atomic expressions are valid.

CONSIDER placing maybeName in the second parameter list.

Value parameters

maybeName

an optional name for the Valuable expression

value

the Valuable associated with the expression

Attributes

Companion
object
Graph
Supertypes
trait Nameable
trait Expression
trait Approximate
trait Lazy
trait Valuable
trait TypeSafe
trait Exactitude
trait Unitary
trait Zeroable
trait Numeric
trait Renderable
class Object
trait Matchable
class Any
Show all
Known subtypes
class Literal
object E
object I
object Infinity
object Half
object MinusOne
object One
object Pi
object Two
object Zero
Show all

Members list

Value members

Abstract methods

Applies the given ExpressionMonoFunction to the current context of the ValueExpression and attempts to produce an atomic expression as its result. NOTE that, if there is a result defined, it is exact and, preferably, a ValueExpression.

Applies the given ExpressionMonoFunction to the current context of the ValueExpression and attempts to produce an atomic expression as its result. NOTE that, if there is a result defined, it is exact and, preferably, a ValueExpression.

Value parameters

f

the ExpressionMonoFunction to be applied. This function determines how the evaluation will transform the current ValueExpression into a potential result.

Attributes

Returns

an Option containing a ValueExpression if the evaluation succeeds, or None if the evaluation fails.

Concrete methods

def approximation(force: Boolean): Option[Real]

Provides an approximation of this number, if applicable.

Provides an approximation of this number, if applicable.

This method attempts to compute an approximate representation of the number in the form of a Real, which encapsulates uncertainty or imprecision in its value. If no meaningful approximation is possible for the number, it returns None.

Attributes

Returns

an Option[Real] containing the approximate representation of this Number, or None if no approximation is available.

override def approximationComplex(force: Boolean = ...): Option[Eager]

Provides an approximation of the ValueExpression as an Eager, if applicable.

Provides an approximation of the ValueExpression as an Eager, if applicable.

This method checks if the associated value is an instance of eager.Complex. If it is, the method directly returns it wrapped in an Option. Otherwise, it delegates to the approximation method to compute a possible approximation.

Value parameters

force

a Boolean flag indicating whether the approximation should be forced. If true, the method may attempt a broader range of approximations. Defaults to false.

Attributes

Returns

an Option[Eager] containing the approximation if one exists, or None if an approximation is not available or applicable.

Definition Classes
override def equals(other: Any): Boolean

Compares this ValueExpression with another object for equality. The comparison considers the value Valuable and whether the other object can be equal to this instance.

Compares this ValueExpression with another object for equality. The comparison considers the value Valuable and whether the other object can be equal to this instance.

Value parameters

other

the object to compare for equality with this instance.

Attributes

Returns

true if the provided object is equal to this instance, false otherwise.

Definition Classes
Any
def evaluate(context: Context): Option[Eager]

Evaluates the current Valuable expression within the given context and determines if the Valuable qualifies based on the context's rules.

Evaluates the current Valuable expression within the given context and determines if the Valuable qualifies based on the context's rules.

NOTE: we now have Valuable values that may not have a unique factor (Algebraics). That's why we need to check first if value has a unique factor.

If the Valuable meets the qualifications specified by the context, the method returns an Option containing the Valuable. Otherwise, it returns None.

Value parameters

context

the context in which the Valuable is evaluated. It defines the qualification rules for determining whether the Valuable is valid.

Attributes

Returns

Some(Valuable) if the Valuable qualifies within the given context, otherwise None.

override def hashCode(): Int

Computes the hash code for this ValueExpression instance.

Computes the hash code for this ValueExpression instance.

The hash code is derived from the hash code of the value Valuable, ensuring that the behavior adheres to the contract of the hashCode method, particularly in relation to the equals method.

Attributes

Returns

an integer hash code that represents this ValueExpression instance.

Definition Classes
Any
def isExact: Boolean

Method to determine if this Structure object is exact. For instance, Number.pi is exact, although if you converted it into a PureNumber, it would no longer be exact.

Method to determine if this Structure object is exact. For instance, Number.pi is exact, although if you converted it into a PureNumber, it would no longer be exact.

Attributes

Returns

true if this Structure object is exact in the context of No factor, else false.

def isUnity: Boolean

Determines whether this object represents unity.

Determines whether this object represents unity.

Attributes

Returns

true if the object represents unity, false otherwise

def isZero: Boolean

Determines if the current number is equal to zero.

Determines if the current number is equal to zero.

Attributes

Returns

true if the number is zero, false otherwise

def maybeDouble: Option[Double]

If this Valuable 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 Valuable 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.

def maybeFactor(context: Context): Option[Factor]

Method to determine what Factor, if there is such, this Structure object is based on.

Method to determine what Factor, if there is such, this Structure object is based on.

Attributes

Returns

an optional Factor.

def signum: Int

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

override def toString: String

Generate a String for debugging purposes. The name is enclosed in square brackets to distinguish such values from the other Literals.

Generate a String for debugging purposes. The name is enclosed in square brackets to distinguish such values from the other Literals.

Attributes

Returns

a String representation of this Literal.

Definition Classes
Any

Inherited methods

Computes an approximate representation of the current value.

Computes an approximate representation of the current value.

This method utilizes the approximation mechanism to derive the most likely value in the form of a Real. Should the approximation fail, an AlgebraException will be raised to indicate the error.

Attributes

Returns

the approximate value as a Real if successful; throws an AlgebraException otherwise.

Inherited from:
Approximate
def category: String

Returns the category of this value in the Valuable type hierarchy.

Returns the category of this value in the Valuable type hierarchy.

Attributes

Returns

a string indicating the high-level category (e.g., "Expression", "Structure", "Nat", "Complex")

Inherited from:
TypeSafe
def describe: String

Returns a descriptive string combining category and type name. Useful for debugging to understand both the hierarchy level and concrete type.

Returns a descriptive string combining category and type name. Useful for debugging to understand both the hierarchy level and concrete type.

Attributes

Returns

a string in the format "Category.TypeName" (e.g., "Structure.WholeNumber")

Inherited from:
TypeSafe
def multiply(other: Lazy): Lazy

Multiplies this Expression with another Lazy instance. If the other instance is of type Expression, a product operation is performed, and the result is simplified. If the other instance is not of type Expression, an ExpressionException is thrown.

Multiplies this Expression with another Lazy instance. If the other instance is of type Expression, a product operation is performed, and the result is simplified. If the other instance is not of type Expression, an ExpressionException is thrown.

Value parameters

other

the Lazy instance to multiply with this Expression

Attributes

Returns

a new Lazy instance representing the product of this Expression and the supplied Lazy instance

Throws
ExpressionException

if the provided other is not of type Expression

Inherited from:
Expression
def product(other: Valuable): Valuable

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

Inherited from:
Valuable
def protectedName: Boolean

Determines if the name is protected, for example, it represents a mathematical symbol for a transcendental value (e.g., pi), or a commonly used root such as √2.

Determines if the name is protected, for example, it represents a mathematical symbol for a transcendental value (e.g., pi), or a commonly used root such as √2.

This method indicates whether a name associated with an entity is considered protected. The protection status could influence how the name is accessed or utilized by external systems.

Attributes

Returns

true if the name is protected, otherwise false

Inherited from:
Nameable
def show: String

Renders and returns a string representation of the object. By default, this is the same as render but it can be overridden in subclasses.

Renders and returns a string representation of the object. By default, this is the same as render but it can be overridden in subclasses.

Attributes

Returns

a string representing the object

Inherited from:
Valuable
def typeName: String

Returns a simple string representation of the runtime type. Useful for debugging and logging.

Returns a simple string representation of the runtime type. Useful for debugging and logging.

Attributes

Returns

the simple class name (e.g., "WholeNumber", "RationalNumber")

Inherited from:
TypeSafe
def unit(x: Eager): Lazy

Transforms an instance of the Eager type into a Lazy instance, allowing for deferred computation or evaluation. NOTE that this method ignores this.

Transforms an instance of the Eager type into a Lazy instance, allowing for deferred computation or evaluation. NOTE that this method ignores this.

Value parameters

x

an instance of Eager that is to be converted into a Lazy representation

Attributes

Returns

a Lazy instance representing the deferred computation or evaluation of the input

Inherited from:
Expression

Inherited and Abstract methods

def simplifyAtomic: em.AutoMatcher[Expression]

Attempts to simplify an atomic expression, for example, we replace Literal(Valuable.pi) with Pi.

Attempts to simplify an atomic expression, for example, we replace Literal(Valuable.pi) with Pi.

Attributes

Returns

an em.AutoMatcher[Expression] representing the process of handling or matching the atomic expression.

Inherited from:
AtomicExpression

Concrete fields

val maybeName: Option[String]

Retrieves an optional name associated with the implementing class.

Retrieves an optional name associated with the implementing class.

The method returns an Option wrapping a String. If a name is available, it will be contained within the Option as Some(name). If no name is present, the method will return None.

Attributes

Returns

an Option[String] representing the optional name

lazy val render: String

Method to render this Structure in a presentable manner.

Method to render this Structure in a presentable manner.

Attributes

Returns

a String

val value: Eager

Inherited fields

lazy val approximation: Option[Real]

A lazily evaluated optional approximation of this Expression.

A lazily evaluated optional approximation of this Expression.

This field holds the result of simplifying the Expression with the approximation flag set to true. If the approximation is possible, it will store the approximated Real value; otherwise, it will remain None.

This approximation is computed based on the context of the simplify method, which determines how the expression should be approximated.

Attributes

Inherited from:
Expression
lazy val approximationComplex: Option[Eager]

Represents the optional approximate evaluation of a complex Expression. The property retrieves the result of invoking the approximationComplex method on the simplify field, passing true as an argument to indicate that the result should be computed for complex approximations.

Represents the optional approximate evaluation of a complex Expression. The property retrieves the result of invoking the approximationComplex method on the simplify field, passing true as an argument to indicate that the result should be computed for complex approximations.

This lazy value ensures that the computation is deferred until accessed, and only computed once for performance optimization.

Attributes

Returns

An Option of type Eager, containing the approximate complex representation of this Expression, if available. Otherwise, None.

Inherited from:
Expression
lazy val asCoreNumber: Option[Number]

Method to determine if the materialized value of this Expression is defined and corresponds to a Number. If this expression is exact, then evaluate as is and convert the result to a Number. Otherwise, we simply materialize this expression and convert the result to a Number.

Method to determine if the materialized value of this Expression is defined and corresponds to a Number. If this expression is exact, then evaluate as is and convert the result to a Number. Otherwise, we simply materialize this expression and convert the result to a Number.

Attributes

Returns

a Some(x) if this materializes as a Number; otherwise None.

Inherited from:
Expression
lazy val asMonotone: Structure

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.

Attributes

Returns

the current instance as a Structure

Note

Throws com.phasmidsoftware.number.algebra.util.AlgebraException if the instance is not of type Structure

Inherited from:
Valuable
lazy val asNumber: Option[Number]

Attempts to cast this instance to a Number. If the instance is of type Number, it is wrapped in an Option and returned. Otherwise, returns None.

Attempts to cast this instance to a Number. If the instance is of type Number, it is wrapped in an Option and returned. Otherwise, returns None.

Attributes

Returns

an Option containing the instance as a Number if applicable, or None if not.

Inherited from:
Valuable
lazy val containsI: Boolean

Determines if the current Expression or any of its sub-expressions contains the constant I.

Determines if the current Expression or any of its sub-expressions contains the constant I.

For Expression instances:

  • If the Expression is exactly the constant I, returns true.
  • If the Expression is a composite of other expressions (CompositeExpression), recursively searches through the terms to check if any of them contains the constant I.
  • For all other cases, returns false.

Attributes

Returns

true if the constant I is present in the Expression or its components, false otherwise.

Inherited from:
Expression
lazy val debug: String

Method to use when debugging to know exactly what an Expression is in tree form. This is needed because toString and other renderers all use some sort of syntactic sugar to make things look nice for us humans.

Method to use when debugging to know exactly what an Expression is in tree form. This is needed because toString and other renderers all use some sort of syntactic sugar to make things look nice for us humans.

Creates a string representation of this Expression for debugging purposes. Depending on the type of this Expression, the output format varies:

  • For atomic Product instances (arity 0), the productPrefix is returned.
  • For non-atomic Product instances, the productPrefix is combined with the debug representation of contained sub-expressions (if they are Expressions) or their toString representation otherwise.
  • For all other cases, the default toString implementation is used.

Attributes

Returns

a String representation useful for debugging.

Inherited from:
Expression
val depth: Int

Attributes

Returns
Inherited from:
AtomicExpression
lazy val evaluateAsIs: Option[Eager]

Evaluates this Expression in the context of AnyContext without simplification or factor-based conversion. This allows obtaining a direct evaluation of the Expression as a Eager, if possible. NOTE: no simplification or factor-based conversion occurs here.

Evaluates this Expression in the context of AnyContext without simplification or factor-based conversion. This allows obtaining a direct evaluation of the Expression as a Eager, if possible. NOTE: no simplification or factor-based conversion occurs here.

Attributes

Returns

either a Some[Eager] containing the value if evaluation is successful, or None otherwise.

Inherited from:
Expression
lazy val fuzzy: Eager

Computes the fuzzy (approximate) value of this Expression. If the current Expression can be matched to a simpler form via matchSimpler, its fuzzy value is derived from the simplified expression. Otherwise, the fuzzy result is delegated to the superclass implementation.

Computes the fuzzy (approximate) value of this Expression. If the current Expression can be matched to a simpler form via matchSimpler, its fuzzy value is derived from the simplified expression. Otherwise, the fuzzy result is delegated to the superclass implementation.

Attributes

Returns

an eager.Real representing the approximate value of this Expression

Inherited from:
Expression
val isAtomic: Boolean

Indicates whether the expression is atomic.

Indicates whether the expression is atomic.

Attributes

Returns

true, as this expression is atomic and cannot be further simplified.

Inherited from:
AtomicExpression
lazy val isEager: Boolean

Method to determine if this Expression can be evaluated as is. If so, then materialize will not lose any precision because no approximation will be required. NOTE that, it is conceivable that simplifying this Expression first might result in an eager value even though this method would return false.

Method to determine if this Expression can be evaluated as is. If so, then materialize will not lose any precision because no approximation will be required. NOTE that, it is conceivable that simplifying this Expression first might result in an eager value even though this method would return false.

Attributes

Returns

true if evaluateAsIs is defined.

Inherited from:
Expression
lazy val keepSymbolic: Boolean

Indicates whether symbolic names are preserved.

Indicates whether symbolic names are preserved.

This method determines if symbolic representation should be retained when working with names in entities implementing the Nameable trait.

Attributes

Returns

true if symbolic names are kept, otherwise false

Inherited from:
Nameable
lazy val materialize: Eager

Materializes this Expression into an Eager object by simplifying it, evaluating as-is, optionally approximating it, and recovering with a defined exception in case of failure.

Materializes this Expression into an Eager object by simplifying it, evaluating as-is, optionally approximating it, and recovering with a defined exception in case of failure.

This method should be the final step when dealing with an Expression. Do not materialize early because that defeats the entire purpose of lazy evaluation.

The method first simplifies the expression, attempts to evaluate it directly, and then tries an approximation if necessary. Ultimately, it ensures the resulting materialized object is an instance of Eager.

The Eager result will be normalized if appropriate.

Attributes

Returns

an Eager representation of this Expression achieved through evaluation and/or approximation.

Inherited from:
Expression
lazy val normalize: Valuable

Normalizes the current Lazy instance to its simplest Valuable equivalent. The method first attempts to simplify the instance using the simplify method. If the simplified result is exact, the method materializes it and normalizes the resulting value. Otherwise, the simplified value is returned as is.

Normalizes the current Lazy instance to its simplest Valuable equivalent. The method first attempts to simplify the instance using the simplify method. If the simplified result is exact, the method materializes it and normalizes the resulting value. Otherwise, the simplified value is returned as is.

Attributes

Returns

the simplest Valuable representation of this value after normalization

Inherited from:
Lazy
lazy val simplify: Expression

Simplifies the current Expression instance. If a simpler representation of the Expression can be determined, it returns that representation. Otherwise, it returns the current Expression unchanged. NOTE: be careful not to invoke recursively.

Simplifies the current Expression instance. If a simpler representation of the Expression can be determined, it returns that representation. Otherwise, it returns the current Expression unchanged. NOTE: be careful not to invoke recursively.

Attributes

Returns

the simplified Expression, or the current Expression if no simplification is possible

Inherited from:
Expression
lazy val toDouble: Double

Converts the approximate representation of this number into a Double.

Converts the approximate representation of this number into a Double.

This method attempts to approximate the value of this number by invoking the approximation method with force = true. If the approximation exists, it is converted to a Double. If no approximation can be obtained, an AlgebraException is thrown to indicate a logic error.

Attributes

Returns

the approximate value as a Double.

Note

Throws an com.phasmidsoftware.number.algebra.util.AlgebraException if no approximation can be obtained.

Inherited from:
Approximate