fuookami.ospf.kotlin.utils.math.value_range.Bound.kt Maven / Gradle / Ivy
package fuookami.ospf.kotlin.utils.math.value_range
import fuookami.ospf.kotlin.utils.concept.*
import fuookami.ospf.kotlin.utils.math.*
import fuookami.ospf.kotlin.utils.operator.*
class Bound(
val value: ValueWrapper,
interval: Interval
) : Cloneable, Copyable>, Ord>, Eq>,
Plus, Bound>, Minus, Bound>,
Times, Bound>, Div, Bound>
where T : RealNumber, T : NumberField
{
val interval: Interval = if (value.isInfinityOrNegativeInfinity) {
Interval.Open
} else {
interval
}
override fun copy(): Bound {
return Bound(value.copy(), interval)
}
fun eq(rhs: T): Boolean = value eq rhs && interval == Interval.Closed
override fun partialEq(rhs: Bound): Boolean? = (value partialEq rhs.value)?.let {
it && interval == rhs.interval
}
override fun partialOrd(rhs: Bound): Order? = when (val result = value partialOrd rhs.value) {
Order.Equal -> {
if (interval outer rhs.interval) {
Order.Less()
} else {
Order.Greater()
}
}
else -> {
result
}
}
operator fun plus(rhs: T): Bound = Bound(value + rhs, interval)
override fun plus(rhs: Bound): Bound = Bound(value + rhs.value, interval intersect rhs.interval)
operator fun minus(rhs: T): Bound = Bound(value - rhs, interval)
override fun minus(rhs: Bound): Bound = Bound(value - rhs.value, interval intersect rhs.interval)
operator fun times(rhs: T): Bound = Bound(value * rhs, interval)
override fun times(rhs: Bound): Bound = Bound(value * rhs.value, interval intersect rhs.interval)
operator fun div(rhs: T): Bound = Bound(value / rhs, interval)
override fun div(rhs: Bound): Bound = Bound(value / rhs.value, interval intersect rhs.interval)
fun toFlt64(): Bound = Bound(ValueWrapper(value.toFlt64()).value!!, interval)
override fun toString(): String {
return "Bound($value, $interval)"
}
}
@JvmName("negBoundFlt32")
operator fun Bound.unaryMinus() = Bound(-value, interval)
@JvmName("negBoundFlt64")
operator fun Bound.unaryMinus() = Bound(-value, interval)
@JvmName("negBoundFltX")
operator fun Bound.unaryMinus() = Bound(-value, interval)
@JvmName("negBoundInt8")
operator fun Bound.unaryMinus() = Bound(-value, interval)
@JvmName("negBoundInt16")
operator fun Bound.unaryMinus() = Bound(-value, interval)
@JvmName("negBoundInt32")
operator fun Bound.unaryMinus() = Bound(-value, interval)
@JvmName("negBoundInt64")
operator fun Bound.unaryMinus() = Bound(-value, interval)
@JvmName("negBoundIntX")
operator fun Bound.unaryMinus() = Bound(-value, interval)