Feature: Polynomials and rational functions #469
@ -139,7 +139,7 @@ internal tailrec fun <C> Ring<C>.exponentiationBySquaring(arg: C, exponent: UInt
|
|||||||
internal tailrec fun <C> RingOps<C>.multiplyExponentiationBySquaring(base: C, arg: C, exponent: UInt): C =
|
internal tailrec fun <C> RingOps<C>.multiplyExponentiationBySquaring(base: C, arg: C, exponent: UInt): C =
|
||||||
when {
|
when {
|
||||||
exponent == 0u -> base
|
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 == 0u -> multiplyExponentiationBySquaring(base, arg * arg, exponent shr 1)
|
||||||
exponent and 1u == 1u -> multiplyExponentiationBySquaring(base * arg, 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")
|
else -> error("Error in multiplication group instant by unsigned integer: got reminder by division by 2 different from 0 and 1")
|
||||||
|
@ -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<Expr> {
|
||||||
|
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)"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,11 +12,12 @@ import kotlin.test.assertEquals
|
|||||||
class PolynomialTest {
|
class PolynomialTest {
|
||||||
@Test
|
@Test
|
||||||
fun simple_polynomial_test() {
|
fun simple_polynomial_test() {
|
||||||
|
val polynomial : Polynomial<Double>
|
||||||
Double.algebra.scalablePolynomial {
|
Double.algebra.scalablePolynomial {
|
||||||
val x = Polynomial(listOf(0.0, 1.0))
|
val x = Polynomial(listOf(0.0, 1.0))
|
||||||
val polynomial = x * x - 2 * x + 1
|
polynomial = x * x - 2 * x + 1
|
||||||
assertEquals(0.0, polynomial.substitute(1.0), 0.001)
|
|
||||||
}
|
}
|
||||||
|
assertEquals(0.0, polynomial.substitute(1.0), 0.001)
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
fun testIntegration() {
|
fun testIntegration() {
|
||||||
|
Loading…
Reference in New Issue
Block a user