Skip to content

Commit 1de1de8

Browse files
committed
cleanup and update versions
1 parent d93e0a5 commit 1de1de8

File tree

10 files changed

+41
-44
lines changed

10 files changed

+41
-44
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Kaliningraph
22

3-
[![Kotlin 1.5.0](https://img.shields.io/badge/Kotlin-1.5.0-blue.svg?style=flat&logo=kotlin)](http://kotlinlang.org)
3+
[![Kotlin 1.5.10](https://img.shields.io/badge/Kotlin-1.5.10-blue.svg?style=flat&logo=kotlin)](http://kotlinlang.org)
44
[![](https://jitpack.io/v/breandan/kaliningraph.svg)](https://jitpack.io/#breandan/kaliningraph)
55
[![CI](https://github.com/breandan/kaliningraph/workflows/CI/badge.svg)](https://github.com/breandan/kaliningraph/actions)
66

@@ -20,7 +20,7 @@ repositories {
2020
}
2121

2222
dependencies {
23-
implementation("com.github.breandan:kaliningraph:0.1.6")
23+
implementation("com.github.breandan:kaliningraph:0.1.7")
2424
}
2525
```
2626

@@ -38,7 +38,7 @@ dependencies {
3838
<dependency>
3939
<groupId>com.github.breandan</groupId>
4040
<artifactId>kaliningraph</artifactId>
41-
<version>0.1.6</version>
41+
<version>0.1.7</version>
4242
</dependency>
4343
</project>
4444
```
@@ -49,7 +49,7 @@ To access notebook support, use the following line magic:
4949

5050
```
5151
@file:Repository("https://jitpack.io")
52-
@file:DependsOn("com.github.breandan:kaliningraph:0.1.6")
52+
@file:DependsOn("com.github.breandan:kaliningraph:0.1.7")
5353
```
5454

5555
For more information, explore our tutorials:
@@ -67,7 +67,7 @@ What are neighbors? Neighbors are a graph.
6767

6868
## Getting Started
6969

70-
Run [the demo](src/main/kotlin/edu/mcgill/kaliningraph/HelloKaliningraph.kt) via `./gradlew HelloKaliningraph` to get started.
70+
Run [the demo](src/test/kotlin/edu/mcgill/kaliningraph/HelloKaliningraph.kt) via `./gradlew HelloKaliningraph` to get started.
7171

7272
## Usage
7373

build.gradle.kts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
44

55
plugins {
66
`maven-publish`
7-
kotlin("jvm") version "1.5.0"
8-
kotlin("jupyter.api") version "0.10.0-17"
7+
kotlin("jvm") version "1.5.10"
8+
kotlin("jupyter.api") version "0.10.0-42"
99
id("com.github.ben-manes.versions") version "0.38.0"
1010
}
1111

1212
group = "com.github.breandan"
13-
version = "0.1.6"
13+
version = "0.1.7"
1414

1515
repositories {
1616
mavenCentral()

notebooks/Hello Kaliningraph.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"outputs": [],
1010
"source": [
1111
"@file:Repository(\"https://jitpack.io\")\n",
12-
"@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.6\")"
12+
"@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.7\")"
1313
]
1414
},
1515
{

notebooks/Program Graphs.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"outputs": [],
88
"source": [
99
"@file:Repository(\"https://jitpack.io\")\n",
10-
"@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.6\")"
10+
"@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.7\")"
1111
]
1212
},
1313
{

src/main/kotlin/edu/mcgill/kaliningraph/Graph.kt

+1-10
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ package edu.mcgill.kaliningraph
22

33
import edu.mcgill.kaliningraph.matrix.*
44
import edu.mcgill.kaliningraph.typefamily.*
5-
import guru.nidi.graphviz.attribute.Label
6-
import guru.nidi.graphviz.model.*
75
import org.ejml.kotlin.*
8-
import kotlin.math.*
6+
import kotlin.math.sqrt
97
import kotlin.random.Random
10-
import kotlin.reflect.KProperty
118

129
abstract class Graph<G, E, V>(override val vertices: Set<V> = setOf()):
1310
IGraph<G, E, V>,
@@ -149,8 +146,6 @@ abstract class Graph<G, E, V>(override val vertices: Set<V> = setOf()):
149146
abstract class Edge<G, E, V>(override val source: V, override val target: V): IEdge<G, E, V>
150147
where G: Graph<G, E, V>, E: Edge<G, E, V>, V: Vertex<G, E, V> {
151148
override val graph by lazy { target.graph }
152-
operator fun component1() = source
153-
operator fun component2() = target
154149
}
155150

156151
abstract class Vertex<G, E, V>(override val id: String): IVertex<G, E, V>
@@ -160,10 +155,6 @@ abstract class Vertex<G, E, V>(override val id: String): IVertex<G, E, V>
160155
override val neighbors by lazy { outgoing.map { it.target }.toSet() }
161156
override val outdegree by lazy { neighbors.size }
162157

163-
override fun encode(): DoubleArray = id.vectorize()
164-
165-
override operator fun getValue(a: Any?, prop: KProperty<*>): V = V(prop.name)
166-
override fun render(): MutableNode = Factory.mutNode(id).add(Label.of(toString()))
167158
override fun equals(other: Any?) =
168159
(other as? Vertex<*, *, *>)?.encode().contentEquals(encode())
169160
override fun hashCode() = id.hashCode()

src/main/kotlin/edu/mcgill/kaliningraph/LabeledGraph.kt

+7-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ open class LabeledGraph(override val vertices: Set<LGVertex> = setOf()):
5353
companion object: LabeledGraph() {
5454
operator fun invoke(builder: LGBuilder.() -> Unit) =
5555
LGBuilder().also { it.builder() }.mutGraph
56+
57+
operator fun invoke(graph: String) =
58+
graph.split(" ").fold(G()) { acc, it ->
59+
acc + G(*it.toList().zipWithNext().toTypedArray())
60+
}
5661
}
5762

5863
var accumuator = mutableSetOf<String>()
@@ -63,7 +68,7 @@ open class LabeledGraph(override val vertices: Set<LGVertex> = setOf()):
6368
fun rewrite(substitution: Pair<String, String>) =
6469
randomWalk().take(200).toList().joinToString("")
6570
.replace(substitution.first, substitution.second)
66-
.let { LabeledGraph.G(it) }
71+
.let { LabeledGraph(it) }
6772

6873
fun propagate() {
6974
val (previousStates, unoccupied) = vertices.partition { it.occupied }
@@ -96,8 +101,7 @@ open class LabeledEdge(
96101
override val source: LGVertex,
97102
override val target: LGVertex,
98103
val label: String? = null
99-
):
100-
Edge<LabeledGraph, LabeledEdge, LGVertex>(source, target) {
104+
): Edge<LabeledGraph, LabeledEdge, LGVertex>(source, target) {
101105
constructor(source: LGVertex, target: LGVertex): this(source, target, null)
102106

103107
override fun render() =

src/main/kotlin/edu/mcgill/kaliningraph/Utils.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fun BMat.show(filename: String = "temp") = matToImg().let { data ->
6363
}
6464
}.show()
6565

66-
val browserCmd = System.getProperty("os.name").toLowerCase().let { os ->
66+
val browserCmd = System.getProperty("os.name").lowercase().let { os ->
6767
when {
6868
"win" in os -> "rundll32 url.dll,FileProtocolHandler"
6969
"mac" in os -> "open"
@@ -91,8 +91,6 @@ fun BMat.matToImg(f: Int = 20) = toEJMLSparse().matToImg(f)
9191

9292
fun randomString() = UUID.randomUUID().toString().take(5)
9393

94-
private operator fun <K, V> Pair<K, V>.component2(): V = second
95-
private operator fun <K, V> Pair<K, V>.component1(): K = first
9694
operator fun MutableNode.minus(target: LinkTarget): Link = addLink(target).links().last()!!
9795

9896
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
10199
fun randomVector(size: Int, rand: () -> Double = { Random.Default.nextDouble() }) =
102100
Array(size) { rand() }.toDoubleArray()
103101

104-
fun Array<DoubleArray>.toEJMLSparse() = SpsMat(size, this[0].size, sumBy { it.count { it == 0.0 } })
102+
fun Array<DoubleArray>.toEJMLSparse() = SpsMat(size, this[0].size, sumOf { it.count { it == 0.0 } })
105103
.also { s -> for (i in indices) for (j in this[0].indices) this[i][j].let { if (0.0 < it) s[i, j] = it } }
106104

107105
fun Array<DoubleArray>.toEJMLDense() = DMatrixRMaj(this)
@@ -115,7 +113,7 @@ fun <T> powBench(constructor: T, matmul: (T, T) -> T): Long =
115113
measureTimeMillis { constructor.power(100, matmul) }
116114

117115
fun <T> T.power(exp: Int, matmul: (T, T) -> T) =
118-
(0..exp).fold(this) { acc, i -> matmul(acc, this) }
116+
generateSequence(this) { matmul(it, this) }.take(exp)
119117

120118
const val DEFAULT_FEATURE_LEN = 20
121119
fun String.vectorize(len: Int = DEFAULT_FEATURE_LEN) =

src/main/kotlin/edu/mcgill/kaliningraph/typefamily/IGraph.kt

+18-14
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ interface IGF<G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> {
4949
}
5050
)
5151

52-
// TODO: generify, only works for labeled graphs
53-
fun G(graph: String): G =
54-
graph.split(" ").fold(G()) { acc, it -> acc + G(it.toCharArray().toList()) }
55-
5652
// Gafter's gadget! http://gafter.blogspot.com/2006/12/super-type-tokens.html
5753
private fun gev(): Array<Class<*>> =
5854
(generateSequence(javaClass) { it.superclass as Class<IGF<G, E, V>> }
@@ -83,11 +79,16 @@ interface IGraph<G, E, V>: IGF<G, E, V>, Set<V>, (V) -> Set<V>
8379
* - Pros: Useful for describing many algebraic path problems
8480
* - Cons: Esoteric API / unsuitable as an abstract interface
8581
*
86-
* Algebraic perspective : https://github.com/snowleopard/alga-paper/releases/download/final/algebraic-graphs.pdf
87-
* Type-family perspective : https://www.cs.cornell.edu/~ross/publications/shapes/shapes-pldi14-tr.pdf#page=3
88-
* Inductive perspective : https://web.engr.oregonstate.edu/~erwig/papers/InductiveGraphs_JFP01.pdf
89-
* Mathematical perspective : https://doi.org/10.1007/978-0-387-75450-5
90-
* Semiring perspective : http://stedolan.net/research/semirings.pdf
82+
* Algebraic perspective : https://github.com/snowleopard/alga-paper/releases/download/final/algebraic-graphs.pdf
83+
* : https://arxiv.org/pdf/1909.04881.pdf
84+
* Type-family perspective : https://www.cs.cornell.edu/~ross/publications/shapes/shapes-pldi14-tr.pdf#page=3
85+
* : https://www.cs.cornell.edu/andru/papers/familia/familia.pdf#page=8
86+
* Inductive perspective : https://web.engr.oregonstate.edu/~erwig/papers/InductiveGraphs_JFP01.pdf
87+
* : https://doi.org/10.1145/258949.258955
88+
* : https://www.cs.utexas.edu/~wcook/Drafts/2012/graphs.pdf
89+
* Semiring perspective : http://stedolan.net/research/semirings.pdf
90+
* : https://doi.org/10.1007/978-0-387-75450-5
91+
* : https://doi.org/10.2200/S00245ED1V01Y201001CNT003
9192
*/
9293

9394
where G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V> {
@@ -134,6 +135,9 @@ interface IEdge<G, E, V>: IGF<G, E, V>
134135
val source: V
135136
val target: V
136137

138+
operator fun component1() = source
139+
operator fun component2() = target
140+
137141
fun render(): Link = (source.render() - target.render()).add(Label.of(""))
138142
}
139143

@@ -146,8 +150,8 @@ interface IVertex<G, E, V>: IGF<G, E, V>, Encodable
146150
val outgoing: Set<E> get() = edgeMap(this as V).toSet()
147151
val edgeMap: (V) -> Set<E> // Make a self-loop by passing this
148152

149-
open val neighbors get() = outgoing.map { it.target }.toSet()
150-
open val outdegree get() = neighbors.size
153+
val neighbors get() = outgoing.map { it.target }.toSet()
154+
val outdegree get() = neighbors.size
151155

152156
// tailrec prohibited on open members? may be possible with deep recursion
153157
// https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deep-recursive-function/
@@ -181,6 +185,6 @@ infix fun Collection<Any>.allAre(that: Any) = all { it isA that }
181185
infix fun Collection<Any>.anyAre(that: Any) = any { it isA that }
182186

183187
// https://github.com/amodeus-science/amod
184-
//abstract class Map : IGraph<Map, Road, City>
185-
//abstract class Road : IEdge<Map, Road, City>
186-
//abstract class City : IVertex<Map, Road, City>
188+
abstract class TMap : IGraph<TMap, TRoad, TCity>
189+
abstract class TRoad : IEdge<TMap, TRoad, TCity>
190+
abstract class TCity : IVertex<TMap, TRoad, TCity>

src/test/kotlin/edu/mcgill/kaliningraph/LabeledGraphTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ class LabeledGraphTest {
2020
)
2121

2222
@Test
23-
fun testStringConstructor() = assertEquals(graph, LabeledGraph.G("abcde ace"))
23+
fun testStringConstructor() = assertEquals(graph, LabeledGraph("abcde ace"))
2424
}

src/test/kotlin/edu/mcgill/kaliningraph/Rewriter.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlin.random.Random
88
@ExperimentalStdlibApi
99
fun main() {
1010
animate(
11-
LabeledGraph.G("abcdecfghia").also { println(it) }
11+
LabeledGraph("abcdecfghia").also { println(it) }
1212
) { _: Document, it: KeyboardEvent, graphs: MutableList<LabeledGraph> ->
1313
when {
1414
"Left" in it.key -> {

0 commit comments

Comments
 (0)