Rational

com.phasmidsoftware.number.core.inner.Rational
See theRational companion class
object Rational

Companion object to Rational. Represents a Rational number, with various methods for conversion, approximation, and utility. Provides a rich set of operations and utilities to handle rational numbers, convert data types, and manage normalization. Rational numbers are represented as fractions with BigInt numerator and denominator.

Fields:

  • bigZero: Commonly used representation for Rational zero.
  • bigOne: Commonly used representation for Rational one.
  • bigTwo: Commonly used representation for Rational two.
  • bigThree: Commonly used representation for Rational three.
  • bigFour: Commonly used representation for Rational four.
  • bigFive: Commonly used representation for Rational five.
  • bigSeven: Commonly used representation for Rational seven.
  • bigNegOne: Commonly used representation for Rational negative one.
  • bigTen: Commonly used representation for Rational ten.
  • zero: Rational representation for zero.
  • infinity: Positive infinity in Rational form.
  • negInfinity: Negative infinity in Rational form.
  • one: Rational representation for one.
  • ten: Rational representation for ten.
  • two: Rational representation for two.
  • half: Rational representation for one-half.
  • NaN: Represents "Not a Number" in Rational form.
  • negZero: Represents negative zero in Rational form.
  • negZeroDouble: Represents negative zero as a Double in Rational form.
  • pi_5000: Predefined Rational representation of π/5000.
  • squareRoots: A collection of predefined square root values in Rational form.

Attributes

Companion
class
Graph
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
Rational.type

Members list

Type members

Classlikes

Implicit object which bestows all the Ordering, Numeric, etc. functionality to a Rational.

Implicit object which bestows all the Ordering, Numeric, etc. functionality to a Rational.

Attributes

Supertypes
trait Fractional[Rational]
trait Numeric[Rational]
trait Ordering[Rational]
trait PartialOrdering[Rational]
trait Equiv[Rational]
trait Serializable
trait Comparator[Rational]
class Object
trait Matchable
class Any
Show all
Self type
final implicit class RationalHelper(val sc: StringContext) extends AnyVal

Implicit class RationalHelper to allow definition of Rationals by Strings of the form r"n/d".

Implicit class RationalHelper to allow definition of Rationals by Strings of the form r"n/d".

Value parameters

sc

a StringContext.

Attributes

Supertypes
class AnyVal
trait Matchable
class Any
trait RationalIsFractional extends RationalIsNumeric, Fractional[Rational]

Trait defined to support the methods of Fractional[Rational].

Trait defined to support the methods of Fractional[Rational].

Attributes

Supertypes
trait Fractional[Rational]
trait Numeric[Rational]
trait Ordering[Rational]
trait PartialOrdering[Rational]
trait Equiv[Rational]
trait Serializable
trait Comparator[Rational]
class Object
trait Matchable
class Any
Show all
Known subtypes

Trait defined to support the methods of Numeric[Rational].

Trait defined to support the methods of Numeric[Rational].

Attributes

Supertypes
trait Numeric[Rational]
trait Ordering[Rational]
trait PartialOrdering[Rational]
trait Equiv[Rational]
trait Serializable
trait Comparator[Rational]
class Object
trait Matchable
class Any
Show all
Known subtypes
trait RationalIsOrdering extends Ordering[Rational]

Trait defined to support the methods of Ordering[Rational].

Trait defined to support the methods of Ordering[Rational].

Attributes

Supertypes
trait Ordering[Rational]
trait PartialOrdering[Rational]
trait Equiv[Rational]
trait Serializable
trait Comparator[Rational]
class Object
trait Matchable
class Any
Show all
Known subtypes
implicit class RationalOps(x: Int)

Implicit class RationalOps to allow an Int to be treated as a Rational for the purpose of allowing operations with right-hand Rational arguments or for division by an Int. A particular case is rational division where we must convert the numerator to Rational so that we don't end up with 0 as the result of an Int division.

Implicit class RationalOps to allow an Int to be treated as a Rational for the purpose of allowing operations with right-hand Rational arguments or for division by an Int. A particular case is rational division where we must convert the numerator to Rational so that we don't end up with 0 as the result of an Int division.

Value parameters

x

an Int

Attributes

Supertypes
class Object
trait Matchable
class Any

Inherited and Abstract types

type MirroredElemLabels <: Tuple

The names of the product elements.

The names of the product elements.

Attributes

Inherited from:
Mirror
type MirroredLabel <: String

The name of the type.

The name of the type.

Attributes

Inherited from:
Mirror

Value members

Concrete methods

def apply(n: BigInt, d: BigInt): Rational

Method to construct a Rational from two BigInt values. NOTE: the this method ensures that the numerator and denominator are normalized.

Method to construct a Rational from two BigInt values. NOTE: the this method ensures that the numerator and denominator are normalized.

Value parameters

d

the denominator.

n

the numerator.

Attributes

Returns

a Rational with the same ratio as n/d.

def apply(n: BigInt, d: Long): Rational

Method to construct a Rational from a BigInt numerator and a Long denominator. NOTE: the this method ensures that the numerator and denominator are normalized.

Method to construct a Rational from a BigInt numerator and a Long denominator. NOTE: the this method ensures that the numerator and denominator are normalized.

Value parameters

d

the denominator.

n

the numerator.

Attributes

Returns

a Rational with the same ratio as n/d.

def apply(n: Long, d: Long): Rational

Creates a Rational number from the given numerator and denominator values.

Creates a Rational number from the given numerator and denominator values.

Value parameters

d

the denominator of the rational number

n

the numerator of the rational number

Attributes

Returns

a Rational number representing the fraction n/d

def apply(n: BigInt): Rational

Method to construct a Rational from a BigInt.

Method to construct a Rational from a BigInt.

Value parameters

n

the value.

Attributes

Returns

a Rational with the same value as n.

def apply(n: BigInt, negative: Boolean): Rational

Method to construct a Rational from a BigInt with sign defined by "negative".

Method to construct a Rational from a BigInt with sign defined by "negative".

Value parameters

n

the value.

negative

if true then the sign of the result will be flipped.

Attributes

Returns

a Rational with the same value as n.

def apply(n: Long): Rational

Method to construct a Rational from a Long.

Method to construct a Rational from a Long.

Value parameters

n

the value.

Attributes

Returns

a Rational with the same value as n.

def apply(n: Int): Rational

Method to construct a Rational from a Long.

Method to construct a Rational from a Long.

Value parameters

n

the value.

Attributes

Returns

a Rational with the same value as n.

def apply(x: BigDecimal): Rational

Method to construct a Rational based on a BigDecimal.

Method to construct a Rational based on a BigDecimal.

Value parameters

x

the BigDecimal to convert.

Attributes

Returns

a Rational which is equal to x.

Note

Throws RationalException if x cannot be represented as a Rational.

def apply(w: String): Rational

Method to construct a Rational based on a String. NOTE: this method is NOT safe. It is much better to invoke parse(w).

Method to construct a Rational based on a String. NOTE: this method is NOT safe. It is much better to invoke parse(w).

Value parameters

w

the String value.

Attributes

Returns

a Rational corresponding to the value given by w.

Note

Throws RationalParserException if w is malformed.

def apply(t: (Int, Int)): Rational

Creates a new Rational object by applying the given tuple consisting of numerator and denominator.

Creates a new Rational object by applying the given tuple consisting of numerator and denominator.

Value parameters

t

a tuple where the first element is the numerator and the second element is the denominator

Attributes

Returns

a new Rational object constructed from the provided numerator and denominator

def approximate(x: Double)(implicit epsilon: Tolerance): Rational

Method to take a Double in the range 0 thru 1 and approximate it by a Rational number to within the given tolerance (epsilon).

Method to take a Double in the range 0 thru 1 and approximate it by a Rational number to within the given tolerance (epsilon).

NOTE: this Farey sequence converges very slowly. In order to put an absolute limit on the recursion (which otherwise might never terminate), we limit the size of the denominator of r1 to be 10E12. It shouldn't matter much whether we test r1 or r2 in this context.

Value parameters

epsilon

(implicit) the tolerance.

x

the value to approximate (should be between 0 and 1).

Attributes

Returns

a Rational such that the difference between the result and x is less than epsilon.

Note

Throws java.lang.IllegalArgumentException if x is not between 0 and 1.

def approximateAny(x: Double)(implicit epsilon: Tolerance): Rational

Method to approximate an irrational number as a Rational. Application code should always call this method or, preferably, Rational.apply(Double).

Method to approximate an irrational number as a Rational. Application code should always call this method or, preferably, Rational.apply(Double).

NOTE: this method is designed for true Doubles, not floating-point representations of decimal numbers. Such decimal numbers should be converted to BigDecimal first using BigDecimal.valueOf(x).

CONSIDER what we need here is to pass in a function and a desired Rational result, and perhaps even the first derivative of the function, so that we can approximate the solution (root) of the function.

Value parameters

epsilon

(implicit) the tolerance.

x

the value to approximate.

Attributes

Returns

a Rational such that the difference between the result and x is less than epsilon.

def convertDouble(x: Double): Rational

converter from Double to Rational.

converter from Double to Rational.

Value parameters

x

the value.

Attributes

Returns

a Rational equal to or approximately equal to x.

Note

Throws java.util.NoSuchElementException because we invoke get on an Option[Rational].

def createExact(x: Double): Try[Rational]

Creates an exact Rational representation of the given Double value if possible.

Creates an exact Rational representation of the given Double value if possible.

Value parameters

x

the Double value to be converted to an exact Rational

Attributes

Returns

a Try containing the exact Rational if the conversion is possible and exact, or a Failure containing an ArithmeticException if the conversion is not possible or loses precision

def exponent(x: Int): Rational

Method to yield a Rational exponent (in the sense of the a literal Double: for example 1.0Ex).

Method to yield a Rational exponent (in the sense of the a literal Double: for example 1.0Ex).

Value parameters

x

the power to which we should raise 10.

Attributes

Returns

10 raised to the power x, expressed as a Rational.

def findRepeatingSequence(n: BigInt, d: BigInt, primeFactors: Seq[Prime]): Try[String]

Generate the String representation of n/d where d is a Prime number (at least, very probably).

Generate the String representation of n/d where d is a Prime number (at least, very probably).

Value parameters

d

the (prime) denominator.

n

the numerator.

Attributes

Returns

a String of repeated digits.

def hasCorrectRatio(r: Rational, top: BigInt, bottom: BigInt): Boolean
def narrow(x: BigInt, min: BigInt, max: BigInt): Try[BigInt]

Narrows a given BigInt value to ensure it falls within a specified range. If the value is outside the range, a Failure is returned with a RationalException.

Narrows a given BigInt value to ensure it falls within a specified range. If the value is outside the range, a Failure is returned with a RationalException.

Value parameters

max

the maximum allowed value (inclusive)

min

the minimum allowed value (inclusive)

x

the BigInt value to be narrowed

Attributes

Returns

a Success containing the value if it falls within the range, or a Failure with a RationalException in case of precision loss

def parse(w: String): Try[Rational]

Method to construct a Try[Rational] based on a String.

Method to construct a Try[Rational] based on a String.

Value parameters

w

the String value.

Attributes

Returns

either Success(Rational) with value corresponding to the value given by w or Failure(RationalParserException) if w is malformed.

def rootOfBigInt(b: BigInt, x: Int): Option[BigInt]

Method to get the (integral) xth root of b.

Method to get the (integral) xth root of b.

Value parameters

b

a BigInt.

x

an Int.

Attributes

Returns

an optional BigInt which, when raised to the power of x, equals b.

def toInt(x: Rational): Try[Int]

This method gets an Int from a Rational.

This method gets an Int from a Rational.

XXX: Needs to be public for testing

Value parameters

x

a Rational.

Attributes

Returns

an Try[Int]

def toIntOption(x: Rational): Option[Int]

This method gets an optional Int from a Rational.

This method gets an optional Int from a Rational.

Value parameters

x

a Rational.

Attributes

Returns

an Option[Int]

def toInts(x: Rational): Option[(Int, Int)]

Get Rational x as a pair of Ints (if possible). The result will be at full precision or an exception will be thrown.

Get Rational x as a pair of Ints (if possible). The result will be at full precision or an exception will be thrown.

Value parameters

x

a Rational.

Attributes

Returns

an optional tuple of Ints.

def toLong(x: Rational): Try[Long]

Attempts to convert a Rational value to a Long within the range of Long values.

Attempts to convert a Rational value to a Long within the range of Long values.

Value parameters

x

the Rational value to convert to a Long

Attributes

Returns

a Try containing the Long value if the conversion is successful, or a Failure if the value is out of range or cannot be converted

def toLongs(x: Rational): Option[(Long, Long)]

Get Rational x as a pair of Longs (if possible). The result will be at full precision or an exception will be thrown.

Get Rational x as a pair of Longs (if possible). The result will be at full precision or an exception will be thrown.

Value parameters

x

a Rational.

Attributes

Returns

an optional tuple of Longs.

Concrete fields

val NaN: Rational

Represents a Not-a-Number (NaN) value as a rational number. It is created by initializing a Rational object with both numerator and denominator as zero. This is a special case and does not represent a valid rational number or a real number.

Represents a Not-a-Number (NaN) value as a rational number. It is created by initializing a Rational object with both numerator and denominator as zero. This is a special case and does not represent a valid rational number or a real number.

Attributes

lazy val bigOne: BigInt

A constant value representing the big integer 1.

A constant value representing the big integer 1.

Attributes

lazy val binaryLogs: Map[Int, Int]

A lazily evaluated map that provides the base-2 logarithms of specific powers of 2. The keys represent powers of 2 (e.g., 1, 2, 4, 8, etc.), and the values are their corresponding base-2 logarithms.

A lazily evaluated map that provides the base-2 logarithms of specific powers of 2. The keys represent powers of 2 (e.g., 1, 2, 4, 8, etc.), and the values are their corresponding base-2 logarithms.

Attributes

lazy val cubeRoots: Map[Int, Int]

A lazy value representing a map of predefined integers and their corresponding cube root values.

A lazy value representing a map of predefined integers and their corresponding cube root values.

This map contains key-value pairs where the key represents a non-negative integer and the value is its integer cube root. The cube root values are exact for perfect cubes included in the map.

Attributes

Represents the reciprocal (inverted form) of the five Rational number.

Represents the reciprocal (inverted form) of the five Rational number.

Attributes

val five: Rational

Represents a Rational number with a value equivalent to five. The value is constructed using an underlying representation of bigFive.

Represents a Rational number with a value equivalent to five. The value is constructed using an underlying representation of bigFive.

Attributes

val four: Rational

A constant value representing the rational number 4. This is created using the Rational class.

A constant value representing the rational number 4. This is created using the Rational class.

Attributes

val half: Rational

A Rational number representing the reciprocal of two. The invert method on the two object is used to calculate this value.

A Rational number representing the reciprocal of two. The invert method on the two object is used to calculate this value.

Attributes

Represents a rational number with an infinite value, defined as the inversion of the zero rational number.

Represents a rational number with an infinite value, defined as the inversion of the zero rational number.

Attributes

val integerForm: Regex

Regular expression pattern to match an integer in string form, which can be positive or negative. The pattern ensures that the entire string represents a valid integer.

Regular expression pattern to match an integer in string form, which can be positive or negative. The pattern ensures that the entire string represents a valid integer.

Examples of matching strings:

  • "123"
  • "-456"
  • "0"

Examples of non-matching strings:

  • "12.3" (not an integer)
  • "abc" (not numeric)
  • "123abc" (contains invalid characters)

Attributes

val maxSquare: Int

A constant value representing the maximum square value that is encoded in squareRoots (above).

A constant value representing the maximum square value that is encoded in squareRoots (above).

Attributes

lazy val negInfinity: Rational

Represents the negation of the Rational value one. The result is a Rational object equivalent to -1.

Represents the negation of the Rational value one. The result is a Rational object equivalent to -1.

Attributes

lazy val negZero: Rational

Represents a Rational number with a numerator of 0 and a denominator of -1. Although mathematically this simplifies to zero, the negative denominator is retained in this instance. This is the only instance in the domain of Rational with a negative denominator.

Represents a Rational number with a numerator of 0 and a denominator of -1. Although mathematically this simplifies to zero, the negative denominator is retained in this instance. This is the only instance in the domain of Rational with a negative denominator.

Attributes

lazy val negZeroDouble: Double
val nine: Rational

Represents a rational number instance initialized with the value 9.

Represents a rational number instance initialized with the value 9.

Attributes

val one: Rational

A constant value representing the rational number equivalent to 1. It is defined as an instance of the Rational class initialized with the value of bigOne.

A constant value representing the rational number equivalent to 1. It is defined as an instance of the Rational class initialized with the value of bigOne.

Attributes

Represents a rational number equivalent to one-fourth by inverting the value of four.

Represents a rational number equivalent to one-fourth by inverting the value of four.

This val performs the inversion of a predefined rational number four, resulting in 1/4 or a quarter of a unit.

Attributes

val rationalForm: Regex
val repeatingDecimals: Regex
lazy val squareRoots: Map[Int, Int]

A lazy value representing a map of predefined integers and their corresponding square root values.

A lazy value representing a map of predefined integers and their corresponding square root values.

This map contains key-value pairs where the key represents a non-negative integer and the value is its integer square root. The square root values are exact for perfect squares included in the map.

Attributes

val ten: Rational

Represents the rational number 10 as an instance of the Rational type. Utilizes the predefined value bigTen to create the Rational instance.

Represents the rational number 10 as an instance of the Rational type. Utilizes the predefined value bigTen to create the Rational instance.

Attributes

A Rational number representing the reciprocal of three. The invert method on the three object is used to calculate this value.

A Rational number representing the reciprocal of three. The invert method on the three object is used to calculate this value.

Attributes

Represents the rational number three as a constant of type Rational. It is constructed using Rational(bigThree).

Represents the rational number three as a constant of type Rational. It is constructed using Rational(bigThree).

Attributes

lazy val two: Rational

Represents the rational number two as a constant of type Rational. It is constructed using Rational(bigTwo), where bigTwo presumably signifies the numerical value 2 in a predefined or imported context.

Represents the rational number two as a constant of type Rational. It is constructed using Rational(bigTwo), where bigTwo presumably signifies the numerical value 2 in a predefined or imported context.

Attributes

val unicodeForm: Regex

A regular expression that matches a single Unicode character belonging to the specified ranges:

A regular expression that matches a single Unicode character belonging to the specified ranges:

  • Superscript digits (⁰)
  • Subscript digits (₉)
  • Vulgar fractions (½ to ⅞)

Attributes

val zero: Rational

A predefined instance of the Rational class representing the value zero. This constant can be used as a default or as a comparison operand in operations involving rational numbers.

A predefined instance of the Rational class representing the value zero. This constant can be used as a default or as a comparison operand in operations involving rational numbers.

Attributes

Implicits

Implicits

final implicit def RationalHelper(sc: StringContext): RationalHelper

Implicit class RationalHelper to allow definition of Rationals by Strings of the form r"n/d".

Implicit class RationalHelper to allow definition of Rationals by Strings of the form r"n/d".

Value parameters

sc

a StringContext.

Attributes

final implicit def RationalOps(x: Int): RationalOps

Implicit class RationalOps to allow an Int to be treated as a Rational for the purpose of allowing operations with right-hand Rational arguments or for division by an Int. A particular case is rational division where we must convert the numerator to Rational so that we don't end up with 0 as the result of an Int division.

Implicit class RationalOps to allow an Int to be treated as a Rational for the purpose of allowing operations with right-hand Rational arguments or for division by an Int. A particular case is rational division where we must convert the numerator to Rational so that we don't end up with 0 as the result of an Int division.

Value parameters

x

an Int

Attributes

implicit def convertBigInt(x: BigInt): Rational

Implicit converter from BigInt to Rational.

Implicit converter from BigInt to Rational.

Value parameters

x

the value.

Attributes

Returns

a Rational equal to x.

implicit def convertLong(x: Long): Rational

Implicit converter from Long to Rational.

Implicit converter from Long to Rational.

Value parameters

x

the value.

Attributes

Returns

a Rational equal to x.

implicit def convertToNumber(x: Rational): Number