Skip to content

Commit

Permalink
Introducing ⊠ as a synonym for the ring module multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikael Vejdemo-Johansson committed Apr 22, 2024
1 parent 14e68b2 commit eb97842
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/org/appliedtopology/tda4j/Chain.scala
Expand Up @@ -385,7 +385,7 @@ package org.appliedtopology.tda4j {
scale(-fr.one, x)

override def shows(x: ChainElement[CellT, CoefficientT]): String =
x.chainHeap.toList.map((cell, coeff) => s"${coeff.toString} *> ${cell.toString}").mkString(" + ")
x.chainHeap.toList.map((cell, coeff) => s"${coeff.toString} ${cell.toString}").mkString(" + ")

override def show(x: ChainElement[CellT, CoefficientT]): Cord = Cord(shows(x))
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/scala/org/appliedtopology/tda4j/RingModule.scala
@@ -1,5 +1,7 @@
package org.appliedtopology.tda4j

import scalaz.Show

import scala.annotation.targetName

/** Specifies what it means for the type `T` to be a module (or vector space) over the [Numeric] (ie ring-like) type
Expand All @@ -26,12 +28,15 @@ trait RingModule[T, R] {
def -(rhs: T): T = minus(t, rhs)
@targetName("scalarMultiplyRight")
def <*(rhs: R): T = scale(rhs, t)
infix def mul(rhs: R): T = scale(rhs,t)
def unary_- : T = negate(t)
}

extension (r: R) {
@targetName("scalarMultiplyLeft")
def *>(t: T): T = scale(r, t)
@targetName("scalarMultiplyLeft2")
def (t:T): T = scale(r,t) // unicode ⊠ for boxed times
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/org/appliedtopology/tda4j/APISpec.scala
Expand Up @@ -10,7 +10,7 @@ class APISpec extends mutable.Specification {
import ctx.{given, *}

"we should be able to create and compute with chains" >> {
1.0 *> s(1, 2) - s(2, 3) should beEqualTo(
1.0 s(1, 2) - s(2, 3) should beEqualTo(
Chain(Simplex(1, 2) -> 1.0, Simplex(2, 3) -> -1.0)
)
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/scala/org/appliedtopology/tda4j/ChainSpec.scala
Expand Up @@ -199,9 +199,9 @@ class HeapChainSpec extends mutable.Specification {
given Conversion[Simplex, HeapChain[Simplex, Double]] = HeapChain.apply
given rm: RingModule[HeapChain[Simplex, Double], Double] = HeapChainOps()
import rm.{given, *}
val z1 = 1.0 *> s(1, 2) + 2.0 *> s(1, 3) - 1.0 *> s(2, 3)
val z2 = 1.0 *> s(1, 2) + 1.0 *> s(2, 3)
val z3 = z2 + (-1.0 *> z2)
val z1 : HeapChain[Simplex,Double] = 1.0 s(1, 2) + (s(1, 3) mul 2.0) - 1.0 *> s(2, 3)
val z2 : HeapChain[Simplex,Double] = 1.0 s(1, 2) + 1.0 s(2, 3)
val z3 : HeapChain[Simplex,Double] = z2 + (-1.0 z2)

"subtracts to zero" ==>
(z3.isZero must beTrue)
Expand Down

0 comments on commit eb97842

Please sign in to comment.