it.grypho.scala.leonardo.core

Members list

Type members

Classlikes

object Environment

Companion holding the global precision constant.

Companion holding the global precision constant.

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
class Environment(val precision: Int, variables: Map[String, _Value], val symmetricLogic: Boolean, val semantics: LogicSemantics, val workingPrecision: Int)

Immutable variable-binding context shared across all domains.

Immutable variable-binding context shared across all domains.

Bindings map names to fully-reduced _Values (a number, a matrix, a boolean) — never to a symbolic expression. Kept in core because eval takes one, and core must not depend on any domain.

withBinding returns a new Environment; the original is unchanged (structural sharing).

Value parameters

precision

decimal places used when rendering numbers

semantics

the t-norm / t-conorm pair the logic connectives evaluate with; defaults to LogicSemantics.MinMax. Like symmetricLogic this is a knob on eval, not a separate node hierarchy.

symmetricLogic

when true, the digits {-1, 0, 1} are read as the symmetric ternary spelling of {false, unknown, true} in logical connective positions, and truth values render in that alphabet. This is an encoding toggle, not a semantics: the min–max rule table is untouched and the two alphabets are related by the affine map t = (s + 1) / 2 (see core._Truth.fromSymmetric). Defaults to false, the false/unknown/true spelling.

workingPrecision

decimal digits an irrational is approximated to before exact arithmetic continues. Read by eval wherever a _Rational meets an operation that is not closed over the rationals. Note this is not the exact-mode switch: whether a value is exact is carried by its own type, so the only component that has to be told is the parser, which decides what a literal becomes. That flag lives in cli.Session beside pretty; an Environment.exact field would be read by nothing.

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
enum GcdPolicy

When a _Rational reduces itself to lowest terms.

When a _Rational reduces itself to lowest terms.

The policy changes representation only, never value: 2/6 and 1/3 are the same rational, and _Rational.equals compares by cross-multiplication precisely so that the choice stays invisible to callers. What it does change is cost — gcd is the expensive step of rational arithmetic, while skipping it roughly doubles operand size per operation — which is why it is a measured decision rather than a guess.

Selected per call site rather than through Environment, following the simplifyLogic(e, symmetric, semantics) precedent: nothing in this tier goes through eval yet, so an environment field would be unread. It moves into Environment with the tier-1 AST integration, alongside the working precision.

Attributes

Supertypes
trait Enum
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

The truth-functional semantics selecting the t-norm / t-conorm pair used by the logic connectives.

The truth-functional semantics selecting the t-norm / t-conorm pair used by the logic connectives.

All three agree with classical logic on the crisp values {0, 1} and differ only on graded degrees, and all three share the strong negation 1 - a. Every one has 0 as the annihilator of its t-norm and 1 as the annihilator of its t-conorm, which is why the connective short-circuits are sound under each.

A parameterization of eval, never separate packages and never separate nodes.

Attributes

Supertypes
trait Enum
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
case class _Based extends _Value

An integer that remembers the base it is written in.

An integer that remembers the base it is written in.

The fourth sibling of _Number, after _Complex, _Truth and _Rational, and built to the same pattern: a private constructor behind a smart factory, and a value that is read as a number through _Number's widening extractor so no existing case _Number(x) site has to learn about it.

Why the base lives on the value. A base conversion that returns bare digits is only a decomposition: [[15, 15]] is indistinguishable from a matrix typed by hand, and the caller has to remember the base to reassemble it. Putting the base on the value is what makes A := 0xFF still be FF when it is printed an hour later.

The tag is shallow, deliberately. 0xFF + 1 prints 256 in decimal, because Sum.eval reads both operands as numbers and builds a plain _Number. Carrying the base through arithmetic would require deciding whose base wins in 0xFF + 0b1011, and there is no defensible answer — so the base survives display and storage, which is what a base is for, and arithmetic falls back to decimal.

Value parameters

balanced

whether the digits are the balanced set (base 3 only)

base

the radix, 2 .. 36

value

the integer value, held as a Double like every other numeric tier

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
trait _Value
trait _Expression
class Object
trait Matchable
class Any
Show all
object _Based

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
_Based.type
case class _Bool(b: Boolean) extends _Value

Concrete boolean value — the result of a fully-reduced relation.

Concrete boolean value — the result of a fully-reduced relation.

Value parameters

b

the truth value

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
trait _Value
trait _Expression
class Object
trait Matchable
class Any
Show all
object _Complex

Companion for the concrete complex value _Complex; holds the smart factory and all arithmetic on _Value operands.

Companion for the concrete complex value _Complex; holds the smart factory and all arithmetic on _Value operands.

Arithmetic returns Option[_Value]None signals a non-finite or undefined result (e.g. log 0, 0 ^ negative) so the caller stays symbolic, mirroring the "domain errors stay symbolic" contract of the real path.

_Complex.of collapses a zero imaginary part back to a plain _Number, so every existing _Number(x) pattern match across the codebase keeps firing on real results — only a genuinely non-real value ever becomes a _Complex. Binary operations promote _Number operands to (x, 0) via _Complex.parts.

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
_Complex.type
case class _Complex extends _Value

Concrete complex value re + im·i where im ≠ 0.

Concrete complex value re + im·i where im ≠ 0.

The imaginary part is never exactly 0 — the _Complex.of factory guarantees this invariant by collapsing to _Number in that case. Construction through of is the only public route; pattern matching _Complex(re, im) remains available.

Like _Number, a _Complex never rounds in eval; rounding is a display concern handled by toString / display(p). A rounded-away imaginary part causes the value to print as a plain real (e.g. exp(i·π)"-1.0"); a pure imaginary value prints as "bi" / "i" / "-i"; the full form is "(a + bi)".

Value parameters

im

imaginary part; guaranteed non-zero by the _Complex.of factory

re

real part

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
trait _Value
trait _Expression
class Object
trait Matchable
class Any
Show all
trait _ElementWise extends _Expression

Marker trait for expression nodes whose children may receive per-element algorithm passes (derive, simplify, expand, integrate).

Marker trait for expression nodes whose children may receive per-element algorithm passes (derive, simplify, expand, integrate).

An algorithm may distribute over children and rebuild the same shape only when this trait is present and the distribution is mathematically valid for all such algorithms. Linear containers qualify; product-like nodes that require product rules must not be marked. Lives in core so domain packages can opt in without creating a cross-domain import.

Attributes

Supertypes
trait _Expression
class Object
trait Matchable
class Any
Known subtypes
class _Equation
class MatSum
class Transpose
class _Matrix
trait _Expression

Base trait of every expression tree node, shared across all domains.

Base trait of every expression tree node, shared across all domains.

The dual-evaluation contract: eval returns Right when the expression fully reduces to a concrete _Value; Left when some variable is still free and the node is returned in its most-reduced symbolic form.

children / rebuild enable generic structural traversal so algorithms that visit every node type (Substitute, Analysis, …) do not need to match each case explicitly. Binder positions (e.g. the differentiation variable) are excluded from children; rebuild carries them through unchanged.

freeVars is cached per instance — computed once from children, then O(1). New node types inherit a correct default automatically.

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes
class _Feedback
class _Parallel
class _Series
trait _ElementWise
class _Equation
class MatSum
class Transpose
class _Matrix
class _Heaviside
trait _Value
class _Based
class _Bool
class _Complex
class _MatrixValue
class _Number
class _Rational
class _Truth
class _Variable
class _Domain
class _Comparison
class _Solve
class _SolveSystem
trait _Connective
class And
class Implies
class Not
class Or
class Xor
class _Defuzzify
trait _Membership
class GaussMF
class SigMF
class Somewhat
class TrapMF
class TriMF
class Very
class _TruthOf
class Determinant
class _MatrixIndex
class Inverse
class MatProduct
class MatScale
class ZeroMatrix
class _Expectation
class _Variance
class Acos
class Acosh
class Asin
class Asinh
class Atan
class Atanh
class Beta
class BetaI
class Binom
class Catalan
class Ci
class Cos
class Cosh
class Cot
class Coth
class Csc
class Csch
class Digamma
class Ei
class Erf
class Erfc
class Exp
class Factorial
class FresnelC
class FresnelS
class Gamma
class GammaP
class GammaQ
class Harmonic
class Li
class Ln
class LogBase
class LogGamma
class Sec
class Sech
class Si
class Sin
class Sinh
class Tanh
class Tg
class _Balanced
class _Function
class _Functional
class _ODE
class _DefIntegral
class _Derivative
class _Integral
class _Laurent
class _Limit
class _Pade
class _Tabulate
class _Taylor
class _Fourier
class _Laplace
class _ZTransform
trait _Operation
class Power
class Product
class Ratio
class Sum
class _Pattern
class _Sequence
class _ToBase
class _Regress
class _Statistic
class _Test
class _Curl
class _Div
class _Grad
class _Hessian
class _Jacobian
class _Laplacian
Show all
case class _Heaviside(arg: _Expression) extends _Expression

Heaviside unit step function: step(t) = 1 when t ≥ 0, 0 otherwise.

Heaviside unit step function: step(t) = 1 when t ≥ 0, 0 otherwise.

Evaluates to a concrete _Number when its argument reduces to a _Number; otherwise stays symbolic so transform rules can pattern-match on it. Not _ElementWise: derive / simplify / expand / integrate do not automatically distribute through step.

Value parameters

arg

the argument expression

Attributes

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

Marker trait for a symbolic matrix node whose children are its cells in row-major order and whose rebuild preserves the rows × cols shape.

Marker trait for a symbolic matrix node whose children are its cells in row-major order and whose rebuild preserves the rows × cols shape.

Lets core and scalar algorithms distribute a scalar function element-wise over a symbolic matrix argument (exp(A), sin(A), … over a matrix with free-variable cells) without importing the matrix package — they see only this marker and the generic children / rebuild. Narrower than _ElementWise on purpose: only the matrix literal opts in. The dense counterpart (_MatrixValue) is handled numerically and is not marked.

Attributes

Supertypes
trait _Expression
class Object
trait Matchable
class Any
Known subtypes
class _Matrix
object _MatrixValue

Companion for the dense concrete matrix value _MatrixValue.

Companion for the dense concrete matrix value _MatrixValue.

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
final class _MatrixValue extends _Value

Dense, fully-reduced matrix value: a row-major Array[Double], not _Number nodes.

Dense, fully-reduced matrix value: a row-major Array[Double], not _Number nodes.

The concrete counterpart of the symbolic matrix node (matrix._Matrix), exactly as _Number is the concrete counterpart of a scalar expression. Lives in core so Environment can bind matrix values without core depending on any domain.

Immutability: the public factory (_MatrixValue$.apply) takes a defensive copy and the storage is private — kernels build results on arrays they own, skipping the copy.

multiply parallelises over independent row blocks above a work-volume threshold; writes are disjoint and require no locking.

Value parameters

cols

number of columns; must be positive

rows

number of rows; must be positive

Attributes

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

Companion for the concrete real scalar value _Number.

Companion for the concrete real scalar value _Number.

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
_Number.type
case class _Number(d: Double) extends _Value

Concrete real scalar value.

Concrete real scalar value.

Rounding is a display concern only: toString and display round for output; eval propagates the stored Double as-is. ±∞ serialises as "inf" / "-inf" for round-trip safety through the parser.

Value parameters

d

the exact double-precision value

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
trait _Value
trait _Expression
class Object
trait Matchable
class Any
Show all
object _Rational

Companion for _Rational: smart factories, constants, and the shared reduction step.

Companion for _Rational: smart factories, constants, and the shared reduction step.

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
_Rational.type
final class _Rational extends _Value, Ordered[_Rational]

An exact rational number, held as an unreduced BigInt pair with the sign in the numerator and a strictly positive denominator.

An exact rational number, held as an unreduced BigInt pair with the sign in the numerator and a strictly positive denominator.

A sibling _Value of _Number, exactly as _Complex and _Truth are — _Number is not replaced and not widened, so every existing _Number(x) pattern match keeps firing unchanged and the Double path stays byte-identical. A _Rational only ever enters an expression through the parser's exact mode; see Environment for the working precision that bounds it.

The promotion lattice (the Int → Double analogy logic.asTruth already uses): rational ⊕ rational is exact, rational ⊕ number is a _Numberfloat contagion. Contagion is deliberate and it is the asymmetric choice: absorbing the _Number into the rational would be perfectly lossless, since every finite Double is a dyadic rational, but it would launder representation error into an exact-looking value. Once a value is inexact it should stay visibly inexact. The contagion needs no code of its own: _Complex.parts reads a _Rational as a Double, so the existing complex kernels produce it for free.

Why a private BigInt pair rather than spire's Rational. spire normalises to lowest terms on every construction, which would have settled GcdPolicy at GcdPolicy.Eager by fiat and made the gcd-policy benchmark unrunnable. spire remains the intended engine for irrationals (Real / Algebraic); the rational representation stays in-house.

Equality is by value, not by representation: 2/6 == 1/3 holds under every policy. hashCode therefore has to reduce, which means a GcdPolicy.Lazy rational used as a hash key pays the gcd it was avoiding.

Value parameters

den

the denominator, always strictly positive

num

the numerator, carrying the sign

Attributes

Companion
object
Supertypes
trait Ordered[_Rational]
trait Comparable[_Rational]
trait _Value
trait _Expression
class Object
trait Matchable
class Any
Show all
object _Truth

Companion for the graded truth value _Truth; holds the smart factory and the named unknown constant.

Companion for the graded truth value _Truth; holds the smart factory and the named unknown constant.

_Truth.of collapses the crisp endpoints 0.0 / 1.0 back to _Bool(false) / _Bool(true), so every existing _Bool(b) pattern match across the codebase keeps firing on crisp results — only a genuinely intermediate degree ever becomes a _Truth. This mirrors _Complex.of's collapse of a zero imaginary part back to _Number.

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
_Truth.type
case class _Truth extends _Value

A graded truth value: a degree strictly between false and true.

A graded truth value: a degree strictly between false and true.

The degree is never exactly 0.0 or 1.0 — the _Truth.of factory guarantees this invariant by collapsing to _Bool in those cases, so _Truth and _Bool together carry the full [0, 1] interval without overlapping. Construction through of is the only public route; pattern matching _Truth(d) remains available.

In three-valued (Kleene) logic the only reachable degree is 0.5, the named constant _Truth.Unknown, which prints and parses as unknown. The [0, 1] carrier is already in place for the fuzzy tier; degrees other than 0.5 print as plain numbers and do not round-trip through the parser yet (there is no grammar literal for them).

Like _Number, a _Truth never rounds in eval; rounding is a display concern handled by toString / display(p).

Value parameters

d

the truth degree, guaranteed to lie strictly inside (0, 1) by _Truth.of

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
trait _Value
trait _Expression
class Object
trait Matchable
class Any
Show all
trait _Value extends _Expression

Marker trait for a fully-reduced, concrete result — a number, a matrix, or a boolean — as opposed to a symbolic atom (free variable) that is not yet concrete.

Marker trait for a fully-reduced, concrete result — a number, a matrix, or a boolean — as opposed to a symbolic atom (free variable) that is not yet concrete.

Attributes

Supertypes
trait _Expression
class Object
trait Matchable
class Any
Known subtypes
class _Based
class _Bool
class _Complex
class _MatrixValue
class _Number
class _Rational
class _Truth
Show all
case class _Variable(variable: String) extends _Expression

A free variable: a symbolic atom that is not yet a concrete value.

A free variable: a symbolic atom that is not yet a concrete value.

eval returns Right only when variable is bound to a _Value in the environment; otherwise it stays Left(this).

Value parameters

variable

the variable name

Attributes

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

Value members

Concrete methods

def eAt(digits: Int): Option[_Rational]

e as an exact rational at digits decimal places.

e as an exact rational at digits decimal places.

Value parameters

digits

the working precision in decimal digits

Attributes

def exactPow(b: _Rational, e: _Rational, digits: Int): Option[_Rational]

A positive rational raised to a rational power, at digits decimal places.

A positive rational raised to a rational power, at digits decimal places.

b^e = exp(e · ln b) in spire's terms; the base must be positive, since a negative one with a fractional exponent is the complex case and belongs to the caller's fallback.

Value parameters

b

the base, which must be strictly positive

digits

the working precision in decimal digits

e

the exponent

Attributes

Returns

the power at that precision, or None when the base is not positive

def exactSqrt(r: _Rational, digits: Int): Option[_Rational]

The square root of a non-negative rational, at digits decimal places.

The square root of a non-negative rational, at digits decimal places.

Uses Real rather than spire's Algebraic, although an earlier plan suggested the latter for polynomial roots. Algebraic's advantage is a decidable sign and comparison — and the only place this is used, the quadratic solver, has already established the sign by comparing the discriminant against zero in exact rational arithmetic, so there is nothing left for that decidability to buy.

Value parameters

digits

the working precision in decimal digits

r

the radicand, which must be non-negative

Attributes

Returns

the root at that precision, or None for a negative radicand

def fromReal(x: Real, digits: Int): Option[_Rational]

Materialises a Real back into the exact tier at digits decimal places.

Materialises a Real back into the exact tier at digits decimal places.

Computes at digits +GuardDigits and then takes the best rational with denominator at most 10^digits, which is the same bounded-denominator step the rest of the tier uses — so the result carries the working precision and no more, and its operands stay small.

Value parameters

digits

the working precision in decimal digits

x

the computed value

Attributes

Returns

the exact approximation, or None if the value could not be evaluated

def piAt(digits: Int): Option[_Rational]

pi as an exact rational at digits decimal places.

pi as an exact rational at digits decimal places.

Genuinely digits correct places, where tier 1 could offer at most fifteen: the constant is now computed rather than read off a Double.

Value parameters

digits

the working precision in decimal digits

Attributes

def toReal(r: _Rational): Real

Lifts an exact rational into spire's Real, losing nothing — Real holds a rational exactly.

Lifts an exact rational into spire's Real, losing nothing — Real holds a rational exactly.

Value parameters

r

the exact value

Attributes

Returns

the same value as a Real

Concrete fields

val GuardDigits: Int

Extra digits computed and then discarded, to absorb the rounding of the final re-approximation and of the arithmetic immediately around it.

Extra digits computed and then discarded, to absorb the rounding of the final re-approximation and of the arithmetic immediately around it.

Without guard digits the last place of a result would be unreliable exactly when a user raised the precision to inspect it.

Attributes

Extensions

Extensions

extension (result: Either[_Expression, _Value])

Collapses an eval result back to a plain _Expression. Used when rebuilding a symbolic node from partially-reduced operands.

Collapses an eval result back to a plain _Expression. Used when rebuilding a symbolic node from partially-reduced operands.

Attributes