From 1de1de88af51f3f1a99f92d1142f73bcc9e9e630 Mon Sep 17 00:00:00 2001 From: breandan Date: Mon, 24 May 2021 22:35:47 -0400 Subject: [PATCH] cleanup and update versions --- README.md | 10 +++--- build.gradle.kts | 6 ++-- notebooks/Hello Kaliningraph.ipynb | 2 +- notebooks/Program Graphs.ipynb | 2 +- .../kotlin/edu/mcgill/kaliningraph/Graph.kt | 11 +------ .../edu/mcgill/kaliningraph/LabeledGraph.kt | 10 ++++-- .../kotlin/edu/mcgill/kaliningraph/Utils.kt | 8 ++--- .../mcgill/kaliningraph/typefamily/IGraph.kt | 32 +++++++++++-------- .../mcgill/kaliningraph/LabeledGraphTest.kt | 2 +- .../edu/mcgill/kaliningraph/Rewriter.kt | 2 +- 10 files changed, 41 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 1364908b..e8a7fb04 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Kaliningraph -[![Kotlin 1.5.0](https://img.shields.io/badge/Kotlin-1.5.0-blue.svg?style=flat&logo=kotlin)](http://kotlinlang.org) +[![Kotlin 1.5.10](https://img.shields.io/badge/Kotlin-1.5.10-blue.svg?style=flat&logo=kotlin)](http://kotlinlang.org) [![](https://jitpack.io/v/breandan/kaliningraph.svg)](https://jitpack.io/#breandan/kaliningraph) [![CI](https://github.com/breandan/kaliningraph/workflows/CI/badge.svg)](https://github.com/breandan/kaliningraph/actions) @@ -20,7 +20,7 @@ repositories { } dependencies { - implementation("com.github.breandan:kaliningraph:0.1.6") + implementation("com.github.breandan:kaliningraph:0.1.7") } ``` @@ -38,7 +38,7 @@ dependencies { com.github.breandan kaliningraph - 0.1.6 + 0.1.7 ``` @@ -49,7 +49,7 @@ To access notebook support, use the following line magic: ``` @file:Repository("https://jitpack.io") -@file:DependsOn("com.github.breandan:kaliningraph:0.1.6") +@file:DependsOn("com.github.breandan:kaliningraph:0.1.7") ``` For more information, explore our tutorials: @@ -67,7 +67,7 @@ What are neighbors? Neighbors are a graph. ## Getting Started -Run [the demo](src/main/kotlin/edu/mcgill/kaliningraph/HelloKaliningraph.kt) via `./gradlew HelloKaliningraph` to get started. +Run [the demo](src/test/kotlin/edu/mcgill/kaliningraph/HelloKaliningraph.kt) via `./gradlew HelloKaliningraph` to get started. ## Usage diff --git a/build.gradle.kts b/build.gradle.kts index cae363ff..9937ede7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,13 +4,13 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { `maven-publish` - kotlin("jvm") version "1.5.0" - kotlin("jupyter.api") version "0.10.0-17" + kotlin("jvm") version "1.5.10" + kotlin("jupyter.api") version "0.10.0-42" id("com.github.ben-manes.versions") version "0.38.0" } group = "com.github.breandan" -version = "0.1.6" +version = "0.1.7" repositories { mavenCentral() diff --git a/notebooks/Hello Kaliningraph.ipynb b/notebooks/Hello Kaliningraph.ipynb index fc388c23..3453266e 100644 --- a/notebooks/Hello Kaliningraph.ipynb +++ b/notebooks/Hello Kaliningraph.ipynb @@ -9,7 +9,7 @@ "outputs": [], "source": [ "@file:Repository(\"https://jitpack.io\")\n", - "@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.6\")" + "@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.7\")" ] }, { diff --git a/notebooks/Program Graphs.ipynb b/notebooks/Program Graphs.ipynb index 725e39c5..cd99ee81 100644 --- a/notebooks/Program Graphs.ipynb +++ b/notebooks/Program Graphs.ipynb @@ -7,7 +7,7 @@ "outputs": [], "source": [ "@file:Repository(\"https://jitpack.io\")\n", - "@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.6\")" + "@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.7\")" ] }, { diff --git a/src/main/kotlin/edu/mcgill/kaliningraph/Graph.kt b/src/main/kotlin/edu/mcgill/kaliningraph/Graph.kt index 8a45b748..c0d3a4ac 100644 --- a/src/main/kotlin/edu/mcgill/kaliningraph/Graph.kt +++ b/src/main/kotlin/edu/mcgill/kaliningraph/Graph.kt @@ -2,12 +2,9 @@ package edu.mcgill.kaliningraph import edu.mcgill.kaliningraph.matrix.* import edu.mcgill.kaliningraph.typefamily.* -import guru.nidi.graphviz.attribute.Label -import guru.nidi.graphviz.model.* import org.ejml.kotlin.* -import kotlin.math.* +import kotlin.math.sqrt import kotlin.random.Random -import kotlin.reflect.KProperty abstract class Graph(override val vertices: Set = setOf()): IGraph, @@ -149,8 +146,6 @@ abstract class Graph(override val vertices: Set = setOf()): abstract class Edge(override val source: V, override val target: V): IEdge where G: Graph, E: Edge, V: Vertex { override val graph by lazy { target.graph } - operator fun component1() = source - operator fun component2() = target } abstract class Vertex(override val id: String): IVertex @@ -160,10 +155,6 @@ abstract class Vertex(override val id: String): IVertex override val neighbors by lazy { outgoing.map { it.target }.toSet() } override val outdegree by lazy { neighbors.size } - override fun encode(): DoubleArray = id.vectorize() - - override operator fun getValue(a: Any?, prop: KProperty<*>): V = V(prop.name) - override fun render(): MutableNode = Factory.mutNode(id).add(Label.of(toString())) override fun equals(other: Any?) = (other as? Vertex<*, *, *>)?.encode().contentEquals(encode()) override fun hashCode() = id.hashCode() diff --git a/src/main/kotlin/edu/mcgill/kaliningraph/LabeledGraph.kt b/src/main/kotlin/edu/mcgill/kaliningraph/LabeledGraph.kt index e6e91682..3070d2af 100644 --- a/src/main/kotlin/edu/mcgill/kaliningraph/LabeledGraph.kt +++ b/src/main/kotlin/edu/mcgill/kaliningraph/LabeledGraph.kt @@ -53,6 +53,11 @@ open class LabeledGraph(override val vertices: Set = setOf()): companion object: LabeledGraph() { operator fun invoke(builder: LGBuilder.() -> Unit) = LGBuilder().also { it.builder() }.mutGraph + + operator fun invoke(graph: String) = + graph.split(" ").fold(G()) { acc, it -> + acc + G(*it.toList().zipWithNext().toTypedArray()) + } } var accumuator = mutableSetOf() @@ -63,7 +68,7 @@ open class LabeledGraph(override val vertices: Set = setOf()): fun rewrite(substitution: Pair) = randomWalk().take(200).toList().joinToString("") .replace(substitution.first, substitution.second) - .let { LabeledGraph.G(it) } + .let { LabeledGraph(it) } fun propagate() { val (previousStates, unoccupied) = vertices.partition { it.occupied } @@ -96,8 +101,7 @@ open class LabeledEdge( override val source: LGVertex, override val target: LGVertex, val label: String? = null -): - Edge(source, target) { +): Edge(source, target) { constructor(source: LGVertex, target: LGVertex): this(source, target, null) override fun render() = diff --git a/src/main/kotlin/edu/mcgill/kaliningraph/Utils.kt b/src/main/kotlin/edu/mcgill/kaliningraph/Utils.kt index 25bf3595..bc2c65e7 100644 --- a/src/main/kotlin/edu/mcgill/kaliningraph/Utils.kt +++ b/src/main/kotlin/edu/mcgill/kaliningraph/Utils.kt @@ -63,7 +63,7 @@ fun BMat.show(filename: String = "temp") = matToImg().let { data -> } }.show() -val browserCmd = System.getProperty("os.name").toLowerCase().let { os -> +val browserCmd = System.getProperty("os.name").lowercase().let { os -> when { "win" in os -> "rundll32 url.dll,FileProtocolHandler" "mac" in os -> "open" @@ -91,8 +91,6 @@ fun BMat.matToImg(f: Int = 20) = toEJMLSparse().matToImg(f) fun randomString() = UUID.randomUUID().toString().take(5) -private operator fun Pair.component2(): V = second -private operator fun Pair.component1(): K = first operator fun MutableNode.minus(target: LinkTarget): Link = addLink(target).links().last()!! fun randomMatrix(rows: Int, cols: Int = rows, rand: () -> Double = { Random.Default.nextDouble() }) = @@ -101,7 +99,7 @@ fun randomMatrix(rows: Int, cols: Int = rows, rand: () -> Double = { Random.Defa fun randomVector(size: Int, rand: () -> Double = { Random.Default.nextDouble() }) = Array(size) { rand() }.toDoubleArray() -fun Array.toEJMLSparse() = SpsMat(size, this[0].size, sumBy { it.count { it == 0.0 } }) +fun Array.toEJMLSparse() = SpsMat(size, this[0].size, sumOf { it.count { it == 0.0 } }) .also { s -> for (i in indices) for (j in this[0].indices) this[i][j].let { if (0.0 < it) s[i, j] = it } } fun Array.toEJMLDense() = DMatrixRMaj(this) @@ -115,7 +113,7 @@ fun powBench(constructor: T, matmul: (T, T) -> T): Long = measureTimeMillis { constructor.power(100, matmul) } fun T.power(exp: Int, matmul: (T, T) -> T) = - (0..exp).fold(this) { acc, i -> matmul(acc, this) } + generateSequence(this) { matmul(it, this) }.take(exp) const val DEFAULT_FEATURE_LEN = 20 fun String.vectorize(len: Int = DEFAULT_FEATURE_LEN) = diff --git a/src/main/kotlin/edu/mcgill/kaliningraph/typefamily/IGraph.kt b/src/main/kotlin/edu/mcgill/kaliningraph/typefamily/IGraph.kt index 62331261..15f9c206 100644 --- a/src/main/kotlin/edu/mcgill/kaliningraph/typefamily/IGraph.kt +++ b/src/main/kotlin/edu/mcgill/kaliningraph/typefamily/IGraph.kt @@ -49,10 +49,6 @@ interface IGF, E: IEdge, V: IVertex> { } ) - // TODO: generify, only works for labeled graphs - fun G(graph: String): G = - graph.split(" ").fold(G()) { acc, it -> acc + G(it.toCharArray().toList()) } - // Gafter's gadget! http://gafter.blogspot.com/2006/12/super-type-tokens.html private fun gev(): Array> = (generateSequence(javaClass) { it.superclass as Class> } @@ -83,11 +79,16 @@ interface IGraph: IGF, Set, (V) -> Set * - Pros: Useful for describing many algebraic path problems * - Cons: Esoteric API / unsuitable as an abstract interface * - * Algebraic perspective : https://github.com/snowleopard/alga-paper/releases/download/final/algebraic-graphs.pdf - * Type-family perspective : https://www.cs.cornell.edu/~ross/publications/shapes/shapes-pldi14-tr.pdf#page=3 - * Inductive perspective : https://web.engr.oregonstate.edu/~erwig/papers/InductiveGraphs_JFP01.pdf - * Mathematical perspective : https://doi.org/10.1007/978-0-387-75450-5 - * Semiring perspective : http://stedolan.net/research/semirings.pdf + * Algebraic perspective : https://github.com/snowleopard/alga-paper/releases/download/final/algebraic-graphs.pdf + * : https://arxiv.org/pdf/1909.04881.pdf + * Type-family perspective : https://www.cs.cornell.edu/~ross/publications/shapes/shapes-pldi14-tr.pdf#page=3 + * : https://www.cs.cornell.edu/andru/papers/familia/familia.pdf#page=8 + * Inductive perspective : https://web.engr.oregonstate.edu/~erwig/papers/InductiveGraphs_JFP01.pdf + * : https://doi.org/10.1145/258949.258955 + * : https://www.cs.utexas.edu/~wcook/Drafts/2012/graphs.pdf + * Semiring perspective : http://stedolan.net/research/semirings.pdf + * : https://doi.org/10.1007/978-0-387-75450-5 + * : https://doi.org/10.2200/S00245ED1V01Y201001CNT003 */ where G: IGraph, E: IEdge, V: IVertex { @@ -134,6 +135,9 @@ interface IEdge: IGF val source: V val target: V + operator fun component1() = source + operator fun component2() = target + fun render(): Link = (source.render() - target.render()).add(Label.of("")) } @@ -146,8 +150,8 @@ interface IVertex: IGF, Encodable val outgoing: Set get() = edgeMap(this as V).toSet() val edgeMap: (V) -> Set // Make a self-loop by passing this - open val neighbors get() = outgoing.map { it.target }.toSet() - open val outdegree get() = neighbors.size + val neighbors get() = outgoing.map { it.target }.toSet() + val outdegree get() = neighbors.size // tailrec prohibited on open members? may be possible with deep recursion // https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deep-recursive-function/ @@ -181,6 +185,6 @@ infix fun Collection.allAre(that: Any) = all { it isA that } infix fun Collection.anyAre(that: Any) = any { it isA that } // https://github.com/amodeus-science/amod -//abstract class Map : IGraph -//abstract class Road : IEdge -//abstract class City : IVertex \ No newline at end of file +abstract class TMap : IGraph +abstract class TRoad : IEdge +abstract class TCity : IVertex \ No newline at end of file diff --git a/src/test/kotlin/edu/mcgill/kaliningraph/LabeledGraphTest.kt b/src/test/kotlin/edu/mcgill/kaliningraph/LabeledGraphTest.kt index 2ae93d43..b2f941d2 100644 --- a/src/test/kotlin/edu/mcgill/kaliningraph/LabeledGraphTest.kt +++ b/src/test/kotlin/edu/mcgill/kaliningraph/LabeledGraphTest.kt @@ -20,5 +20,5 @@ class LabeledGraphTest { ) @Test - fun testStringConstructor() = assertEquals(graph, LabeledGraph.G("abcde ace")) + fun testStringConstructor() = assertEquals(graph, LabeledGraph("abcde ace")) } \ No newline at end of file diff --git a/src/test/kotlin/edu/mcgill/kaliningraph/Rewriter.kt b/src/test/kotlin/edu/mcgill/kaliningraph/Rewriter.kt index e38062ea..e9e36be1 100644 --- a/src/test/kotlin/edu/mcgill/kaliningraph/Rewriter.kt +++ b/src/test/kotlin/edu/mcgill/kaliningraph/Rewriter.kt @@ -8,7 +8,7 @@ import kotlin.random.Random @ExperimentalStdlibApi fun main() { animate( - LabeledGraph.G("abcdecfghia").also { println(it) } + LabeledGraph("abcdecfghia").also { println(it) } ) { _: Document, it: KeyboardEvent, graphs: MutableList -> when { "Left" in it.key -> {