From 85cd3b4de64b35718d603033dba4aec97879578d Mon Sep 17 00:00:00 2001 From: Gleb Minaev <43728100+lounres@users.noreply.github.com> Date: Fri, 18 Mar 2022 20:39:01 +0300 Subject: [PATCH] Added some test. Fixed bug in algebraicStub.kt --- .../kscience/kmath/functions/algebraicStub.kt | 2 +- .../kmath/functions/AlgebraicStubTest.kt | 589 ++++++++++++++++++ .../kmath/functions/PolynomialTest.kt | 5 +- 3 files changed, 593 insertions(+), 3 deletions(-) create mode 100644 kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/AlgebraicStubTest.kt diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/algebraicStub.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/algebraicStub.kt index ee81f3d79..ca93e4259 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/algebraicStub.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/algebraicStub.kt @@ -139,7 +139,7 @@ internal tailrec fun Ring.exponentiationBySquaring(arg: C, exponent: UInt internal tailrec fun RingOps.multiplyExponentiationBySquaring(base: C, arg: C, exponent: UInt): C = when { exponent == 0u -> base - exponent == 1u -> base + arg + exponent == 1u -> base * arg exponent and 1u == 0u -> multiplyExponentiationBySquaring(base, arg * arg, exponent shr 1) exponent and 1u == 1u -> multiplyExponentiationBySquaring(base * arg, arg * arg, exponent shr 1) else -> error("Error in multiplication group instant by unsigned integer: got reminder by division by 2 different from 0 and 1") diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/AlgebraicStubTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/AlgebraicStubTest.kt new file mode 100644 index 000000000..fe4a82f11 --- /dev/null +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/AlgebraicStubTest.kt @@ -0,0 +1,589 @@ +/* + * Copyright 2018-2021 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.functions + + +import space.kscience.kmath.operations.invoke +import space.kscience.kmath.operations.Field +import kotlin.jvm.JvmInline +import kotlin.test.Test +import kotlin.test.assertEquals + +@JvmInline +value class Expr(val expr: String) + +object ExprRing : Field { + override fun Expr.unaryMinus(): Expr = Expr("-${expr}") + override fun add(left: Expr, right: Expr): Expr = Expr("(${left.expr} + ${right.expr})") + override fun multiply(left: Expr, right: Expr): Expr = Expr("(${left.expr} * ${right.expr})") + override val zero: Expr = Expr("0") + override val one: Expr = Expr("1") + override fun divide(left: Expr, right: Expr): Expr = Expr("(${left.expr} / ${right.expr})") + override fun scale(a: Expr, value: Double): Expr = Expr("(${a.expr} / $value)") +} + +class AlgebraicStubTest { + @Test + fun test_addMultipliedBySquaring_for_UInt() { + ExprRing { + assertEquals( + "57", + addMultipliedBySquaring(Expr("57"), Expr("179"), 0u).expr, + "tried addMultipliedBySquaring(57, 179, 0u)" + ) + assertEquals( + "(57 + 179)", + addMultipliedBySquaring(Expr("57"), Expr("179"), 1u).expr, + "tried addMultipliedBySquaring(57, 179, 1u)" + ) + assertEquals( + "(57 + (179 + 179))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 2u).expr, + "tried addMultipliedBySquaring(57, 179, 2u)" + ) + assertEquals( + "((57 + 179) + (179 + 179))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 3u).expr, + "tried addMultipliedBySquaring(57, 179, 3u)" + ) + assertEquals( + "(57 + ((179 + 179) + (179 + 179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 4u).expr, + "tried addMultipliedBySquaring(57, 179, 4u)" + ) + assertEquals( + "((57 + 179) + ((179 + 179) + (179 + 179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 5u).expr, + "tried addMultipliedBySquaring(57, 179, 5u)" + ) + assertEquals( + "((57 + (179 + 179)) + ((179 + 179) + (179 + 179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 6u).expr, + "tried addMultipliedBySquaring(57, 179, 6u)" + ) + assertEquals( + "(((57 + 179) + (179 + 179)) + ((179 + 179) + (179 + 179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 7u).expr, + "tried addMultipliedBySquaring(57, 179, 7u)" + ) + assertEquals( + "(57 + (((179 + 179) + (179 + 179)) + ((179 + 179) + (179 + 179))))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 8u).expr, + "tried addMultipliedBySquaring(57, 179, 8u)" + ) + } + } + @Test + fun test_multiplyBySquaring_for_UInt() { + ExprRing { + assertEquals( + "0", + multiplyBySquaring(Expr("57"), 0u).expr, + "tried multiplyBySquaring(57, 0u)" + ) + assertEquals( + "57", + multiplyBySquaring(Expr("57"), 1u).expr, + "tried multiplyBySquaring(57, 1u)" + ) + assertEquals( + "(57 + 57)", + multiplyBySquaring(Expr("57"), 2u).expr, + "tried multiplyBySquaring(57, 2u)" + ) + assertEquals( + "(57 + (57 + 57))", + multiplyBySquaring(Expr("57"), 3u).expr, + "tried multiplyBySquaring(57, 3u)" + ) + assertEquals( + "((57 + 57) + (57 + 57))", + multiplyBySquaring(Expr("57"), 4u).expr, + "tried multiplyBySquaring(57, 4u)" + ) + assertEquals( + "(57 + ((57 + 57) + (57 + 57)))", + multiplyBySquaring(Expr("57"), 5u).expr, + "tried multiplyBySquaring(57, 5u)" + ) + assertEquals( + "((57 + 57) + ((57 + 57) + (57 + 57)))", + multiplyBySquaring(Expr("57"), 6u).expr, + "tried multiplyBySquaring(57, 6u)" + ) + assertEquals( + "((57 + (57 + 57)) + ((57 + 57) + (57 + 57)))", + multiplyBySquaring(Expr("57"), 7u).expr, + "tried multiplyBySquaring(57, 7u)" + ) + assertEquals( + "(((57 + 57) + (57 + 57)) + ((57 + 57) + (57 + 57)))", + multiplyBySquaring(Expr("57"), 8u).expr, + "tried multiplyBySquaring(57, 8u)" + ) + } + } + @Test + fun test_addMultipliedBySquaring_for_Int() { + ExprRing { + assertEquals( + "57", + addMultipliedBySquaring(Expr("57"), Expr("179"), 0).expr, + "tried addMultipliedBySquaring(57, 179, 0)" + ) + assertEquals( + "(57 + 179)", + addMultipliedBySquaring(Expr("57"), Expr("179"), 1).expr, + "tried addMultipliedBySquaring(57, 179, 1)" + ) + assertEquals( + "(57 + -179)", + addMultipliedBySquaring(Expr("57"), Expr("179"), -1).expr, + "tried addMultipliedBySquaring(57, 179, -1)" + ) + assertEquals( + "(57 + (179 + 179))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 2).expr, + "tried addMultipliedBySquaring(57, 179, 2)" + ) + assertEquals( + "(57 + (-179 + -179))", + addMultipliedBySquaring(Expr("57"), Expr("179"), -2).expr, + "tried addMultipliedBySquaring(57, 179, -2)" + ) + assertEquals( + "((57 + 179) + (179 + 179))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 3).expr, + "tried addMultipliedBySquaring(57, 179, 3)" + ) + assertEquals( + "((57 + -179) + (-179 + -179))", + addMultipliedBySquaring(Expr("57"), Expr("179"), -3).expr, + "tried addMultipliedBySquaring(57, 179, -3)" + ) + assertEquals( + "(57 + ((179 + 179) + (179 + 179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 4).expr, + "tried addMultipliedBySquaring(57, 179, 4)" + ) + assertEquals( + "(57 + ((-179 + -179) + (-179 + -179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), -4).expr, + "tried addMultipliedBySquaring(57, 179, -4)" + ) + assertEquals( + "((57 + 179) + ((179 + 179) + (179 + 179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 5).expr, + "tried addMultipliedBySquaring(57, 179, 5)" + ) + assertEquals( + "((57 + -179) + ((-179 + -179) + (-179 + -179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), -5).expr, + "tried addMultipliedBySquaring(57, 179, -5)" + ) + assertEquals( + "((57 + (179 + 179)) + ((179 + 179) + (179 + 179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 6).expr, + "tried addMultipliedBySquaring(57, 179, 6)" + ) + assertEquals( + "((57 + (-179 + -179)) + ((-179 + -179) + (-179 + -179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), -6).expr, + "tried addMultipliedBySquaring(57, 179, -6)" + ) + assertEquals( + "(((57 + 179) + (179 + 179)) + ((179 + 179) + (179 + 179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 7).expr, + "tried addMultipliedBySquaring(57, 179, 7)" + ) + assertEquals( + "(((57 + -179) + (-179 + -179)) + ((-179 + -179) + (-179 + -179)))", + addMultipliedBySquaring(Expr("57"), Expr("179"), -7).expr, + "tried addMultipliedBySquaring(57, 179, -7)" + ) + assertEquals( + "(57 + (((179 + 179) + (179 + 179)) + ((179 + 179) + (179 + 179))))", + addMultipliedBySquaring(Expr("57"), Expr("179"), 8).expr, + "tried addMultipliedBySquaring(57, 179, 8)" + ) + assertEquals( + "(57 + (((-179 + -179) + (-179 + -179)) + ((-179 + -179) + (-179 + -179))))", + addMultipliedBySquaring(Expr("57"), Expr("179"), -8).expr, + "tried addMultipliedBySquaring(57, 179, -8)" + ) + } + } + @Test + fun test_multiplyBySquaring_for_Int() { + ExprRing { + assertEquals( + "0", + multiplyBySquaring(Expr("57"), 0).expr, + "tried multiplyBySquaring(57, 0)" + ) + assertEquals( + "57", + multiplyBySquaring(Expr("57"), 1).expr, + "tried multiplyBySquaring(57, 1)" + ) + assertEquals( + "-57", + multiplyBySquaring(Expr("57"), -1).expr, + "tried multiplyBySquaring(57, -1)" + ) + assertEquals( + "(57 + 57)", + multiplyBySquaring(Expr("57"), 2).expr, + "tried multiplyBySquaring(57, 2)" + ) + assertEquals( + "(-57 + -57)", + multiplyBySquaring(Expr("57"), -2).expr, + "tried multiplyBySquaring(57, -2)" + ) + assertEquals( + "(57 + (57 + 57))", + multiplyBySquaring(Expr("57"), 3).expr, + "tried multiplyBySquaring(57, 3)" + ) + assertEquals( + "(-57 + (-57 + -57))", + multiplyBySquaring(Expr("57"), -3).expr, + "tried multiplyBySquaring(57, -3)" + ) + assertEquals( + "((57 + 57) + (57 + 57))", + multiplyBySquaring(Expr("57"), 4).expr, + "tried multiplyBySquaring(57, 4)" + ) + assertEquals( + "((-57 + -57) + (-57 + -57))", + multiplyBySquaring(Expr("57"), -4).expr, + "tried multiplyBySquaring(57, -4)" + ) + assertEquals( + "(57 + ((57 + 57) + (57 + 57)))", + multiplyBySquaring(Expr("57"), 5).expr, + "tried multiplyBySquaring(57, 5)" + ) + assertEquals( + "(-57 + ((-57 + -57) + (-57 + -57)))", + multiplyBySquaring(Expr("57"), -5).expr, + "tried multiplyBySquaring(57, -5)" + ) + assertEquals( + "((57 + 57) + ((57 + 57) + (57 + 57)))", + multiplyBySquaring(Expr("57"), 6).expr, + "tried multiplyBySquaring(57, 6)" + ) + assertEquals( + "((-57 + -57) + ((-57 + -57) + (-57 + -57)))", + multiplyBySquaring(Expr("57"), -6).expr, + "tried multiplyBySquaring(57, -6)" + ) + assertEquals( + "((57 + (57 + 57)) + ((57 + 57) + (57 + 57)))", + multiplyBySquaring(Expr("57"), 7).expr, + "tried multiplyBySquaring(57, 7)" + ) + assertEquals( + "((-57 + (-57 + -57)) + ((-57 + -57) + (-57 + -57)))", + multiplyBySquaring(Expr("57"), -7).expr, + "tried multiplyBySquaring(57, -7)" + ) + assertEquals( + "(((57 + 57) + (57 + 57)) + ((57 + 57) + (57 + 57)))", + multiplyBySquaring(Expr("57"), 8).expr, + "tried multiplyBySquaring(57, 8)" + ) + assertEquals( + "(((-57 + -57) + (-57 + -57)) + ((-57 + -57) + (-57 + -57)))", + multiplyBySquaring(Expr("57"), -8).expr, + "tried multiplyBySquaring(57, -8)" + ) + } + } + @Test + fun test_multiplyExponentiationBySquaring_for_UInt() { + ExprRing { + assertEquals( + "57", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 0u).expr, + "tried multiplyExponentiationBySquaring(57, 179, 0u)" + ) + assertEquals( + "(57 * 179)", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 1u).expr, + "tried multiplyExponentiationBySquaring(57, 179, 1u)" + ) + assertEquals( + "(57 * (179 * 179))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 2u).expr, + "tried multiplyExponentiationBySquaring(57, 179, 2u)" + ) + assertEquals( + "((57 * 179) * (179 * 179))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 3u).expr, + "tried multiplyExponentiationBySquaring(57, 179, 3u)" + ) + assertEquals( + "(57 * ((179 * 179) * (179 * 179)))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 4u).expr, + "tried multiplyExponentiationBySquaring(57, 179, 4u)" + ) + assertEquals( + "((57 * 179) * ((179 * 179) * (179 * 179)))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 5u).expr, + "tried multiplyExponentiationBySquaring(57, 179, 5u)" + ) + assertEquals( + "((57 * (179 * 179)) * ((179 * 179) * (179 * 179)))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 6u).expr, + "tried multiplyExponentiationBySquaring(57, 179, 6u)" + ) + assertEquals( + "(((57 * 179) * (179 * 179)) * ((179 * 179) * (179 * 179)))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 7u).expr, + "tried multiplyExponentiationBySquaring(57, 179, 7u)" + ) + assertEquals( + "(57 * (((179 * 179) * (179 * 179)) * ((179 * 179) * (179 * 179))))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 8u).expr, + "tried multiplyExponentiationBySquaring(57, 179, 8u)" + ) + } + } + @Test + fun test_exponentiationBySquaring_for_UInt() { + ExprRing { + assertEquals( + "0", + exponentiationBySquaring(Expr("57"), 0u).expr, + "tried exponentiationBySquaring(57, 0u)" + ) + assertEquals( + "57", + exponentiationBySquaring(Expr("57"), 1u).expr, + "tried exponentiationBySquaring(57, 1u)" + ) + assertEquals( + "(57 * 57)", + exponentiationBySquaring(Expr("57"), 2u).expr, + "tried exponentiationBySquaring(57, 2u)" + ) + assertEquals( + "(57 * (57 * 57))", + exponentiationBySquaring(Expr("57"), 3u).expr, + "tried exponentiationBySquaring(57, 3u)" + ) + assertEquals( + "((57 * 57) * (57 * 57))", + exponentiationBySquaring(Expr("57"), 4u).expr, + "tried exponentiationBySquaring(57, 4u)" + ) + assertEquals( + "(57 * ((57 * 57) * (57 * 57)))", + exponentiationBySquaring(Expr("57"), 5u).expr, + "tried exponentiationBySquaring(57, 5u)" + ) + assertEquals( + "((57 * 57) * ((57 * 57) * (57 * 57)))", + exponentiationBySquaring(Expr("57"), 6u).expr, + "tried exponentiationBySquaring(57, 6u)" + ) + assertEquals( + "((57 * (57 * 57)) * ((57 * 57) * (57 * 57)))", + exponentiationBySquaring(Expr("57"), 7u).expr, + "tried exponentiationBySquaring(57, 7u)" + ) + assertEquals( + "(((57 * 57) * (57 * 57)) * ((57 * 57) * (57 * 57)))", + exponentiationBySquaring(Expr("57"), 8u).expr, + "tried exponentiationBySquaring(57, 8u)" + ) + } + } + @Test + fun test_multiplyExponentiationBySquaring_for_Int() { + ExprRing { + assertEquals( + "57", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 0).expr, + "tried multiplyExponentiationBySquaring(57, 179, 0)" + ) + assertEquals( + "(57 * 179)", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 1).expr, + "tried multiplyExponentiationBySquaring(57, 179, 1)" + ) + assertEquals( + "(57 * (1 / 179))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), -1).expr, + "tried multiplyExponentiationBySquaring(57, 179, -1)" + ) + assertEquals( + "(57 * (179 * 179))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 2).expr, + "tried multiplyExponentiationBySquaring(57, 179, 2)" + ) + assertEquals( + "(57 * ((1 / 179) * (1 / 179)))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), -2).expr, + "tried multiplyExponentiationBySquaring(57, 179, -2)" + ) + assertEquals( + "((57 * 179) * (179 * 179))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 3).expr, + "tried multiplyExponentiationBySquaring(57, 179, 3)" + ) + assertEquals( + "((57 * (1 / 179)) * ((1 / 179) * (1 / 179)))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), -3).expr, + "tried multiplyExponentiationBySquaring(57, 179, -3)" + ) + assertEquals( + "(57 * ((179 * 179) * (179 * 179)))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 4).expr, + "tried multiplyExponentiationBySquaring(57, 179, 4)" + ) + assertEquals( + "(57 * (((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179))))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), -4).expr, + "tried multiplyExponentiationBySquaring(57, 179, -4)" + ) + assertEquals( + "((57 * 179) * ((179 * 179) * (179 * 179)))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 5).expr, + "tried multiplyExponentiationBySquaring(57, 179, 5)" + ) + assertEquals( + "((57 * (1 / 179)) * (((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179))))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), -5).expr, + "tried multiplyExponentiationBySquaring(57, 179, -5)" + ) + assertEquals( + "((57 * (179 * 179)) * ((179 * 179) * (179 * 179)))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 6).expr, + "tried multiplyExponentiationBySquaring(57, 179, 6)" + ) + assertEquals( + "((57 * ((1 / 179) * (1 / 179))) * (((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179))))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), -6).expr, + "tried multiplyExponentiationBySquaring(57, 179, -6)" + ) + assertEquals( + "(((57 * 179) * (179 * 179)) * ((179 * 179) * (179 * 179)))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 7).expr, + "tried multiplyExponentiationBySquaring(57, 179, 7)" + ) + assertEquals( + "(((57 * (1 / 179)) * ((1 / 179) * (1 / 179))) * (((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179))))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), -7).expr, + "tried multiplyExponentiationBySquaring(57, 179, -7)" + ) + assertEquals( + "(57 * (((179 * 179) * (179 * 179)) * ((179 * 179) * (179 * 179))))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), 8).expr, + "tried multiplyExponentiationBySquaring(57, 179, 8)" + ) + assertEquals( + "(57 * ((((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179))) * (((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179)))))", + multiplyExponentiationBySquaring(Expr("57"), Expr("179"), -8).expr, + "tried multiplyExponentiationBySquaring(57, 179, -8)" + ) + } + } + @Test + fun test_exponentiationBySquaring_for_Int() { + ExprRing { + assertEquals( + "0", + exponentiationBySquaring(Expr("57"), 0).expr, + "tried exponentiationBySquaring(57, 0)" + ) + assertEquals( + "57", + exponentiationBySquaring(Expr("57"), 1).expr, + "tried exponentiationBySquaring(57, 1)" + ) + assertEquals( + "(1 / 57)", + exponentiationBySquaring(Expr("57"), -1).expr, + "tried exponentiationBySquaring(57, -1)" + ) + assertEquals( + "(57 * 57)", + exponentiationBySquaring(Expr("57"), 2).expr, + "tried exponentiationBySquaring(57, 2)" + ) + assertEquals( + "((1 / 57) * (1 / 57))", + exponentiationBySquaring(Expr("57"), -2).expr, + "tried exponentiationBySquaring(57, -2)" + ) + assertEquals( + "(57 * (57 * 57))", + exponentiationBySquaring(Expr("57"), 3).expr, + "tried exponentiationBySquaring(57, 3)" + ) + assertEquals( + "((1 / 57) * ((1 / 57) * (1 / 57)))", + exponentiationBySquaring(Expr("57"), -3).expr, + "tried exponentiationBySquaring(57, -3)" + ) + assertEquals( + "((57 * 57) * (57 * 57))", + exponentiationBySquaring(Expr("57"), 4).expr, + "tried exponentiationBySquaring(57, 4)" + ) + assertEquals( + "(((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57)))", + exponentiationBySquaring(Expr("57"), -4).expr, + "tried exponentiationBySquaring(57, -4)" + ) + assertEquals( + "(57 * ((57 * 57) * (57 * 57)))", + exponentiationBySquaring(Expr("57"), 5).expr, + "tried exponentiationBySquaring(57, 5)" + ) + assertEquals( + "((1 / 57) * (((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57))))", + exponentiationBySquaring(Expr("57"), -5).expr, + "tried exponentiationBySquaring(57, -5)" + ) + assertEquals( + "((57 * 57) * ((57 * 57) * (57 * 57)))", + exponentiationBySquaring(Expr("57"), 6).expr, + "tried exponentiationBySquaring(57, 6)" + ) + assertEquals( + "(((1 / 57) * (1 / 57)) * (((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57))))", + exponentiationBySquaring(Expr("57"), -6).expr, + "tried exponentiationBySquaring(57, -6)" + ) + assertEquals( + "((57 * (57 * 57)) * ((57 * 57) * (57 * 57)))", + exponentiationBySquaring(Expr("57"), 7).expr, + "tried exponentiationBySquaring(57, 7)" + ) + assertEquals( + "(((1 / 57) * ((1 / 57) * (1 / 57))) * (((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57))))", + exponentiationBySquaring(Expr("57"), -7).expr, + "tried exponentiationBySquaring(57, -7)" + ) + assertEquals( + "(((57 * 57) * (57 * 57)) * ((57 * 57) * (57 * 57)))", + exponentiationBySquaring(Expr("57"), 8).expr, + "tried exponentiationBySquaring(57, 8)" + ) + assertEquals( + "((((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57))) * (((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57))))", + exponentiationBySquaring(Expr("57"), -8).expr, + "tried exponentiationBySquaring(57, -8)" + ) + } + } +} \ No newline at end of file diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialTest.kt index e0f0e32a4..a11242b2a 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialTest.kt @@ -12,11 +12,12 @@ import kotlin.test.assertEquals class PolynomialTest { @Test fun simple_polynomial_test() { + val polynomial : Polynomial Double.algebra.scalablePolynomial { val x = Polynomial(listOf(0.0, 1.0)) - val polynomial = x * x - 2 * x + 1 - assertEquals(0.0, polynomial.substitute(1.0), 0.001) + polynomial = x * x - 2 * x + 1 } + assertEquals(0.0, polynomial.substitute(1.0), 0.001) } @Test fun testIntegration() {