fuookami.ospf.kotlin.utils.math.ordinary.Pow.kt Maven / Gradle / Ivy
package fuookami.ospf.kotlin.utils.math.ordinary
import fuookami.ospf.kotlin.utils.math.*
private tailrec fun > powPosImpl(value: T, base: T, index: Int): T =
if (index == 1) value * base
else powPosImpl(value * base, base, index - 1)
private tailrec fun > powNegImpl(value: T, base: T, index: Int): T =
if (index == -1) value / base
else powNegImpl(value / base, base, index + 1)
@Throws(IllegalArgumentException::class)
fun pow(base: T, index: Int, constants: RealNumberConstants): T where T : TimesSemiGroup, T : RealNumber {
return if (index >= 1) {
powPosImpl(constants.one, base, index)
} else if (index <= -1) {
throw IllegalArgumentException("Invalid argument for negative index exponential function: ${base.javaClass}")
} else {
constants.one
}
}
fun pow(base: T, index: Int, constants: RealNumberConstants): T where T : TimesGroup, T : RealNumber {
return if (index >= 1) {
powPosImpl(constants.one, base, index)
} else if (index <= -1) {
powNegImpl(constants.one, base, index)
} else {
constants.one
}
}
fun > pow(base: T, index: T, constants: FloatingNumberConstants): T {
// todo: use taylor formula to replace it
return base
}