Remove power overriding and algebraic stub.

This commit is contained in:
Gleb Minaev 2022-07-16 21:55:35 +03:00
parent 99c7174802
commit 2d86cf1cc7
3 changed files with 0 additions and 184 deletions

View File

@ -247,10 +247,6 @@ public open class PolynomialSpace<C, A>(
)
}
}
/**
* Raises [arg] to the integer power [exponent].
*/ // TODO: To optimize boxing
override fun power(arg: Polynomial<C>, exponent: UInt): Polynomial<C> = exponentiateBySquaring(arg, exponent)
/**
* Instance of zero polynomial (zero of the polynomial ring).

View File

@ -1,51 +0,0 @@
/*
* 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.*
// TODO: All of this should be moved to algebraic structures' place for utilities
// FIXME: Move receiver to context receiver
/**
* Raises [arg] to the integer power [exponent].
*
* This is implementation of variation of [exponentiation by squaring](https://en.wikipedia.org/wiki/Exponentiation_by_squaring)
*
* @param arg the base of the power.
* @param exponent the exponent of the power.
* @return [arg] raised to the power [exponent].
* @author Gleb Minaev
*/
internal tailrec fun <C> Ring<C>.exponentiateBySquaring(arg: C, exponent: UInt): C =
when {
exponent == 0u -> zero
exponent == 1u -> arg
exponent and 1u == 0u -> exponentiateBySquaring(arg * arg, exponent shr 1)
exponent and 1u == 1u -> multiplyExponentiatedBySquaring(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")
}
// FIXME: Move receiver to context receiver
/**
* Multiplies [base] and [arg] raised to the integer power [exponent].
*
* This is implementation of variation of [exponentiation by squaring](https://en.wikipedia.org/wiki/Exponentiation_by_squaring)
*
* @param base the multiplicand.
* @param arg the base of the power.
* @param exponent the exponent of the power.
* @return product of [base] and [arg] raised to the power [exponent].
* @author Gleb Minaev
*/
internal tailrec fun <C> RingOps<C>.multiplyExponentiatedBySquaring(base: C, arg: C, exponent: UInt): C =
when {
exponent == 0u -> base
exponent == 1u -> base * arg
exponent and 1u == 0u -> multiplyExponentiatedBySquaring(base, arg * arg, exponent shr 1)
exponent and 1u == 1u -> multiplyExponentiatedBySquaring(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")
}

View File

@ -1,129 +0,0 @@
/*
* 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_multiplyExponentiationBySquaring_for_UInt() {
ExprRing {
assertEquals(
"57",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 0u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 0u)"
)
assertEquals(
"(57 * 179)",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 1u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 1u)"
)
assertEquals(
"(57 * (179 * 179))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 2u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 2u)"
)
assertEquals(
"((57 * 179) * (179 * 179))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 3u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 3u)"
)
assertEquals(
"(57 * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 4u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 4u)"
)
assertEquals(
"((57 * 179) * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 5u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 5u)"
)
assertEquals(
"((57 * (179 * 179)) * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 6u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 6u)"
)
assertEquals(
"(((57 * 179) * (179 * 179)) * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 7u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 7u)"
)
assertEquals(
"(57 * (((179 * 179) * (179 * 179)) * ((179 * 179) * (179 * 179))))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 8u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 8u)"
)
}
}
@Test
fun test_exponentiationBySquaring_for_UInt() {
ExprRing {
assertEquals(
"0",
exponentiateBySquaring(Expr("57"), 0u).expr,
"tried exponentiationBySquaring(57, 0u)"
)
assertEquals(
"57",
exponentiateBySquaring(Expr("57"), 1u).expr,
"tried exponentiationBySquaring(57, 1u)"
)
assertEquals(
"(57 * 57)",
exponentiateBySquaring(Expr("57"), 2u).expr,
"tried exponentiationBySquaring(57, 2u)"
)
assertEquals(
"(57 * (57 * 57))",
exponentiateBySquaring(Expr("57"), 3u).expr,
"tried exponentiationBySquaring(57, 3u)"
)
assertEquals(
"((57 * 57) * (57 * 57))",
exponentiateBySquaring(Expr("57"), 4u).expr,
"tried exponentiationBySquaring(57, 4u)"
)
assertEquals(
"(57 * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 5u).expr,
"tried exponentiationBySquaring(57, 5u)"
)
assertEquals(
"((57 * 57) * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 6u).expr,
"tried exponentiationBySquaring(57, 6u)"
)
assertEquals(
"((57 * (57 * 57)) * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 7u).expr,
"tried exponentiationBySquaring(57, 7u)"
)
assertEquals(
"(((57 * 57) * (57 * 57)) * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 8u).expr,
"tried exponentiationBySquaring(57, 8u)"
)
}
}
}