_Rationalize

it.grypho.scala.leonardo.matrix._Rationalize
case class _Rationalize(x: _Expression, digits: Option[_Expression] = ...) extends _Expression

rationalize(x) / rationalize(x, digits) — every decimal of x replaced by the rational it is closest to, with a denominator at most 10^digits (F_0170).

The way back into the exact tier, and only when asked. A Double stays visibly inexact by design (the one-way float contagion of 4.L), so a value known to be exact that arrived as a decimal — a lu factor in exact mode, a frozen consolidate result, a number read from data — had no way back. Applied silently, the conversion would relabel a computed 0.3333333333333333 as an exact 1/3; asked for, it is the reader's statement that the value is rational.

  • A number becomes _Rational.fromApproximation(d, digits): the best fraction by continued-fraction truncation, so 0.3333333333333333 is 1/3.
  • A dense matrix becomes the exact _Matrix carrier — the reason this node lives in matrix: scalar cannot build that carrier.
  • A symbolic expression keeps its shape, its decimal leaves made exact; an exact leaf is left as it is.
  • digits defaults to the fewest that make every decimal its small fraction when each looks rational (rationalizeDigits, F_0240), else to the working precision, capped at 15, the digits a Double carries (_Rational.DoubleReliableDigits); an explicit one must be a whole number in 1 .. 15.
  • A value it cannot make exact declines the whole call (the owner's decision): a complex number (no exact complex carrier exists), an interval, a non-finite value. Converting the rest would leave a result in two tiers that demotes again on evaluation.

It works outside exact mode too, since it is asked for; :save groups the exact value it leaves under exact on (F_0090).

Value parameters

digits

the decimal digits the approximation is good for, optional

x

the expression whose decimals to convert

Attributes

Graph
Supertypes
trait Serializable
trait Product
trait Equals
trait _Expression
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

override def children: List[_Expression]

Sub-expressions subject to recursive structural traversal. Binder positions (e.g. the differentiation variable) are excluded.

Sub-expressions subject to recursive structural traversal. Binder positions (e.g. the differentiation variable) are excluded.

Attributes

Definition Classes
def declineReason(env: Environment): Option[String]

Why this call stays unevaluated: the digits, or the leaf that has no exact value.

Why this call stays unevaluated: the digits, or the leaf that has no exact value.

Value parameters

env

the evaluation environment

Attributes

Returns

the reason, or None when it answers

override def eval(env: Environment): Either[_Expression, _Value]

Reduces this expression in the given environment.

Reduces this expression in the given environment.

Value parameters

env

variable bindings and display precision

Attributes

Returns

Right(v) when all free variables resolved to concrete values; Left(e) when reduction is partial or impossible

Definition Classes
override def rebuild(c: List[_Expression]): _Expression

Reconstructs the same node shape with replacement sub-expressions.

Reconstructs the same node shape with replacement sub-expressions.

Value parameters

newChildren

replacements in the same order and count as children

Attributes

Definition Classes
override def toString: String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns

a string representation of the object.

Definition Classes
Any

Inherited methods

def productElementNames: Iterator[String]

Attributes

Inherited from:
Product
def productIterator: Iterator[Any]

Attributes

Inherited from:
Product

Inherited fields

lazy val freeVars: Set[String]

Cached set of free variable names; O(1) after the first access.

Cached set of free variable names; O(1) after the first access.

Attributes

Inherited from:
_Expression