-
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.
Expanded on the context-driven paradigm. Dramatically slimmed down im…
…plementation of `Chain` without any test regressions.
- Loading branch information
Mikael Vejdemo-Johansson
committed
Feb 23, 2024
1 parent
d57f975
commit 9ede311
Showing
11 changed files
with
222 additions
and
84 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
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,36 @@ | ||
package org.appliedtopology.tda4j | ||
|
||
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 `R`. | ||
* | ||
* @tparam T Type of the module elements. | ||
* @tparam R Type of the ring coefficients | ||
*/ | ||
trait RingModule[T, R] { | ||
val zero: T | ||
def plus(x: T, y: T): T | ||
def minus(x: T, y: T): T = plus(x, negate(y)) | ||
def negate(x: T): T = minus(zero, x) | ||
def scale(x: R, y: T): T | ||
|
||
extension (t: T) { | ||
@targetName("add") | ||
def +(rhs: T): T = plus(t, rhs) | ||
@targetName("subtract") | ||
def -(rhs: T): T = minus(t, rhs) | ||
@targetName("scalarMultiplyRight") | ||
def <*(rhs: R): T = scale(rhs, t) | ||
def unary_- = negate(t) | ||
} | ||
|
||
extension (r: R) { | ||
@targetName("scalarMultiplyLeft") | ||
def *>(t: T): T = scale(r, t) | ||
} | ||
} | ||
|
||
object RingModule: | ||
def apply[T,R](using rm: RingModule[T,R]) = rm | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
package org.appliedtopology | ||
|
||
import math.Ordering.Implicits.sortedSetOrdering | ||
|
||
/** Package for the Scala library TDA4j | ||
*/ | ||
package object tda4j {} | ||
package object tda4j { | ||
class TDAContext[VertexT : Ordering, CoefficientT : Fractional] | ||
extends ChainOps[AbstractSimplex[VertexT], CoefficientT], SimplexContext[VertexT] { | ||
given Conversion[Simplex, Chain[Simplex,CoefficientT]] = Chain.apply | ||
} | ||
} |
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,19 @@ | ||
package org.appliedtopology.tda4j | ||
|
||
import org.specs2.mutable | ||
|
||
class APISpec extends mutable.Specification { | ||
"""Test case class for developing the non-Scala facing API functionality | ||
|and the non-expert API functionality""".stripMargin | ||
|
||
given ctx : TDAContext[Char, Double]() | ||
import ctx.{*,given} | ||
|
||
"we should be able to create and compute with chains" >> { | ||
1.0 *> s(1,2) - s(2,3) should beEqualTo( | ||
Chain( | ||
Simplex(1,2) -> 1.0, | ||
Simplex(2,3) -> -1.0) | ||
) | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
src/test/scala/org/appliedtopology/tda4j/RingModuleSpec.scala
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,37 @@ | ||
package org.appliedtopology.tda4j | ||
import org.specs2.mutable | ||
|
||
class RingModuleSpec extends mutable.Specification { | ||
"""This is a test module for developing the RingModule[M,R] interface | ||
|and make sure that it does what we need it to do. | ||
|""".stripMargin | ||
|
||
object rm extends RingModule[(Int,Int), Int] { | ||
val zero: (Int, Int) = (0, 0) | ||
|
||
def plus(ls: (Int, Int), rs: (Int, Int)): (Int, Int) = (ls._1 + rs._1, ls._2 + rs._2) | ||
|
||
override def negate(x: (Int, Int)): (Int, Int) = (-x._1, -x._2) | ||
|
||
def scale(x: Int, y: (Int, Int)): (Int, Int) = (x * y._1, x * y._2) | ||
} | ||
|
||
given RingModule[(Int,Int), Int] = rm | ||
|
||
"zero should exist" >> { | ||
val v : (Int,Int) = rm.zero | ||
v must be_==(rm.zero) | ||
} | ||
|
||
"addition should work" >> { | ||
((2,3) + (4,5)) should be_== (6,8) | ||
} | ||
|
||
"scalar multiplication should work" >> { | ||
(2,3) <* 4 should be_== (8,12) | ||
} | ||
|
||
"scalar left-multiplication should work" >> { | ||
(4 *> (2,3)) should be_== (8,12) | ||
} | ||
} |
Oops, something went wrong.