From abb3d9fe5024f31d72354a38efeb3582b9357f46 Mon Sep 17 00:00:00 2001 From: therealansh Date: Thu, 3 Jun 2021 10:17:13 +0530 Subject: [PATCH] add: Double using StrictMath --- .../kscience/kmath/jafama/KMathJafama.kt | 103 +++++++++--------- 1 file changed, 53 insertions(+), 50 deletions(-) diff --git a/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt b/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt index 2557004e9..9d4341036 100644 --- a/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt +++ b/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt @@ -4,56 +4,7 @@ import space.kscience.kmath.operations.* import net.jafama.* /** - * Advanced Number-like semifield that implements basic operations. - */ -public interface ExtendedFieldOperations : - FieldOperations, - TrigonometricOperations, - PowerOperations, - ExponentialOperations { - public override fun tan(arg: T): T = sin(arg) / cos(arg) - public override fun tanh(arg: T): T = sinh(arg) / cosh(arg) - - public override fun unaryOperationFunction(operation: String): (arg: T) -> T = when (operation) { - TrigonometricOperations.COS_OPERATION -> ::cos - TrigonometricOperations.SIN_OPERATION -> ::sin - TrigonometricOperations.TAN_OPERATION -> ::tan - TrigonometricOperations.ACOS_OPERATION -> ::acos - TrigonometricOperations.ASIN_OPERATION -> ::asin - TrigonometricOperations.ATAN_OPERATION -> ::atan - PowerOperations.SQRT_OPERATION -> ::sqrt - ExponentialOperations.EXP_OPERATION -> ::exp - ExponentialOperations.LN_OPERATION -> ::ln - ExponentialOperations.COSH_OPERATION -> ::cosh - ExponentialOperations.SINH_OPERATION -> ::sinh - ExponentialOperations.TANH_OPERATION -> ::tanh - ExponentialOperations.ACOSH_OPERATION -> ::acosh - ExponentialOperations.ASINH_OPERATION -> ::asinh - ExponentialOperations.ATANH_OPERATION -> ::atanh - else -> super.unaryOperationFunction(operation) - } -} - -/** - * Advanced Number-like field that implements basic operations. - */ -public interface ExtendedField : ExtendedFieldOperations, Field, NumericAlgebra, ScaleOperations { - public override fun sinh(arg: T): T = (exp(arg) - exp(-arg)) / 2.0 - public override fun cosh(arg: T): T = (exp(arg) + exp(-arg)) / 2.0 - public override fun tanh(arg: T): T = (exp(arg) - exp(-arg)) / (exp(-arg) + exp(arg)) - public override fun asinh(arg: T): T = ln(sqrt(arg * arg + one) + arg) - public override fun acosh(arg: T): T = ln(arg + sqrt((arg - one) * (arg + one))) - public override fun atanh(arg: T): T = (ln(arg + one) - ln(one - arg)) / 2.0 - - public override fun rightSideNumberOperationFunction(operation: String): (left: T, right: Number) -> T = - when (operation) { - PowerOperations.POW_OPERATION -> ::power - else -> super.rightSideNumberOperationFunction(operation) - } -} - -/** - * A field for [Double] without boxing. Does not produce appropriate field element. + * A field for [Double] (using FastMath) without boxing. Does not produce appropriate field element. */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") public object JafamaDoubleField : ExtendedField, Norm, ScaleOperations { @@ -101,3 +52,55 @@ public object JafamaDoubleField : ExtendedField, Norm, S public override inline fun Double.times(b: Double): Double = this * b public override inline fun Double.div(b: Double): Double = this / b } + +/** + * A field for [Double] (using StrictMath) without boxing. Does not produce appropriate field element. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") +public object StrictJafamaDoubleField : ExtendedField, Norm, ScaleOperations { + public override inline val zero: Double get() = 0.0 + public override inline val one: Double get() = 1.0 + + public override fun number(value: Number): Double = value.toDouble() + + public override fun binaryOperationFunction(operation: String): (left: Double, right: Double) -> Double = + when (operation) { + PowerOperations.POW_OPERATION -> ::power + else -> super.binaryOperationFunction(operation) + } + + public override inline fun add(a: Double, b: Double): Double = a + b + + public override inline fun multiply(a: Double, b: Double): Double = a * b + public override inline fun divide(a: Double, b: Double): Double = a / b + + public override fun scale(a: Double, value: Double): Double = a * value + + public override inline fun sin(arg: Double): Double = StrictFastMath.sin(arg) + public override inline fun cos(arg: Double): Double = StrictFastMath.cos(arg) + public override inline fun tan(arg: Double): Double = StrictFastMath.tan(arg) + public override inline fun acos(arg: Double): Double = StrictFastMath.acos(arg) + public override inline fun asin(arg: Double): Double = StrictFastMath.asin(arg) + public override inline fun atan(arg: Double): Double = StrictFastMath.atan(arg) + + public override inline fun sinh(arg: Double): Double = StrictFastMath.sinh(arg) + public override inline fun cosh(arg: Double): Double = StrictFastMath.cosh(arg) + public override inline fun tanh(arg: Double): Double = StrictFastMath.tanh(arg) + public override inline fun asinh(arg: Double): Double = StrictFastMath.asinh(arg) + public override inline fun acosh(arg: Double): Double = StrictFastMath.acosh(arg) + public override inline fun atanh(arg: Double): Double = StrictFastMath.atanh(arg) + + public override inline fun power(arg: Double, pow: Number): Double = StrictFastMath.pow(arg, pow.toDouble()) + public override inline fun exp(arg: Double): Double = StrictFastMath.exp(arg) + public override inline fun ln(arg: Double): Double = StrictFastMath.log(arg) + + public override inline fun norm(arg: Double): Double = StrictFastMath.abs(arg) + + public override inline fun Double.unaryMinus(): Double = -this + public override inline fun Double.plus(b: Double): Double = this + b + public override inline fun Double.minus(b: Double): Double = this - b + public override inline fun Double.times(b: Double): Double = this * b + public override inline fun Double.div(b: Double): Double = this / b +} + +