0.3.0-dev-2 #244

Merged
altavir merged 48 commits from dev into master 2021-03-15 19:52:27 +03:00
18 changed files with 30 additions and 29 deletions
Showing only changes of commit d9743978ab - Show all commits

View File

@ -28,7 +28,7 @@ public fun <T, R : Expression<T>> DifferentiableExpression<T, R>.derivative(name
/** /**
* A [DifferentiableExpression] that defines only first derivatives * A [DifferentiableExpression] that defines only first derivatives
*/ */
public abstract class FirstDerivativeExpression<T, R : Expression<T>> : DifferentiableExpression<T,R> { public abstract class FirstDerivativeExpression<T, R : Expression<T>> : DifferentiableExpression<T, R> {
/** /**
* Returns first derivative of this expression by given [symbol]. * Returns first derivative of this expression by given [symbol].
*/ */

View File

@ -95,7 +95,7 @@ public fun <T, E> ExpressionAlgebra<T, E>.bindSymbol(symbol: Symbol): E =
/** /**
* A delegate to create a symbol with a string identity in this scope * A delegate to create a symbol with a string identity in this scope
*/ */
public val symbol: ReadOnlyProperty<Any?, Symbol> = ReadOnlyProperty { _, property -> public val symbol: ReadOnlyProperty<Any?, Symbol> = ReadOnlyProperty { _, property ->
StringSymbol(property.name) StringSymbol(property.name)
} }

View File

@ -98,7 +98,7 @@ public open class FunctionalExpressionRing<T, A : Ring<T>>(
super<FunctionalExpressionGroup>.binaryOperationFunction(operation) super<FunctionalExpressionGroup>.binaryOperationFunction(operation)
} }
public open class FunctionalExpressionField<T, A: Field<T>>( public open class FunctionalExpressionField<T, A : Field<T>>(
algebra: A, algebra: A,
) : FunctionalExpressionRing<T, A>(algebra), Field<Expression<T>>, ) : FunctionalExpressionRing<T, A>(algebra), Field<Expression<T>>,
ScaleOperations<Expression<T>> { ScaleOperations<Expression<T>> {

View File

@ -48,7 +48,7 @@ public class BufferedLinearSpace<T : Any, A : Ring<T>>(
override fun Matrix<T>.dot(other: Matrix<T>): Matrix<T> { override fun Matrix<T>.dot(other: Matrix<T>): Matrix<T> {
require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" } require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" }
return elementAlgebra { return elementAlgebra {
val rows = this@dot.rows.map{it.linearize()} val rows = this@dot.rows.map { it.linearize() }
val columns = other.columns.map { it.linearize() } val columns = other.columns.map { it.linearize() }
buildMatrix(rowNum, other.colNum) { i, j -> buildMatrix(rowNum, other.colNum) { i, j ->
val r = rows[i] val r = rows[i]

View File

@ -175,7 +175,7 @@ public interface LinearSpace<T : Any, out A : Ring<T>> {
public fun <T : Any, A : Ring<T>> buffered( public fun <T : Any, A : Ring<T>> buffered(
algebra: A, algebra: A,
bufferFactory: BufferFactory<T> = Buffer.Companion::boxing, bufferFactory: BufferFactory<T> = Buffer.Companion::boxing,
): LinearSpace<T, A> = BufferedLinearSpace(algebra,bufferFactory) ): LinearSpace<T, A> = BufferedLinearSpace(algebra, bufferFactory)
public val real: LinearSpace<Double, RealField> = buffered(RealField, ::RealBuffer) public val real: LinearSpace<Double, RealField> = buffered(RealField, ::RealBuffer)

View File

@ -10,7 +10,7 @@ import space.kscience.kmath.nd.NDStructure
public class VirtualMatrix<T : Any>( public class VirtualMatrix<T : Any>(
override val rowNum: Int, override val rowNum: Int,
override val colNum: Int, override val colNum: Int,
public val generator: (i: Int, j: Int) -> T public val generator: (i: Int, j: Int) -> T,
) : Matrix<T> { ) : Matrix<T> {
override val shape: IntArray get() = intArrayOf(rowNum, colNum) override val shape: IntArray get() = intArrayOf(rowNum, colNum)

View File

@ -19,7 +19,7 @@ public class ShapeMismatchException(public val expected: IntArray, public val ac
* @param C the type of the element context. * @param C the type of the element context.
* @param N the type of the structure. * @param N the type of the structure.
*/ */
public interface NDAlgebra<T, C: Algebra<T>> { public interface NDAlgebra<T, C : Algebra<T>> {
/** /**
* The shape of ND-structures this algebra operates on. * The shape of ND-structures this algebra operates on.
*/ */
@ -65,11 +65,12 @@ public interface NDAlgebra<T, C: Algebra<T>> {
* @param structures the structures to check. * @param structures the structures to check.
* @return the array of valid structures. * @return the array of valid structures.
*/ */
internal fun <T, C: Algebra<T>> NDAlgebra<T, C>.checkShape(vararg structures: NDStructure<T>): Array<out NDStructure<T>> = structures internal fun <T, C : Algebra<T>> NDAlgebra<T, C>.checkShape(vararg structures: NDStructure<T>): Array<out NDStructure<T>> =
.map(NDStructure<T>::shape) structures
.singleOrNull { !shape.contentEquals(it) } .map(NDStructure<T>::shape)
?.let<IntArray, Array<out NDStructure<T>>> { throw ShapeMismatchException(shape, it) } .singleOrNull { !shape.contentEquals(it) }
?: structures ?.let<IntArray, Array<out NDStructure<T>>> { throw ShapeMismatchException(shape, it) }
?: structures
/** /**
* Checks if given element is consistent with this context. * Checks if given element is consistent with this context.
@ -77,7 +78,7 @@ internal fun <T, C: Algebra<T>> NDAlgebra<T, C>.checkShape(vararg structures: ND
* @param element the structure to check. * @param element the structure to check.
* @return the valid structure. * @return the valid structure.
*/ */
internal fun <T, C: Algebra<T>> NDAlgebra<T, C>.checkShape(element: NDStructure<T>): NDStructure<T> { internal fun <T, C : Algebra<T>> NDAlgebra<T, C>.checkShape(element: NDStructure<T>): NDStructure<T> {
if (!element.shape.contentEquals(shape)) throw ShapeMismatchException(shape, element.shape) if (!element.shape.contentEquals(shape)) throw ShapeMismatchException(shape, element.shape)
return element return element
} }

View File

@ -41,7 +41,7 @@ public interface AlgebraElement<T, C : Algebra<T>> {
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public operator fun <T : AlgebraElement<T, S>, S : NumbersAddOperations<T>> T.minus(b: T): T = public operator fun <T : AlgebraElement<T, S>, S : NumbersAddOperations<T>> T.minus(b: T): T =
context.add(this, context.run { -b}) context.add(this, context.run { -b })
/** /**
* Adds element to this one. * Adds element to this one.

View File

@ -71,7 +71,7 @@ public fun <T> Sequence<T>.sumWith(space: Group<T>): T = space.sum(this)
* @return the average value. * @return the average value.
* @author Iaroslav Postovalov * @author Iaroslav Postovalov
*/ */
public fun <T, S> Iterable<T>.averageWith(space: S): T where S : Group<T>, S : ScaleOperations<T> = public fun <T, S> Iterable<T>.averageWith(space: S): T where S : Group<T>, S : ScaleOperations<T> =
space.average(this) space.average(this)
/** /**
@ -82,7 +82,7 @@ public fun <T, S> Iterable<T>.averageWith(space: S): T where S : Group<T>, S :
* @return the average value. * @return the average value.
* @author Iaroslav Postovalov * @author Iaroslav Postovalov
*/ */
public fun <T, S> Sequence<T>.averageWith(space: S): T where S : Group<T>, S : ScaleOperations<T> = public fun <T, S> Sequence<T>.averageWith(space: S): T where S : Group<T>, S : ScaleOperations<T> =
space.average(this) space.average(this)
//TODO optimized power operation //TODO optimized power operation

View File

@ -13,10 +13,10 @@ internal class BufferAccessor2D<T : Any>(
public val colNum: Int, public val colNum: Int,
val factory: MutableBufferFactory<T>, val factory: MutableBufferFactory<T>,
) { ) {
public operator fun Buffer<T>.get(i: Int, j: Int): T = get(i*colNum + j) public operator fun Buffer<T>.get(i: Int, j: Int): T = get(i * colNum + j)
public operator fun MutableBuffer<T>.set(i: Int, j: Int, value: T) { public operator fun MutableBuffer<T>.set(i: Int, j: Int, value: T) {
set(i*colNum + j, value) set(i * colNum + j, value)
} }
public inline fun create(crossinline init: (i: Int, j: Int) -> T): MutableBuffer<T> = public inline fun create(crossinline init: (i: Int, j: Int) -> T): MutableBuffer<T> =

View File

@ -38,7 +38,7 @@ public fun FloatBuffer(vararg floats: Float): FloatBuffer = FloatBuffer(floats)
/** /**
* Returns a new [FloatArray] containing all of the elements of this [Buffer]. * Returns a new [FloatArray] containing all of the elements of this [Buffer].
*/ */
public fun Buffer<Float>.toFloatArray(): FloatArray = when(this) { public fun Buffer<Float>.toFloatArray(): FloatArray = when (this) {
is FloatBuffer -> array.copyOf() is FloatBuffer -> array.copyOf()
else -> FloatArray(size, ::get) else -> FloatArray(size, ::get)
} }

View File

@ -37,7 +37,7 @@ public fun IntBuffer(vararg ints: Int): IntBuffer = IntBuffer(ints)
/** /**
* Returns a new [IntArray] containing all of the elements of this [Buffer]. * Returns a new [IntArray] containing all of the elements of this [Buffer].
*/ */
public fun Buffer<Int>.toIntArray(): IntArray = when(this) { public fun Buffer<Int>.toIntArray(): IntArray = when (this) {
is IntBuffer -> array.copyOf() is IntBuffer -> array.copyOf()
else -> IntArray(size, ::get) else -> IntArray(size, ::get)
} }

View File

@ -37,7 +37,7 @@ public fun LongBuffer(vararg longs: Long): LongBuffer = LongBuffer(longs)
/** /**
* Returns a new [LongArray] containing all of the elements of this [Buffer]. * Returns a new [LongArray] containing all of the elements of this [Buffer].
*/ */
public fun Buffer<Long>.toLongArray(): LongArray = when(this) { public fun Buffer<Long>.toLongArray(): LongArray = when (this) {
is LongBuffer -> array.copyOf() is LongBuffer -> array.copyOf()
else -> LongArray(size, ::get) else -> LongArray(size, ::get)
} }

View File

@ -24,7 +24,7 @@ public open class MemoryBuffer<T : Any>(protected val memory: Memory, protected
public inline fun <T : Any> create( public inline fun <T : Any> create(
spec: MemorySpec<T>, spec: MemorySpec<T>,
size: Int, size: Int,
initializer: (Int) -> T initializer: (Int) -> T,
): MemoryBuffer<T> = MutableMemoryBuffer(Memory.allocate(size * spec.objectSize), spec).also { buffer -> ): MemoryBuffer<T> = MutableMemoryBuffer(Memory.allocate(size * spec.objectSize), spec).also { buffer ->
(0 until size).forEach { buffer[it] = initializer(it) } (0 until size).forEach { buffer[it] = initializer(it) }
} }
@ -53,7 +53,7 @@ public class MutableMemoryBuffer<T : Any>(memory: Memory, spec: MemorySpec<T>) :
public inline fun <T : Any> create( public inline fun <T : Any> create(
spec: MemorySpec<T>, spec: MemorySpec<T>,
size: Int, size: Int,
initializer: (Int) -> T initializer: (Int) -> T,
): MutableMemoryBuffer<T> = MutableMemoryBuffer(Memory.allocate(size * spec.objectSize), spec).also { buffer -> ): MutableMemoryBuffer<T> = MutableMemoryBuffer(Memory.allocate(size * spec.objectSize), spec).also { buffer ->
(0 until size).forEach { buffer[it] = initializer(it) } (0 until size).forEach { buffer[it] = initializer(it) }
} }

View File

@ -42,7 +42,7 @@ public fun RealBuffer.contentEquals(vararg doubles: Double): Boolean = array.con
/** /**
* Returns a new [DoubleArray] containing all of the elements of this [Buffer]. * Returns a new [DoubleArray] containing all of the elements of this [Buffer].
*/ */
public fun Buffer<Double>.toDoubleArray(): DoubleArray = when(this) { public fun Buffer<Double>.toDoubleArray(): DoubleArray = when (this) {
is RealBuffer -> array.copyOf() is RealBuffer -> array.copyOf()
else -> DoubleArray(size, ::get) else -> DoubleArray(size, ::get)
} }

View File

@ -35,7 +35,7 @@ public fun ShortBuffer(vararg shorts: Short): ShortBuffer = ShortBuffer(shorts)
/** /**
* Returns a new [ShortArray] containing all of the elements of this [Buffer]. * Returns a new [ShortArray] containing all of the elements of this [Buffer].
*/ */
public fun Buffer<Short>.toShortArray(): ShortArray = when(this) { public fun Buffer<Short>.toShortArray(): ShortArray = when (this) {
is ShortBuffer -> array.copyOf() is ShortBuffer -> array.copyOf()
else -> ShortArray(size, ::get) else -> ShortArray(size, ::get)
} }

View File

@ -17,7 +17,7 @@ class RealLUSolverTest {
@Test @Test
fun testDecomposition() { fun testDecomposition() {
LinearSpace.real.run { LinearSpace.real.run {
val matrix = matrix(2,2)( val matrix = matrix(2, 2)(
3.0, 1.0, 3.0, 1.0,
2.0, 3.0 2.0, 3.0
) )
@ -33,14 +33,14 @@ class RealLUSolverTest {
@Test @Test
fun testInvert() { fun testInvert() {
val matrix = LinearSpace.real.matrix(2,2)( val matrix = LinearSpace.real.matrix(2, 2)(
3.0, 1.0, 3.0, 1.0,
1.0, 3.0 1.0, 3.0
) )
val inverted = LinearSpace.real.inverseWithLup(matrix) val inverted = LinearSpace.real.inverseWithLup(matrix)
val expected = LinearSpace.real.matrix(2,2)( val expected = LinearSpace.real.matrix(2, 2)(
0.375, -0.125, 0.375, -0.125,
-0.125, 0.375 -0.125, 0.375
) )

View File

@ -11,7 +11,7 @@ import kotlin.test.assertEquals
@Suppress("UNUSED_VARIABLE") @Suppress("UNUSED_VARIABLE")
class NumberNDFieldTest { class NumberNDFieldTest {
val algebra = NDAlgebra.real(3,3) val algebra = NDAlgebra.real(3, 3)
val array1 = algebra.produce { (i, j) -> (i + j).toDouble() } val array1 = algebra.produce { (i, j) -> (i + j).toDouble() }
val array2 = algebra.produce { (i, j) -> (i - j).toDouble() } val array2 = algebra.produce { (i, j) -> (i - j).toDouble() }