All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fuookami.ospf.kotlin.utils.math.Arithmetic.kt Maven / Gradle / Ivy

There is a newer version: 1.0.29
Show newest version
package fuookami.ospf.kotlin.utils.math

import kotlinx.serialization.*
import fuookami.ospf.kotlin.utils.concept.*
import fuookami.ospf.kotlin.utils.operator.*

interface Arithmetic : Copyable, PartialEq {
    val constants: ArithmeticConstants

    infix fun equiv(rhs: Self): Boolean
}

interface ArithmeticConstants {
    val zero: Self
    val one: Self
}

interface Invariant {
    @Suppress("UNCHECKED_CAST")
    fun value(): T = this as T
}

interface Variant {
    fun value(): T? = null
}

interface PlusSemiGroup : Plus, Inc
interface PlusGroup : PlusSemiGroup,
    Neg, Minus, Dec

interface TimesSemiGroup : Times
interface TimesGroup : TimesSemiGroup,
    Reciprocal, Div, IntDiv, Rem

interface NumberRing : PlusGroup, TimesSemiGroup
interface NumberField : NumberRing, TimesGroup

interface Scalar> : Arithmetic,
    PlusSemiGroup, TimesSemiGroup,
    Cross, Abs {
    override infix fun x(rhs: Self) = this * rhs
}

interface RealNumber> : Scalar, Invariant, Ord, Eq,
    Log, FloatingNumber<*>>,
    PowF, FloatingNumber<*>>,
    Exp> {
    override val constants: RealNumberConstants

    fun isInfinity(): Boolean = this == constants.infinity
    fun isNegativeInfinity(): Boolean = this == constants.negativeInfinity

    override infix fun equiv(rhs: Self) = this == rhs

    fun toInt8(): Int8
    fun toInt16(): Int16
    fun toInt32(): Int32
    fun toInt64(): Int64
    fun toIntX(): IntX

    fun toUInt8(): UInt8
    fun toUInt16(): UInt16
    fun toUInt32(): UInt32
    fun toUInt64(): UInt64
    fun toUIntX(): UIntX

    fun toNInt8(): NInt8 = NInt8(toInt8())
    fun toNInt16(): NInt16 = NInt16(toInt16())
    fun toNInt32(): NInt32 = NInt32(toInt32())
    fun toNInt64(): NInt64 = NInt64(toInt64())
    fun toNIntX(): NIntX = NIntX(toIntX())

    fun toNUInt8(): NUInt8 = NUInt8(toUInt8())
    fun toNUInt16(): NUInt16 = NUInt16(toUInt16())
    fun toNUInt32(): NUInt32 = NUInt32(toUInt32())
    fun toNUInt64(): NUInt64 = NUInt64(toUInt64())
    fun toNUIntX(): NUIntX = NUIntX(toUIntX())

    fun toRtn8(): Rtn8 = Rtn8(toInt8(), Int8.one)
    fun toRtn16(): Rtn16 = Rtn16(toInt16(), Int16.one)
    fun toRtn32(): Rtn32 = Rtn32(toInt32(), Int32.one)
    fun toRtn64(): Rtn64 = Rtn64(toInt64(), Int64.one)
    fun toRtnX(): RtnX = RtnX(toIntX(), IntX.one)

    fun toURtn8(): URtn8 = URtn8(toUInt8(), UInt8.one)
    fun toURtn16(): URtn16 = URtn16(toUInt16(), UInt16.one)
    fun toURtn32(): URtn32 = URtn32(toUInt32(), UInt32.one)
    fun toURtn64(): URtn64 = URtn64(toUInt64(), UInt64.one)
    fun toURtnX(): URtnX = URtnX(toUIntX(), UIntX.one)

    fun toFlt32(): Flt32
    fun toFlt64(): Flt64
    fun toFltX(): FltX
}

interface RealNumberConstants> : ArithmeticConstants {
    val two: Self
    val three: Self
    val ten: Self

    val minimum: Self
    val maximum: Self
    val positiveMinimum get() = one

    val decimalDigits: Int? get() = null
    val decimalPrecision: Self get() = zero
    val epsilon: Self get() = zero

    val nan: Self? get() = null
    val infinity: Self? get() = null
    val negativeInfinity: Self? get() = null
}

interface Integer> : RealNumber, RangeTo
interface IntegerNumber> : Integer, NumberField, Pow
interface UIntegerNumber> : Integer, NumberField, Pow

interface RationalNumber, I> : RealNumber, NumberField, Pow
        where I : Integer, I : NumberField

interface FloatingNumber> : RealNumber, NumberField, Pow {
    override val constants: FloatingNumberConstants
}

interface FloatingNumberConstants> : RealNumberConstants {
    override val positiveMinimum: Self get() = epsilon

    val pi: Self
    val e: Self
}

interface NumericIntegerNumber, I : IntegerNumber> : Integer,
    PlusGroup, TimesSemiGroup,
    Reciprocal>, Div>, IntDiv, Rem,
    Pow>

interface NumericUIntegerNumber, I : UIntegerNumber> : Integer,
    PlusSemiGroup, TimesSemiGroup,
    Dec, Neg>, Minus>,
    Reciprocal>, Div>, IntDiv, Rem,
    Pow>
typealias NaturalNumber = NumericUIntegerNumber

data object Infinity {
    override fun toString() = "inf"
}

data object NegativeInfinity {
    override fun toString() = "-inf"
}

val  Collection.usize get() = UInt64(size)
val  Map.usize get() = UInt64(size)
operator fun  List.get(index: UInt32) = get(index.toInt())
operator fun  MutableList.set(index: UInt32, element: T) = set(index.toInt(), element)
operator fun  List.get(index: UInt64) = get(index.toInt())
operator fun  MutableList.set(index: UInt64, element: T) = set(index.toInt(), element)