-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06c76a9
commit b977663
Showing
19 changed files
with
790 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 23 additions & 1 deletion
24
exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/expressions/ExpressionImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,26 @@ | ||
package xyz.lbres.exactnumbers.expressions | ||
|
||
import xyz.lbres.exactnumbers.utils.castToByte | ||
import xyz.lbres.exactnumbers.utils.castToChar | ||
import xyz.lbres.exactnumbers.utils.castToDouble | ||
import xyz.lbres.exactnumbers.utils.castToFloat | ||
import xyz.lbres.exactnumbers.utils.castToInt | ||
import xyz.lbres.exactnumbers.utils.castToLong | ||
import xyz.lbres.exactnumbers.utils.castToShort | ||
import java.math.BigDecimal | ||
|
||
// internal implementation of expression | ||
internal abstract class ExpressionImpl : Expression() | ||
@Suppress("EqualsOrHashCode") | ||
internal abstract class ExpressionImpl : Expression() { | ||
override fun getValue(): BigDecimal = toTerm().getValue() | ||
|
||
override fun equals(other: Any?): Boolean = other is Expression && toTerm() == other.toTerm() | ||
|
||
override fun toByte(): Byte = castToByte(getValue(), this, "Expression") | ||
override fun toChar(): Char = castToChar(getValue(), this, "Expression") | ||
override fun toShort(): Short = castToShort(getValue(), this, "Expression") | ||
override fun toInt(): Int = castToInt(getValue(), this, "Expression") | ||
override fun toLong(): Long = castToLong(getValue(), this, "Expression") | ||
override fun toFloat(): Float = castToFloat(getValue(), this, "Expression") | ||
override fun toDouble(): Double = castToDouble(getValue(), this, "Expression") | ||
} |
53 changes: 53 additions & 0 deletions
53
...src/main/kotlin/xyz/lbres/exactnumbers/expressions/expression/MultiplicativeExpression.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package xyz.lbres.exactnumbers.expressions.expression | ||
|
||
import xyz.lbres.exactnumbers.expressions.Expression | ||
import xyz.lbres.exactnumbers.expressions.ExpressionImpl | ||
import xyz.lbres.exactnumbers.expressions.term.Term | ||
import xyz.lbres.exactnumbers.utils.createHashCode | ||
import xyz.lbres.exactnumbers.utils.getOrSet | ||
import xyz.lbres.kotlinutils.bigdecimal.ext.isZero | ||
import xyz.lbres.kotlinutils.collection.ext.toConstMultiSet | ||
import xyz.lbres.kotlinutils.set.multiset.anyConsistent | ||
import xyz.lbres.kotlinutils.set.multiset.const.ConstMultiSet | ||
import xyz.lbres.kotlinutils.set.multiset.const.constMultiSetOf | ||
import xyz.lbres.kotlinutils.set.multiset.mapToSetConsistent | ||
|
||
/** | ||
* Expression which is the product of several other expressions | ||
*/ | ||
@Suppress("EqualsOrHashCode") | ||
internal class MultiplicativeExpression private constructor(expressions: ConstMultiSet<Expression>) : ExpressionImpl() { | ||
private val expressions: ConstMultiSet<Expression> | ||
private var term: Term? = null | ||
|
||
init { | ||
this.expressions = when { | ||
expressions.isEmpty() -> throw Exception("Invalid expression") | ||
expressions.anyConsistent { it.getValue().isZero() } -> constMultiSetOf(ZERO) | ||
else -> expressions | ||
} | ||
} | ||
|
||
constructor(expr1: Expression, expr2: Expression) : this(constMultiSetOf(expr1, expr2)) | ||
|
||
override fun unaryPlus(): Expression = this | ||
override fun unaryMinus(): Expression { | ||
val newExpressions = (expressions + constMultiSetOf(-ONE)).toConstMultiSet() | ||
return MultiplicativeExpression(newExpressions) | ||
} | ||
|
||
override fun inverse(): Expression { | ||
val newExpressions = expressions.mapToSetConsistent { it.inverse() }.toConstMultiSet() | ||
return MultiplicativeExpression(newExpressions) | ||
} | ||
|
||
override fun toTerm(): Term { | ||
return getOrSet({ term }, { term = it }) { | ||
expressions.fold(Term.ONE) { acc, expr -> acc * expr.toTerm() }.getSimplified() | ||
} | ||
} | ||
|
||
override fun hashCode(): Int = createHashCode(listOf(expressions, "MultiplicativeExpression")) | ||
|
||
override fun toString(): String = "(${expressions.joinToString("x")})" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...test/kotlin/xyz/lbres/exactnumbers/expressions/expression/MultiplicativeExpressionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package xyz.lbres.exactnumbers.expressions.expression | ||
|
||
import xyz.lbres.exactnumbers.expressions.expression.multiplicative.* // ktlint-disable no-wildcard-imports no-unused-imports | ||
import kotlin.test.Test | ||
|
||
class MultiplicativeExpressionTest { | ||
@Test fun testEquals() = runEqualsTests() | ||
@Test fun testToString() = runToStringTests() | ||
|
||
@Test fun testUnaryMinus() = runUnaryMinusTests() | ||
@Test fun testUnaryPlus() = runUnaryPlusTests() | ||
@Test fun testInverse() = runInverseTests() | ||
@Test fun testGetValue() = runGetValueTests() | ||
|
||
@Test fun testToTerm() = runToTermTests() | ||
@Test fun testToByte() = runToByteTests() | ||
@Test fun testToChar() = runToCharTests() | ||
@Test fun testToShort() = runToShortTests() | ||
@Test fun testToInt() = runToIntTests() | ||
@Test fun testToLong() = runToLongTests() | ||
@Test fun testToFloat() = runToFloatTests() | ||
@Test fun testToDouble() = runToDoubleTests() | ||
} |
Oops, something went wrong.