-
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.
Swapped out to making
Simplex
a type alias for SortedSet
with top…
…ological methods added through self-type style type class implementations.
- Loading branch information
Mikael Vejdemo-Johansson
committed
Oct 23, 2024
1 parent
2991013
commit d7dc236
Showing
12 changed files
with
99 additions
and
159 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
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,81 +1,44 @@ | ||
package org.appliedtopology.tda4j | ||
|
||
import scala.collection.{SortedIterableFactory, SortedSetFactoryDefaults, StrictOptimizedSortedSetOps, mutable} | ||
import scala.collection.immutable.{Set, SortedMap, SortedSet, SortedSetOps, TreeSet} | ||
import scala.math.Ordering.IntOrdering | ||
import scala.math.Ordering.Double.IeeeOrdering | ||
import scala.collection.mutable | ||
import scala.collection.immutable.SortedSet | ||
import math.Ordering.Implicits.sortedSetOrdering | ||
|
||
/** Class representing an abstract simplex. Abstract simplices are given by sets (of totally ordered vertices) | ||
* and inherit from `Cell` so that the class has a `boundary` and a `dim` method. | ||
/** Simplices really are just sets, outright. We provide an implementation of the [OrderedCell] typeclass | ||
* for simplicial complex structures, to enable their use. | ||
* | ||
* You should never have reason to use the constructor directly (...and if you do, you should make sure to give the | ||
* internal `SortedSet` yourself) - instead use the factory method in the companion object. In code this means that | ||
* instead of `new Simplex[Self](a,b,c)` you would write `Simplex[Self](a,b,c)`. | ||
* | ||
* @param vertices | ||
* Vertices of the simplex | ||
* @param ordering | ||
* Ordering of the vertex type | ||
* @tparam VertexT | ||
* Vertex type | ||
*/ | ||
case class Simplex[VertexT : Ordering] private[tda4j] (vertices : SortedSet[VertexT]) { | ||
override def toString(): String = | ||
vertices.mkString(s"∆(", ",", ")") | ||
|
||
def union(other: Simplex[VertexT]) = | ||
new Simplex(vertices.union(other.vertices)) | ||
|
||
def incl(x : VertexT) = | ||
new Simplex(vertices + x) | ||
|
||
def contains(x : VertexT) = vertices.contains(x) | ||
} | ||
|
||
def simplexOrdering[VertexT : Ordering as vtxOrdering]: Ordering[Simplex[VertexT]] = | ||
Ordering.by{(spx: Simplex[VertexT]) => spx.vertices}(sortedSetOrdering[SortedSet, VertexT](vtxOrdering)) | ||
type Simplex[VertexT] = SortedSet[VertexT] | ||
|
||
given [VertexT : Ordering] => Ordering[Simplex[VertexT]] = simplexOrdering | ||
extension [VertexT : Ordering](spx : Simplex[VertexT]) | ||
def show : String = spx.mkString(s"∆(", ",", ")") | ||
|
||
// Ordering.by{(spx: Simplex[VertexT]) => spx.vertices}(sortedSetOrdering[SortedSet, VertexT](vtxOrdering)) | ||
|
||
|
||
/** Simplex companion object with factory methods | ||
*/ | ||
object Simplex { | ||
def apply[VertexT: Ordering](vertices: VertexT*) = | ||
new Simplex[VertexT](SortedSet.from(vertices)) | ||
|
||
def empty[VertexT: Ordering]: Simplex[VertexT] = | ||
new Simplex[VertexT](SortedSet.empty) | ||
|
||
def from[VertexT: Ordering](source: IterableOnce[VertexT]): Simplex[VertexT] = | ||
new Simplex(SortedSet.from(source.iterator)) | ||
|
||
def simplexIsOrderedCell[VertexT](using vtxOrd : Ordering[VertexT])(using ord : Ordering[Simplex[VertexT]]) : Simplex[VertexT] is OrderedCell = | ||
new (Simplex[VertexT] is OrderedCell): | ||
override lazy val ordering = ord | ||
extension (t: Simplex[VertexT]) { | ||
def boundary[CoefficientT](using fr: (CoefficientT is Field)): Chain[Simplex[VertexT], CoefficientT] = | ||
if (t.dim <= 0) Chain()(using ord) | ||
else Chain.from( | ||
t.vertices | ||
.to(Seq) | ||
.zipWithIndex | ||
.map((vtx, i) => Simplex.from(t.vertices.toSeq.patch(i, Seq.empty, 1))) | ||
.zip(Iterator.unfold(fr.one)(s => Some((s, fr.negate(s))))) | ||
)(using ord) | ||
def dim: Int = t.vertices.size - 1 | ||
} | ||
def compare(x: Simplex[VertexT], y: Simplex[VertexT]): Int = ord.compare(x, y) | ||
|
||
given [VertexT : Ordering as vtxOrdering] => (Simplex[VertexT] is OrderedCell) = | ||
simplexIsOrderedCell(using vtxOrdering)(using simplexOrdering[VertexT](using vtxOrdering)) | ||
} | ||
object Simplex: | ||
def from[VertexT : Ordering, T <: Seq[VertexT]](vertices : T) : Simplex[VertexT] = SortedSet.from(vertices) | ||
def apply[VertexT : Ordering](vertices : VertexT*) : Simplex[VertexT] = SortedSet.from(vertices) | ||
|
||
/** Convenience method for defining simplices | ||
* | ||
* The character ∆ is typed as Alt+J on Mac GB layout, and has unicode code 0x0394. | ||
*/ | ||
def ∆[T: Ordering](ts: T*): Simplex[T] = Simplex.from(ts) | ||
* | ||
* The character ∆ is typed as Alt+J on Mac GB layout, and has unicode code 0x0394. | ||
*/ | ||
def ∆[VertexT : Ordering](vertices : VertexT*) : Simplex[VertexT] = SortedSet.from(vertices) | ||
|
||
def simplexOrdering[VertexT](using vtxOrd : Ordering[VertexT]) : Ordering[Simplex[VertexT]] = sortedSetOrdering(vtxOrd) | ||
def SortedSet_is_OrderedCell[VertexT](using vtxOrd : Ordering[VertexT])(setOrdering : Ordering[SortedSet[VertexT]] = simplexOrdering(using vtxOrd)): (SortedSet[VertexT] is OrderedCell) = | ||
new(SortedSet[VertexT] is OrderedCell) { | ||
override lazy val ordering = setOrdering | ||
extension (spx: SortedSet[VertexT]) { | ||
override def dim = spx.size - 1 | ||
override def boundary[CoefficientT: Field as fr] = | ||
if (spx.dim <= 0) Chain() | ||
else Chain.from( | ||
spx.to(Seq) | ||
.zipWithIndex | ||
.map((vtx, i) => spx -- spx.slice(i, i + 1)) | ||
.zip(Iterator.unfold(fr.one)(s => Some((s, fr.negate(s))))) | ||
) | ||
} | ||
} | ||
given default_SortedSet_is_OrderedCell[VertexT : Ordering] : (SortedSet[VertexT] is OrderedCell) = | ||
SortedSet_is_OrderedCell[VertexT]() |
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
Oops, something went wrong.