From 67def3dcf8cfac9fda04d39235baba776d18dcdc Mon Sep 17 00:00:00 2001 From: lbressler13 Date: Tue, 23 Jan 2024 19:18:19 -0500 Subject: [PATCH] refactor: remove new term properties --- .../exactnumbers/expressions/term/Term.kt | 30 ++++++---- .../exactnumbers/expressions/term/TermImpl.kt | 21 ------- .../expressions/term/constuctorTests.kt | 48 +++++++-------- .../term/simplifyGetValuesTests.kt | 59 ++++++------------- .../expressions/term/testutils.kt | 21 +------ signatures/v1.0.0/updates.md | 4 -- signatures/v1.0.0/v1.0.0.md | 4 -- 7 files changed, 60 insertions(+), 127 deletions(-) diff --git a/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/expressions/term/Term.kt b/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/expressions/term/Term.kt index fa466d7..9990b53 100644 --- a/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/expressions/term/Term.kt +++ b/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/expressions/term/Term.kt @@ -16,6 +16,7 @@ import xyz.lbres.exactnumbers.utils.deprecatedV1 import xyz.lbres.exactnumbers.utils.irrationalsPackage import xyz.lbres.kotlinutils.collection.ext.toConstMultiSet import xyz.lbres.kotlinutils.general.simpleIf +import xyz.lbres.kotlinutils.iterable.ext.countElement import java.math.BigDecimal import kotlin.math.abs @@ -25,10 +26,6 @@ import kotlin.math.abs sealed class Term : Number() { abstract val coefficient: ExactFraction abstract val factors: List> - abstract val logs: List - abstract val pis: List - abstract val piCount: Int - abstract val squareRoots: List abstract operator fun unaryMinus(): Term abstract operator fun unaryPlus(): Term @@ -68,17 +65,24 @@ sealed class Term : Number() { override fun toFloat(): Float = castToFloat(getValue(), this, "Term") override fun toDouble(): Double = castToDouble(getValue(), this, "Term") - @Deprecated("Method $deprecatedV1", ReplaceWith("getIrrationalsByType(Log.TYPE)", "$irrationalsPackage.log.Log"), DeprecationLevel.WARNING) - @JvmName("getLogsDeprecated") - fun getLogs(): List = logs + @Deprecated("Method $deprecatedV1", ReplaceWith("getFactorsByType(Log.TYPE)", "$irrationalsPackage.log.Log"), DeprecationLevel.WARNING) + @Suppress("UNCHECKED_CAST") + fun getLogs(): List { + return getFactorsByType(Log.TYPE) as List + } - @Deprecated("Method $deprecatedV1", ReplaceWith("piCount"), DeprecationLevel.WARNING) - @JvmName("getPiCountDeprecated") - fun getPiCount(): Int = piCount + @Deprecated("Method $deprecatedV1", ReplaceWith("getFactorsByType(Pi.TYPE)"), DeprecationLevel.WARNING) + @Suppress("UNCHECKED_CAST") + fun getPiCount(): Int { + val pis = getFactorsByType(Pi.TYPE) as List + return pis.countElement(Pi()) * 2 - pis.size // positive - (numbers.size - positive) + } - @Deprecated("Method $deprecatedV1", ReplaceWith("getIrrationalsByType(Sqrt.TYPE)", "$irrationalsPackage.sqrt.Sqrt"), DeprecationLevel.WARNING) - @JvmName("getSquareRootsDeprecated") - fun getSquareRoots(): List = squareRoots + @Deprecated("Method $deprecatedV1", ReplaceWith("getFactorsByType(Sqrt.TYPE)", "$irrationalsPackage.sqrt.Sqrt"), DeprecationLevel.WARNING) + @Suppress("UNCHECKED_CAST") + fun getSquareRoots(): List { + return getFactorsByType(Sqrt.TYPE) as List + } companion object { val ZERO = fromValues(ExactFraction.ZERO, emptyList()) diff --git a/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/expressions/term/TermImpl.kt b/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/expressions/term/TermImpl.kt index 18bd599..d05c6e9 100644 --- a/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/expressions/term/TermImpl.kt +++ b/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/expressions/term/TermImpl.kt @@ -3,9 +3,6 @@ package xyz.lbres.exactnumbers.expressions.term import xyz.lbres.exactnumbers.exactfraction.ExactFraction import xyz.lbres.exactnumbers.ext.divideBy import xyz.lbres.exactnumbers.irrationals.IrrationalNumber -import xyz.lbres.exactnumbers.irrationals.log.Log -import xyz.lbres.exactnumbers.irrationals.pi.Pi -import xyz.lbres.exactnumbers.irrationals.sqrt.Sqrt import xyz.lbres.exactnumbers.utils.createHashCode import xyz.lbres.exactnumbers.utils.divideByZero import xyz.lbres.kotlinutils.collection.ext.toConstMultiSet @@ -26,11 +23,6 @@ internal class TermImpl(coefficient: ExactFraction, factors: ConstMultiSet> override val factors: List> - override val logs: List - override val squareRoots: List - override val pis: List - override val piCount: Int - // previously computed values for method returns private var simplified: Term? = null private var value: BigDecimal? = null @@ -42,24 +34,11 @@ internal class TermImpl(coefficient: ExactFraction, factors: ConstMultiSet - @Suppress("UNCHECKED_CAST") - this.squareRoots = factorTypeMapping.getOrDefault(Sqrt.TYPE, emptyList()) as List - @Suppress("UNCHECKED_CAST") - this.pis = factorTypeMapping.getOrDefault(Pi.TYPE, emptyList()) as List - this.piCount = factorSet.getCountOf(Pi()) - factorSet.getCountOf(Pi().inverse()) } } diff --git a/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/constuctorTests.kt b/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/constuctorTests.kt index 4249498..cdc0bbc 100644 --- a/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/constuctorTests.kt +++ b/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/constuctorTests.kt @@ -1,6 +1,7 @@ package xyz.lbres.exactnumbers.expressions.term import xyz.lbres.exactnumbers.exactfraction.ExactFraction +import xyz.lbres.exactnumbers.irrationals.IrrationalNumber import xyz.lbres.exactnumbers.irrationals.log.Log import xyz.lbres.exactnumbers.irrationals.pi.Pi import xyz.lbres.exactnumbers.irrationals.sqrt.Sqrt @@ -54,40 +55,39 @@ private fun runFactorsConstructorTests() { checkTerm(term, ExactFraction(-17, 100043)) term = Term.fromValues(one, listOf(logWhole, logInverse)) - var logs = listOf(logWhole, logInverse) - checkTerm(term, one, logs, logs = logs) + var factors: List> = listOf(logWhole, logInverse) + checkTerm(term, one, factors) term = Term.fromValues(one, listOf(sqrtWhole)) - checkTerm(term, one, listOf(sqrtWhole), sqrts = listOf(sqrtWhole)) + checkTerm(term, one, listOf(sqrtWhole)) term = Term.fromValues(one, listOf(pi)) - checkTerm(term, one, listOf(pi), pis = listOf(pi), piCount = 1) + checkTerm(term, one, listOf(pi)) term = Term.fromValues(one, listOf(pi, piInverse)) - var pis = listOf(pi, piInverse) - checkTerm(term, one, pis, pis = pis, piCount = 0) + factors = listOf(pi, piInverse) + checkTerm(term, one, factors) // multi type term = Term.fromValues(ExactFraction(-17, 100043), listOf(logWhole)) - checkTerm(term, ExactFraction(-17, 100043), listOf(logWhole), logs = listOf(logWhole)) + checkTerm(term, ExactFraction(-17, 100043), listOf(logWhole)) term = Term.fromValues(ExactFraction.NEG_ONE, listOf(piInverse, logWhole)) - logs = listOf(logWhole) - pis = listOf(piInverse) - checkTerm(term, ExactFraction.NEG_ONE, listOf(piInverse, logWhole), logs = logs, pis = pis, piCount = -1) + factors = listOf(piInverse, logWhole) + checkTerm(term, ExactFraction.NEG_ONE, factors) - logs = listOf(logChangeBase, logInverse, logDecimal) + val logs = listOf(logChangeBase, logInverse, logDecimal) val sqrts = listOf(sqrtDecimal, Sqrt.ONE) - pis = listOf(pi, pi, piInverse) + val pis = listOf(pi, pi, piInverse) term = Term.fromValues(ExactFraction(-1, 5), logs + sqrts) - checkTerm(term, ExactFraction(-1, 5), logs + sqrts, logs, sqrts, emptyList(), 0) + checkTerm(term, ExactFraction(-1, 5), logs + sqrts) term = Term.fromValues(ExactFraction(-1, 5), sqrts + pis) - checkTerm(term, ExactFraction(-1, 5), sqrts + pis, emptyList(), sqrts, pis, 1) + checkTerm(term, ExactFraction(-1, 5), sqrts + pis) term = Term.fromValues(one, pis + logs + listOf(testNumber1, testNumber2)) - checkTerm(term, one, pis + logs + listOf(testNumber1, testNumber2), logs, sqrts = emptyList(), pis, 1) + checkTerm(term, one, pis + logs + listOf(testNumber1, testNumber2)) } private fun runComponentConstructorTests() { @@ -113,35 +113,35 @@ private fun runComponentConstructorTests() { term = Term.fromValues(one, listOf(logWhole, logInverse), emptyList(), 0) var logs = listOf(logWhole, logInverse) - checkTerm(term, one, logs, logs = logs) + checkTerm(term, one, logs) term = Term.fromValues(one, emptyList(), listOf(sqrtPartialWhole), 0) - checkTerm(term, one, listOf(sqrtPartialWhole), sqrts = listOf(sqrtPartialWhole)) + checkTerm(term, one, listOf(sqrtPartialWhole)) term = Term.fromValues(one, emptyList(), emptyList(), -2) var pis = listOf(piInverse, piInverse) - checkTerm(term, one, pis, pis = pis, piCount = -2) + checkTerm(term, one, pis) // multi type term = Term.fromValues(ExactFraction(-17, 100043), listOf(logWhole), emptyList(), 0) - checkTerm(term, ExactFraction(-17, 100043), listOf(logWhole), logs = listOf(logWhole)) + checkTerm(term, ExactFraction(-17, 100043), listOf(logWhole)) term = Term.fromValues(ExactFraction.NEG_ONE, emptyList(), emptyList(), -1) - checkTerm(term, ExactFraction.NEG_ONE, listOf(piInverse), pis = listOf(piInverse), piCount = -1) + checkTerm(term, ExactFraction.NEG_ONE, listOf(piInverse)) logs = listOf(logChangeBase, logInverse, logDecimal) val sqrts = listOf(sqrtDecimal, Sqrt.ONE) pis = listOf(pi, pi) term = Term.fromValues(ExactFraction(-1, 5), logs, sqrts, 0) - checkTerm(term, ExactFraction(-1, 5), logs + sqrts, logs, sqrts, emptyList(), 0) + checkTerm(term, ExactFraction(-1, 5), logs + sqrts) term = Term.fromValues(ExactFraction(-1, 5), logs, emptyList(), 2) - checkTerm(term, ExactFraction(-1, 5), logs + pis, logs, emptyList(), pis, 2) + checkTerm(term, ExactFraction(-1, 5), logs + pis) term = Term.fromValues(ExactFraction(-1, 5), emptyList(), sqrts, 2) - checkTerm(term, ExactFraction(-1, 5), sqrts + pis, emptyList(), sqrts, pis, 2) + checkTerm(term, ExactFraction(-1, 5), sqrts + pis) term = Term.fromValues(ExactFraction(-1, 5), logs, sqrts, 2) - checkTerm(term, ExactFraction(-1, 5), logs + sqrts + pis, logs, sqrts, pis, 2) + checkTerm(term, ExactFraction(-1, 5), logs + sqrts + pis) } diff --git a/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/simplifyGetValuesTests.kt b/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/simplifyGetValuesTests.kt index aea8ac1..91c4142 100644 --- a/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/simplifyGetValuesTests.kt +++ b/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/simplifyGetValuesTests.kt @@ -1,7 +1,6 @@ package xyz.lbres.exactnumbers.expressions.term import xyz.lbres.exactnumbers.exactfraction.ExactFraction -import xyz.lbres.exactnumbers.irrationals.IrrationalNumber import xyz.lbres.exactnumbers.irrationals.log.Log import xyz.lbres.exactnumbers.irrationals.pi.Pi import xyz.lbres.exactnumbers.irrationals.sqrt.Sqrt @@ -22,66 +21,56 @@ fun runCommonSimplifyTests(simplify: (Term) -> Term) { // simplified var term = Term.fromValues(ExactFraction.EIGHT, listOf(pi, piInverse)) var result = simplify(term) - var expectedCoeff = ExactFraction.EIGHT - checkTerm(result, expectedCoeff) + checkTerm(result, ExactFraction.EIGHT) term = Term.fromValues(ExactFraction(-3, 2), listOf(log1, pi, piInverse, pi)) result = simplify(term) - expectedCoeff = ExactFraction(-3, 2) - var expectedFactors: List> = listOf(log1, pi) - checkTerm(result, expectedCoeff, expectedFactors, logs = listOf(log1), pis = listOf(pi), piCount = 1) + var expectedFactors = listOf(log1, pi) + checkTerm(result, ExactFraction(-3, 2), expectedFactors) term = Term.fromValues(ExactFraction.HALF, listOf(Log.ONE, log1, testNumber2, testNumber2)) result = simplify(term) - expectedCoeff = ExactFraction(49, 2) expectedFactors = listOf(log1) - checkTerm(result, expectedCoeff, expectedFactors, logs = listOf(log1)) + checkTerm(result, ExactFraction(49, 2), expectedFactors) term = Term.fromValues(-ExactFraction.HALF, listOf(Log.ONE, piInverse)) result = simplify(term) - expectedCoeff = -ExactFraction.HALF expectedFactors = listOf(piInverse) - checkTerm(result, expectedCoeff, expectedFactors, pis = listOf(piInverse), piCount = -1) + checkTerm(result, -ExactFraction.HALF, expectedFactors) term = Term.fromValues(ExactFraction.TEN, listOf(Sqrt.ONE, sqrt, testNumber1, testNumber1, testNumber1.inverse(), testNumber1.inverse(), testNumber1.inverse())) result = simplify(term) - expectedCoeff = ExactFraction(20) expectedFactors = listOf(Sqrt(ExactFraction(5, 33)), testNumber1.inverse()) var sqrts = listOf(Sqrt(ExactFraction(5, 33))) - checkTerm(result, expectedCoeff, expectedFactors, sqrts = sqrts) + checkTerm(result, ExactFraction(20), expectedFactors) term = Term.fromValues(ExactFraction.TWO, listOf(Sqrt(64), Sqrt(ExactFraction(75, 98)), Sqrt(26))) result = simplify(term) - expectedCoeff = ExactFraction(80, 7) expectedFactors = listOf(Sqrt(ExactFraction(39))) - checkTerm(result, expectedCoeff, expectedFactors, sqrts = listOf(Sqrt(ExactFraction(39)))) + checkTerm(result, ExactFraction(80, 7), expectedFactors) term = Term.fromValues( ExactFraction(18, 5), listOf(log2, log2, log1, log2.inverse(), piInverse, piInverse, piInverse, pi) ) result = simplify(term) - expectedCoeff = ExactFraction(18, 5) var logs = listOf(log2, log1) var pis = listOf(piInverse, piInverse) - checkTerm(result, expectedCoeff, logs + pis, logs = logs, pis = pis, piCount = -2) + checkTerm(result, ExactFraction(18, 5), logs + pis) term = Term.fromValues(ExactFraction.FOUR, listOf(Log(100), Sqrt(9), testNumber1, Sqrt(ExactFraction(1, 4)))) result = simplify(term) - expectedCoeff = ExactFraction(12) - checkTerm(result, expectedCoeff, listOf(testNumber1)) + checkTerm(result, ExactFraction(12), listOf(testNumber1)) term = Term.fromValues(-ExactFraction.EIGHT, listOf(Sqrt(ExactFraction(27, 98)), piInverse)) result = simplify(term) - expectedCoeff = ExactFraction(-24, 7) expectedFactors = listOf(Sqrt(ExactFraction(3, 2)), piInverse) sqrts = listOf(Sqrt(ExactFraction(3, 2))) - checkTerm(result, expectedCoeff, expectedFactors, sqrts = sqrts, pis = listOf(piInverse), piCount = -1) + checkTerm(result, ExactFraction(-24, 7), expectedFactors) term = Term.fromValues(ExactFraction(20), listOf(Log(ExactFraction(1, 27), 3).inverse())) result = simplify(term) - expectedCoeff = ExactFraction(-20, 3) - checkTerm(result, expectedCoeff) + checkTerm(result, ExactFraction(-20, 3)) term = Term.fromValues( ExactFraction(3, 5), @@ -97,52 +86,40 @@ fun runCommonSimplifyTests(simplify: (Term) -> Term) { ) ) result = simplify(term) - expectedCoeff = ExactFraction(-6) expectedFactors = listOf(Log(4), Log(1000, 12), Sqrt(ExactFraction(78, 7)), pi) - logs = listOf(Log(4), Log(1000, 12)) - sqrts = listOf(Sqrt(ExactFraction(78, 7))) - checkTerm(result, expectedCoeff, expectedFactors, logs, sqrts, listOf(pi), 1) + checkTerm(result, ExactFraction(-6), expectedFactors) term = Term.fromValues( ExactFraction(4, 7), listOf(testNumber2, pi, piInverse, Log.ONE, Sqrt(ExactFraction(9, 49)), Log(1000), Log(ExactFraction(1, 32), 2)) ) result = simplify(term) - expectedCoeff = ExactFraction(-180, 7) - checkTerm(result, expectedCoeff) + checkTerm(result, ExactFraction(-180, 7)) // no changes term = Term.fromValues(ExactFraction.EIGHT, emptyList()) result = simplify(term) - expectedCoeff = ExactFraction.EIGHT - checkTerm(result, expectedCoeff) + checkTerm(result, ExactFraction.EIGHT) term = Term.fromValues(ExactFraction.EIGHT, listOf(log1)) result = simplify(term) - expectedCoeff = ExactFraction.EIGHT expectedFactors = listOf(log1) - checkTerm(result, expectedCoeff, expectedFactors, logs = listOf(log1)) + checkTerm(result, ExactFraction.EIGHT, expectedFactors) term = Term.fromValues(ExactFraction.EIGHT, listOf(Sqrt(ExactFraction(1, 46)))) result = simplify(term) - expectedCoeff = ExactFraction.EIGHT expectedFactors = listOf(Sqrt(ExactFraction(1, 46))) - sqrts = listOf(Sqrt(ExactFraction(1, 46))) - checkTerm(result, expectedCoeff, expectedFactors, sqrts = sqrts) + checkTerm(result, ExactFraction.EIGHT, expectedFactors) term = Term.fromValues(ExactFraction(-5, 6), listOf(piInverse, testNumber1)) result = simplify(term) - expectedCoeff = ExactFraction(-5, 6) expectedFactors = listOf(piInverse, testNumber1) - checkTerm(result, expectedCoeff, expectedFactors, pis = listOf(piInverse), piCount = -1) + checkTerm(result, ExactFraction(-5, 6), expectedFactors) term = Term.fromValues(ExactFraction.SEVEN, listOf(log1, log1, log2.inverse(), Sqrt(5), pi, pi)) result = simplify(term) - expectedCoeff = ExactFraction.SEVEN expectedFactors = listOf(log1, log1, log2.inverse(), Sqrt(5), pi, pi) - logs = listOf(log1, log1, log2.inverse()) - pis = listOf(pi, pi) - checkTerm(result, expectedCoeff, expectedFactors, logs, listOf(Sqrt(5)), pis, 2) + checkTerm(result, ExactFraction.SEVEN, expectedFactors) } fun runGetValueTests() { diff --git a/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/testutils.kt b/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/testutils.kt index 27ff16d..2d20a89 100644 --- a/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/testutils.kt +++ b/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/term/testutils.kt @@ -2,9 +2,6 @@ package xyz.lbres.exactnumbers.expressions.term import xyz.lbres.exactnumbers.exactfraction.ExactFraction import xyz.lbres.exactnumbers.irrationals.IrrationalNumber -import xyz.lbres.exactnumbers.irrationals.log.Log -import xyz.lbres.exactnumbers.irrationals.pi.Pi -import xyz.lbres.exactnumbers.irrationals.sqrt.Sqrt import kotlin.test.assertEquals /** @@ -13,24 +10,8 @@ import kotlin.test.assertEquals * @param term [Term]: term to check * @param coeff [ExactFraction]: expected coefficient * @param factors [List]>: expected factors. Defaults to empty list - * @param logs [List]: expected logs. Defaults to empty list - * @param sqrts [List]: expected squareRoots. Defaults to empty list - * @param pis [List]: expected pis. Defaults to empty list - * @param piCount [Int]: expected piCount. Defaults to 0 */ -fun checkTerm( - term: Term, - coeff: ExactFraction, - factors: List> = emptyList(), - logs: List = emptyList(), - sqrts: List = emptyList(), - pis: List = emptyList(), - piCount: Int = 0 -) { +fun checkTerm(term: Term, coeff: ExactFraction, factors: List> = emptyList()) { assertEquals(coeff, term.coefficient) assertEquals(factors, term.factors) - assertEquals(logs.sorted(), term.logs.sorted()) - assertEquals(sqrts.sorted(), term.squareRoots.sorted()) - assertEquals(pis.sorted(), term.pis.sorted()) - assertEquals(piCount, term.piCount) } diff --git a/signatures/v1.0.0/updates.md b/signatures/v1.0.0/updates.md index b61d9f5..5b43f02 100644 --- a/signatures/v1.0.0/updates.md +++ b/signatures/v1.0.0/updates.md @@ -50,10 +50,6 @@ ADD class Term DEL class Term: Number() ADD val factors: List> -ADD val logs: List -ADD val pis: List -ADD val piCount: Int -ADD val squareRoots: List ADD fun getFactorsByType(irrationalType: String): List> diff --git a/signatures/v1.0.0/v1.0.0.md b/signatures/v1.0.0/v1.0.0.md index 4bb84c3..bebfdca 100644 --- a/signatures/v1.0.0/v1.0.0.md +++ b/signatures/v1.0.0/v1.0.0.md @@ -127,10 +127,6 @@ class Term: Number() val coefficient: ExactFraction val factors: List> -val logs: List -val pis: List -val piCount: Int -val squareRoots: List fun getFactorsByType(irrationalType: String): List> fun getSimplified(): Term