Replace x pow 2 with multiplication x by x because of precision

This commit is contained in:
Iaroslav 2020-07-03 19:20:44 +07:00
parent 77625cca2b
commit 995a0f916b
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
2 changed files with 8 additions and 9 deletions

View File

@ -90,8 +90,8 @@ object ComplexField : ExtendedField<Complex> {
return i * (e1 - e2) / (e1 + e2) return i * (e1 - e2) / (e1 + e2)
} }
override fun asin(arg: Complex): Complex = -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 pow 2)) + 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 { override fun atan(arg: Complex): Complex {
val iArg = i * arg val iArg = i * arg
@ -101,7 +101,7 @@ object ComplexField : ExtendedField<Complex> {
override fun sinh(arg: Complex): Complex = (exp(arg) - exp(-arg)) / 2 override fun sinh(arg: Complex): Complex = (exp(arg) - exp(-arg)) / 2
override fun cosh(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 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 acosh(arg: Complex): Complex = ln(arg + sqrt((arg - 1) * (arg + 1)))
override fun atanh(arg: Complex): Complex = (ln(arg + 1) - ln(1 - arg)) / 2 override fun atanh(arg: Complex): Complex = (ln(arg + 1) - ln(1 - arg)) / 2

View File

@ -1,8 +1,9 @@
package scientifik.kmath.operations package scientifik.kmath.operations
import kotlin.math.PI import kotlin.math.*
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertTrue
internal class ComplexFieldTest { internal class ComplexFieldTest {
@Test @Test
@ -44,16 +45,14 @@ internal class ComplexFieldTest {
@Test @Test
fun testInverseSine() { fun testInverseSine() {
assertEquals(Complex(0, -0.0), ComplexField { asin(zero) }) assertEquals(Complex(0, -0.0), ComplexField { asin(zero) })
assertTrue(abs(ComplexField { i * asinh(one) }.r - ComplexField { asin(i) }.r) < 0.000000000000001)
assertEquals(ComplexField { i * asinh(one) }.let { it.im.toInt() to it.re.toInt() },
ComplexField { asin(i) }.let { it.im.toInt() to it.re.toInt() })
} }
@Test @Test
fun testInverseHyperbolicSine() { fun testInverseHyperbolicSine() {
assertEquals( assertEquals(
ComplexField { i * PI.toComplex() / 2 }.let { it.im.toInt() to it.re.toInt() }, ComplexField { i * PI.toComplex() / 2 },
ComplexField { asinh(i) }.let { it.im.toInt() to it.re.toInt() }) ComplexField { asinh(i) })
} }
@Test @Test