_Surd

it.grypho.scala.leonardo.core._Surd
See the_Surd companion object
final class _Surd extends _Value

An exact real a + b·√d (F_0231): a, b rational, d a positive integer that is not a perfect square, b != 0.

The sixth sibling of _Number, after _Complex, _Truth, _Rational, _Based and _Interval, and like _Rational it widens: case _Number(x) reads it as the real number it is, so a site that knows nothing of surds computes in Double -- float contagion, the safe direction -- while exactness is opt-in. Built only through _Surd.of.

Why one radical, not the field of all square roots: a + b√d is closed under + − × ÷ over a fixed d and covers every quadratic root and every 2-norm; √2 + √3 leaves it, and leaves the exact set the way every irrational always did.

Equality without a canonical radicand: a + b√d = a' + b'√d' iff a = a', b and b' share a sign, and b²d = b'²d' -- all exact, no factoring. hashCode hashes exactly those.

Value parameters

a

the rational part

b

the coefficient of the radical, never zero

d

the radicand, a positive non-square integer

Attributes

Companion
object
Graph
Supertypes
trait _Value
trait _Expression
class Object
trait Matchable
class Any

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 display(precision: Int): String

3 + 2√13, √2/2, -√5: the form a reader writes and the grammar's √ prefix reads back (Decision A). A value with a part past _Rational.MaxDisplayDigits shows as a decimal at precision places, the _Rational rule; :save writes exact whatever is shown.

3 + 2√13, √2/2, -√5: the form a reader writes and the grammar's √ prefix reads back (Decision A). A value with a part past _Rational.MaxDisplayDigits shows as a decimal at precision places, the _Rational rule; :save writes exact whatever is shown.

Value parameters

precision

decimal places for the decimal form

Attributes

Returns

the text

override def equals(that: Any): Boolean

Value equality, decided exactly (see the class note).

Value equality, decided exactly (see the class note).

Attributes

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

Returns Right(this) -- a concrete value needs no further reduction.

Returns Right(this) -- a concrete value needs no further reduction.

Attributes

Definition Classes
def exact: String

The grammar's ASCII form, always exact: (a + b * d^(1/2)). What :save writes (Decision A), re-read in exact mode as this same surd.

The grammar's ASCII form, always exact: (a + b * d^(1/2)). What :save writes (Decision A), re-read in exact mode as this same surd.

Attributes

override def hashCode: Int

Calculates a hash code value for the object.

Calculates a hash code value for the object.

The default hashing algorithm is platform dependent.

Note that it is allowed for two objects to have identical hash codes (o1.hashCode.equals(o2.hashCode)) yet not be equal (o1.equals(o2) returns false). A degenerate implementation could always return 0. However, it is required that if two objects are equal (o1.equals(o2) returns true) that they have identical hash codes (o1.hashCode.equals(o2.hashCode)). Therefore, when overriding this method, be sure to verify that the behavior is consistent with the equals method.

Attributes

Returns

the hash code value for this object.

Definition Classes
Any
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
def toDouble: Double

The value as a Double, through Real so a large radicand does not overflow on the way.

The value as a Double, through Real so a large radicand does not overflow on the way.

Never spire's Real.toDouble (F_0244): it misrounded a surd with a large radicand and a tiny coefficient, so norm([[√2], [√2]]) read as 1.0, and it lost relative precision on a value small only after cancellation (√(10⁶⁰ + 1) − 10³⁰). Nor spire's Rational.toDouble (F_0283): the rational reading is converted through spireToDouble, since spire's own read 0.4999999999999999875 at 128 bits as 0.25. The value is read instead as a rational at absolute precision 2⁻ᵇ with b doubling until it carries 64 significant bits; that ends, because a surd is never zero (√d is irrational), and a value below the smallest subnormal stops the loop as 0.0 with its sign.

Attributes

override def toString: String

Renders at Environment.DefaultPrecision, as display does.

Renders at Environment.DefaultPrecision, as display does.

Attributes

Definition Classes
Any

Concrete fields

val a: _Rational
val b: _Rational
val d: BigInt

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