Last active
August 1, 2023 16:44
-
-
Save thomasnield/e933b3bf1430b9eea52d1f3304d81fa7 to your computer and use it in GitHub Desktop.
Matrices with Kotlin (using kotlin-statistics)
This file contains hidden or 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
| import java.time.LocalDate | |
| import java.time.temporal.ChronoUnit | |
| import org.apache.commons.math3.linear.Array2DRowRealMatrix | |
| import org.apache.commons.math3.linear.RealMatrix | |
| fun main(args: Array<String>) { | |
| val matrix = patients.toMatrix( | |
| { it.age }, | |
| { it.whiteBloodCellCount }, | |
| { if (it.gender == Gender.MALE) 0 else 1 } | |
| ) | |
| println(matrix.toFormattedString()) | |
| } | |
| data class Patient(val firstName: String, | |
| val lastName: String, | |
| val gender: Gender, | |
| val birthday: LocalDate, | |
| val whiteBloodCellCount: Int) { | |
| val age = ChronoUnit.YEARS.between(birthday, LocalDate.now()) | |
| } | |
| val patients = listOf( | |
| Patient("John", "Simone", Gender.MALE, LocalDate.of(1989, 1, 7), 4500), | |
| Patient("Sarah", "Marley", Gender.FEMALE, LocalDate.of(1970, 2, 5), 6700), | |
| Patient("Jessica", "Arnold", Gender.FEMALE, LocalDate.of(1980, 3, 9), 3400), | |
| Patient("Sam", "Beasley", Gender.MALE, LocalDate.of(1981, 4, 17), 8800), | |
| Patient("Dan", "Forney", Gender.MALE, LocalDate.of(1985, 9, 13), 5400), | |
| Patient("Lauren", "Michaels", Gender.FEMALE, LocalDate.of(1975, 8, 21), 5000), | |
| Patient("Michael", "Erlich", Gender.MALE, LocalDate.of(1985, 12, 17), 4100), | |
| Patient("Jason", "Miles", Gender.MALE, LocalDate.of(1991, 11, 1), 3900), | |
| Patient("Rebekah", "Earley", Gender.FEMALE, LocalDate.of(1985, 2, 18), 4600), | |
| Patient("James", "Larson", Gender.MALE, LocalDate.of(1974, 4, 10), 5100), | |
| Patient("Dan", "Ulrech", Gender.MALE, LocalDate.of(1991, 7, 11), 6000), | |
| Patient("Heather", "Eisner", Gender.FEMALE, LocalDate.of(1994, 3, 6), 6000), | |
| Patient("Jasper", "Martin", Gender.MALE, LocalDate.of(1971, 7, 1), 6000) | |
| ) | |
| enum class Gender { | |
| MALE, | |
| FEMALE | |
| } | |
| class Matrix(private val m: RealMatrix) { | |
| operator fun get(row: Int, col: Int) = m.getEntry(row, col) | |
| operator fun get(row: Int) = m.getRow(row) | |
| fun transpose() = m.transpose().let(::Matrix) | |
| val data get() = m.data | |
| fun toFormattedString() = data.asSequence().map { it.joinToString(" ")}.joinToString("\r\n") | |
| } | |
| fun <T> Iterable<T>.toMatrix(vararg fieldMappers: (T) -> Number) = | |
| toList().let { | |
| val colCount = fieldMappers.size | |
| val rowCount = it.size | |
| val m = Array2DRowRealMatrix(rowCount, colCount) | |
| it.forEachIndexed { rowIndex, item -> | |
| fieldMappers.forEachIndexed { colIndex, mapper -> | |
| m.setEntry(rowIndex, colIndex,mapper(item).toDouble()) | |
| } | |
| } | |
| Matrix(m) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment