Skip to content

Commit

Permalink
A little refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
belyaev-mikhail committed Mar 24, 2020
1 parent 8452cae commit d1e0c94
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
28 changes: 28 additions & 0 deletions src/main/kotlin/ru/spbstu/DSL.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.spbstu

import kotlin.reflect.KProperty


object SymbolicScope {
operator fun getValue(self: Any?, prop: KProperty<*>): Var = Var(prop.name)

operator fun Rational.invoke() = Const(this)
operator fun Long.invoke() = Const(Rational(this))
operator fun Int.invoke() = Const(Rational(this.toLong()))

operator fun Rational.plus(that: Symbolic): Symbolic = Const(this).plus(that)
operator fun Long.plus(that: Symbolic): Symbolic = Const(this).plus(that)
operator fun Int.plus(that: Symbolic): Symbolic = Const(this).plus(that)

operator fun Rational.minus(that: Symbolic): Symbolic = Const(this).minus(that)
operator fun Long.minus(that: Symbolic): Symbolic = Const(this).minus(that)
operator fun Int.minus(that: Symbolic): Symbolic = Const(this).minus(that)

operator fun Rational.times(that: Symbolic): Symbolic = Const(this).times(that)
operator fun Long.times(that: Symbolic): Symbolic = Const(this).times(that)
operator fun Int.times(that: Symbolic): Symbolic = Const(this).times(that)

operator fun Rational.div(that: Symbolic): Symbolic = Const(this).div(that)
operator fun Long.div(that: Symbolic): Symbolic = Const(this).div(that)
operator fun Int.div(that: Symbolic): Symbolic = Const(this).div(that)
}
18 changes: 5 additions & 13 deletions src/main/kotlin/ru/spbstu/Symbolics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ sealed class SumLike: Symbolic() {
abstract fun asSum(): Sum
}
sealed class ProductLike: SumLike() {
abstract fun asProd(): Product
abstract fun asProduct(): Product
}
sealed class AtomLike: ProductLike() {
override fun asSum(): Sum = Sum(parts = mapOf(this to Rational.ONE))
override fun asProd(): Product = Product(parts = mapOf(this to Rational.ONE))
override fun asProduct(): Product = Product(parts = mapOf(this to Rational.ONE))
override fun simplify(): Symbolic = this
}
data class Const(val value: Rational): AtomLike() {
constructor(value: Long): this(Rational(value))
constructor(value: Int): this(Rational(value))

override fun asSum(): Sum = Sum(constant = value)
override fun asProd(): Product = Product(constant = value)
override fun asProduct(): Product = Product(constant = value)

override fun subst(substitution: Map<Var, Symbolic>) = this
override fun <C : MutableCollection<Var>> vars(mutableCollection: C): C = mutableCollection
Expand Down Expand Up @@ -167,7 +167,7 @@ data class Product(val constant: Rational = Rational.ONE,
override fun asSum(): Sum = Sum(parts = mapOf(copy(constant = Rational.ONE) to constant))
override fun simplify(): Symbolic = simplifyData(constant, parts)

override fun asProd(): Product = this
override fun asProduct(): Product = this

override fun subst(substitution: Map<Var, Symbolic>): Symbolic =
of(Const(constant), *parts.mapToArray { c, r -> c.subst(substitution) pow r })
Expand Down Expand Up @@ -217,7 +217,7 @@ data class Product(val constant: Rational = Rational.ONE,
var constantBuilder: Rational = Rational.ONE
val partsBuilder: PartsBuilder<AtomLike> = PartsBuilder()
for(part in prods) {
val prod = part.asProd()
val prod = part.asProduct()
constantBuilder *= prod.constant
for ((k, v) in prod.parts) {
partsBuilder[k] += v
Expand Down Expand Up @@ -325,11 +325,3 @@ infix fun Symbolic.pow(power: Rational): Symbolic = when {
}
}
}

object SymbolicScope {
operator fun getValue(self: Any?, prop: KProperty<*>): Var = Var(prop.name)

operator fun Rational.invoke() = Const(this)
operator fun Long.invoke() = Const(Rational(this))
operator fun Int.invoke() = Const(Rational(this.toLong()))
}
2 changes: 1 addition & 1 deletion src/test/kotlin/ru/spbstu/RowSumTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class RowSumTest {

val i by SymbolicScope

fun crossCheck(functionOfI: Symbolic) {
private fun crossCheck(functionOfI: Symbolic) {
val v = Var.fresh()
val sum = RowSum.of(i, Const.ONE, v, functionOfI)
assertEquals(finiteDifference(sum, v), functionOfI.subst(i to v))
Expand Down

0 comments on commit d1e0c94

Please sign in to comment.