Skip to content

Commit

Permalink
initial permutation code
Browse files Browse the repository at this point in the history
  • Loading branch information
fdilke committed Feb 25, 2024
1 parent 490c6d3 commit 65db1f2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import com.fdilke.bewl2.sets.SetsUtilities.bulkJoin
trait SetsGroupAssistant extends BaseSets:
Ɛ: FindGroupActionGenerators =>

override protected val groupAssistant: GroupAssistant = LocalGroupAssistant
override protected val groupAssistant: GroupAssistant =
LocalGroupAssistant

object LocalGroupAssistant extends GroupAssistant:
def findCandidates[G, X, Y](group: Group[G])(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,56 @@
package com.fdilke.bewl2.sets.permutations

class Permutation[T](
map: Map[T, T]
class Permutation[A](
map: Map[A, A]
):
()
def domain: Set[A] =
map.keySet
def apply(a: A): A =
map.get(a).getOrElse(
throw new IllegalArgumentException(s"key $a not found in map $map")
)

class Cycle[A](
cycle: Seq[A]
) {
if (cycle.toSet.size < cycle.length)
throw new IllegalArgumentException("cycle repeats")

def toMap: Map[A, A] =
val x: Seq[(A, A)] = cycle.indices.map { i =>
cycle(i) -> cycle( (i + 1) % cycle.length )
}
x.toMap[A, A]
}

class OngoingPermutation[A](
cycles: Seq[Cycle[A]]
):
def toPermutation: Permutation[A] =
new Permutation(
cycles map {
_.toMap
} reduce {
_ ++ _
}
)

def apply(a: A, tail: A*): OngoingPermutation[A] =
new OngoingPermutation(
cycles :+ Cycle(a +: tail)
)

object Permutation:
def apply[A](a: A, tail: A*): OngoingPermutation[A] =
new OngoingPermutation(
Seq[Cycle[A]](
Cycle(a +: tail)
)
)

implicit def convertOngoing[A](
ongoing: OngoingPermutation[A]
): Permutation[A] =
ongoing.toPermutation


Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import munit.FunSuite
import com.fdilke.bewl2.utility.RichFunSuite._
import SetsUtilities.*
import SetsWithSlowActions.*

import scala.language.postfixOps

import java.util.concurrent.atomic.AtomicBoolean

class SetsUtilitiesSpec extends FunSuite:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.fdilke.bewl2.sets.permutations

import munit.FunSuite
import com.fdilke.bewl2.utility.RichFunSuite._
import Permutation._

class PermutationSpec extends FunSuite:
test("Permutation can permute distinct objects in a cycle, and knows its domain"):
Expand Down

0 comments on commit 65db1f2

Please sign in to comment.