Skip to content

Commit

Permalink
feat: term inverse + simple expression
Browse files Browse the repository at this point in the history
  • Loading branch information
lbressler13 committed Feb 2, 2024
1 parent 18d5ab6 commit 77388a4
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package xyz.lbres.exactnumbers.expressions.expression

abstract class Expression : Number() {
abstract operator fun unaryMinus(): Expression
abstract operator fun unaryPlus(): Expression
abstract fun inverse(): Expression

// open operator fun plus(other: Expression): Expression = AdditiveExpression(constMultiSetOf(this, other))
// open operator fun minus(other: Expression): Expression = AdditiveExpression(constMultiSetOf(this, -other))
// open operator fun times(other: Expression): Expression = MultiplicativeExpression(constMultiSetOf(this, other))
// open operator fun div(other: Expression): Expression = MultiplicativeExpression(constMultiSetOf(this, other.inverse()))

override fun toByte(): Byte = 0 // TODO
override fun toChar(): Char = '0' // TODO
override fun toShort(): Short = 0 // TODO
override fun toInt(): Int = 0 // TODO
override fun toLong(): Long = 0 // TODO
override fun toFloat(): Float = 0f // TODO
override fun toDouble(): Double = 0.0 // TODO
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package xyz.lbres.exactnumbers.expressions.expression

import xyz.lbres.exactnumbers.expressions.term.Term

/**
* Expression consisting of a single term
*/
internal class SimpleExpression(private val term: Term) : Expression() {
override fun unaryMinus(): Expression = SimpleExpression(-term)

override fun unaryPlus(): Expression = this

override fun inverse(): Expression = SimpleExpression(term.inverse())

override fun equals(other: Any?): Boolean = other is SimpleExpression && term == other.term
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sealed class Term : Number() {
abstract operator fun div(other: Term): Term

abstract fun isZero(): Boolean
abstract fun inverse(): Term

/**
* Simplify coefficient and factors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ internal class TermImpl(coefficient: ExactFraction, factors: ConstMultiSet<Irrat

override fun isZero(): Boolean = coefficient.isZero()

override fun inverse(): Term {
if (isZero()) {
throw divideByZero
}

val newFactors = factorSet.mapToSet { it.inverse() }
return TermImpl(coefficient.inverse(), newFactors.toConstMultiSet())
}

/**
* Simplify coefficient and factors
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package xyz.lbres.exactnumbers.expressions.expression

import kotlin.test.Test

class SimpleExpressionTest {
@Test
fun testConstructor() {
// TODO
}

@Test
fun testUnaryMinus() {
// TODO
}

@Test
fun testUnaryPlus() {
// TODO
}

@Test
fun testInverse() {
// TODO
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TermTest {
@Test fun testUnaryMinus() = runUnaryMinusTests()
@Test fun testUnaryPlus() = runUnaryPlusTests()
@Test fun testIsZero() = runIsZeroTests()
@Test fun testInverse() = runInverseTests()

@Test fun testGetFactorsByType() = runGetFactorsByTypeTests()
@Test fun testGetLogs() = runGetLogsTests()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import xyz.lbres.exactnumbers.exactfraction.ExactFraction
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.testutils.assertDivByZero
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
Expand Down Expand Up @@ -94,3 +95,45 @@ fun runIsZeroTests() {

assertFalse(term.isZero())
}

fun runInverseTests() {
assertDivByZero { Term.ZERO.inverse() }

var term1 = Term.ONE
var term2 = Term.ONE
assertEquals(term1, term2.inverse())

term1 = Term.fromValues(ExactFraction(17, 12), emptyList())
term2 = Term.fromValues(ExactFraction(12, 17), emptyList())
assertEquals(term1, term2.inverse())
assertEquals(term2, term1.inverse())

term1 = Term.fromValues(one, listOf(log3))
term2 = Term.fromValues(one, listOf(log3.inverse()))
assertEquals(term1, term2.inverse())
assertEquals(term2, term1.inverse())

term1 = Term.fromValues(one, listOf(pi, piInverse, sqrt2))
term2 = Term.fromValues(one, listOf(pi, piInverse, sqrt2.inverse()))
assertEquals(term1, term2.inverse())
assertEquals(term2, term1.inverse())

term1 = Term.fromValues(ExactFraction(-1, 9), listOf(testNumber2, testNumber2, pi, log4, pi, sqrt1))
term2 = Term.fromValues(
-ExactFraction.NINE,
listOf(testNumber2.inverse(), testNumber2.inverse(), piInverse, piInverse, log4.inverse(), sqrt1.inverse())
)
assertEquals(term1, term2.inverse())
assertEquals(term2, term1.inverse())

term1 = Term.fromValues(
ExactFraction(100, 99999999999),
listOf(Log(4), pi, pi, Log(14, 3))
)
term2 = Term.fromValues(
ExactFraction(99999999999, 100),
listOf(Log(4).inverse(), piInverse, piInverse, Log(14, 3).inverse())
)
assertEquals(term1, term2.inverse())
assertEquals(term2, term1.inverse())
}

0 comments on commit 77388a4

Please sign in to comment.