Skip to content

Commit

Permalink
Deprecated size, added length
Browse files Browse the repository at this point in the history
Also changed internal usages. See #37
  • Loading branch information
dievsky committed Oct 8, 2021
1 parent b13e968 commit 53b6048
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 111 deletions.
28 changes: 17 additions & 11 deletions src/main/kotlin/org/jetbrains/bio/viktor/F64Array.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ open class F64Array protected constructor(
val nDim: Int = shape.size

/** Number of elements along the first axis. */
val size: Int = shape[0]
val length: Int = shape[0]

@Deprecated(
"This property will change semantics in 2.0.0, see https://github.com/JetBrains-Research/viktor/issues/37",
ReplaceWith("length")
)
val size: Int get() = length

/**
* Returns `true` if this array can be flattened using [flatten].
Expand Down Expand Up @@ -549,7 +555,7 @@ open class F64Array protected constructor(
open fun sd(): Double {
val s = sum()
val s2 = dot(this)
return sqrt((s2 - s * s / size) / (size - 1))
return sqrt((s2 - s * s / length) / (length - 1))
}

/**
Expand Down Expand Up @@ -941,7 +947,7 @@ open class F64Array protected constructor(
*
* Copying method. Not implemented for flat arrays.
*/
open fun toGenericArray(): Array<*> = Array(size) { view(it).toArray() }
open fun toGenericArray(): Array<*> = Array(length) { view(it).toArray() }

/**
* Converts this vector to a [DoubleArray].
Expand All @@ -961,24 +967,24 @@ open class F64Array protected constructor(
): String {
val sb = StringBuilder()
sb.append('[')
if (maxDisplay < size) {
if (maxDisplay < length) {
for (r in 0 until maxDisplay / 2) {
sb.append(V[r].toString(maxDisplay, format)).append(", ")
}

sb.append("..., ")

val leftover = maxDisplay - maxDisplay / 2
for (r in size - leftover until size) {
for (r in length - leftover until length) {
sb.append(V[r].toString(maxDisplay, format))
if (r < size - 1) {
if (r < length - 1) {
sb.append(", ")
}
}
} else {
for (r in 0 until size) {
for (r in 0 until length) {
sb.append(V[r].toString(maxDisplay, format))
if (r < size - 1) {
if (r < length - 1) {
sb.append(", ")
}
}
Expand All @@ -994,10 +1000,10 @@ open class F64Array protected constructor(
this === other -> true
other !is F64Array -> false
!shape.contentEquals(other.shape) -> false
else -> (0 until size).all { view(it) == other.view(it) }
else -> (0 until length).all { view(it) == other.view(it) }
}

override fun hashCode(): Int = (0 until size).fold(1) { acc, r ->
override fun hashCode(): Int = (0 until length).fold(1) { acc, r ->
31 * acc + view(r).hashCode()
}

Expand Down Expand Up @@ -1100,7 +1106,7 @@ open class F64Array protected constructor(
val result = invoke(*shape)
var offset = 0
for (a in arrayOf(first, *rest)) {
if (a.size > 0) {
if (a.length > 0) {
a.copyTo(result.slice(offset, offset + a.shape[axis], axis = axis))
offset += a.shape[axis]
}
Expand Down
70 changes: 35 additions & 35 deletions src/main/kotlin/org/jetbrains/bio/viktor/F64DenseFlatArray.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,40 @@ internal sealed class F64DenseFlatArray(
size: Int
) : F64FlatArray(data, offset, 1, size) {

override fun fill(init: Double) = data.fill(init, offset, offset + size)
override fun fill(init: Double) = data.fill(init, offset, offset + length)

override fun copy(): F64FlatArray {
val copyData = DoubleArray(size)
System.arraycopy(data, offset, copyData, 0, size)
return create(copyData, 0, size)
val copyData = DoubleArray(length)
System.arraycopy(data, offset, copyData, 0, length)
return create(copyData, 0, length)
}

override fun copyTo(other: F64Array) {
if (other is F64DenseFlatArray) {
checkShape(other)
System.arraycopy(data, offset, other.data, other.offset, size)
System.arraycopy(data, offset, other.data, other.offset, length)
} else {
super.copyTo(other)
}
}

override fun clone(): F64DenseFlatArray = create(data.clone(), offset, size)
override fun clone(): F64DenseFlatArray = create(data.clone(), offset, length)

private inline fun denseTransformInPlace(op: (Double) -> Double) {
val dst = data
var dstOffset = offset
val dstEnd = dstOffset + size
val dstEnd = dstOffset + length
while (dstOffset < dstEnd) {
dst[dstOffset] = op.invoke(dst[dstOffset])
dstOffset++
}
}

private inline fun denseTransform(op: (Double) -> Double): F64FlatArray {
val dst = DoubleArray(size)
val dst = DoubleArray(length)
val src = data
var srcOffset = offset
val length = size
val length = length
if (srcOffset == 0) {
for (i in 0 until length) {
dst[i] = op.invoke(src[i])
Expand All @@ -56,15 +56,15 @@ internal sealed class F64DenseFlatArray(
srcOffset++
}
}
return create(dst, 0, size)
return create(dst, 0, this.length)
}

private inline fun denseEBEInPlace(other: F64DenseFlatArray, op: (Double, Double) -> Double) {
val dst = data
val src = other.data
var dstOffset = offset
var srcOffset = other.offset
val length = size
val length = length
if (dstOffset == 0 && srcOffset == 0) {
for (i in 0 until length) {
dst[i] = op.invoke(dst[i], src[i])
Expand Down Expand Up @@ -93,12 +93,12 @@ internal sealed class F64DenseFlatArray(
}

private inline fun denseEBE(other: F64DenseFlatArray, op: (Double, Double) -> Double): F64DenseFlatArray {
val dst = DoubleArray(size)
val dst = DoubleArray(length)
val src1 = data
val src2 = other.data
var src1Offset = offset
var src2Offset = other.offset
val length = size
val length = length
if (src1Offset == 0 && src2Offset == 0) {
for (i in 0 until length) {
dst[i] = op.invoke(src1[i], src2[i])
Expand All @@ -110,7 +110,7 @@ internal sealed class F64DenseFlatArray(
src2Offset++
}
}
return create(dst, 0, size)
return create(dst, 0, this.length)
}

private inline fun ebe(
Expand All @@ -132,7 +132,7 @@ internal sealed class F64DenseFlatArray(
var res = initial
val dst = data
var dstOffset = offset
val dstEnd = dstOffset + size
val dstEnd = dstOffset + length
while (dstOffset < dstEnd) {
res = op.invoke(res, dst[dstOffset])
dstOffset++
Expand All @@ -143,7 +143,7 @@ internal sealed class F64DenseFlatArray(
override fun reduce(op: (Double, Double) -> Double): Double {
val dst = data
var dstOffset = offset
val dstEnd = dstOffset + size
val dstEnd = dstOffset + length
var res = dst[dstOffset]
dstOffset++
while (dstOffset < dstEnd) {
Expand Down Expand Up @@ -187,7 +187,7 @@ internal sealed class F64DenseFlatArray(

override fun div(other: F64Array) = ebe(other, { a, b -> a / b }, { super.div(it) })

override fun toDoubleArray() = data.copyOfRange(offset, offset + size)
override fun toDoubleArray() = data.copyOfRange(offset, offset + length)

companion object {
/**
Expand Down Expand Up @@ -229,22 +229,22 @@ internal class F64LargeDenseArray(
size: Int
) : F64DenseFlatArray(data, offset, size) {

override fun sd() = NativeSpeedups.unsafeSD(data, offset, size)
override fun sd() = NativeSpeedups.unsafeSD(data, offset, length)

override fun sum() = NativeSpeedups.unsafeSum(data, offset, size)
override fun sum() = NativeSpeedups.unsafeSum(data, offset, length)

override fun cumSum() {
if (!NativeSpeedups.unsafeCumSum(data, offset, size)) super.cumSum()
if (!NativeSpeedups.unsafeCumSum(data, offset, length)) super.cumSum()
}

override fun min() = NativeSpeedups.unsafeMin(data, offset, size)
override fun min() = NativeSpeedups.unsafeMin(data, offset, length)

override fun max() = NativeSpeedups.unsafeMax(data, offset, size)
override fun max() = NativeSpeedups.unsafeMax(data, offset, length)

override fun dot(other: F64Array): Double {
return if (other is F64LargeDenseArray) {
checkShape(other)
NativeSpeedups.unsafeDot(data, offset, other.data, other.offset, size)
NativeSpeedups.unsafeDot(data, offset, other.data, other.offset, length)
} else {
super.dot(other)
}
Expand All @@ -254,53 +254,53 @@ internal class F64LargeDenseArray(
nativeOp: (DoubleArray, Int, DoubleArray, Int, Int) -> Boolean,
superOp: F64FlatArray.() -> F64FlatArray
): F64FlatArray {
val dst = DoubleArray(size)
if (nativeOp(dst, 0, data, offset, size)) {
return create(dst, 0, size)
val dst = DoubleArray(length)
if (nativeOp(dst, 0, data, offset, length)) {
return create(dst, 0, length)
}
return superOp()
}

override fun expInPlace() {
if (!NativeSpeedups.unsafeExp(data, offset, data, offset, size)) super.expInPlace()
if (!NativeSpeedups.unsafeExp(data, offset, data, offset, length)) super.expInPlace()
}

override fun exp() = nativeTransform(NativeSpeedups::unsafeExp) { super.exp() }

override fun expm1InPlace() {
if (!NativeSpeedups.unsafeExpm1(data, offset, data, offset, size)) super.expm1InPlace()
if (!NativeSpeedups.unsafeExpm1(data, offset, data, offset, length)) super.expm1InPlace()
}

override fun expm1() = nativeTransform(NativeSpeedups::unsafeExpm1) { super.expm1() }

override fun logInPlace() {
if (!NativeSpeedups.unsafeLog(data, offset, data, offset, size)) super.logInPlace()
if (!NativeSpeedups.unsafeLog(data, offset, data, offset, length)) super.logInPlace()
}

override fun log() = nativeTransform(NativeSpeedups::unsafeLog) { super.log() }

override fun log1pInPlace(){
if (!NativeSpeedups.unsafeLog1p(data, offset, data, offset, size)) super.log1pInPlace()
if (!NativeSpeedups.unsafeLog1p(data, offset, data, offset, length)) super.log1pInPlace()
}

override fun log1p() = nativeTransform(NativeSpeedups::unsafeLog1p) { super.log1p() }

override fun logSumExp() = NativeSpeedups.unsafeLogSumExp(data, offset, size)
override fun logSumExp() = NativeSpeedups.unsafeLogSumExp(data, offset, length)

override fun logAddExpAssign(other: F64Array) {
if (other is F64LargeDenseArray) {
checkShape(other)
if (NativeSpeedups.unsafeLogAddExp(data, offset, data, offset, other.data, other.offset, size)) return
if (NativeSpeedups.unsafeLogAddExp(data, offset, data, offset, other.data, other.offset, length)) return
}
super.logAddExpAssign(other)
}

override fun logAddExp(other: F64Array): F64FlatArray {
if (other is F64LargeDenseArray) {
checkShape(other)
val res = DoubleArray(size)
if (NativeSpeedups.unsafeLogAddExp(res, 0, data, offset, other.data, other.offset, size)) {
return create(res, 0, size)
val res = DoubleArray(length)
if (NativeSpeedups.unsafeLogAddExp(res, 0, data, offset, other.data, other.offset, length)) {
return create(res, 0, length)
}
}
return super.logAddExp(other)
Expand Down
Loading

0 comments on commit 53b6048

Please sign in to comment.