https://github.com/thomasnield/numky
Idiomatic linear algebra for Kotlin using KMath
https://github.com/thomasnield/numky
Last synced: 3 months ago
JSON representation
Idiomatic linear algebra for Kotlin using KMath
- Host: GitHub
- URL: https://github.com/thomasnield/numky
- Owner: thomasnield
- License: apache-2.0
- Created: 2019-06-06T18:10:39.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-06-09T05:16:01.000Z (about 6 years ago)
- Last Synced: 2025-02-10T03:43:01.219Z (5 months ago)
- Language: Kotlin
- Size: 24.4 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# numky
Idiomatic linear algebra for Kotlin using KMath** Experimental work in progress! Do not use for production **
```kotlin
import org.nield.numky.linear.*
import org.ojalgo.random.Normal
import java.net.URLfun main() {
val points = URL("https://tinyurl.com/y58sesrr")
.readText().split(Regex("\\r?\\n"))
.asSequence()
.drop(1)
.filter { it.isNotEmpty() }
.map { it.split(",").map { it.toDouble() }.toDoubleArray() }
.toMatrix()val x = points.extractColumn(0)
val y = points.extractColumn(1)val normal = Normal(0.0,1.0)
val epochs = 200_000
val n = x.rowNum.toDouble()
var m = 0.0
var b = 0.0var bestLoss = 10000000000000.0
repeat(epochs){
val mAdjust = normal.get()
val bAdjust = normal.get()m += mAdjust
b += bAdjustval newLoss = ((y - (x * m + b)).square() ).sum() / n
if (newLoss < bestLoss) {
bestLoss = newLoss
} else {
m -= mAdjust
b -= bAdjust
}
}println("f(x) = ${m}x + $b")
}```