0.3.0-dev-2 #244
@ -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].
|
||||||
*/
|
*/
|
||||||
|
@ -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>> {
|
||||||
|
@ -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]
|
||||||
|
@ -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)
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
@ -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,7 +65,8 @@ 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>> =
|
||||||
|
structures
|
||||||
.map(NDStructure<T>::shape)
|
.map(NDStructure<T>::shape)
|
||||||
.singleOrNull { !shape.contentEquals(it) }
|
.singleOrNull { !shape.contentEquals(it) }
|
||||||
?.let<IntArray, Array<out NDStructure<T>>> { throw ShapeMismatchException(shape, it) }
|
?.let<IntArray, Array<out NDStructure<T>>> { throw ShapeMismatchException(shape, it) }
|
||||||
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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.
|
||||||
|
@ -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> =
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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) }
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
)
|
)
|
||||||
|
@ -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() }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user