Skip to content

Commit

Permalink
Updated to Kotlin 1.0.0-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
superbobry committed Oct 22, 2015
1 parent afe1d10 commit 8842799
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 33 deletions.
10 changes: 10 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ viktor Changelog

Here you can see the full list of changes between each viktor release.

Version 0.1.1
-------------

Technical release, released on October 22th, 2015

- Updated to Kotlin 1.0.0-beta.
- Renamed '_' to '_I' because the latter isn't a valid identifier as
of Kotlin M14. This is a temporary solution. I hope we'll find
a more human-readable workaround eventually.

Version 0.1.0
-------------

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Kotlin. Here're some of the higlights:
```kotlin
val m = StridedMatrix(4, 3)
m[0] = StridedVector.full(3, 42.0) // row-view.
m[_, 0] // column-view.
m[_I, 0] // column-view.
m[0] = 42.0 // broadcasting.
m + m // arithmetic operations.
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '0.14.+'
ext.kotlin_version = '1.0.0-beta-+'

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
groupId=org.jetbrains.bio
version=0.1.0
version=0.1.1
2 changes: 1 addition & 1 deletion src/main/kotlin/org/jetbrains/bio/viktor/MoreMath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.apache.commons.math3.util.FastMath
*
* assuming a >= b.
*/
fun Double.logAddExp(b: Double): Double {
infix fun Double.logAddExp(b: Double): Double {
val a = this
return when {
a.isInfinite() && a < 0 -> b
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/jetbrains/bio/viktor/StridedMagic.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jetbrains.bio.viktor

/** A special object used to denote *all indices*. */
public object _ {}
public object _I {}
14 changes: 7 additions & 7 deletions src/main/kotlin/org/jetbrains/bio/viktor/StridedMatrix2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ open class StridedMatrix2(val rowsNumber: Int,
* of memory. This allows to use SIMD operations, e.g. when
* computing the sum of elements.
*/
protected inline val isDense: Boolean get() {
protected val isDense: Boolean get() {
return rowStride == columnsNumber && columnStride == 1
}

Expand Down Expand Up @@ -80,18 +80,18 @@ open class StridedMatrix2(val rowsNumber: Int,
/**
* A less-verbose alias to [.columnView].
*
* Use in conjunction with [_], e.g. `m[_, i]`.
* Use in conjunction with [_I], e.g. `m[_I, i]`.
*/
operator fun get(any: _, c: Int) = columnView(c)
operator fun get(any: _I, c: Int) = columnView(c)

operator fun set(any: _, c: Int, other: StridedVector) = other.copyTo(columnView(c))
operator fun set(any: _I, c: Int, other: StridedVector) = other.copyTo(columnView(c))

operator fun set(any: _, c: Int, init: Double) = columnView(c).fill(init)
operator fun set(any: _I, c: Int, init: Double) = columnView(c).fill(init)

operator fun set(row: Int, any: _, init: Double) = columnView(row).fill(init)
operator fun set(row: Int, any: _I, init: Double) = columnView(row).fill(init)

/** A useful shortcut for transposed matrix. */
inline val T: StridedMatrix2 get() = transpose()
val T: StridedMatrix2 get() = transpose()

fun transpose() = StridedMatrix2(columnsNumber, rowsNumber, offset, data,
columnStride, rowStride)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/jetbrains/bio/viktor/StridedMatrix3.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ open class StridedMatrix3(val depth: Int,
* of memory. This allows to use SIMD operations, e.g. when
* computing the sum of elements.
*/
protected inline val isDense: Boolean get() {
protected val isDense: Boolean get() {
return depthStride == rowsNumber * columnsNumber &&
rowStride == columnsNumber &&
columnStride == 1
Expand Down Expand Up @@ -72,7 +72,7 @@ open class StridedMatrix3(val depth: Int,
require(depthStride == other.depthStride &&
rowStride == other.rowStride &&
columnStride == other.columnStride)
System.arraycopy(data, 0, other.data, 0, data.size())
System.arraycopy(data, 0, other.data, 0, data.size)
}

fun flatten(): StridedVector {
Expand Down
34 changes: 17 additions & 17 deletions src/main/kotlin/org/jetbrains/bio/viktor/StridedVector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.util.*
/**
* Wrap a given array of elements. The array will not be copied.
*/
fun DoubleArray.asStrided() = StridedVector.create(this, 0, size(), 1)
fun DoubleArray.asStrided() = StridedVector.create(this, 0, size, 1)

/**
* Strided floating point containers.
Expand Down Expand Up @@ -40,9 +40,9 @@ open class StridedVector(protected val data: DoubleArray,
* Create a vector with given elements.
*/
@JvmStatic fun of(first: Double, vararg rest: Double): StridedVector {
val data = DoubleArray(rest.size() + 1)
val data = DoubleArray(rest.size + 1)
data[0] = first
System.arraycopy(rest, 0, data, 1, rest.size())
System.arraycopy(rest, 0, data, 1, rest.size)
return data.asStrided()
}

Expand All @@ -65,11 +65,11 @@ open class StridedVector(protected val data: DoubleArray,
* TODO: remove this once we get rid of all Java usages.
*/
@JvmStatic fun wrap(data: DoubleArray): StridedVector {
return create(data, 0, data.size(), 1)
return create(data, 0, data.size, 1)
}

fun create(data: DoubleArray, offset: Int, size: Int, stride: Int): StridedVector {
require(offset + size <= data.size()) { "not enough data" }
require(offset + size <= data.size) { "not enough data" }
return if (stride == 1) {
DenseVector.create(data, offset, size)
} else {
Expand All @@ -78,7 +78,7 @@ open class StridedVector(protected val data: DoubleArray,
}
}

inline val indices: IntRange get() = 0..size() - 1
val indices: IntRange get() = 0..size() - 1

operator fun get(pos: Int): Double {
try {
Expand Down Expand Up @@ -108,9 +108,9 @@ open class StridedVector(protected val data: DoubleArray,
return StridedVector(data, offset + from, to - from, stride)
}

operator fun set(any: _, init: Double): Unit = fill(init)
operator fun set(any: _I, init: Double): Unit = fill(init)

operator fun set(any: _, other: StridedVector): Unit = other.copyTo(this)
operator fun set(any: _I, other: StridedVector): Unit = other.copyTo(this)

open fun fill(init: Double) {
for (pos in 0..size - 1) {
Expand All @@ -127,7 +127,7 @@ open class StridedVector(protected val data: DoubleArray,
}

/** A useful shortcut for column-vector. */
inline val T: StridedMatrix2 get() = transpose()
val T: StridedMatrix2 get() = transpose()

fun transpose() = reshape(size, 1)

Expand Down Expand Up @@ -163,7 +163,7 @@ open class StridedVector(protected val data: DoubleArray,
}

fun reorder(indices: IntArray) {
require(size == indices.size())
require(size == indices.size)
val copy = indices.clone()
for (pos in 0..size - 1) {
val value = unsafeGet(pos)
Expand All @@ -189,7 +189,7 @@ open class StridedVector(protected val data: DoubleArray,
}

fun dot(other: ShortArray): Double {
require(other.size() == size) { "non-conformable arrays" }
require(other.size == size) { "non-conformable arrays" }
var acc = 0.0
for (pos in 0..size - 1) {
acc += unsafeGet(pos) * other[pos].toDouble()
Expand All @@ -199,7 +199,7 @@ open class StridedVector(protected val data: DoubleArray,
}

fun dot(other: IntArray): Double {
require(other.size() == size) { "non-conformable arrays" }
require(other.size == size) { "non-conformable arrays" }
var acc = 0.0
for (pos in 0..size - 1) {
acc += unsafeGet(pos) * other[pos].toDouble()
Expand All @@ -208,8 +208,8 @@ open class StridedVector(protected val data: DoubleArray,
return acc
}

open fun dot(other: DoubleArray): Double {
require(other.size() == size) { "non-conformable arrays" }
open infix fun dot(other: DoubleArray): Double {
require(other.size == size) { "non-conformable arrays" }
var acc = 0.0
for (pos in 0..size - 1) {
acc += unsafeGet(pos) * other[pos]
Expand Down Expand Up @@ -508,16 +508,16 @@ class LargeDenseVector(data: DoubleArray, offset: Int, size: Int) :
override fun max() = Core.Max_V64f_S64f(data, offset, size)

override fun dot(other: DoubleArray): Double {
require(other.size() == size) { "non-conformable arrays" }
require(other.size == size) { "non-conformable arrays" }
return Core.DotProduct_V64fV64f_S64f(data, offset, other, 0, size)
}

override fun expInPlace() {
info.yeppp.Math.Exp_V64f_V64f(data, 0, data, 0, data.size())
info.yeppp.Math.Exp_V64f_V64f(data, 0, data, 0, data.size)
}

override fun logInPlace() {
info.yeppp.Math.Log_V64f_V64f(data, 0, data, 0, data.size())
info.yeppp.Math.Log_V64f_V64f(data, 0, data, 0, data.size)
}

override fun logRescale() = SIMDMath.logRescale(data, offset, size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class StridedVectorTest {
copy.sort()

for ((i, j) in indices.withIndex()) {
assertEquals(copy[copy.size() - 1 - i], v[j])
assertEquals(copy[copy.size - 1 - i], v[j])
}
}

Expand All @@ -97,7 +97,7 @@ class StridedVectorTest {
Double.NaN, 0.0,
Double.NaN, 4.0,
Double.NaN, 2.0),
offset = 2, size = values.size(), stride = 2)
offset = 2, size = values.size, stride = 2)
v.reorder(indices)
assertArrayEquals(doubleArrayOf(-1.0, 0.0, 2.0, 2.0, 4.0, 42.0),
v.toArray(), Precision.EPSILON)
Expand Down

0 comments on commit 8842799

Please sign in to comment.