From 995a0f916be2c4989cc0df1897a71da84ce5c83c Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Fri, 3 Jul 2020 19:20:44 +0700 Subject: [PATCH] Replace x pow 2 with multiplication x by x because of precision --- .../kotlin/scientifik/kmath/operations/Complex.kt | 6 +++--- .../scientifik/kmath/operations/ComplexFieldTest.kt | 11 +++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt index e45dcb1ea..3e1f53ddc 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt @@ -90,8 +90,8 @@ object ComplexField : ExtendedField { return i * (e1 - e2) / (e1 + e2) } - override fun asin(arg: Complex): Complex = -i * ln(sqrt(1 - (arg pow 2)) + i * arg) - override fun acos(arg: Complex): Complex = PI_DIV_2 + i * ln(sqrt(1 - (arg pow 2)) + i * arg) + override fun asin(arg: Complex): Complex = -i * ln(sqrt(1 - (arg * arg)) + i * arg) + override fun acos(arg: Complex): Complex = PI_DIV_2 + i * ln(sqrt(1 - (arg * arg)) + i * arg) override fun atan(arg: Complex): Complex { val iArg = i * arg @@ -101,7 +101,7 @@ object ComplexField : ExtendedField { override fun sinh(arg: Complex): Complex = (exp(arg) - exp(-arg)) / 2 override fun cosh(arg: Complex): Complex = (exp(arg) + exp(-arg)) / 2 override fun tanh(arg: Complex): Complex = (exp(arg) - exp(-arg)) / (exp(-arg) + exp(arg)) - override fun asinh(arg: Complex): Complex = ln(sqrt((arg pow 2) + 1) + arg) + override fun asinh(arg: Complex): Complex = ln(sqrt(arg * arg + 1) + arg) override fun acosh(arg: Complex): Complex = ln(arg + sqrt((arg - 1) * (arg + 1))) override fun atanh(arg: Complex): Complex = (ln(arg + 1) - ln(1 - arg)) / 2 diff --git a/kmath-core/src/commonTest/kotlin/scientifik/kmath/operations/ComplexFieldTest.kt b/kmath-core/src/commonTest/kotlin/scientifik/kmath/operations/ComplexFieldTest.kt index 2986efd5f..1091a8de1 100644 --- a/kmath-core/src/commonTest/kotlin/scientifik/kmath/operations/ComplexFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/scientifik/kmath/operations/ComplexFieldTest.kt @@ -1,8 +1,9 @@ package scientifik.kmath.operations -import kotlin.math.PI +import kotlin.math.* import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertTrue internal class ComplexFieldTest { @Test @@ -44,16 +45,14 @@ internal class ComplexFieldTest { @Test fun testInverseSine() { assertEquals(Complex(0, -0.0), ComplexField { asin(zero) }) - - assertEquals(ComplexField { i * asinh(one) }.let { it.im.toInt() to it.re.toInt() }, - ComplexField { asin(i) }.let { it.im.toInt() to it.re.toInt() }) + assertTrue(abs(ComplexField { i * asinh(one) }.r - ComplexField { asin(i) }.r) < 0.000000000000001) } @Test fun testInverseHyperbolicSine() { assertEquals( - ComplexField { i * PI.toComplex() / 2 }.let { it.im.toInt() to it.re.toInt() }, - ComplexField { asinh(i) }.let { it.im.toInt() to it.re.toInt() }) + ComplexField { i * PI.toComplex() / 2 }, + ComplexField { asinh(i) }) } @Test