pre-0.0.3 #46

Merged
altavir merged 75 commits from dev into master 2019-02-20 13:05:39 +03:00
5 changed files with 25 additions and 79 deletions
Showing only changes of commit 12b343e905 - Show all commits

View File

@ -9,7 +9,7 @@ fun main(args: Array<String>) {
val bufferedField = NDField.auto(intArrayOf(dim, dim), RealField) val bufferedField = NDField.auto(intArrayOf(dim, dim), RealField)
val specializedField = NDField.real(intArrayOf(dim, dim)) val specializedField = NDField.real(intArrayOf(dim, dim))
val genericField = NDField.generic(intArrayOf(dim, dim), RealField) val genericField = NDField.buffered(intArrayOf(dim, dim), RealField)
val lazyNDField = NDField.lazy(intArrayOf(dim, dim), RealField) val lazyNDField = NDField.lazy(intArrayOf(dim, dim), RealField)
// val action: NDField<Double, DoubleField, NDStructure<Double>>.() -> Unit = { // val action: NDField<Double, DoubleField, NDStructure<Double>>.() -> Unit = {
@ -75,7 +75,7 @@ fun main(args: Array<String>) {
val genericTime = measureTimeMillis { val genericTime = measureTimeMillis {
//genericField.run(action) //genericField.run(action)
genericField.run { genericField.run {
var res = one var res: NDBuffer<Double> = one
repeat(n) { repeat(n) {
res += 1.0 res += 1.0
} }

View File

@ -32,6 +32,6 @@ fun ClosedFloatingPointRange<Double>.toSequence(step: Double): Sequence<Double>
* Convert double range to array of evenly spaced doubles, where the size of array equals [numPoints] * Convert double range to array of evenly spaced doubles, where the size of array equals [numPoints]
*/ */
fun ClosedFloatingPointRange<Double>.toGrid(numPoints: Int): DoubleArray { fun ClosedFloatingPointRange<Double>.toGrid(numPoints: Int): DoubleArray {
if (numPoints < 2) error("Can't generic grid with less than two points") if (numPoints < 2) error("Can't buffered grid with less than two points")
return DoubleArray(numPoints) { i -> start + (endInclusive - start) / (numPoints - 1) * i } return DoubleArray(numPoints) { i -> start + (endInclusive - start) / (numPoints - 1) * i }
} }

View File

@ -35,24 +35,22 @@ object NDElements {
/** /**
* Simple boxing NDArray * Simple boxing NDArray
*/ */
fun <T : Any, F : Field<T>> generic( fun <T : Any, F : Field<T>> buffered(
shape: IntArray, shape: IntArray,
field: F, field: F,
initializer: F.(IntArray) -> T initializer: F.(IntArray) -> T
): GenericNDElement<T, F> { ): BufferNDElement<T, F> {
val ndField = GenericNDField(shape, field) val ndField = BufferNDField(shape, field, ::boxingBuffer)
val structure = ndStructure(shape) { index -> field.initializer(index) } return ndField.produce(initializer)
return GenericNDElement(ndField, structure)
} }
inline fun <reified T : Any, F : Field<T>> inline( inline fun <reified T : Any, F : Field<T>> auto(
shape: IntArray, shape: IntArray,
field: F, field: F,
noinline initializer: F.(IntArray) -> T noinline initializer: F.(IntArray) -> T
): GenericNDElement<T, F> { ): BufferNDElement<T, F> {
val ndField = GenericNDField(shape, field) val ndField = NDField.auto(shape, field)
val structure = ndStructure(shape, ::autoBuffer) { index -> field.initializer(index) } return ndField.produce(initializer)
return GenericNDElement(ndField, structure)
} }
} }

View File

@ -22,43 +22,10 @@ interface NDField<T, F : Field<T>, N : NDStructure<T>> : Field<N> {
val elementField: F val elementField: F
fun produce(initializer: F.(IntArray) -> T): N fun produce(initializer: F.(IntArray) -> T): N
fun map(arg: N, transform: F.(T) -> T): N fun map(arg: N, transform: F.(T) -> T): N
fun mapIndexed(arg: N, transform: F.(index: IntArray, T) -> T): N fun mapIndexed(arg: N, transform: F.(index: IntArray, T) -> T): N
fun combine(a: N, b: N, transform: F.(T, T) -> T): N fun combine(a: N, b: N, transform: F.(T, T) -> T): N
/**
* Element by element application of any operation on elements to the whole array. Just like in numpy
*/
operator fun Function1<T, T>.invoke(structure: N): N
/**
* Summation operation for [NDElements] and single element
*/
operator fun N.plus(arg: T): N
/**
* Subtraction operation between [NDElements] and single element
*/
operator fun N.minus(arg: T): N
/**
* Product operation for [NDElements] and single element
*/
operator fun N.times(arg: T): N
/**
* Division operation between [NDElements] and single element
*/
operator fun N.div(arg: T): N
operator fun T.plus(arg: N): N
operator fun T.minus(arg: N): N
operator fun T.times(arg: N): N
operator fun T.div(arg: N): N
companion object { companion object {
/** /**
* Create a nd-field for [Double] values * Create a nd-field for [Double] values
@ -68,14 +35,14 @@ interface NDField<T, F : Field<T>, N : NDStructure<T>> : Field<N> {
/** /**
* Create a nd-field with boxing generic buffer * Create a nd-field with boxing generic buffer
*/ */
fun <T : Any, F : Field<T>> generic(shape: IntArray, field: F) = GenericNDField(shape, field) fun <T : Any, F : Field<T>> buffered(shape: IntArray, field: F) =
BufferNDField(shape, field, ::boxingBuffer)
/** /**
* Create a most suitable implementation for nd-field using reified class. * Create a most suitable implementation for nd-field using reified class.
*/ */
inline fun <reified T : Any, F : Field<T>> auto(shape: IntArray, field: F): BufferNDField<T, F> { inline fun <reified T : Any, F : Field<T>> auto(shape: IntArray, field: F) =
return BufferNDField(shape, field, ::autoBuffer) BufferNDField(shape, field, ::autoBuffer)
}
} }
} }
@ -88,16 +55,16 @@ abstract class AbstractNDField<T, F : Field<T>, N : NDStructure<T>>(
override val one: N by lazy { produce { one } } override val one: N by lazy { produce { one } }
final override operator fun Function1<T, T>.invoke(structure: N) = map(structure) { value -> this@invoke(value) } operator fun Function1<T, T>.invoke(structure: N) = map(structure) { value -> this@invoke(value) }
final override operator fun N.plus(arg: T) = map(this) { value -> elementField.run { arg + value } } operator fun N.plus(arg: T) = map(this) { value -> elementField.run { arg + value } }
final override operator fun N.minus(arg: T) = map(this) { value -> elementField.run { arg - value } } operator fun N.minus(arg: T) = map(this) { value -> elementField.run { arg - value } }
final override operator fun N.times(arg: T) = map(this) { value -> elementField.run { arg * value } } operator fun N.times(arg: T) = map(this) { value -> elementField.run { arg * value } }
final override operator fun N.div(arg: T) = map(this) { value -> elementField.run { arg / value } } operator fun N.div(arg: T) = map(this) { value -> elementField.run { arg / value } }
final override operator fun T.plus(arg: N) = arg + this operator fun T.plus(arg: N) = arg + this
final override operator fun T.minus(arg: N) = arg - this operator fun T.minus(arg: N) = arg - this
final override operator fun T.times(arg: N) = arg * this operator fun T.times(arg: N) = arg * this
final override operator fun T.div(arg: N) = arg / this operator fun T.div(arg: N) = arg / this
/** /**
@ -135,23 +102,4 @@ abstract class AbstractNDField<T, F : Field<T>, N : NDStructure<T>>(
} }
} }
} }
}
class GenericNDField<T : Any, F : Field<T>>(
shape: IntArray,
elementField: F,
val bufferFactory: BufferFactory<T> = ::boxingBuffer
) : AbstractNDField<T, F, NDStructure<T>>(shape, elementField) {
override fun produce(initializer: F.(IntArray) -> T): NDStructure<T> =
ndStructure(shape, bufferFactory) { elementField.initializer(it) }
override fun map(arg: NDStructure<T>, transform: F.(T) -> T): NDStructure<T> =
produce { index -> transform(arg.get(index)) }
override fun mapIndexed(arg: NDStructure<T>, transform: F.(index: IntArray, T) -> T): NDStructure<T> =
produce { index -> transform(index, arg.get(index)) }
override fun combine(a: NDStructure<T>, b: NDStructure<T>, transform: F.(T, T) -> T): NDStructure<T> =
produce { index -> transform(a[index], b[index]) }
} }

View File

@ -9,7 +9,7 @@ class LazyNDFieldTest {
@Test @Test
fun testLazyStructure() { fun testLazyStructure() {
var counter = 0 var counter = 0
val regularStructure = NDField.generic(intArrayOf(2, 2, 2), IntField).produce { it[0] + it[1] - it[2] } val regularStructure = NDField.auto(intArrayOf(2, 2, 2), IntField).produce { it[0] + it[1] - it[2] }
val result = (regularStructure.lazy(IntField) + 2).map { val result = (regularStructure.lazy(IntField) + 2).map {
counter++ counter++
it * it it * it