Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ internal fun <T, D : Dimension> ndarrayCommon(elements: Collection<T>, shape: In
requireElementsWithShape(elements.size, shape.fold(1, Int::times))
val size = shape.reduce { acc, el -> acc * el }

val dtypeFromFirst = DataType.of(elements.first())
val data = initMemoryView<T>(size, dtype ?: dtypeFromFirst).apply {// TODO: boxing/unboxing!!! Find all usages
val actualDtype = dtype ?: if (elements.isNotEmpty()) DataType.of(elements.first()) else throw IllegalArgumentException("Cannot determine DataType of an empty collection when dtype is not specified.")
val data = initMemoryView<T>(size, actualDtype).apply {// TODO: boxing/unboxing!!! Find all usages
var count = 0
for (el in elements)
this[count++] = el
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,7 @@ public inline fun <T, D : Dimension, K> MultiArray<T, D>.distinctBy(selector: (T
if (set.add(key))
list.add(e)
}
val dtype = DataType.of(list.first())
return list.toCommonNDArray(dtype)
return list.toCommonNDArray(this.dtype)
}

/**
Expand Down Expand Up @@ -397,8 +396,7 @@ public inline fun <T> MultiArray<T, D1>.dropWhile(predicate: (T) -> Boolean): ND
public inline fun <T, D : Dimension> MultiArray<T, D>.filter(predicate: (T) -> Boolean): D1Array<T> {
val list = ArrayList<T>()
forEach { if (predicate(it)) list.add(it) }
val dtype = DataType.of(list.first())
return list.toCommonNDArray(dtype)
return list.toCommonNDArray(this.dtype)
}

/**
Expand All @@ -408,8 +406,7 @@ public inline fun <T, D : Dimension> MultiArray<T, D>.filter(predicate: (T) -> B
public inline fun <T> MultiArray<T, D1>.filterIndexed(predicate: (index: Int, T) -> Boolean): D1Array<T> {
val list = ArrayList<T>()
forEachIndexed { index, element -> if (predicate(index, element)) list.add(element) }
val dtype = DataType.of(list.first())
return list.toCommonNDArray(dtype)
return list.toCommonNDArray(this.dtype)
}


Expand All @@ -420,8 +417,7 @@ public inline fun <T> MultiArray<T, D1>.filterIndexed(predicate: (index: Int, T)
public inline fun <T, D : Dimension> MultiArray<T, D>.filterMultiIndexed(predicate: (index: IntArray, T) -> Boolean): D1Array<T> {
val list = ArrayList<T>()
forEachMultiIndexed { index, element -> if (predicate(index, element)) list.add(element) }
val dtype = DataType.of(list.first())
return list.toCommonNDArray(dtype)
return list.toCommonNDArray(this.dtype)
}

/**
Expand All @@ -430,8 +426,7 @@ public inline fun <T, D : Dimension> MultiArray<T, D>.filterMultiIndexed(predica
public inline fun <T, D : Dimension> MultiArray<T, D>.filterNot(predicate: (T) -> Boolean): D1Array<T> {
val list = ArrayList<T>()
for (element in this) if (!predicate(element)) list.add(element)
val dtype = DataType.of(list.first())
return list.toCommonNDArray(dtype)
return list.toCommonNDArray(this.dtype)
}

/**
Expand Down Expand Up @@ -1077,8 +1072,7 @@ public inline fun <T, D : Dimension> MultiArray<T, D>.partition(predicate: (T) -
second.add(element)
}
}
val dtype = DataType.of(first.first())
return Pair(first.toCommonNDArray(dtype), second.toCommonNDArray(dtype))
return Pair(first.toCommonNDArray(this.dtype), second.toCommonNDArray(this.dtype))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ import org.jetbrains.kotlinx.multik.ndarray.operations.distinct
import org.jetbrains.kotlinx.multik.ndarray.operations.distinctBy
import org.jetbrains.kotlinx.multik.ndarray.operations.drop
import org.jetbrains.kotlinx.multik.ndarray.operations.dropWhile
import org.jetbrains.kotlinx.multik.api.zeros
import org.jetbrains.kotlinx.multik.ndarray.data.D1
import org.jetbrains.kotlinx.multik.ndarray.data.DataType
import org.jetbrains.kotlinx.multik.ndarray.operations.filter
import org.jetbrains.kotlinx.multik.ndarray.operations.filterIndexed
import org.jetbrains.kotlinx.multik.ndarray.operations.filterMultiIndexed
import org.jetbrains.kotlinx.multik.ndarray.operations.filterNot
import org.jetbrains.kotlinx.multik.ndarray.operations.find
import org.jetbrains.kotlinx.multik.ndarray.operations.findLast
Expand Down Expand Up @@ -181,6 +185,37 @@ class IterableNDArrayTest {
assertEquals(mk.ndarray(expectedList), actual)
}

@Test
fun `test_filter_empty_result`() {
val data = mk.arange<Int>(10, 30, 1)
val actual = data.filter { it > 100 }
assertEquals(0, actual.size)
assertEquals(DataType.IntDataType, actual.dtype)

val actualIndexed = data.filterIndexed { _, v -> v > 100 }
assertEquals(0, actualIndexed.size)
assertEquals(DataType.IntDataType, actualIndexed.dtype)

val actualMultiIndexed = data.filterMultiIndexed { _, v -> v > 100 }
assertEquals(0, actualMultiIndexed.size)
assertEquals(DataType.IntDataType, actualMultiIndexed.dtype)

val actualNot = data.filterNot { it < 100 }
assertEquals(0, actualNot.size)
assertEquals(DataType.IntDataType, actualNot.dtype)

val emptyData = mk.zeros<Int>(0)
val actualDistinctBy = emptyData.distinctBy { it }
assertEquals(0, actualDistinctBy.size)
assertEquals(DataType.IntDataType, actualDistinctBy.dtype)

val (firstEmpty, secondAll) = data.partition { it > 100 }
assertEquals(0, firstEmpty.size)
assertEquals(data.size, secondAll.size)
assertEquals(DataType.IntDataType, firstEmpty.dtype)
assertEquals(DataType.IntDataType, secondAll.dtype)
}

@Test
fun `test_find`() {
val list = listOf(1, 2, 3, 4, 5, 6, 7)
Expand Down