dump (another useless message)

This commit is contained in:
minjaesong
2018-08-05 21:57:18 +09:00
parent 24b2e2a2af
commit eee8a18875
130 changed files with 1350 additions and 563 deletions

View File

@@ -0,0 +1,134 @@
package net.torvald.dataclass
import java.util.function.BiConsumer
import java.util.function.BiFunction
import java.util.function.Function
/**
* Created by minjaesong on 2018-07-15.
*/
class ArrayListMap<K, V> : MutableMap<K, V> {
private val keysArray = ArrayList<K>()
private val valuesArray = ArrayList<V>()
override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
override val size: Int
get() = keysArray.size
override val values: MutableCollection<V>
get() = valuesArray.toMutableSet()
override val keys: MutableSet<K>
get() = keysArray.toMutableSet()
override fun containsKey(key: K): Boolean {
return keysArray.contains(key)
}
override fun containsValue(value: V): Boolean {
return valuesArray.contains(value)
}
override fun forEach(action: BiConsumer<in K, in V>) {
for (i in 0 until size) {
action.accept(keysArray[i], valuesArray[i])
}
}
override fun get(key: K): V? {
val index = keysArray.linearSearch(key)
index?.let {
return valuesArray[index]
}
return null
}
override fun getOrDefault(key: K, defaultValue: V): V {
return get(key) ?: defaultValue
}
override fun isEmpty(): Boolean {
return size == 0
}
private fun ArrayList<K>.linearSearch(element: K): Int? {
var found = 0
while (found < keysArray.size) {
if (keysArray[found] == element)
return found
found++
}
return null
}
override fun put(key: K, value: V): V? {
val index = keysArray.linearSearch(key)
if (index != null) {
val oldValue = valuesArray[index]
valuesArray[index] = value
return oldValue
}
else {
keysArray.add(key)
valuesArray.add(value)
return null
}
}
override fun clear() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun compute(key: K, remappingFunction: BiFunction<in K, in V?, out V?>): V? {
return super.compute(key, remappingFunction)
}
override fun computeIfAbsent(key: K, mappingFunction: Function<in K, out V>): V {
return super.computeIfAbsent(key, mappingFunction)
}
override fun computeIfPresent(key: K, remappingFunction: BiFunction<in K, in V, out V?>): V? {
return super.computeIfPresent(key, remappingFunction)
}
override fun merge(key: K, value: V, remappingFunction: BiFunction<in V, in V, out V?>): V? {
return super.merge(key, value, remappingFunction)
}
override fun putAll(from: Map<out K, V>) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun putIfAbsent(key: K, value: V): V? {
return super.putIfAbsent(key, value)
}
override fun remove(key: K): V? {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun remove(key: K, value: V): Boolean {
return super.remove(key, value)
}
override fun replace(key: K, oldValue: V, newValue: V): Boolean {
return super.replace(key, oldValue, newValue)
}
override fun replace(key: K, value: V): V? {
return super.replace(key, value)
}
override fun replaceAll(function: BiFunction<in K, in V, out V>) {
super.replaceAll(function)
}
}

View File

@@ -0,0 +1,171 @@
package net.torvald.dataclass
/**
* Taken and improved from https://introcs.cs.princeton.edu/java/95linear/Matrix.java.html
*
* Created by minjaesong on 2018-08-01.
*/
class Matrix {
val rows: Int // number of rows; M
val cols: Int // number of columns; N
val data: Array<DoubleArray> // M-by-N array
// create M-by-N matrix of 0's
constructor(M: Int, N: Int) {
this.rows = M
this.cols = N
data = Array(M) { DoubleArray(N) }
}
// create matrix based on 2d array
constructor(data: Array<DoubleArray>) {
rows = data.size
cols = data[0].size
this.data = Array(rows) { DoubleArray(cols) }
for (i in 0 until rows)
for (j in 0 until cols)
this.data[i][j] = data[i][j]
}
// copy constructor
private constructor(A: Matrix) : this(A.data) {}
// swap rows i and j
private fun swap(i: Int, j: Int) {
val temp = data[i]
data[i] = data[j]
data[j] = temp
}
// create and return the transpose of the invoking matrix
fun transpose(): Matrix {
val A = Matrix(cols, rows)
for (i in 0 until rows)
for (j in 0 until cols)
A.data[j][i] = this.data[i][j]
return A
}
// return C = A + B
operator fun plus(B: Matrix): Matrix {
val A = this
if (B.rows != A.rows || B.cols != A.cols) throw RuntimeException("Illegal matrix dimensions.")
val C = Matrix(rows, cols)
for (i in 0 until rows)
for (j in 0 until cols)
C.data[i][j] = A.data[i][j] + B.data[i][j]
return C
}
// return C = A - B
operator fun minus(B: Matrix): Matrix {
val A = this
if (B.rows != A.rows || B.cols != A.cols) throw RuntimeException("Illegal matrix dimensions.")
val C = Matrix(rows, cols)
for (i in 0 until rows)
for (j in 0 until cols)
C.data[i][j] = A.data[i][j] - B.data[i][j]
return C
}
// does A = B exactly?
override fun equals(B: Any?): Boolean {
if (B !is Matrix) throw RuntimeException("Not a Matrix.")
val A = this
if (B.rows != A.rows || B.cols != A.cols) throw RuntimeException("Illegal matrix dimensions.")
for (i in 0 until rows)
for (j in 0 until cols)
if (A.data[i][j] != B.data[i][j]) return false
return true
}
// return C = A * B
operator fun times(B: Matrix): Matrix {
val A = this
if (A.cols != B.rows) throw RuntimeException("Illegal matrix dimensions.")
val C = Matrix(A.rows, B.cols)
for (i in 0 until C.rows)
for (j in 0 until C.cols)
for (k in 0 until A.cols)
C.data[i][j] += A.data[i][k] * B.data[k][j]
return C
}
// return x = A^-1 b, assuming A is square and has full rank
fun solve(rhs: Matrix): Matrix {
if (rows != cols || rhs.rows != cols || rhs.cols != 1)
throw RuntimeException("Illegal matrix dimensions.")
// create copies of the data
val A = Matrix(this)
val b = Matrix(rhs)
// Gaussian elimination with partial pivoting
for (i in 0 until cols) {
// find pivot row and swap
var max = i
for (j in i + 1 until cols)
if (Math.abs(A.data[j][i]) > Math.abs(A.data[max][i]))
max = j
A.swap(i, max)
b.swap(i, max)
// singular
if (A.data[i][i] == 0.0) throw RuntimeException("Matrix is singular.")
// pivot within b
for (j in i + 1 until cols)
b.data[j][0] -= b.data[i][0] * A.data[j][i] / A.data[i][i]
// pivot within A
for (j in i + 1 until cols) {
val m = A.data[j][i] / A.data[i][i]
for (k in i + 1 until cols) {
A.data[j][k] -= A.data[i][k] * m
}
A.data[j][i] = 0.0
}
}
// back substitution
val x = Matrix(cols, 1)
for (j in cols - 1 downTo 0) {
var t = 0.0
for (k in j + 1 until cols)
t += A.data[j][k] * x.data[k][0]
x.data[j][0] = (b.data[j][0] - t) / A.data[j][j]
}
return x
}
/**
* for idioms of ```element = mat[row][column]```
*/
operator fun get(i: Int): DoubleArray = data[i]
companion object {
// create and return a random M-by-N matrix with values between 0 and 1
fun random(M: Int, N: Int): Matrix {
val A = Matrix(M, N)
for (i in 0 until M)
for (j in 0 until N)
A.data[i][j] = Math.random()
return A
}
// create and return the N-by-N identity matrix
fun identity(N: Int): Matrix {
val I = Matrix(N, N)
for (i in 0 until N)
I.data[i][i] = 1.0
return I
}
}
}