Replace public override with override
This commit is contained in:
parent
3023bd639e
commit
b1998ed1a9
@ -14,12 +14,12 @@ private fun Appendable.appendEjmlVector(type: String, ejmlMatrixType: String) {
|
||||
@Language("kotlin") val text = """/**
|
||||
* [EjmlVector] specialization for [$type].
|
||||
*/
|
||||
public class Ejml${type}Vector<out M : $ejmlMatrixType>(public override val origin: M) : EjmlVector<$type, M>(origin) {
|
||||
public class Ejml${type}Vector<out M : $ejmlMatrixType>(override val origin: M) : EjmlVector<$type, M>(origin) {
|
||||
init {
|
||||
require(origin.numRows == 1) { "The origin matrix must have only one row to form a vector" }
|
||||
}
|
||||
|
||||
public override operator fun get(index: Int): $type = origin[0, index]
|
||||
override operator fun get(index: Int): $type = origin[0, index]
|
||||
}"""
|
||||
appendLine(text)
|
||||
appendLine()
|
||||
@ -29,8 +29,8 @@ private fun Appendable.appendEjmlMatrix(type: String, ejmlMatrixType: String) {
|
||||
val text = """/**
|
||||
* [EjmlMatrix] specialization for [$type].
|
||||
*/
|
||||
public class Ejml${type}Matrix<out M : $ejmlMatrixType>(public override val origin: M) : EjmlMatrix<$type, M>(origin) {
|
||||
public override operator fun get(i: Int, j: Int): $type = origin[i, j]
|
||||
public class Ejml${type}Matrix<out M : $ejmlMatrixType>(override val origin: M) : EjmlMatrix<$type, M>(origin) {
|
||||
override operator fun get(i: Int, j: Int): $type = origin[i, j]
|
||||
}"""
|
||||
appendLine(text)
|
||||
appendLine()
|
||||
@ -54,23 +54,23 @@ public object EjmlLinearSpace${ops} : EjmlLinearSpace<${type}, ${kmathAlgebra},
|
||||
/**
|
||||
* The [${kmathAlgebra}] reference.
|
||||
*/
|
||||
public override val elementAlgebra: $kmathAlgebra get() = $kmathAlgebra
|
||||
override val elementAlgebra: $kmathAlgebra get() = $kmathAlgebra
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override fun Matrix<${type}>.toEjml(): Ejml${type}Matrix<${ejmlMatrixType}> = when {
|
||||
override fun Matrix<${type}>.toEjml(): Ejml${type}Matrix<${ejmlMatrixType}> = when {
|
||||
this is Ejml${type}Matrix<*> && origin is $ejmlMatrixType -> this as Ejml${type}Matrix<${ejmlMatrixType}>
|
||||
else -> buildMatrix(rowNum, colNum) { i, j -> get(i, j) }
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override fun Point<${type}>.toEjml(): Ejml${type}Vector<${ejmlMatrixType}> = when {
|
||||
override fun Point<${type}>.toEjml(): Ejml${type}Vector<${ejmlMatrixType}> = when {
|
||||
this is Ejml${type}Vector<*> && origin is $ejmlMatrixType -> this as Ejml${type}Vector<${ejmlMatrixType}>
|
||||
else -> Ejml${type}Vector(${ejmlMatrixType}(size, 1).also {
|
||||
(0 until it.numRows).forEach { row -> it[row, 0] = get(row) }
|
||||
})
|
||||
}
|
||||
|
||||
public override fun buildMatrix(
|
||||
override fun buildMatrix(
|
||||
rows: Int,
|
||||
columns: Int,
|
||||
initializer: ${kmathAlgebra}.(i: Int, j: Int) -> ${type},
|
||||
@ -80,7 +80,7 @@ public object EjmlLinearSpace${ops} : EjmlLinearSpace<${type}, ${kmathAlgebra},
|
||||
}
|
||||
}.wrapMatrix()
|
||||
|
||||
public override fun buildVector(
|
||||
override fun buildVector(
|
||||
size: Int,
|
||||
initializer: ${kmathAlgebra}.(Int) -> ${type},
|
||||
): Ejml${type}Vector<${ejmlMatrixType}> = Ejml${type}Vector(${ejmlMatrixType}(size, 1).also {
|
||||
@ -90,21 +90,21 @@ public object EjmlLinearSpace${ops} : EjmlLinearSpace<${type}, ${kmathAlgebra},
|
||||
private fun <T : ${ejmlMatrixParentTypeMatrix}> T.wrapMatrix() = Ejml${type}Matrix(this)
|
||||
private fun <T : ${ejmlMatrixParentTypeMatrix}> T.wrapVector() = Ejml${type}Vector(this)
|
||||
|
||||
public override fun Matrix<${type}>.unaryMinus(): Matrix<${type}> = this * elementAlgebra { -one }
|
||||
override fun Matrix<${type}>.unaryMinus(): Matrix<${type}> = this * elementAlgebra { -one }
|
||||
|
||||
public override fun Matrix<${type}>.dot(other: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> {
|
||||
override fun Matrix<${type}>.dot(other: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> {
|
||||
val out = ${ejmlMatrixType}(1, 1)
|
||||
CommonOps_${ops}.mult(toEjml().origin, other.toEjml().origin, out)
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Matrix<${type}>.dot(vector: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> {
|
||||
override fun Matrix<${type}>.dot(vector: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> {
|
||||
val out = ${ejmlMatrixType}(1, 1)
|
||||
CommonOps_${ops}.mult(toEjml().origin, vector.toEjml().origin, out)
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override operator fun Matrix<${type}>.minus(other: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> {
|
||||
override operator fun Matrix<${type}>.minus(other: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> {
|
||||
val out = ${ejmlMatrixType}(1, 1)
|
||||
|
||||
CommonOps_${ops}.add(
|
||||
@ -123,19 +123,19 @@ public object EjmlLinearSpace${ops} : EjmlLinearSpace<${type}, ${kmathAlgebra},
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override operator fun Matrix<${type}>.times(value: ${type}): Ejml${type}Matrix<${ejmlMatrixType}> {
|
||||
override operator fun Matrix<${type}>.times(value: ${type}): Ejml${type}Matrix<${ejmlMatrixType}> {
|
||||
val res = ${ejmlMatrixType}(1, 1)
|
||||
CommonOps_${ops}.scale(value, toEjml().origin, res)
|
||||
return res.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Point<${type}>.unaryMinus(): Ejml${type}Vector<${ejmlMatrixType}> {
|
||||
override fun Point<${type}>.unaryMinus(): Ejml${type}Vector<${ejmlMatrixType}> {
|
||||
val res = ${ejmlMatrixType}(1, 1)
|
||||
CommonOps_${ops}.changeSign(toEjml().origin, res)
|
||||
return res.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Matrix<${type}>.plus(other: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> {
|
||||
override fun Matrix<${type}>.plus(other: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> {
|
||||
val out = ${ejmlMatrixType}(1, 1)
|
||||
|
||||
CommonOps_${ops}.add(
|
||||
@ -154,7 +154,7 @@ public object EjmlLinearSpace${ops} : EjmlLinearSpace<${type}, ${kmathAlgebra},
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Point<${type}>.plus(other: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> {
|
||||
override fun Point<${type}>.plus(other: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> {
|
||||
val out = ${ejmlMatrixType}(1, 1)
|
||||
|
||||
CommonOps_${ops}.add(
|
||||
@ -173,7 +173,7 @@ public object EjmlLinearSpace${ops} : EjmlLinearSpace<${type}, ${kmathAlgebra},
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Point<${type}>.minus(other: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> {
|
||||
override fun Point<${type}>.minus(other: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> {
|
||||
val out = ${ejmlMatrixType}(1, 1)
|
||||
|
||||
CommonOps_${ops}.add(
|
||||
@ -192,18 +192,18 @@ public object EjmlLinearSpace${ops} : EjmlLinearSpace<${type}, ${kmathAlgebra},
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override fun ${type}.times(m: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> = m * this
|
||||
override fun ${type}.times(m: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> = m * this
|
||||
|
||||
public override fun Point<${type}>.times(value: ${type}): Ejml${type}Vector<${ejmlMatrixType}> {
|
||||
override fun Point<${type}>.times(value: ${type}): Ejml${type}Vector<${ejmlMatrixType}> {
|
||||
val res = ${ejmlMatrixType}(1, 1)
|
||||
CommonOps_${ops}.scale(value, toEjml().origin, res)
|
||||
return res.wrapVector()
|
||||
}
|
||||
|
||||
public override fun ${type}.times(v: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> = v * this
|
||||
override fun ${type}.times(v: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> = v * this
|
||||
|
||||
@UnstableKMathAPI
|
||||
public override fun <F : StructureFeature> getFeature(structure: Matrix<${type}>, type: KClass<out F>): F? {
|
||||
override fun <F : StructureFeature> getFeature(structure: Matrix<${type}>, type: KClass<out F>): F? {
|
||||
structure.getFeature(type)?.let { return it }
|
||||
val origin = structure.toEjml().origin
|
||||
|
||||
|
@ -27,7 +27,7 @@ import space.kscience.kmath.misc.UnstableKMathAPI
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public object LatexSyntaxRenderer : SyntaxRenderer {
|
||||
public override fun render(node: MathSyntax, output: Appendable): Unit = output.run {
|
||||
override fun render(node: MathSyntax, output: Appendable): Unit = output.run {
|
||||
fun render(syntax: MathSyntax) = render(syntax, output)
|
||||
|
||||
when (node) {
|
||||
|
@ -16,7 +16,7 @@ import space.kscience.kmath.misc.UnstableKMathAPI
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public object MathMLSyntaxRenderer : SyntaxRenderer {
|
||||
public override fun render(node: MathSyntax, output: Appendable) {
|
||||
override fun render(node: MathSyntax, output: Appendable) {
|
||||
output.append("<math xmlns=\"https://www.w3.org/1998/Math/MathML\"><mrow>")
|
||||
renderPart(node, output)
|
||||
output.append("</mrow></math>")
|
||||
|
@ -29,7 +29,7 @@ public fun interface MathRenderer {
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public open class FeaturedMathRenderer(public val features: List<RenderFeature>) : MathRenderer {
|
||||
public override fun render(mst: MST): MathSyntax {
|
||||
override fun render(mst: MST): MathSyntax {
|
||||
for (feature in features) feature.render(this, mst)?.let { return it }
|
||||
throw UnsupportedOperationException("Renderer $this has no appropriate feature to render node $mst.")
|
||||
}
|
||||
@ -56,7 +56,7 @@ public open class FeaturedMathRendererWithPostProcess(
|
||||
features: List<RenderFeature>,
|
||||
public val stages: List<PostProcessPhase>,
|
||||
) : FeaturedMathRenderer(features) {
|
||||
public override fun render(mst: MST): MathSyntax {
|
||||
override fun render(mst: MST): MathSyntax {
|
||||
val res = super.render(mst)
|
||||
for (stage in stages) stage.perform(res)
|
||||
return res
|
||||
|
@ -150,9 +150,9 @@ public data class OperandSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class UnaryOperatorSyntax(
|
||||
public override val operation: String,
|
||||
override val operation: String,
|
||||
public var prefix: MathSyntax,
|
||||
public override val operand: OperandSyntax,
|
||||
override val operand: OperandSyntax,
|
||||
) : UnarySyntax() {
|
||||
init {
|
||||
operand.parent = this
|
||||
@ -166,8 +166,8 @@ public data class UnaryOperatorSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class UnaryPlusSyntax(
|
||||
public override val operation: String,
|
||||
public override val operand: OperandSyntax,
|
||||
override val operation: String,
|
||||
override val operand: OperandSyntax,
|
||||
) : UnarySyntax() {
|
||||
init {
|
||||
operand.parent = this
|
||||
@ -181,8 +181,8 @@ public data class UnaryPlusSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class UnaryMinusSyntax(
|
||||
public override val operation: String,
|
||||
public override val operand: OperandSyntax,
|
||||
override val operation: String,
|
||||
override val operand: OperandSyntax,
|
||||
) : UnarySyntax() {
|
||||
init {
|
||||
operand.parent = this
|
||||
@ -197,8 +197,8 @@ public data class UnaryMinusSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class RadicalSyntax(
|
||||
public override val operation: String,
|
||||
public override val operand: MathSyntax,
|
||||
override val operation: String,
|
||||
override val operand: MathSyntax,
|
||||
) : UnarySyntax() {
|
||||
init {
|
||||
operand.parent = this
|
||||
@ -215,8 +215,8 @@ public data class RadicalSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class ExponentSyntax(
|
||||
public override val operation: String,
|
||||
public override val operand: OperandSyntax,
|
||||
override val operation: String,
|
||||
override val operand: OperandSyntax,
|
||||
public var useOperatorForm: Boolean,
|
||||
) : UnarySyntax() {
|
||||
init {
|
||||
@ -233,9 +233,9 @@ public data class ExponentSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class SuperscriptSyntax(
|
||||
public override val operation: String,
|
||||
public override val left: MathSyntax,
|
||||
public override val right: MathSyntax,
|
||||
override val operation: String,
|
||||
override val left: MathSyntax,
|
||||
override val right: MathSyntax,
|
||||
) : BinarySyntax() {
|
||||
init {
|
||||
left.parent = this
|
||||
@ -252,9 +252,9 @@ public data class SuperscriptSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class SubscriptSyntax(
|
||||
public override val operation: String,
|
||||
public override val left: MathSyntax,
|
||||
public override val right: MathSyntax,
|
||||
override val operation: String,
|
||||
override val left: MathSyntax,
|
||||
override val right: MathSyntax,
|
||||
) : BinarySyntax() {
|
||||
init {
|
||||
left.parent = this
|
||||
@ -270,10 +270,10 @@ public data class SubscriptSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class BinaryOperatorSyntax(
|
||||
public override val operation: String,
|
||||
override val operation: String,
|
||||
public var prefix: MathSyntax,
|
||||
public override val left: MathSyntax,
|
||||
public override val right: MathSyntax,
|
||||
override val left: MathSyntax,
|
||||
override val right: MathSyntax,
|
||||
) : BinarySyntax() {
|
||||
init {
|
||||
left.parent = this
|
||||
@ -290,9 +290,9 @@ public data class BinaryOperatorSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class BinaryPlusSyntax(
|
||||
public override val operation: String,
|
||||
public override val left: OperandSyntax,
|
||||
public override val right: OperandSyntax,
|
||||
override val operation: String,
|
||||
override val left: OperandSyntax,
|
||||
override val right: OperandSyntax,
|
||||
) : BinarySyntax() {
|
||||
init {
|
||||
left.parent = this
|
||||
@ -309,9 +309,9 @@ public data class BinaryPlusSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class BinaryMinusSyntax(
|
||||
public override val operation: String,
|
||||
public override val left: OperandSyntax,
|
||||
public override val right: OperandSyntax,
|
||||
override val operation: String,
|
||||
override val left: OperandSyntax,
|
||||
override val right: OperandSyntax,
|
||||
) : BinarySyntax() {
|
||||
init {
|
||||
left.parent = this
|
||||
@ -329,9 +329,9 @@ public data class BinaryMinusSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class FractionSyntax(
|
||||
public override val operation: String,
|
||||
public override val left: OperandSyntax,
|
||||
public override val right: OperandSyntax,
|
||||
override val operation: String,
|
||||
override val left: OperandSyntax,
|
||||
override val right: OperandSyntax,
|
||||
public var infix: Boolean,
|
||||
) : BinarySyntax() {
|
||||
init {
|
||||
@ -349,9 +349,9 @@ public data class FractionSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class RadicalWithIndexSyntax(
|
||||
public override val operation: String,
|
||||
public override val left: MathSyntax,
|
||||
public override val right: MathSyntax,
|
||||
override val operation: String,
|
||||
override val left: MathSyntax,
|
||||
override val right: MathSyntax,
|
||||
) : BinarySyntax() {
|
||||
init {
|
||||
left.parent = this
|
||||
@ -369,9 +369,9 @@ public data class RadicalWithIndexSyntax(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public data class MultiplicationSyntax(
|
||||
public override val operation: String,
|
||||
public override val left: OperandSyntax,
|
||||
public override val right: OperandSyntax,
|
||||
override val operation: String,
|
||||
override val left: OperandSyntax,
|
||||
override val right: OperandSyntax,
|
||||
public var times: Boolean,
|
||||
) : BinarySyntax() {
|
||||
init {
|
||||
|
@ -57,7 +57,7 @@ else
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class PrettyPrintFloats(public val types: Set<KClass<out Number>>) : RenderFeature {
|
||||
public override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? {
|
||||
override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? {
|
||||
if (node !is MST.Numeric || node.value::class !in types) return null
|
||||
|
||||
val toString = when (val v = node.value) {
|
||||
@ -117,7 +117,7 @@ public class PrettyPrintFloats(public val types: Set<KClass<out Number>>) : Rend
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class PrettyPrintIntegers(public val types: Set<KClass<out Number>>) : RenderFeature {
|
||||
public override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
|
||||
override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
|
||||
if (node !is MST.Numeric || node.value::class !in types)
|
||||
null
|
||||
else
|
||||
@ -140,7 +140,7 @@ public class PrettyPrintIntegers(public val types: Set<KClass<out Number>>) : Re
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class PrettyPrintPi(public val symbols: Set<String>) : RenderFeature {
|
||||
public override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
|
||||
override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
|
||||
if (node !is Symbol || node.identity !in symbols)
|
||||
null
|
||||
else
|
||||
@ -202,7 +202,7 @@ public abstract class Binary(public val operations: Collection<String>?) : Rende
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class BinaryPlus(operations: Collection<String>?) : Binary(operations) {
|
||||
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
||||
BinaryPlusSyntax(
|
||||
operation = node.operation,
|
||||
left = OperandSyntax(parent.render(node.left), true),
|
||||
@ -224,7 +224,7 @@ public class BinaryPlus(operations: Collection<String>?) : Binary(operations) {
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class BinaryMinus(operations: Collection<String>?) : Binary(operations) {
|
||||
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
||||
BinaryMinusSyntax(
|
||||
operation = node.operation,
|
||||
left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
|
||||
@ -246,7 +246,7 @@ public class BinaryMinus(operations: Collection<String>?) : Binary(operations) {
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class UnaryPlus(operations: Collection<String>?) : Unary(operations) {
|
||||
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryPlusSyntax(
|
||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryPlusSyntax(
|
||||
operation = node.operation,
|
||||
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
|
||||
)
|
||||
@ -266,7 +266,7 @@ public class UnaryPlus(operations: Collection<String>?) : Unary(operations) {
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class UnaryMinus(operations: Collection<String>?) : Unary(operations) {
|
||||
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryMinusSyntax(
|
||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryMinusSyntax(
|
||||
operation = node.operation,
|
||||
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
|
||||
)
|
||||
@ -286,7 +286,7 @@ public class UnaryMinus(operations: Collection<String>?) : Unary(operations) {
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class Fraction(operations: Collection<String>?) : Binary(operations) {
|
||||
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = FractionSyntax(
|
||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = FractionSyntax(
|
||||
operation = node.operation,
|
||||
left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
|
||||
right = OperandSyntax(operand = parent.render(node.right), parentheses = true),
|
||||
@ -308,7 +308,7 @@ public class Fraction(operations: Collection<String>?) : Binary(operations) {
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class BinaryOperator(operations: Collection<String>?) : Binary(operations) {
|
||||
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
||||
BinaryOperatorSyntax(
|
||||
operation = node.operation,
|
||||
prefix = OperatorNameSyntax(name = node.operation),
|
||||
@ -331,7 +331,7 @@ public class BinaryOperator(operations: Collection<String>?) : Binary(operations
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class UnaryOperator(operations: Collection<String>?) : Unary(operations) {
|
||||
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
||||
UnaryOperatorSyntax(
|
||||
operation = node.operation,
|
||||
prefix = OperatorNameSyntax(node.operation),
|
||||
@ -353,7 +353,7 @@ public class UnaryOperator(operations: Collection<String>?) : Unary(operations)
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class Power(operations: Collection<String>?) : Binary(operations) {
|
||||
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
||||
SuperscriptSyntax(
|
||||
operation = node.operation,
|
||||
left = OperandSyntax(parent.render(node.left), true),
|
||||
@ -373,7 +373,7 @@ public class Power(operations: Collection<String>?) : Binary(operations) {
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class SquareRoot(operations: Collection<String>?) : Unary(operations) {
|
||||
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
||||
RadicalSyntax(operation = node.operation, operand = parent.render(node.value))
|
||||
|
||||
public companion object {
|
||||
@ -391,7 +391,7 @@ public class SquareRoot(operations: Collection<String>?) : Unary(operations) {
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class Exponent(operations: Collection<String>?) : Unary(operations) {
|
||||
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = ExponentSyntax(
|
||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = ExponentSyntax(
|
||||
operation = node.operation,
|
||||
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
|
||||
useOperatorForm = true,
|
||||
@ -412,7 +412,7 @@ public class Exponent(operations: Collection<String>?) : Unary(operations) {
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class Multiplication(operations: Collection<String>?) : Binary(operations) {
|
||||
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
||||
MultiplicationSyntax(
|
||||
operation = node.operation,
|
||||
left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
|
||||
@ -435,7 +435,7 @@ public class Multiplication(operations: Collection<String>?) : Binary(operations
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class InverseTrigonometricOperations(operations: Collection<String>?) : Unary(operations) {
|
||||
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
||||
UnaryOperatorSyntax(
|
||||
operation = node.operation,
|
||||
prefix = OperatorNameSyntax(name = node.operation.replaceFirst("a", "arc")),
|
||||
@ -462,7 +462,7 @@ public class InverseTrigonometricOperations(operations: Collection<String>?) : U
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class InverseHyperbolicOperations(operations: Collection<String>?) : Unary(operations) {
|
||||
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
||||
UnaryOperatorSyntax(
|
||||
operation = node.operation,
|
||||
prefix = OperatorNameSyntax(name = node.operation.replaceFirst("a", "ar")),
|
||||
|
@ -205,7 +205,7 @@ public val BetterExponent: PostProcessPhase = PostProcessPhase { node ->
|
||||
@UnstableKMathAPI
|
||||
public class SimplifyParentheses(public val precedenceFunction: (MathSyntax) -> Int) :
|
||||
PostProcessPhase {
|
||||
public override fun perform(node: MathSyntax): Unit = when (node) {
|
||||
override fun perform(node: MathSyntax): Unit = when (node) {
|
||||
is NumberSyntax -> Unit
|
||||
is SymbolSyntax -> Unit
|
||||
is OperatorNameSyntax -> Unit
|
||||
|
@ -25,10 +25,10 @@ public class DerivativeStructureField(
|
||||
NumbersAddOperations<DerivativeStructure> {
|
||||
public val numberOfVariables: Int = bindings.size
|
||||
|
||||
public override val zero: DerivativeStructure by lazy { DerivativeStructure(numberOfVariables, order) }
|
||||
public override val one: DerivativeStructure by lazy { DerivativeStructure(numberOfVariables, order, 1.0) }
|
||||
override val zero: DerivativeStructure by lazy { DerivativeStructure(numberOfVariables, order) }
|
||||
override val one: DerivativeStructure by lazy { DerivativeStructure(numberOfVariables, order, 1.0) }
|
||||
|
||||
public override fun number(value: Number): DerivativeStructure = const(value.toDouble())
|
||||
override fun number(value: Number): DerivativeStructure = const(value.toDouble())
|
||||
|
||||
/**
|
||||
* A class that implements both [DerivativeStructure] and a [Symbol]
|
||||
@ -39,10 +39,10 @@ public class DerivativeStructureField(
|
||||
symbol: Symbol,
|
||||
value: Double,
|
||||
) : DerivativeStructure(size, order, index, value), Symbol {
|
||||
public override val identity: String = symbol.identity
|
||||
public override fun toString(): String = identity
|
||||
public override fun equals(other: Any?): Boolean = this.identity == (other as? Symbol)?.identity
|
||||
public override fun hashCode(): Int = identity.hashCode()
|
||||
override val identity: String = symbol.identity
|
||||
override fun toString(): String = identity
|
||||
override fun equals(other: Any?): Boolean = this.identity == (other as? Symbol)?.identity
|
||||
override fun hashCode(): Int = identity.hashCode()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -52,10 +52,10 @@ public class DerivativeStructureField(
|
||||
key.identity to DerivativeStructureSymbol(numberOfVariables, index, key, value)
|
||||
}.toMap()
|
||||
|
||||
public override fun const(value: Double): DerivativeStructure = DerivativeStructure(numberOfVariables, order, value)
|
||||
override fun const(value: Double): DerivativeStructure = DerivativeStructure(numberOfVariables, order, value)
|
||||
|
||||
public override fun bindSymbolOrNull(value: String): DerivativeStructureSymbol? = variables[value]
|
||||
public override fun bindSymbol(value: String): DerivativeStructureSymbol = variables.getValue(value)
|
||||
override fun bindSymbolOrNull(value: String): DerivativeStructureSymbol? = variables[value]
|
||||
override fun bindSymbol(value: String): DerivativeStructureSymbol = variables.getValue(value)
|
||||
|
||||
public fun bindSymbolOrNull(symbol: Symbol): DerivativeStructureSymbol? = variables[symbol.identity]
|
||||
public fun bindSymbol(symbol: Symbol): DerivativeStructureSymbol = variables.getValue(symbol.identity)
|
||||
@ -68,45 +68,45 @@ public class DerivativeStructureField(
|
||||
|
||||
public fun DerivativeStructure.derivative(vararg symbols: Symbol): Double = derivative(symbols.toList())
|
||||
|
||||
public override fun DerivativeStructure.unaryMinus(): DerivativeStructure = negate()
|
||||
override fun DerivativeStructure.unaryMinus(): DerivativeStructure = negate()
|
||||
|
||||
public override fun add(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.add(b)
|
||||
override fun add(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.add(b)
|
||||
|
||||
public override fun scale(a: DerivativeStructure, value: Double): DerivativeStructure = a.multiply(value)
|
||||
override fun scale(a: DerivativeStructure, value: Double): DerivativeStructure = a.multiply(value)
|
||||
|
||||
public override fun multiply(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.multiply(b)
|
||||
public override fun divide(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.divide(b)
|
||||
public override fun sin(arg: DerivativeStructure): DerivativeStructure = arg.sin()
|
||||
public override fun cos(arg: DerivativeStructure): DerivativeStructure = arg.cos()
|
||||
public override fun tan(arg: DerivativeStructure): DerivativeStructure = arg.tan()
|
||||
public override fun asin(arg: DerivativeStructure): DerivativeStructure = arg.asin()
|
||||
public override fun acos(arg: DerivativeStructure): DerivativeStructure = arg.acos()
|
||||
public override fun atan(arg: DerivativeStructure): DerivativeStructure = arg.atan()
|
||||
public override fun sinh(arg: DerivativeStructure): DerivativeStructure = arg.sinh()
|
||||
public override fun cosh(arg: DerivativeStructure): DerivativeStructure = arg.cosh()
|
||||
public override fun tanh(arg: DerivativeStructure): DerivativeStructure = arg.tanh()
|
||||
public override fun asinh(arg: DerivativeStructure): DerivativeStructure = arg.asinh()
|
||||
public override fun acosh(arg: DerivativeStructure): DerivativeStructure = arg.acosh()
|
||||
public override fun atanh(arg: DerivativeStructure): DerivativeStructure = arg.atanh()
|
||||
override fun multiply(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.multiply(b)
|
||||
override fun divide(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.divide(b)
|
||||
override fun sin(arg: DerivativeStructure): DerivativeStructure = arg.sin()
|
||||
override fun cos(arg: DerivativeStructure): DerivativeStructure = arg.cos()
|
||||
override fun tan(arg: DerivativeStructure): DerivativeStructure = arg.tan()
|
||||
override fun asin(arg: DerivativeStructure): DerivativeStructure = arg.asin()
|
||||
override fun acos(arg: DerivativeStructure): DerivativeStructure = arg.acos()
|
||||
override fun atan(arg: DerivativeStructure): DerivativeStructure = arg.atan()
|
||||
override fun sinh(arg: DerivativeStructure): DerivativeStructure = arg.sinh()
|
||||
override fun cosh(arg: DerivativeStructure): DerivativeStructure = arg.cosh()
|
||||
override fun tanh(arg: DerivativeStructure): DerivativeStructure = arg.tanh()
|
||||
override fun asinh(arg: DerivativeStructure): DerivativeStructure = arg.asinh()
|
||||
override fun acosh(arg: DerivativeStructure): DerivativeStructure = arg.acosh()
|
||||
override fun atanh(arg: DerivativeStructure): DerivativeStructure = arg.atanh()
|
||||
|
||||
public override fun power(arg: DerivativeStructure, pow: Number): DerivativeStructure = when (pow) {
|
||||
override fun power(arg: DerivativeStructure, pow: Number): DerivativeStructure = when (pow) {
|
||||
is Double -> arg.pow(pow)
|
||||
is Int -> arg.pow(pow)
|
||||
else -> arg.pow(pow.toDouble())
|
||||
}
|
||||
|
||||
public fun power(arg: DerivativeStructure, pow: DerivativeStructure): DerivativeStructure = arg.pow(pow)
|
||||
public override fun exp(arg: DerivativeStructure): DerivativeStructure = arg.exp()
|
||||
public override fun ln(arg: DerivativeStructure): DerivativeStructure = arg.log()
|
||||
override fun exp(arg: DerivativeStructure): DerivativeStructure = arg.exp()
|
||||
override fun ln(arg: DerivativeStructure): DerivativeStructure = arg.log()
|
||||
|
||||
public override operator fun DerivativeStructure.plus(b: Number): DerivativeStructure = add(b.toDouble())
|
||||
public override operator fun DerivativeStructure.minus(b: Number): DerivativeStructure = subtract(b.toDouble())
|
||||
public override operator fun Number.plus(b: DerivativeStructure): DerivativeStructure = b + this
|
||||
public override operator fun Number.minus(b: DerivativeStructure): DerivativeStructure = b - this
|
||||
override operator fun DerivativeStructure.plus(b: Number): DerivativeStructure = add(b.toDouble())
|
||||
override operator fun DerivativeStructure.minus(b: Number): DerivativeStructure = subtract(b.toDouble())
|
||||
override operator fun Number.plus(b: DerivativeStructure): DerivativeStructure = b + this
|
||||
override operator fun Number.minus(b: DerivativeStructure): DerivativeStructure = b - this
|
||||
|
||||
public companion object :
|
||||
AutoDiffProcessor<Double, DerivativeStructure, DerivativeStructureField, Expression<Double>> {
|
||||
public override fun process(function: DerivativeStructureField.() -> DerivativeStructure): DifferentiableExpression<Double> =
|
||||
override fun process(function: DerivativeStructureField.() -> DerivativeStructure): DifferentiableExpression<Double> =
|
||||
DerivativeStructureExpression(function)
|
||||
}
|
||||
}
|
||||
@ -117,13 +117,13 @@ public class DerivativeStructureField(
|
||||
public class DerivativeStructureExpression(
|
||||
public val function: DerivativeStructureField.() -> DerivativeStructure,
|
||||
) : DifferentiableExpression<Double> {
|
||||
public override operator fun invoke(arguments: Map<Symbol, Double>): Double =
|
||||
override operator fun invoke(arguments: Map<Symbol, Double>): Double =
|
||||
DerivativeStructureField(0, arguments).function().value
|
||||
|
||||
/**
|
||||
* Get the derivative expression with given orders
|
||||
*/
|
||||
public override fun derivativeOrNull(symbols: List<Symbol>): Expression<Double> = Expression { arguments ->
|
||||
override fun derivativeOrNull(symbols: List<Symbol>): Expression<Double> = Expression { arguments ->
|
||||
with(DerivativeStructureField(symbols.size, arguments)) { function().derivative(symbols) }
|
||||
}
|
||||
}
|
||||
|
@ -15,18 +15,18 @@ import kotlin.reflect.KClass
|
||||
import kotlin.reflect.cast
|
||||
|
||||
public class CMMatrix(public val origin: RealMatrix) : Matrix<Double> {
|
||||
public override val rowNum: Int get() = origin.rowDimension
|
||||
public override val colNum: Int get() = origin.columnDimension
|
||||
override val rowNum: Int get() = origin.rowDimension
|
||||
override val colNum: Int get() = origin.columnDimension
|
||||
|
||||
public override operator fun get(i: Int, j: Int): Double = origin.getEntry(i, j)
|
||||
override operator fun get(i: Int, j: Int): Double = origin.getEntry(i, j)
|
||||
}
|
||||
|
||||
public class CMVector(public val origin: RealVector) : Point<Double> {
|
||||
public override val size: Int get() = origin.dimension
|
||||
override val size: Int get() = origin.dimension
|
||||
|
||||
public override operator fun get(index: Int): Double = origin.getEntry(index)
|
||||
override operator fun get(index: Int): Double = origin.getEntry(index)
|
||||
|
||||
public override operator fun iterator(): Iterator<Double> = origin.toArray().iterator()
|
||||
override operator fun iterator(): Iterator<Double> = origin.toArray().iterator()
|
||||
}
|
||||
|
||||
public fun RealVector.toPoint(): CMVector = CMVector(this)
|
||||
@ -34,7 +34,7 @@ public fun RealVector.toPoint(): CMVector = CMVector(this)
|
||||
public object CMLinearSpace : LinearSpace<Double, DoubleField> {
|
||||
override val elementAlgebra: DoubleField get() = DoubleField
|
||||
|
||||
public override fun buildMatrix(
|
||||
override fun buildMatrix(
|
||||
rows: Int,
|
||||
columns: Int,
|
||||
initializer: DoubleField.(i: Int, j: Int) -> Double,
|
||||
@ -73,16 +73,16 @@ public object CMLinearSpace : LinearSpace<Double, DoubleField> {
|
||||
override fun Point<Double>.minus(other: Point<Double>): CMVector =
|
||||
toCM().origin.subtract(other.toCM().origin).wrap()
|
||||
|
||||
public override fun Matrix<Double>.dot(other: Matrix<Double>): CMMatrix =
|
||||
override fun Matrix<Double>.dot(other: Matrix<Double>): CMMatrix =
|
||||
toCM().origin.multiply(other.toCM().origin).wrap()
|
||||
|
||||
public override fun Matrix<Double>.dot(vector: Point<Double>): CMVector =
|
||||
override fun Matrix<Double>.dot(vector: Point<Double>): CMVector =
|
||||
toCM().origin.preMultiply(vector.toCM().origin).wrap()
|
||||
|
||||
public override operator fun Matrix<Double>.minus(other: Matrix<Double>): CMMatrix =
|
||||
override operator fun Matrix<Double>.minus(other: Matrix<Double>): CMMatrix =
|
||||
toCM().origin.subtract(other.toCM().origin).wrap()
|
||||
|
||||
public override operator fun Matrix<Double>.times(value: Double): CMMatrix =
|
||||
override operator fun Matrix<Double>.times(value: Double): CMMatrix =
|
||||
toCM().origin.scalarMultiply(value).wrap()
|
||||
|
||||
override fun Double.times(m: Matrix<Double>): CMMatrix =
|
||||
|
@ -52,11 +52,11 @@ public class CMOptimization(
|
||||
|
||||
public fun exportOptimizationData(): List<OptimizationData> = optimizationData.values.toList()
|
||||
|
||||
public override fun initialGuess(map: Map<Symbol, Double>) {
|
||||
override fun initialGuess(map: Map<Symbol, Double>) {
|
||||
addOptimizationData(InitialGuess(map.toDoubleArray()))
|
||||
}
|
||||
|
||||
public override fun function(expression: Expression<Double>) {
|
||||
override fun function(expression: Expression<Double>) {
|
||||
val objectiveFunction = ObjectiveFunction {
|
||||
val args = it.toMap()
|
||||
expression(args)
|
||||
@ -64,7 +64,7 @@ public class CMOptimization(
|
||||
addOptimizationData(objectiveFunction)
|
||||
}
|
||||
|
||||
public override fun diffFunction(expression: DifferentiableExpression<Double>) {
|
||||
override fun diffFunction(expression: DifferentiableExpression<Double>) {
|
||||
function(expression)
|
||||
val gradientFunction = ObjectiveFunctionGradient {
|
||||
val args = it.toMap()
|
||||
|
@ -16,31 +16,31 @@ public class CMRandomGeneratorWrapper(
|
||||
) : org.apache.commons.math3.random.RandomGenerator {
|
||||
private var generator: RandomGenerator = factory(intArrayOf())
|
||||
|
||||
public override fun nextBoolean(): Boolean = generator.nextBoolean()
|
||||
public override fun nextFloat(): Float = generator.nextDouble().toFloat()
|
||||
override fun nextBoolean(): Boolean = generator.nextBoolean()
|
||||
override fun nextFloat(): Float = generator.nextDouble().toFloat()
|
||||
|
||||
public override fun setSeed(seed: Int) {
|
||||
override fun setSeed(seed: Int) {
|
||||
generator = factory(intArrayOf(seed))
|
||||
}
|
||||
|
||||
public override fun setSeed(seed: IntArray) {
|
||||
override fun setSeed(seed: IntArray) {
|
||||
generator = factory(seed)
|
||||
}
|
||||
|
||||
public override fun setSeed(seed: Long) {
|
||||
override fun setSeed(seed: Long) {
|
||||
setSeed(seed.toInt())
|
||||
}
|
||||
|
||||
public override fun nextBytes(bytes: ByteArray) {
|
||||
override fun nextBytes(bytes: ByteArray) {
|
||||
generator.fillBytes(bytes)
|
||||
}
|
||||
|
||||
public override fun nextInt(): Int = generator.nextInt()
|
||||
public override fun nextInt(n: Int): Int = generator.nextInt(n)
|
||||
override fun nextInt(): Int = generator.nextInt()
|
||||
override fun nextInt(n: Int): Int = generator.nextInt(n)
|
||||
|
||||
@PerformancePitfall
|
||||
public override fun nextGaussian(): Double = runBlocking { GaussianSampler(0.0, 1.0).next(generator) }
|
||||
override fun nextGaussian(): Double = runBlocking { GaussianSampler(0.0, 1.0).next(generator) }
|
||||
|
||||
public override fun nextDouble(): Double = generator.nextDouble()
|
||||
public override fun nextLong(): Long = generator.nextLong()
|
||||
override fun nextDouble(): Double = generator.nextDouble()
|
||||
override fun nextLong(): Long = generator.nextLong()
|
||||
}
|
||||
|
@ -54,8 +54,8 @@ private val PI_DIV_2 = Complex(PI / 2, 0)
|
||||
@OptIn(UnstableKMathAPI::class)
|
||||
public object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex>, NumbersAddOperations<Complex>,
|
||||
ScaleOperations<Complex> {
|
||||
public override val zero: Complex = 0.0.toComplex()
|
||||
public override val one: Complex = 1.0.toComplex()
|
||||
override val zero: Complex = 0.0.toComplex()
|
||||
override val one: Complex = 1.0.toComplex()
|
||||
|
||||
/**
|
||||
* The imaginary unit.
|
||||
@ -68,13 +68,13 @@ public object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex>, Num
|
||||
|
||||
override fun scale(a: Complex, value: Double): Complex = Complex(a.re * value, a.im * value)
|
||||
|
||||
public override fun add(a: Complex, b: Complex): Complex = Complex(a.re + b.re, a.im + b.im)
|
||||
// public override fun multiply(a: Complex, k: Number): Complex = Complex(a.re * k.toDouble(), a.im * k.toDouble())
|
||||
override fun add(a: Complex, b: Complex): Complex = Complex(a.re + b.re, a.im + b.im)
|
||||
// override fun multiply(a: Complex, k: Number): Complex = Complex(a.re * k.toDouble(), a.im * k.toDouble())
|
||||
|
||||
public override fun multiply(a: Complex, b: Complex): Complex =
|
||||
override fun multiply(a: Complex, b: Complex): Complex =
|
||||
Complex(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re)
|
||||
|
||||
public override fun divide(a: Complex, b: Complex): Complex = when {
|
||||
override fun divide(a: Complex, b: Complex): Complex = when {
|
||||
abs(b.im) < abs(b.re) -> {
|
||||
val wr = b.im / b.re
|
||||
val wd = b.re + wr * b.im
|
||||
@ -100,31 +100,31 @@ public object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex>, Num
|
||||
|
||||
override operator fun Complex.div(k: Number): Complex = Complex(re / k.toDouble(), im / k.toDouble())
|
||||
|
||||
public override fun sin(arg: Complex): Complex = i * (exp(-i * arg) - exp(i * arg)) / 2.0
|
||||
public override fun cos(arg: Complex): Complex = (exp(-i * arg) + exp(i * arg)) / 2.0
|
||||
override fun sin(arg: Complex): Complex = i * (exp(-i * arg) - exp(i * arg)) / 2.0
|
||||
override fun cos(arg: Complex): Complex = (exp(-i * arg) + exp(i * arg)) / 2.0
|
||||
|
||||
public override fun tan(arg: Complex): Complex {
|
||||
override fun tan(arg: Complex): Complex {
|
||||
val e1 = exp(-i * arg)
|
||||
val e2 = exp(i * arg)
|
||||
return i * (e1 - e2) / (e1 + e2)
|
||||
}
|
||||
|
||||
public override fun asin(arg: Complex): Complex = -i * ln(sqrt(1 - (arg * arg)) + i * arg)
|
||||
public override fun acos(arg: Complex): Complex = PI_DIV_2 + i * ln(sqrt(1 - (arg * arg)) + i * arg)
|
||||
override fun asin(arg: Complex): Complex = -i * ln(sqrt(1 - (arg * arg)) + i * arg)
|
||||
override fun acos(arg: Complex): Complex = PI_DIV_2 + i * ln(sqrt(1 - (arg * arg)) + i * arg)
|
||||
|
||||
public override fun atan(arg: Complex): Complex {
|
||||
override fun atan(arg: Complex): Complex {
|
||||
val iArg = i * arg
|
||||
return i * (ln(1 - iArg) - ln(1 + iArg)) / 2
|
||||
}
|
||||
|
||||
public override fun power(arg: Complex, pow: Number): Complex = if (arg.im == 0.0)
|
||||
override fun power(arg: Complex, pow: Number): Complex = if (arg.im == 0.0)
|
||||
arg.re.pow(pow.toDouble()).toComplex()
|
||||
else
|
||||
exp(pow * ln(arg))
|
||||
|
||||
public override fun exp(arg: Complex): Complex = exp(arg.re) * (cos(arg.im) + i * sin(arg.im))
|
||||
override fun exp(arg: Complex): Complex = exp(arg.re) * (cos(arg.im) + i * sin(arg.im))
|
||||
|
||||
public override fun ln(arg: Complex): Complex = ln(arg.r) + i * atan2(arg.im, arg.re)
|
||||
override fun ln(arg: Complex): Complex = ln(arg.r) + i * atan2(arg.im, arg.re)
|
||||
|
||||
/**
|
||||
* Adds complex number to real one.
|
||||
@ -171,9 +171,9 @@ public object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex>, Num
|
||||
*/
|
||||
public operator fun Double.times(c: Complex): Complex = Complex(c.re * this, c.im * this)
|
||||
|
||||
public override fun norm(arg: Complex): Complex = sqrt(arg.conjugate * arg)
|
||||
override fun norm(arg: Complex): Complex = sqrt(arg.conjugate * arg)
|
||||
|
||||
public override fun bindSymbolOrNull(value: String): Complex? = if (value == "i") i else null
|
||||
override fun bindSymbolOrNull(value: String): Complex? = if (value == "i") i else null
|
||||
}
|
||||
|
||||
/**
|
||||
@ -187,16 +187,16 @@ public data class Complex(val re: Double, val im: Double) {
|
||||
public constructor(re: Number, im: Number) : this(re.toDouble(), im.toDouble())
|
||||
public constructor(re: Number) : this(re.toDouble(), 0.0)
|
||||
|
||||
public override fun toString(): String = "($re + i * $im)"
|
||||
override fun toString(): String = "($re + i * $im)"
|
||||
|
||||
public companion object : MemorySpec<Complex> {
|
||||
public override val objectSize: Int
|
||||
override val objectSize: Int
|
||||
get() = 16
|
||||
|
||||
public override fun MemoryReader.read(offset: Int): Complex =
|
||||
override fun MemoryReader.read(offset: Int): Complex =
|
||||
Complex(readDouble(offset), readDouble(offset + 8))
|
||||
|
||||
public override fun MemoryWriter.write(offset: Int, value: Complex) {
|
||||
override fun MemoryWriter.write(offset: Int, value: Complex) {
|
||||
writeDouble(offset, value.re)
|
||||
writeDouble(offset + 8, value.im)
|
||||
}
|
||||
|
@ -27,10 +27,10 @@ public class ComplexFieldND(
|
||||
NumbersAddOperations<StructureND<Complex>>,
|
||||
ExtendedField<StructureND<Complex>> {
|
||||
|
||||
public override val zero: BufferND<Complex> by lazy { produce { zero } }
|
||||
public override val one: BufferND<Complex> by lazy { produce { one } }
|
||||
override val zero: BufferND<Complex> by lazy { produce { zero } }
|
||||
override val one: BufferND<Complex> by lazy { produce { one } }
|
||||
|
||||
public override fun number(value: Number): BufferND<Complex> {
|
||||
override fun number(value: Number): BufferND<Complex> {
|
||||
val d = value.toComplex() // minimize conversions
|
||||
return produce { d }
|
||||
}
|
||||
@ -81,25 +81,25 @@ public class ComplexFieldND(
|
||||
// return BufferedNDFieldElement(this, buffer)
|
||||
// }
|
||||
|
||||
public override fun power(arg: StructureND<Complex>, pow: Number): BufferND<Complex> = arg.map { power(it, pow) }
|
||||
override fun power(arg: StructureND<Complex>, pow: Number): BufferND<Complex> = arg.map { power(it, pow) }
|
||||
|
||||
public override fun exp(arg: StructureND<Complex>): BufferND<Complex> = arg.map { exp(it) }
|
||||
override fun exp(arg: StructureND<Complex>): BufferND<Complex> = arg.map { exp(it) }
|
||||
|
||||
public override fun ln(arg: StructureND<Complex>): BufferND<Complex> = arg.map { ln(it) }
|
||||
override fun ln(arg: StructureND<Complex>): BufferND<Complex> = arg.map { ln(it) }
|
||||
|
||||
public override fun sin(arg: StructureND<Complex>): BufferND<Complex> = arg.map { sin(it) }
|
||||
public override fun cos(arg: StructureND<Complex>): BufferND<Complex> = arg.map { cos(it) }
|
||||
public override fun tan(arg: StructureND<Complex>): BufferND<Complex> = arg.map { tan(it) }
|
||||
public override fun asin(arg: StructureND<Complex>): BufferND<Complex> = arg.map { asin(it) }
|
||||
public override fun acos(arg: StructureND<Complex>): BufferND<Complex> = arg.map { acos(it) }
|
||||
public override fun atan(arg: StructureND<Complex>): BufferND<Complex> = arg.map { atan(it) }
|
||||
override fun sin(arg: StructureND<Complex>): BufferND<Complex> = arg.map { sin(it) }
|
||||
override fun cos(arg: StructureND<Complex>): BufferND<Complex> = arg.map { cos(it) }
|
||||
override fun tan(arg: StructureND<Complex>): BufferND<Complex> = arg.map { tan(it) }
|
||||
override fun asin(arg: StructureND<Complex>): BufferND<Complex> = arg.map { asin(it) }
|
||||
override fun acos(arg: StructureND<Complex>): BufferND<Complex> = arg.map { acos(it) }
|
||||
override fun atan(arg: StructureND<Complex>): BufferND<Complex> = arg.map { atan(it) }
|
||||
|
||||
public override fun sinh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { sinh(it) }
|
||||
public override fun cosh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { cosh(it) }
|
||||
public override fun tanh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { tanh(it) }
|
||||
public override fun asinh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { asinh(it) }
|
||||
public override fun acosh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { acosh(it) }
|
||||
public override fun atanh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { atanh(it) }
|
||||
override fun sinh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { sinh(it) }
|
||||
override fun cosh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { cosh(it) }
|
||||
override fun tanh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { tanh(it) }
|
||||
override fun asinh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { asinh(it) }
|
||||
override fun acosh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { acosh(it) }
|
||||
override fun atanh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { atanh(it) }
|
||||
}
|
||||
|
||||
|
||||
|
@ -63,20 +63,20 @@ public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>,
|
||||
*/
|
||||
public val k: Quaternion = Quaternion(0, 0, 0, 1)
|
||||
|
||||
public override fun add(a: Quaternion, b: Quaternion): Quaternion =
|
||||
override fun add(a: Quaternion, b: Quaternion): Quaternion =
|
||||
Quaternion(a.w + b.w, a.x + b.x, a.y + b.y, a.z + b.z)
|
||||
|
||||
public override fun scale(a: Quaternion, value: Double): Quaternion =
|
||||
override fun scale(a: Quaternion, value: Double): Quaternion =
|
||||
Quaternion(a.w * value, a.x * value, a.y * value, a.z * value)
|
||||
|
||||
public override fun multiply(a: Quaternion, b: Quaternion): Quaternion = Quaternion(
|
||||
override fun multiply(a: Quaternion, b: Quaternion): Quaternion = Quaternion(
|
||||
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
|
||||
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
|
||||
a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
|
||||
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
|
||||
)
|
||||
|
||||
public override fun divide(a: Quaternion, b: Quaternion): Quaternion {
|
||||
override fun divide(a: Quaternion, b: Quaternion): Quaternion {
|
||||
val s = b.w * b.w + b.x * b.x + b.y * b.y + b.z * b.z
|
||||
|
||||
return Quaternion(
|
||||
@ -87,7 +87,7 @@ public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>,
|
||||
)
|
||||
}
|
||||
|
||||
public override fun power(arg: Quaternion, pow: Number): Quaternion {
|
||||
override fun power(arg: Quaternion, pow: Number): Quaternion {
|
||||
if (pow is Int) return pwr(arg, pow)
|
||||
if (floor(pow.toDouble()) == pow.toDouble()) return pwr(arg, pow.toInt())
|
||||
return exp(pow * ln(arg))
|
||||
@ -131,7 +131,7 @@ public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>,
|
||||
return Quaternion(a2 * a2 - 6 * a2 * n1 + n1 * n1, x.x * n2, x.y * n2, x.z * n2)
|
||||
}
|
||||
|
||||
public override fun exp(arg: Quaternion): Quaternion {
|
||||
override fun exp(arg: Quaternion): Quaternion {
|
||||
val un = arg.x * arg.x + arg.y * arg.y + arg.z * arg.z
|
||||
if (un == 0.0) return exp(arg.w).toQuaternion()
|
||||
val n1 = sqrt(un)
|
||||
@ -140,7 +140,7 @@ public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>,
|
||||
return Quaternion(ea * cos(n1), n2 * arg.x, n2 * arg.y, n2 * arg.z)
|
||||
}
|
||||
|
||||
public override fun ln(arg: Quaternion): Quaternion {
|
||||
override fun ln(arg: Quaternion): Quaternion {
|
||||
val nu2 = arg.x * arg.x + arg.y * arg.y + arg.z * arg.z
|
||||
|
||||
if (nu2 == 0.0)
|
||||
@ -158,21 +158,21 @@ public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>,
|
||||
return Quaternion(ln(n), th * arg.x, th * arg.y, th * arg.z)
|
||||
}
|
||||
|
||||
public override operator fun Number.plus(b: Quaternion): Quaternion = Quaternion(toDouble() + b.w, b.x, b.y, b.z)
|
||||
override operator fun Number.plus(b: Quaternion): Quaternion = Quaternion(toDouble() + b.w, b.x, b.y, b.z)
|
||||
|
||||
public override operator fun Number.minus(b: Quaternion): Quaternion =
|
||||
override operator fun Number.minus(b: Quaternion): Quaternion =
|
||||
Quaternion(toDouble() - b.w, -b.x, -b.y, -b.z)
|
||||
|
||||
public override operator fun Quaternion.plus(b: Number): Quaternion = Quaternion(w + b.toDouble(), x, y, z)
|
||||
public override operator fun Quaternion.minus(b: Number): Quaternion = Quaternion(w - b.toDouble(), x, y, z)
|
||||
override operator fun Quaternion.plus(b: Number): Quaternion = Quaternion(w + b.toDouble(), x, y, z)
|
||||
override operator fun Quaternion.minus(b: Number): Quaternion = Quaternion(w - b.toDouble(), x, y, z)
|
||||
|
||||
public override operator fun Number.times(b: Quaternion): Quaternion =
|
||||
override operator fun Number.times(b: Quaternion): Quaternion =
|
||||
Quaternion(toDouble() * b.w, toDouble() * b.x, toDouble() * b.y, toDouble() * b.z)
|
||||
|
||||
public override fun Quaternion.unaryMinus(): Quaternion = Quaternion(-w, -x, -y, -z)
|
||||
public override fun norm(arg: Quaternion): Quaternion = sqrt(arg.conjugate * arg)
|
||||
override fun Quaternion.unaryMinus(): Quaternion = Quaternion(-w, -x, -y, -z)
|
||||
override fun norm(arg: Quaternion): Quaternion = sqrt(arg.conjugate * arg)
|
||||
|
||||
public override fun bindSymbolOrNull(value: String): Quaternion? = when (value) {
|
||||
override fun bindSymbolOrNull(value: String): Quaternion? = when (value) {
|
||||
"i" -> i
|
||||
"j" -> j
|
||||
"k" -> k
|
||||
@ -181,12 +181,12 @@ public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>,
|
||||
|
||||
override fun number(value: Number): Quaternion = value.toQuaternion()
|
||||
|
||||
public override fun sinh(arg: Quaternion): Quaternion = (exp(arg) - exp(-arg)) / 2.0
|
||||
public override fun cosh(arg: Quaternion): Quaternion = (exp(arg) + exp(-arg)) / 2.0
|
||||
public override fun tanh(arg: Quaternion): Quaternion = (exp(arg) - exp(-arg)) / (exp(-arg) + exp(arg))
|
||||
public override fun asinh(arg: Quaternion): Quaternion = ln(sqrt(arg * arg + one) + arg)
|
||||
public override fun acosh(arg: Quaternion): Quaternion = ln(arg + sqrt((arg - one) * (arg + one)))
|
||||
public override fun atanh(arg: Quaternion): Quaternion = (ln(arg + one) - ln(one - arg)) / 2.0
|
||||
override fun sinh(arg: Quaternion): Quaternion = (exp(arg) - exp(-arg)) / 2.0
|
||||
override fun cosh(arg: Quaternion): Quaternion = (exp(arg) + exp(-arg)) / 2.0
|
||||
override fun tanh(arg: Quaternion): Quaternion = (exp(arg) - exp(-arg)) / (exp(-arg) + exp(arg))
|
||||
override fun asinh(arg: Quaternion): Quaternion = ln(sqrt(arg * arg + one) + arg)
|
||||
override fun acosh(arg: Quaternion): Quaternion = ln(arg + sqrt((arg - one) * (arg + one)))
|
||||
override fun atanh(arg: Quaternion): Quaternion = (ln(arg + one) - ln(one - arg)) / 2.0
|
||||
}
|
||||
|
||||
/**
|
||||
@ -224,16 +224,16 @@ public data class Quaternion(
|
||||
/**
|
||||
* Returns a string representation of this quaternion.
|
||||
*/
|
||||
public override fun toString(): String = "($w + $x * i + $y * j + $z * k)"
|
||||
override fun toString(): String = "($w + $x * i + $y * j + $z * k)"
|
||||
|
||||
public companion object : MemorySpec<Quaternion> {
|
||||
public override val objectSize: Int
|
||||
override val objectSize: Int
|
||||
get() = 32
|
||||
|
||||
public override fun MemoryReader.read(offset: Int): Quaternion =
|
||||
override fun MemoryReader.read(offset: Int): Quaternion =
|
||||
Quaternion(readDouble(offset), readDouble(offset + 8), readDouble(offset + 16), readDouble(offset + 24))
|
||||
|
||||
public override fun MemoryWriter.write(offset: Int, value: Quaternion) {
|
||||
override fun MemoryWriter.write(offset: Int, value: Quaternion) {
|
||||
writeDouble(offset, value.w)
|
||||
writeDouble(offset + 8, value.x)
|
||||
writeDouble(offset + 16, value.y)
|
||||
|
@ -17,17 +17,17 @@ import space.kscience.kmath.structures.indices
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public class HyperSquareDomain(private val lower: Buffer<Double>, private val upper: Buffer<Double>) : DoubleDomain {
|
||||
public override val dimension: Int get() = lower.size
|
||||
override val dimension: Int get() = lower.size
|
||||
|
||||
public override operator fun contains(point: Point<Double>): Boolean = point.indices.all { i ->
|
||||
override operator fun contains(point: Point<Double>): Boolean = point.indices.all { i ->
|
||||
point[i] in lower[i]..upper[i]
|
||||
}
|
||||
|
||||
public override fun getLowerBound(num: Int): Double = lower[num]
|
||||
override fun getLowerBound(num: Int): Double = lower[num]
|
||||
|
||||
public override fun getUpperBound(num: Int): Double = upper[num]
|
||||
override fun getUpperBound(num: Int): Double = upper[num]
|
||||
|
||||
public override fun volume(): Double {
|
||||
override fun volume(): Double {
|
||||
var res = 1.0
|
||||
|
||||
for (i in 0 until dimension) {
|
||||
|
@ -8,12 +8,12 @@ import space.kscience.kmath.linear.Point
|
||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
||||
|
||||
@UnstableKMathAPI
|
||||
public class UnconstrainedDomain(public override val dimension: Int) : DoubleDomain {
|
||||
public override operator fun contains(point: Point<Double>): Boolean = true
|
||||
public class UnconstrainedDomain(override val dimension: Int) : DoubleDomain {
|
||||
override operator fun contains(point: Point<Double>): Boolean = true
|
||||
|
||||
public override fun getLowerBound(num: Int): Double = Double.NEGATIVE_INFINITY
|
||||
override fun getLowerBound(num: Int): Double = Double.NEGATIVE_INFINITY
|
||||
|
||||
public override fun getUpperBound(num: Int): Double = Double.POSITIVE_INFINITY
|
||||
override fun getUpperBound(num: Int): Double = Double.POSITIVE_INFINITY
|
||||
|
||||
public override fun volume(): Double = Double.POSITIVE_INFINITY
|
||||
override fun volume(): Double = Double.POSITIVE_INFINITY
|
||||
}
|
||||
|
@ -10,24 +10,24 @@ import space.kscience.kmath.misc.UnstableKMathAPI
|
||||
|
||||
@UnstableKMathAPI
|
||||
public class UnivariateDomain(public val range: ClosedFloatingPointRange<Double>) : DoubleDomain {
|
||||
public override val dimension: Int get() = 1
|
||||
override val dimension: Int get() = 1
|
||||
|
||||
public operator fun contains(d: Double): Boolean = range.contains(d)
|
||||
|
||||
public override operator fun contains(point: Point<Double>): Boolean {
|
||||
override operator fun contains(point: Point<Double>): Boolean {
|
||||
require(point.size == 0)
|
||||
return contains(point[0])
|
||||
}
|
||||
|
||||
public override fun getLowerBound(num: Int): Double {
|
||||
override fun getLowerBound(num: Int): Double {
|
||||
require(num == 0)
|
||||
return range.start
|
||||
}
|
||||
|
||||
public override fun getUpperBound(num: Int): Double {
|
||||
override fun getUpperBound(num: Int): Double {
|
||||
require(num == 0)
|
||||
return range.endInclusive
|
||||
}
|
||||
|
||||
public override fun volume(): Double = range.endInclusive - range.start
|
||||
override fun volume(): Double = range.endInclusive - range.start
|
||||
}
|
||||
|
@ -20,25 +20,25 @@ public abstract class FunctionalExpressionAlgebra<T, out A : Algebra<T>>(
|
||||
/**
|
||||
* Builds an Expression of constant expression which does not depend on arguments.
|
||||
*/
|
||||
public override fun const(value: T): Expression<T> = Expression { value }
|
||||
override fun const(value: T): Expression<T> = Expression { value }
|
||||
|
||||
/**
|
||||
* Builds an Expression to access a variable.
|
||||
*/
|
||||
public override fun bindSymbolOrNull(value: String): Expression<T>? = Expression { arguments ->
|
||||
override fun bindSymbolOrNull(value: String): Expression<T>? = Expression { arguments ->
|
||||
algebra.bindSymbolOrNull(value)
|
||||
?: arguments[StringSymbol(value)]
|
||||
?: error("Symbol '$value' is not supported in $this")
|
||||
}
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: Expression<T>, right: Expression<T>) -> Expression<T> =
|
||||
override fun binaryOperationFunction(operation: String): (left: Expression<T>, right: Expression<T>) -> Expression<T> =
|
||||
{ left, right ->
|
||||
Expression { arguments ->
|
||||
algebra.binaryOperationFunction(operation)(left.invoke(arguments), right.invoke(arguments))
|
||||
}
|
||||
}
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: Expression<T>) -> Expression<T> = { arg ->
|
||||
override fun unaryOperationFunction(operation: String): (arg: Expression<T>) -> Expression<T> = { arg ->
|
||||
Expression { arguments -> algebra.unaryOperationFunction(operation)(arg.invoke(arguments)) }
|
||||
}
|
||||
}
|
||||
@ -49,21 +49,21 @@ public abstract class FunctionalExpressionAlgebra<T, out A : Algebra<T>>(
|
||||
public open class FunctionalExpressionGroup<T, out A : Group<T>>(
|
||||
algebra: A,
|
||||
) : FunctionalExpressionAlgebra<T, A>(algebra), Group<Expression<T>> {
|
||||
public override val zero: Expression<T> get() = const(algebra.zero)
|
||||
override val zero: Expression<T> get() = const(algebra.zero)
|
||||
|
||||
public override fun Expression<T>.unaryMinus(): Expression<T> =
|
||||
override fun Expression<T>.unaryMinus(): Expression<T> =
|
||||
unaryOperation(GroupOperations.MINUS_OPERATION, this)
|
||||
|
||||
/**
|
||||
* Builds an Expression of addition of two another expressions.
|
||||
*/
|
||||
public override fun add(a: Expression<T>, b: Expression<T>): Expression<T> =
|
||||
override fun add(a: Expression<T>, b: Expression<T>): Expression<T> =
|
||||
binaryOperation(GroupOperations.PLUS_OPERATION, a, b)
|
||||
|
||||
// /**
|
||||
// * Builds an Expression of multiplication of expression by number.
|
||||
// */
|
||||
// public override fun multiply(a: Expression<T>, k: Number): Expression<T> = Expression { arguments ->
|
||||
// override fun multiply(a: Expression<T>, k: Number): Expression<T> = Expression { arguments ->
|
||||
// algebra.multiply(a.invoke(arguments), k)
|
||||
// }
|
||||
|
||||
@ -72,10 +72,10 @@ public open class FunctionalExpressionGroup<T, out A : Group<T>>(
|
||||
public operator fun T.plus(arg: Expression<T>): Expression<T> = arg + this
|
||||
public operator fun T.minus(arg: Expression<T>): Expression<T> = arg - this
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: Expression<T>) -> Expression<T> =
|
||||
override fun unaryOperationFunction(operation: String): (arg: Expression<T>) -> Expression<T> =
|
||||
super<FunctionalExpressionAlgebra>.unaryOperationFunction(operation)
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: Expression<T>, right: Expression<T>) -> Expression<T> =
|
||||
override fun binaryOperationFunction(operation: String): (left: Expression<T>, right: Expression<T>) -> Expression<T> =
|
||||
super<FunctionalExpressionAlgebra>.binaryOperationFunction(operation)
|
||||
|
||||
}
|
||||
@ -83,21 +83,21 @@ public open class FunctionalExpressionGroup<T, out A : Group<T>>(
|
||||
public open class FunctionalExpressionRing<T, out A : Ring<T>>(
|
||||
algebra: A,
|
||||
) : FunctionalExpressionGroup<T, A>(algebra), Ring<Expression<T>> {
|
||||
public override val one: Expression<T> get() = const(algebra.one)
|
||||
override val one: Expression<T> get() = const(algebra.one)
|
||||
|
||||
/**
|
||||
* Builds an Expression of multiplication of two expressions.
|
||||
*/
|
||||
public override fun multiply(a: Expression<T>, b: Expression<T>): Expression<T> =
|
||||
override fun multiply(a: Expression<T>, b: Expression<T>): Expression<T> =
|
||||
binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, b)
|
||||
|
||||
public operator fun Expression<T>.times(arg: T): Expression<T> = this * const(arg)
|
||||
public operator fun T.times(arg: Expression<T>): Expression<T> = arg * this
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: Expression<T>) -> Expression<T> =
|
||||
override fun unaryOperationFunction(operation: String): (arg: Expression<T>) -> Expression<T> =
|
||||
super<FunctionalExpressionGroup>.unaryOperationFunction(operation)
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: Expression<T>, right: Expression<T>) -> Expression<T> =
|
||||
override fun binaryOperationFunction(operation: String): (left: Expression<T>, right: Expression<T>) -> Expression<T> =
|
||||
super<FunctionalExpressionGroup>.binaryOperationFunction(operation)
|
||||
}
|
||||
|
||||
@ -107,65 +107,65 @@ public open class FunctionalExpressionField<T, out A : Field<T>>(
|
||||
/**
|
||||
* Builds an Expression of division an expression by another one.
|
||||
*/
|
||||
public override fun divide(a: Expression<T>, b: Expression<T>): Expression<T> =
|
||||
override fun divide(a: Expression<T>, b: Expression<T>): Expression<T> =
|
||||
binaryOperationFunction(FieldOperations.DIV_OPERATION)(a, b)
|
||||
|
||||
public operator fun Expression<T>.div(arg: T): Expression<T> = this / const(arg)
|
||||
public operator fun T.div(arg: Expression<T>): Expression<T> = arg / this
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: Expression<T>) -> Expression<T> =
|
||||
override fun unaryOperationFunction(operation: String): (arg: Expression<T>) -> Expression<T> =
|
||||
super<FunctionalExpressionRing>.unaryOperationFunction(operation)
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: Expression<T>, right: Expression<T>) -> Expression<T> =
|
||||
override fun binaryOperationFunction(operation: String): (left: Expression<T>, right: Expression<T>) -> Expression<T> =
|
||||
super<FunctionalExpressionRing>.binaryOperationFunction(operation)
|
||||
|
||||
public override fun scale(a: Expression<T>, value: Double): Expression<T> = algebra {
|
||||
override fun scale(a: Expression<T>, value: Double): Expression<T> = algebra {
|
||||
Expression { args -> a(args) * value }
|
||||
}
|
||||
|
||||
public override fun bindSymbolOrNull(value: String): Expression<T>? =
|
||||
override fun bindSymbolOrNull(value: String): Expression<T>? =
|
||||
super<FunctionalExpressionRing>.bindSymbolOrNull(value)
|
||||
}
|
||||
|
||||
public open class FunctionalExpressionExtendedField<T, out A : ExtendedField<T>>(
|
||||
algebra: A,
|
||||
) : FunctionalExpressionField<T, A>(algebra), ExtendedField<Expression<T>> {
|
||||
public override fun number(value: Number): Expression<T> = const(algebra.number(value))
|
||||
override fun number(value: Number): Expression<T> = const(algebra.number(value))
|
||||
|
||||
public override fun sqrt(arg: Expression<T>): Expression<T> =
|
||||
override fun sqrt(arg: Expression<T>): Expression<T> =
|
||||
unaryOperationFunction(PowerOperations.SQRT_OPERATION)(arg)
|
||||
|
||||
public override fun sin(arg: Expression<T>): Expression<T> =
|
||||
override fun sin(arg: Expression<T>): Expression<T> =
|
||||
unaryOperationFunction(TrigonometricOperations.SIN_OPERATION)(arg)
|
||||
|
||||
public override fun cos(arg: Expression<T>): Expression<T> =
|
||||
override fun cos(arg: Expression<T>): Expression<T> =
|
||||
unaryOperationFunction(TrigonometricOperations.COS_OPERATION)(arg)
|
||||
|
||||
public override fun asin(arg: Expression<T>): Expression<T> =
|
||||
override fun asin(arg: Expression<T>): Expression<T> =
|
||||
unaryOperationFunction(TrigonometricOperations.ASIN_OPERATION)(arg)
|
||||
|
||||
public override fun acos(arg: Expression<T>): Expression<T> =
|
||||
override fun acos(arg: Expression<T>): Expression<T> =
|
||||
unaryOperationFunction(TrigonometricOperations.ACOS_OPERATION)(arg)
|
||||
|
||||
public override fun atan(arg: Expression<T>): Expression<T> =
|
||||
override fun atan(arg: Expression<T>): Expression<T> =
|
||||
unaryOperationFunction(TrigonometricOperations.ATAN_OPERATION)(arg)
|
||||
|
||||
public override fun power(arg: Expression<T>, pow: Number): Expression<T> =
|
||||
override fun power(arg: Expression<T>, pow: Number): Expression<T> =
|
||||
binaryOperationFunction(PowerOperations.POW_OPERATION)(arg, number(pow))
|
||||
|
||||
public override fun exp(arg: Expression<T>): Expression<T> =
|
||||
override fun exp(arg: Expression<T>): Expression<T> =
|
||||
unaryOperationFunction(ExponentialOperations.EXP_OPERATION)(arg)
|
||||
|
||||
public override fun ln(arg: Expression<T>): Expression<T> =
|
||||
override fun ln(arg: Expression<T>): Expression<T> =
|
||||
unaryOperationFunction(ExponentialOperations.LN_OPERATION)(arg)
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: Expression<T>) -> Expression<T> =
|
||||
override fun unaryOperationFunction(operation: String): (arg: Expression<T>) -> Expression<T> =
|
||||
super<FunctionalExpressionField>.unaryOperationFunction(operation)
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: Expression<T>, right: Expression<T>) -> Expression<T> =
|
||||
override fun binaryOperationFunction(operation: String): (left: Expression<T>, right: Expression<T>) -> Expression<T> =
|
||||
super<FunctionalExpressionField>.binaryOperationFunction(operation)
|
||||
|
||||
public override fun bindSymbol(value: String): Expression<T> = super<FunctionalExpressionField>.bindSymbol(value)
|
||||
override fun bindSymbol(value: String): Expression<T> = super<FunctionalExpressionField>.bindSymbol(value)
|
||||
}
|
||||
|
||||
public inline fun <T, A : Group<T>> A.expressionInGroup(
|
||||
|
@ -12,14 +12,14 @@ import space.kscience.kmath.operations.*
|
||||
* [Algebra] over [MST] nodes.
|
||||
*/
|
||||
public object MstNumericAlgebra : NumericAlgebra<MST> {
|
||||
public override fun number(value: Number): MST.Numeric = MST.Numeric(value)
|
||||
public override fun bindSymbolOrNull(value: String): Symbol = StringSymbol(value)
|
||||
override fun number(value: Number): MST.Numeric = MST.Numeric(value)
|
||||
override fun bindSymbolOrNull(value: String): Symbol = StringSymbol(value)
|
||||
override fun bindSymbol(value: String): Symbol = bindSymbolOrNull(value)
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
|
||||
override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
|
||||
{ arg -> MST.Unary(operation, arg) }
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
|
||||
override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
|
||||
{ left, right -> MST.Binary(operation, left, right) }
|
||||
}
|
||||
|
||||
@ -27,27 +27,27 @@ public object MstNumericAlgebra : NumericAlgebra<MST> {
|
||||
* [Group] over [MST] nodes.
|
||||
*/
|
||||
public object MstGroup : Group<MST>, NumericAlgebra<MST>, ScaleOperations<MST> {
|
||||
public override val zero: MST.Numeric = number(0.0)
|
||||
override val zero: MST.Numeric = number(0.0)
|
||||
|
||||
public override fun number(value: Number): MST.Numeric = MstNumericAlgebra.number(value)
|
||||
public override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value)
|
||||
public override fun add(a: MST, b: MST): MST.Binary = binaryOperationFunction(GroupOperations.PLUS_OPERATION)(a, b)
|
||||
public override operator fun MST.unaryPlus(): MST.Unary =
|
||||
override fun number(value: Number): MST.Numeric = MstNumericAlgebra.number(value)
|
||||
override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value)
|
||||
override fun add(a: MST, b: MST): MST.Binary = binaryOperationFunction(GroupOperations.PLUS_OPERATION)(a, b)
|
||||
override operator fun MST.unaryPlus(): MST.Unary =
|
||||
unaryOperationFunction(GroupOperations.PLUS_OPERATION)(this)
|
||||
|
||||
public override operator fun MST.unaryMinus(): MST.Unary =
|
||||
override operator fun MST.unaryMinus(): MST.Unary =
|
||||
unaryOperationFunction(GroupOperations.MINUS_OPERATION)(this)
|
||||
|
||||
public override operator fun MST.minus(b: MST): MST.Binary =
|
||||
override operator fun MST.minus(b: MST): MST.Binary =
|
||||
binaryOperationFunction(GroupOperations.MINUS_OPERATION)(this, b)
|
||||
|
||||
public override fun scale(a: MST, value: Double): MST.Binary =
|
||||
override fun scale(a: MST, value: Double): MST.Binary =
|
||||
binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, number(value))
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
|
||||
override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
|
||||
MstNumericAlgebra.binaryOperationFunction(operation)
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
|
||||
override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
|
||||
MstNumericAlgebra.unaryOperationFunction(operation)
|
||||
}
|
||||
|
||||
@ -57,27 +57,27 @@ public object MstGroup : Group<MST>, NumericAlgebra<MST>, ScaleOperations<MST> {
|
||||
@Suppress("OVERRIDE_BY_INLINE")
|
||||
@OptIn(UnstableKMathAPI::class)
|
||||
public object MstRing : Ring<MST>, NumbersAddOperations<MST>, ScaleOperations<MST> {
|
||||
public override inline val zero: MST.Numeric get() = MstGroup.zero
|
||||
public override val one: MST.Numeric = number(1.0)
|
||||
override inline val zero: MST.Numeric get() = MstGroup.zero
|
||||
override val one: MST.Numeric = number(1.0)
|
||||
|
||||
public override fun number(value: Number): MST.Numeric = MstGroup.number(value)
|
||||
public override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value)
|
||||
public override fun add(a: MST, b: MST): MST.Binary = MstGroup.add(a, b)
|
||||
override fun number(value: Number): MST.Numeric = MstGroup.number(value)
|
||||
override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value)
|
||||
override fun add(a: MST, b: MST): MST.Binary = MstGroup.add(a, b)
|
||||
|
||||
public override fun scale(a: MST, value: Double): MST.Binary =
|
||||
override fun scale(a: MST, value: Double): MST.Binary =
|
||||
MstGroup.binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, MstGroup.number(value))
|
||||
|
||||
public override fun multiply(a: MST, b: MST): MST.Binary =
|
||||
override fun multiply(a: MST, b: MST): MST.Binary =
|
||||
binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, b)
|
||||
|
||||
public override operator fun MST.unaryPlus(): MST.Unary = MstGroup { +this@unaryPlus }
|
||||
public override operator fun MST.unaryMinus(): MST.Unary = MstGroup { -this@unaryMinus }
|
||||
public override operator fun MST.minus(b: MST): MST.Binary = MstGroup { this@minus - b }
|
||||
override operator fun MST.unaryPlus(): MST.Unary = MstGroup { +this@unaryPlus }
|
||||
override operator fun MST.unaryMinus(): MST.Unary = MstGroup { -this@unaryMinus }
|
||||
override operator fun MST.minus(b: MST): MST.Binary = MstGroup { this@minus - b }
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
|
||||
override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
|
||||
MstGroup.binaryOperationFunction(operation)
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
|
||||
override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
|
||||
MstNumericAlgebra.unaryOperationFunction(operation)
|
||||
}
|
||||
|
||||
@ -87,28 +87,28 @@ public object MstRing : Ring<MST>, NumbersAddOperations<MST>, ScaleOperations<MS
|
||||
@Suppress("OVERRIDE_BY_INLINE")
|
||||
@OptIn(UnstableKMathAPI::class)
|
||||
public object MstField : Field<MST>, NumbersAddOperations<MST>, ScaleOperations<MST> {
|
||||
public override inline val zero: MST.Numeric get() = MstRing.zero
|
||||
public override inline val one: MST.Numeric get() = MstRing.one
|
||||
override inline val zero: MST.Numeric get() = MstRing.zero
|
||||
override inline val one: MST.Numeric get() = MstRing.one
|
||||
|
||||
public override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value)
|
||||
public override fun number(value: Number): MST.Numeric = MstRing.number(value)
|
||||
public override fun add(a: MST, b: MST): MST.Binary = MstRing.add(a, b)
|
||||
override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value)
|
||||
override fun number(value: Number): MST.Numeric = MstRing.number(value)
|
||||
override fun add(a: MST, b: MST): MST.Binary = MstRing.add(a, b)
|
||||
|
||||
public override fun scale(a: MST, value: Double): MST.Binary =
|
||||
override fun scale(a: MST, value: Double): MST.Binary =
|
||||
MstGroup.binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, MstGroup.number(value))
|
||||
|
||||
public override fun multiply(a: MST, b: MST): MST.Binary = MstRing.multiply(a, b)
|
||||
public override fun divide(a: MST, b: MST): MST.Binary =
|
||||
override fun multiply(a: MST, b: MST): MST.Binary = MstRing.multiply(a, b)
|
||||
override fun divide(a: MST, b: MST): MST.Binary =
|
||||
binaryOperationFunction(FieldOperations.DIV_OPERATION)(a, b)
|
||||
|
||||
public override operator fun MST.unaryPlus(): MST.Unary = MstRing { +this@unaryPlus }
|
||||
public override operator fun MST.unaryMinus(): MST.Unary = MstRing { -this@unaryMinus }
|
||||
public override operator fun MST.minus(b: MST): MST.Binary = MstRing { this@minus - b }
|
||||
override operator fun MST.unaryPlus(): MST.Unary = MstRing { +this@unaryPlus }
|
||||
override operator fun MST.unaryMinus(): MST.Unary = MstRing { -this@unaryMinus }
|
||||
override operator fun MST.minus(b: MST): MST.Binary = MstRing { this@minus - b }
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
|
||||
override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
|
||||
MstRing.binaryOperationFunction(operation)
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
|
||||
override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
|
||||
MstRing.unaryOperationFunction(operation)
|
||||
}
|
||||
|
||||
@ -117,45 +117,45 @@ public object MstField : Field<MST>, NumbersAddOperations<MST>, ScaleOperations<
|
||||
*/
|
||||
@Suppress("OVERRIDE_BY_INLINE")
|
||||
public object MstExtendedField : ExtendedField<MST>, NumericAlgebra<MST> {
|
||||
public override inline val zero: MST.Numeric get() = MstField.zero
|
||||
public override inline val one: MST.Numeric get() = MstField.one
|
||||
override inline val zero: MST.Numeric get() = MstField.zero
|
||||
override inline val one: MST.Numeric get() = MstField.one
|
||||
|
||||
public override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value)
|
||||
public override fun number(value: Number): MST.Numeric = MstRing.number(value)
|
||||
public override fun sin(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.SIN_OPERATION)(arg)
|
||||
public override fun cos(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.COS_OPERATION)(arg)
|
||||
public override fun tan(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.TAN_OPERATION)(arg)
|
||||
public override fun asin(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.ASIN_OPERATION)(arg)
|
||||
public override fun acos(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.ACOS_OPERATION)(arg)
|
||||
public override fun atan(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.ATAN_OPERATION)(arg)
|
||||
public override fun sinh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.SINH_OPERATION)(arg)
|
||||
public override fun cosh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.COSH_OPERATION)(arg)
|
||||
public override fun tanh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.TANH_OPERATION)(arg)
|
||||
public override fun asinh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ASINH_OPERATION)(arg)
|
||||
public override fun acosh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ACOSH_OPERATION)(arg)
|
||||
public override fun atanh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ATANH_OPERATION)(arg)
|
||||
public override fun add(a: MST, b: MST): MST.Binary = MstField.add(a, b)
|
||||
public override fun sqrt(arg: MST): MST = unaryOperationFunction(PowerOperations.SQRT_OPERATION)(arg)
|
||||
override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value)
|
||||
override fun number(value: Number): MST.Numeric = MstRing.number(value)
|
||||
override fun sin(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.SIN_OPERATION)(arg)
|
||||
override fun cos(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.COS_OPERATION)(arg)
|
||||
override fun tan(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.TAN_OPERATION)(arg)
|
||||
override fun asin(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.ASIN_OPERATION)(arg)
|
||||
override fun acos(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.ACOS_OPERATION)(arg)
|
||||
override fun atan(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.ATAN_OPERATION)(arg)
|
||||
override fun sinh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.SINH_OPERATION)(arg)
|
||||
override fun cosh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.COSH_OPERATION)(arg)
|
||||
override fun tanh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.TANH_OPERATION)(arg)
|
||||
override fun asinh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ASINH_OPERATION)(arg)
|
||||
override fun acosh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ACOSH_OPERATION)(arg)
|
||||
override fun atanh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ATANH_OPERATION)(arg)
|
||||
override fun add(a: MST, b: MST): MST.Binary = MstField.add(a, b)
|
||||
override fun sqrt(arg: MST): MST = unaryOperationFunction(PowerOperations.SQRT_OPERATION)(arg)
|
||||
|
||||
public override fun scale(a: MST, value: Double): MST =
|
||||
override fun scale(a: MST, value: Double): MST =
|
||||
binaryOperation(GroupOperations.PLUS_OPERATION, a, number(value))
|
||||
|
||||
public override fun multiply(a: MST, b: MST): MST.Binary = MstField.multiply(a, b)
|
||||
public override fun divide(a: MST, b: MST): MST.Binary = MstField.divide(a, b)
|
||||
public override operator fun MST.unaryPlus(): MST.Unary = MstField { +this@unaryPlus }
|
||||
public override operator fun MST.unaryMinus(): MST.Unary = MstField { -this@unaryMinus }
|
||||
public override operator fun MST.minus(b: MST): MST.Binary = MstField { this@minus - b }
|
||||
override fun multiply(a: MST, b: MST): MST.Binary = MstField.multiply(a, b)
|
||||
override fun divide(a: MST, b: MST): MST.Binary = MstField.divide(a, b)
|
||||
override operator fun MST.unaryPlus(): MST.Unary = MstField { +this@unaryPlus }
|
||||
override operator fun MST.unaryMinus(): MST.Unary = MstField { -this@unaryMinus }
|
||||
override operator fun MST.minus(b: MST): MST.Binary = MstField { this@minus - b }
|
||||
|
||||
public override fun power(arg: MST, pow: Number): MST.Binary =
|
||||
override fun power(arg: MST, pow: Number): MST.Binary =
|
||||
binaryOperationFunction(PowerOperations.POW_OPERATION)(arg, number(pow))
|
||||
|
||||
public override fun exp(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.EXP_OPERATION)(arg)
|
||||
public override fun ln(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.LN_OPERATION)(arg)
|
||||
override fun exp(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.EXP_OPERATION)(arg)
|
||||
override fun ln(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.LN_OPERATION)(arg)
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
|
||||
override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
|
||||
MstField.binaryOperationFunction(operation)
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
|
||||
override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
|
||||
MstField.unaryOperationFunction(operation)
|
||||
}
|
||||
|
||||
@ -164,7 +164,7 @@ public object MstExtendedField : ExtendedField<MST>, NumericAlgebra<MST> {
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public object MstLogicAlgebra : LogicAlgebra<MST> {
|
||||
public override fun bindSymbolOrNull(value: String): MST = super.bindSymbolOrNull(value) ?: StringSymbol(value)
|
||||
override fun bindSymbolOrNull(value: String): MST = super.bindSymbolOrNull(value) ?: StringSymbol(value)
|
||||
|
||||
override fun const(boolean: Boolean): Symbol = if (boolean) {
|
||||
LogicAlgebra.TRUE
|
||||
|
@ -60,8 +60,8 @@ public open class SimpleAutoDiffField<T : Any, F : Field<T>>(
|
||||
public val context: F,
|
||||
bindings: Map<Symbol, T>,
|
||||
) : Field<AutoDiffValue<T>>, ExpressionAlgebra<T, AutoDiffValue<T>>, NumbersAddOperations<AutoDiffValue<T>> {
|
||||
public override val zero: AutoDiffValue<T> get() = const(context.zero)
|
||||
public override val one: AutoDiffValue<T> get() = const(context.one)
|
||||
override val zero: AutoDiffValue<T> get() = const(context.zero)
|
||||
override val one: AutoDiffValue<T> get() = const(context.one)
|
||||
|
||||
// this stack contains pairs of blocks and values to apply them to
|
||||
private var stack: Array<Any?> = arrayOfNulls<Any?>(8)
|
||||
@ -149,17 +149,17 @@ public open class SimpleAutoDiffField<T : Any, F : Field<T>>(
|
||||
|
||||
// // Overloads for Double constants
|
||||
//
|
||||
// public override operator fun Number.plus(b: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
// override operator fun Number.plus(b: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
// derive(const { this@plus.toDouble() * one + b.value }) { z ->
|
||||
// b.d += z.d
|
||||
// }
|
||||
//
|
||||
// public override operator fun AutoDiffValue<T>.plus(b: Number): AutoDiffValue<T> = b.plus(this)
|
||||
// override operator fun AutoDiffValue<T>.plus(b: Number): AutoDiffValue<T> = b.plus(this)
|
||||
//
|
||||
// public override operator fun Number.minus(b: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
// override operator fun Number.minus(b: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
// derive(const { this@minus.toDouble() * one - b.value }) { z -> b.d -= z.d }
|
||||
//
|
||||
// public override operator fun AutoDiffValue<T>.minus(b: Number): AutoDiffValue<T> =
|
||||
// override operator fun AutoDiffValue<T>.minus(b: Number): AutoDiffValue<T> =
|
||||
// derive(const { this@minus.value - one * b.toDouble() }) { z -> d += z.d }
|
||||
|
||||
|
||||
@ -168,25 +168,25 @@ public open class SimpleAutoDiffField<T : Any, F : Field<T>>(
|
||||
|
||||
// Basic math (+, -, *, /)
|
||||
|
||||
public override fun add(a: AutoDiffValue<T>, b: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun add(a: AutoDiffValue<T>, b: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
derive(const { a.value + b.value }) { z ->
|
||||
a.d += z.d
|
||||
b.d += z.d
|
||||
}
|
||||
|
||||
public override fun multiply(a: AutoDiffValue<T>, b: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun multiply(a: AutoDiffValue<T>, b: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
derive(const { a.value * b.value }) { z ->
|
||||
a.d += z.d * b.value
|
||||
b.d += z.d * a.value
|
||||
}
|
||||
|
||||
public override fun divide(a: AutoDiffValue<T>, b: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun divide(a: AutoDiffValue<T>, b: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
derive(const { a.value / b.value }) { z ->
|
||||
a.d += z.d / b.value
|
||||
b.d -= z.d * a.value / (b.value * b.value)
|
||||
}
|
||||
|
||||
public override fun scale(a: AutoDiffValue<T>, value: Double): AutoDiffValue<T> =
|
||||
override fun scale(a: AutoDiffValue<T>, value: Double): AutoDiffValue<T> =
|
||||
derive(const { value * a.value }) { z ->
|
||||
a.d += z.d * value
|
||||
}
|
||||
@ -236,12 +236,12 @@ public class SimpleAutoDiffExpression<T : Any, F : Field<T>>(
|
||||
public val field: F,
|
||||
public val function: SimpleAutoDiffField<T, F>.() -> AutoDiffValue<T>,
|
||||
) : FirstDerivativeExpression<T>() {
|
||||
public override operator fun invoke(arguments: Map<Symbol, T>): T {
|
||||
override operator fun invoke(arguments: Map<Symbol, T>): T {
|
||||
//val bindings = arguments.entries.map { it.key.bind(it.value) }
|
||||
return SimpleAutoDiffField(field, arguments).function().value
|
||||
}
|
||||
|
||||
public override fun derivativeOrNull(symbol: Symbol): Expression<T> = Expression { arguments ->
|
||||
override fun derivativeOrNull(symbol: Symbol): Expression<T> = Expression { arguments ->
|
||||
//val bindings = arguments.entries.map { it.key.bind(it.value) }
|
||||
val derivationResult = SimpleAutoDiffField(field, arguments).differentiate(function)
|
||||
derivationResult.derivative(symbol)
|
||||
@ -346,28 +346,28 @@ public class SimpleAutoDiffExtendedField<T : Any, F : ExtendedField<T>>(
|
||||
|
||||
override fun bindSymbol(value: String): AutoDiffValue<T> = super<SimpleAutoDiffField>.bindSymbol(value)
|
||||
|
||||
public override fun number(value: Number): AutoDiffValue<T> = const { number(value) }
|
||||
override fun number(value: Number): AutoDiffValue<T> = const { number(value) }
|
||||
|
||||
public override fun scale(a: AutoDiffValue<T>, value: Double): AutoDiffValue<T> = a * number(value)
|
||||
override fun scale(a: AutoDiffValue<T>, value: Double): AutoDiffValue<T> = a * number(value)
|
||||
|
||||
// x ^ 2
|
||||
public fun sqr(x: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).sqr(x)
|
||||
|
||||
// x ^ 1/2
|
||||
public override fun sqrt(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun sqrt(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).sqrt(arg)
|
||||
|
||||
// x ^ y (const)
|
||||
public override fun power(arg: AutoDiffValue<T>, pow: Number): AutoDiffValue<T> =
|
||||
override fun power(arg: AutoDiffValue<T>, pow: Number): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).pow(arg, pow.toDouble())
|
||||
|
||||
// exp(x)
|
||||
public override fun exp(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun exp(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).exp(arg)
|
||||
|
||||
// ln(x)
|
||||
public override fun ln(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun ln(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).ln(arg)
|
||||
|
||||
// x ^ y (any)
|
||||
@ -377,40 +377,40 @@ public class SimpleAutoDiffExtendedField<T : Any, F : ExtendedField<T>>(
|
||||
): AutoDiffValue<T> = exp(y * ln(x))
|
||||
|
||||
// sin(x)
|
||||
public override fun sin(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun sin(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).sin(arg)
|
||||
|
||||
// cos(x)
|
||||
public override fun cos(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun cos(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).cos(arg)
|
||||
|
||||
public override fun tan(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun tan(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).tan(arg)
|
||||
|
||||
public override fun asin(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun asin(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).asin(arg)
|
||||
|
||||
public override fun acos(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun acos(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).acos(arg)
|
||||
|
||||
public override fun atan(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun atan(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).atan(arg)
|
||||
|
||||
public override fun sinh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun sinh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).sinh(arg)
|
||||
|
||||
public override fun cosh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun cosh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).cosh(arg)
|
||||
|
||||
public override fun tanh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun tanh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).tanh(arg)
|
||||
|
||||
public override fun asinh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun asinh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).asinh(arg)
|
||||
|
||||
public override fun acosh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun acosh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).acosh(arg)
|
||||
|
||||
public override fun atanh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
override fun atanh(arg: AutoDiffValue<T>): AutoDiffValue<T> =
|
||||
(this as SimpleAutoDiffField<T, F>).atanh(arg)
|
||||
}
|
||||
|
@ -26,11 +26,11 @@ public class MatrixWrapper<out T : Any> internal constructor(
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override fun <F : StructureFeature> getFeature(type: KClass<out F>): F? =
|
||||
override fun <F : StructureFeature> getFeature(type: KClass<out F>): F? =
|
||||
features.singleOrNull(type::isInstance) as? F
|
||||
?: origin.getFeature(type)
|
||||
|
||||
public override fun toString(): String = "MatrixWrapper(matrix=$origin, features=$features)"
|
||||
override fun toString(): String = "MatrixWrapper(matrix=$origin, features=$features)"
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -127,7 +127,7 @@ public interface GroupND<T, out S : Group<T>> : Group<StructureND<T>>, AlgebraND
|
||||
* @param b the addend.
|
||||
* @return the sum.
|
||||
*/
|
||||
public override fun add(a: StructureND<T>, b: StructureND<T>): StructureND<T> =
|
||||
override fun add(a: StructureND<T>, b: StructureND<T>): StructureND<T> =
|
||||
combine(a, b) { aValue, bValue -> add(aValue, bValue) }
|
||||
|
||||
// /**
|
||||
@ -137,7 +137,7 @@ public interface GroupND<T, out S : Group<T>> : Group<StructureND<T>>, AlgebraND
|
||||
// * @param k the multiplier.
|
||||
// * @return the product.
|
||||
// */
|
||||
// public override fun multiply(a: NDStructure<T>, k: Number): NDStructure<T> = a.map { multiply(it, k) }
|
||||
// override fun multiply(a: NDStructure<T>, k: Number): NDStructure<T> = a.map { multiply(it, k) }
|
||||
|
||||
// TODO move to extensions after KEEP-176
|
||||
|
||||
@ -194,7 +194,7 @@ public interface RingND<T, out R : Ring<T>> : Ring<StructureND<T>>, GroupND<T, R
|
||||
* @param b the multiplier.
|
||||
* @return the product.
|
||||
*/
|
||||
public override fun multiply(a: StructureND<T>, b: StructureND<T>): StructureND<T> =
|
||||
override fun multiply(a: StructureND<T>, b: StructureND<T>): StructureND<T> =
|
||||
combine(a, b) { aValue, bValue -> multiply(aValue, bValue) }
|
||||
|
||||
//TODO move to extensions after KEEP-176
|
||||
@ -234,7 +234,7 @@ public interface FieldND<T, out F : Field<T>> : Field<StructureND<T>>, RingND<T,
|
||||
* @param b the divisor.
|
||||
* @return the quotient.
|
||||
*/
|
||||
public override fun divide(a: StructureND<T>, b: StructureND<T>): StructureND<T> =
|
||||
override fun divide(a: StructureND<T>, b: StructureND<T>): StructureND<T> =
|
||||
combine(a, b) { aValue, bValue -> divide(aValue, bValue) }
|
||||
|
||||
//TODO move to extensions after KEEP-176
|
||||
|
@ -22,15 +22,15 @@ public class DoubleFieldND(
|
||||
ScaleOperations<StructureND<Double>>,
|
||||
ExtendedField<StructureND<Double>> {
|
||||
|
||||
public override val zero: BufferND<Double> by lazy { produce { zero } }
|
||||
public override val one: BufferND<Double> by lazy { produce { one } }
|
||||
override val zero: BufferND<Double> by lazy { produce { zero } }
|
||||
override val one: BufferND<Double> by lazy { produce { one } }
|
||||
|
||||
public override fun number(value: Number): BufferND<Double> {
|
||||
override fun number(value: Number): BufferND<Double> {
|
||||
val d = value.toDouble() // minimize conversions
|
||||
return produce { d }
|
||||
}
|
||||
|
||||
public override val StructureND<Double>.buffer: DoubleBuffer
|
||||
override val StructureND<Double>.buffer: DoubleBuffer
|
||||
get() = when {
|
||||
!shape.contentEquals(this@DoubleFieldND.shape) -> throw ShapeMismatchException(
|
||||
this@DoubleFieldND.shape,
|
||||
@ -41,7 +41,7 @@ public class DoubleFieldND(
|
||||
}
|
||||
|
||||
@Suppress("OVERRIDE_BY_INLINE")
|
||||
public override inline fun StructureND<Double>.map(
|
||||
override inline fun StructureND<Double>.map(
|
||||
transform: DoubleField.(Double) -> Double,
|
||||
): BufferND<Double> {
|
||||
val buffer = DoubleBuffer(strides.linearSize) { offset -> DoubleField.transform(buffer.array[offset]) }
|
||||
@ -49,7 +49,7 @@ public class DoubleFieldND(
|
||||
}
|
||||
|
||||
@Suppress("OVERRIDE_BY_INLINE")
|
||||
public override inline fun produce(initializer: DoubleField.(IntArray) -> Double): BufferND<Double> {
|
||||
override inline fun produce(initializer: DoubleField.(IntArray) -> Double): BufferND<Double> {
|
||||
val array = DoubleArray(strides.linearSize) { offset ->
|
||||
val index = strides.index(offset)
|
||||
DoubleField.initializer(index)
|
||||
@ -58,7 +58,7 @@ public class DoubleFieldND(
|
||||
}
|
||||
|
||||
@Suppress("OVERRIDE_BY_INLINE")
|
||||
public override inline fun StructureND<Double>.mapIndexed(
|
||||
override inline fun StructureND<Double>.mapIndexed(
|
||||
transform: DoubleField.(index: IntArray, Double) -> Double,
|
||||
): BufferND<Double> = BufferND(
|
||||
strides,
|
||||
@ -70,7 +70,7 @@ public class DoubleFieldND(
|
||||
})
|
||||
|
||||
@Suppress("OVERRIDE_BY_INLINE")
|
||||
public override inline fun combine(
|
||||
override inline fun combine(
|
||||
a: StructureND<Double>,
|
||||
b: StructureND<Double>,
|
||||
transform: DoubleField.(Double, Double) -> Double,
|
||||
@ -81,26 +81,26 @@ public class DoubleFieldND(
|
||||
return BufferND(strides, buffer)
|
||||
}
|
||||
|
||||
public override fun scale(a: StructureND<Double>, value: Double): StructureND<Double> = a.map { it * value }
|
||||
override fun scale(a: StructureND<Double>, value: Double): StructureND<Double> = a.map { it * value }
|
||||
|
||||
public override fun power(arg: StructureND<Double>, pow: Number): BufferND<Double> = arg.map { power(it, pow) }
|
||||
override fun power(arg: StructureND<Double>, pow: Number): BufferND<Double> = arg.map { power(it, pow) }
|
||||
|
||||
public override fun exp(arg: StructureND<Double>): BufferND<Double> = arg.map { exp(it) }
|
||||
public override fun ln(arg: StructureND<Double>): BufferND<Double> = arg.map { ln(it) }
|
||||
override fun exp(arg: StructureND<Double>): BufferND<Double> = arg.map { exp(it) }
|
||||
override fun ln(arg: StructureND<Double>): BufferND<Double> = arg.map { ln(it) }
|
||||
|
||||
public override fun sin(arg: StructureND<Double>): BufferND<Double> = arg.map { sin(it) }
|
||||
public override fun cos(arg: StructureND<Double>): BufferND<Double> = arg.map { cos(it) }
|
||||
public override fun tan(arg: StructureND<Double>): BufferND<Double> = arg.map { tan(it) }
|
||||
public override fun asin(arg: StructureND<Double>): BufferND<Double> = arg.map { asin(it) }
|
||||
public override fun acos(arg: StructureND<Double>): BufferND<Double> = arg.map { acos(it) }
|
||||
public override fun atan(arg: StructureND<Double>): BufferND<Double> = arg.map { atan(it) }
|
||||
override fun sin(arg: StructureND<Double>): BufferND<Double> = arg.map { sin(it) }
|
||||
override fun cos(arg: StructureND<Double>): BufferND<Double> = arg.map { cos(it) }
|
||||
override fun tan(arg: StructureND<Double>): BufferND<Double> = arg.map { tan(it) }
|
||||
override fun asin(arg: StructureND<Double>): BufferND<Double> = arg.map { asin(it) }
|
||||
override fun acos(arg: StructureND<Double>): BufferND<Double> = arg.map { acos(it) }
|
||||
override fun atan(arg: StructureND<Double>): BufferND<Double> = arg.map { atan(it) }
|
||||
|
||||
public override fun sinh(arg: StructureND<Double>): BufferND<Double> = arg.map { sinh(it) }
|
||||
public override fun cosh(arg: StructureND<Double>): BufferND<Double> = arg.map { cosh(it) }
|
||||
public override fun tanh(arg: StructureND<Double>): BufferND<Double> = arg.map { tanh(it) }
|
||||
public override fun asinh(arg: StructureND<Double>): BufferND<Double> = arg.map { asinh(it) }
|
||||
public override fun acosh(arg: StructureND<Double>): BufferND<Double> = arg.map { acosh(it) }
|
||||
public override fun atanh(arg: StructureND<Double>): BufferND<Double> = arg.map { atanh(it) }
|
||||
override fun sinh(arg: StructureND<Double>): BufferND<Double> = arg.map { sinh(it) }
|
||||
override fun cosh(arg: StructureND<Double>): BufferND<Double> = arg.map { cosh(it) }
|
||||
override fun tanh(arg: StructureND<Double>): BufferND<Double> = arg.map { tanh(it) }
|
||||
override fun asinh(arg: StructureND<Double>): BufferND<Double> = arg.map { asinh(it) }
|
||||
override fun acosh(arg: StructureND<Double>): BufferND<Double> = arg.map { acosh(it) }
|
||||
override fun atanh(arg: StructureND<Double>): BufferND<Double> = arg.map { atanh(it) }
|
||||
}
|
||||
|
||||
public fun AlgebraND.Companion.real(vararg shape: Int): DoubleFieldND = DoubleFieldND(shape)
|
||||
|
@ -16,14 +16,14 @@ import kotlin.jvm.JvmInline
|
||||
* A structure that is guaranteed to be one-dimensional
|
||||
*/
|
||||
public interface Structure1D<out T> : StructureND<T>, Buffer<T> {
|
||||
public override val dimension: Int get() = 1
|
||||
override val dimension: Int get() = 1
|
||||
|
||||
public override operator fun get(index: IntArray): T {
|
||||
override operator fun get(index: IntArray): T {
|
||||
require(index.size == 1) { "Index dimension mismatch. Expected 1 but found ${index.size}" }
|
||||
return get(index[0])
|
||||
}
|
||||
|
||||
public override operator fun iterator(): Iterator<T> = (0 until size).asSequence().map(::get).iterator()
|
||||
override operator fun iterator(): Iterator<T> = (0 until size).asSequence().map(::get).iterator()
|
||||
|
||||
public companion object
|
||||
}
|
||||
@ -32,7 +32,7 @@ public interface Structure1D<out T> : StructureND<T>, Buffer<T> {
|
||||
* A mutable structure that is guaranteed to be one-dimensional
|
||||
*/
|
||||
public interface MutableStructure1D<T> : Structure1D<T>, MutableStructureND<T>, MutableBuffer<T> {
|
||||
public override operator fun set(index: IntArray, value: T) {
|
||||
override operator fun set(index: IntArray, value: T) {
|
||||
require(index.size == 1) { "Index dimension mismatch. Expected 1 but found ${index.size}" }
|
||||
set(index[0], value)
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ public interface Structure2D<out T> : StructureND<T> {
|
||||
*/
|
||||
public val colNum: Int
|
||||
|
||||
public override val shape: IntArray get() = intArrayOf(rowNum, colNum)
|
||||
override val shape: IntArray get() = intArrayOf(rowNum, colNum)
|
||||
|
||||
/**
|
||||
* The buffer of rows of this structure. It gets elements from the structure dynamically.
|
||||
|
@ -166,13 +166,13 @@ public interface GroupOperations<T> : Algebra<T> {
|
||||
*/
|
||||
public operator fun T.minus(b: T): T = add(this, -b)
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: T) -> T = when (operation) {
|
||||
override fun unaryOperationFunction(operation: String): (arg: T) -> T = when (operation) {
|
||||
PLUS_OPERATION -> { arg -> +arg }
|
||||
MINUS_OPERATION -> { arg -> -arg }
|
||||
else -> super.unaryOperationFunction(operation)
|
||||
}
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = when (operation) {
|
||||
override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = when (operation) {
|
||||
PLUS_OPERATION -> ::add
|
||||
MINUS_OPERATION -> { left, right -> left - right }
|
||||
else -> super.binaryOperationFunction(operation)
|
||||
@ -226,7 +226,7 @@ public interface RingOperations<T> : GroupOperations<T> {
|
||||
*/
|
||||
public operator fun T.times(b: T): T = multiply(this, b)
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = when (operation) {
|
||||
override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = when (operation) {
|
||||
TIMES_OPERATION -> ::multiply
|
||||
else -> super.binaryOperationFunction(operation)
|
||||
}
|
||||
@ -277,7 +277,7 @@ public interface FieldOperations<T> : RingOperations<T> {
|
||||
*/
|
||||
public operator fun T.div(b: T): T = divide(this, b)
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = when (operation) {
|
||||
override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = when (operation) {
|
||||
DIV_OPERATION -> ::divide
|
||||
else -> super.binaryOperationFunction(operation)
|
||||
}
|
||||
@ -298,5 +298,5 @@ public interface FieldOperations<T> : RingOperations<T> {
|
||||
* @param T the type of element of this field.
|
||||
*/
|
||||
public interface Field<T> : Ring<T>, FieldOperations<T>, ScaleOperations<T>, NumericAlgebra<T> {
|
||||
public override fun number(value: Number): T = scale(one, value.toDouble())
|
||||
override fun number(value: Number): T = scale(one, value.toDouble())
|
||||
}
|
||||
|
@ -49,16 +49,16 @@ public class BigInt internal constructor(
|
||||
private val sign: Byte,
|
||||
private val magnitude: Magnitude,
|
||||
) : Comparable<BigInt> {
|
||||
public override fun compareTo(other: BigInt): Int = when {
|
||||
override fun compareTo(other: BigInt): Int = when {
|
||||
(sign == 0.toByte()) and (other.sign == 0.toByte()) -> 0
|
||||
sign < other.sign -> -1
|
||||
sign > other.sign -> 1
|
||||
else -> sign * compareMagnitudes(magnitude, other.magnitude)
|
||||
}
|
||||
|
||||
public override fun equals(other: Any?): Boolean = other is BigInt && compareTo(other) == 0
|
||||
override fun equals(other: Any?): Boolean = other is BigInt && compareTo(other) == 0
|
||||
|
||||
public override fun hashCode(): Int = magnitude.hashCode() + sign
|
||||
override fun hashCode(): Int = magnitude.hashCode() + sign
|
||||
|
||||
public fun abs(): BigInt = if (sign == 0.toByte()) this else BigInt(1, magnitude)
|
||||
|
||||
|
@ -87,7 +87,7 @@ public interface NumericAlgebra<T> : Algebra<T> {
|
||||
public fun rightSideNumberOperation(operation: String, left: T, right: Number): T =
|
||||
rightSideNumberOperationFunction(operation)(left, right)
|
||||
|
||||
public override fun bindSymbolOrNull(value: String): T? = when (value) {
|
||||
override fun bindSymbolOrNull(value: String): T? = when (value) {
|
||||
"pi" -> number(PI)
|
||||
"e" -> number(E)
|
||||
else -> super.bindSymbolOrNull(value)
|
||||
|
@ -15,10 +15,10 @@ public interface ExtendedFieldOperations<T> :
|
||||
TrigonometricOperations<T>,
|
||||
PowerOperations<T>,
|
||||
ExponentialOperations<T> {
|
||||
public override fun tan(arg: T): T = sin(arg) / cos(arg)
|
||||
public override fun tanh(arg: T): T = sinh(arg) / cosh(arg)
|
||||
override fun tan(arg: T): T = sin(arg) / cos(arg)
|
||||
override fun tanh(arg: T): T = sinh(arg) / cosh(arg)
|
||||
|
||||
public override fun unaryOperationFunction(operation: String): (arg: T) -> T = when (operation) {
|
||||
override fun unaryOperationFunction(operation: String): (arg: T) -> T = when (operation) {
|
||||
TrigonometricOperations.COS_OPERATION -> ::cos
|
||||
TrigonometricOperations.SIN_OPERATION -> ::sin
|
||||
TrigonometricOperations.TAN_OPERATION -> ::tan
|
||||
@ -42,14 +42,14 @@ public interface ExtendedFieldOperations<T> :
|
||||
* Advanced Number-like field that implements basic operations.
|
||||
*/
|
||||
public interface ExtendedField<T> : ExtendedFieldOperations<T>, Field<T>, NumericAlgebra<T>, ScaleOperations<T> {
|
||||
public override fun sinh(arg: T): T = (exp(arg) - exp(-arg)) / 2.0
|
||||
public override fun cosh(arg: T): T = (exp(arg) + exp(-arg)) / 2.0
|
||||
public override fun tanh(arg: T): T = (exp(arg) - exp(-arg)) / (exp(-arg) + exp(arg))
|
||||
public override fun asinh(arg: T): T = ln(sqrt(arg * arg + one) + arg)
|
||||
public override fun acosh(arg: T): T = ln(arg + sqrt((arg - one) * (arg + one)))
|
||||
public override fun atanh(arg: T): T = (ln(arg + one) - ln(one - arg)) / 2.0
|
||||
override fun sinh(arg: T): T = (exp(arg) - exp(-arg)) / 2.0
|
||||
override fun cosh(arg: T): T = (exp(arg) + exp(-arg)) / 2.0
|
||||
override fun tanh(arg: T): T = (exp(arg) - exp(-arg)) / (exp(-arg) + exp(arg))
|
||||
override fun asinh(arg: T): T = ln(sqrt(arg * arg + one) + arg)
|
||||
override fun acosh(arg: T): T = ln(arg + sqrt((arg - one) * (arg + one)))
|
||||
override fun atanh(arg: T): T = (ln(arg + one) - ln(one - arg)) / 2.0
|
||||
|
||||
public override fun rightSideNumberOperationFunction(operation: String): (left: T, right: Number) -> T =
|
||||
override fun rightSideNumberOperationFunction(operation: String): (left: T, right: Number) -> T =
|
||||
when (operation) {
|
||||
PowerOperations.POW_OPERATION -> ::power
|
||||
else -> super<Field>.rightSideNumberOperationFunction(operation)
|
||||
@ -61,50 +61,50 @@ public interface ExtendedField<T> : ExtendedFieldOperations<T>, Field<T>, Numeri
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||
public object DoubleField : ExtendedField<Double>, Norm<Double, Double>, ScaleOperations<Double> {
|
||||
public override inline val zero: Double get() = 0.0
|
||||
public override inline val one: Double get() = 1.0
|
||||
override inline val zero: Double get() = 0.0
|
||||
override inline val one: Double get() = 1.0
|
||||
|
||||
public override inline fun number(value: Number): Double = value.toDouble()
|
||||
override inline fun number(value: Number): Double = value.toDouble()
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: Double, right: Double) -> Double =
|
||||
override fun binaryOperationFunction(operation: String): (left: Double, right: Double) -> Double =
|
||||
when (operation) {
|
||||
PowerOperations.POW_OPERATION -> ::power
|
||||
else -> super<ExtendedField>.binaryOperationFunction(operation)
|
||||
}
|
||||
|
||||
public override inline fun add(a: Double, b: Double): Double = a + b
|
||||
override inline fun add(a: Double, b: Double): Double = a + b
|
||||
|
||||
public override inline fun multiply(a: Double, b: Double): Double = a * b
|
||||
public override inline fun divide(a: Double, b: Double): Double = a / b
|
||||
override inline fun multiply(a: Double, b: Double): Double = a * b
|
||||
override inline fun divide(a: Double, b: Double): Double = a / b
|
||||
|
||||
public override inline fun scale(a: Double, value: Double): Double = a * value
|
||||
override inline fun scale(a: Double, value: Double): Double = a * value
|
||||
|
||||
public override inline fun sin(arg: Double): Double = kotlin.math.sin(arg)
|
||||
public override inline fun cos(arg: Double): Double = kotlin.math.cos(arg)
|
||||
public override inline fun tan(arg: Double): Double = kotlin.math.tan(arg)
|
||||
public override inline fun acos(arg: Double): Double = kotlin.math.acos(arg)
|
||||
public override inline fun asin(arg: Double): Double = kotlin.math.asin(arg)
|
||||
public override inline fun atan(arg: Double): Double = kotlin.math.atan(arg)
|
||||
override inline fun sin(arg: Double): Double = kotlin.math.sin(arg)
|
||||
override inline fun cos(arg: Double): Double = kotlin.math.cos(arg)
|
||||
override inline fun tan(arg: Double): Double = kotlin.math.tan(arg)
|
||||
override inline fun acos(arg: Double): Double = kotlin.math.acos(arg)
|
||||
override inline fun asin(arg: Double): Double = kotlin.math.asin(arg)
|
||||
override inline fun atan(arg: Double): Double = kotlin.math.atan(arg)
|
||||
|
||||
public override inline fun sinh(arg: Double): Double = kotlin.math.sinh(arg)
|
||||
public override inline fun cosh(arg: Double): Double = kotlin.math.cosh(arg)
|
||||
public override inline fun tanh(arg: Double): Double = kotlin.math.tanh(arg)
|
||||
public override inline fun asinh(arg: Double): Double = kotlin.math.asinh(arg)
|
||||
public override inline fun acosh(arg: Double): Double = kotlin.math.acosh(arg)
|
||||
public override inline fun atanh(arg: Double): Double = kotlin.math.atanh(arg)
|
||||
override inline fun sinh(arg: Double): Double = kotlin.math.sinh(arg)
|
||||
override inline fun cosh(arg: Double): Double = kotlin.math.cosh(arg)
|
||||
override inline fun tanh(arg: Double): Double = kotlin.math.tanh(arg)
|
||||
override inline fun asinh(arg: Double): Double = kotlin.math.asinh(arg)
|
||||
override inline fun acosh(arg: Double): Double = kotlin.math.acosh(arg)
|
||||
override inline fun atanh(arg: Double): Double = kotlin.math.atanh(arg)
|
||||
|
||||
public override inline fun sqrt(arg: Double): Double = kotlin.math.sqrt(arg)
|
||||
public override inline fun power(arg: Double, pow: Number): Double = arg.kpow(pow.toDouble())
|
||||
public override inline fun exp(arg: Double): Double = kotlin.math.exp(arg)
|
||||
public override inline fun ln(arg: Double): Double = kotlin.math.ln(arg)
|
||||
override inline fun sqrt(arg: Double): Double = kotlin.math.sqrt(arg)
|
||||
override inline fun power(arg: Double, pow: Number): Double = arg.kpow(pow.toDouble())
|
||||
override inline fun exp(arg: Double): Double = kotlin.math.exp(arg)
|
||||
override inline fun ln(arg: Double): Double = kotlin.math.ln(arg)
|
||||
|
||||
public override inline fun norm(arg: Double): Double = abs(arg)
|
||||
override inline fun norm(arg: Double): Double = abs(arg)
|
||||
|
||||
public override inline fun Double.unaryMinus(): Double = -this
|
||||
public override inline fun Double.plus(b: Double): Double = this + b
|
||||
public override inline fun Double.minus(b: Double): Double = this - b
|
||||
public override inline fun Double.times(b: Double): Double = this * b
|
||||
public override inline fun Double.div(b: Double): Double = this / b
|
||||
override inline fun Double.unaryMinus(): Double = -this
|
||||
override inline fun Double.plus(b: Double): Double = this + b
|
||||
override inline fun Double.minus(b: Double): Double = this - b
|
||||
override inline fun Double.times(b: Double): Double = this * b
|
||||
override inline fun Double.div(b: Double): Double = this / b
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,50 +112,50 @@ public object DoubleField : ExtendedField<Double>, Norm<Double, Double>, ScaleOp
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||
public object FloatField : ExtendedField<Float>, Norm<Float, Float> {
|
||||
public override inline val zero: Float get() = 0.0f
|
||||
public override inline val one: Float get() = 1.0f
|
||||
override inline val zero: Float get() = 0.0f
|
||||
override inline val one: Float get() = 1.0f
|
||||
|
||||
public override fun number(value: Number): Float = value.toFloat()
|
||||
override fun number(value: Number): Float = value.toFloat()
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: Float, right: Float) -> Float =
|
||||
override fun binaryOperationFunction(operation: String): (left: Float, right: Float) -> Float =
|
||||
when (operation) {
|
||||
PowerOperations.POW_OPERATION -> ::power
|
||||
else -> super.binaryOperationFunction(operation)
|
||||
}
|
||||
|
||||
public override inline fun add(a: Float, b: Float): Float = a + b
|
||||
public override fun scale(a: Float, value: Double): Float = a * value.toFloat()
|
||||
override inline fun add(a: Float, b: Float): Float = a + b
|
||||
override fun scale(a: Float, value: Double): Float = a * value.toFloat()
|
||||
|
||||
public override inline fun multiply(a: Float, b: Float): Float = a * b
|
||||
override inline fun multiply(a: Float, b: Float): Float = a * b
|
||||
|
||||
public override inline fun divide(a: Float, b: Float): Float = a / b
|
||||
override inline fun divide(a: Float, b: Float): Float = a / b
|
||||
|
||||
public override inline fun sin(arg: Float): Float = kotlin.math.sin(arg)
|
||||
public override inline fun cos(arg: Float): Float = kotlin.math.cos(arg)
|
||||
public override inline fun tan(arg: Float): Float = kotlin.math.tan(arg)
|
||||
public override inline fun acos(arg: Float): Float = kotlin.math.acos(arg)
|
||||
public override inline fun asin(arg: Float): Float = kotlin.math.asin(arg)
|
||||
public override inline fun atan(arg: Float): Float = kotlin.math.atan(arg)
|
||||
override inline fun sin(arg: Float): Float = kotlin.math.sin(arg)
|
||||
override inline fun cos(arg: Float): Float = kotlin.math.cos(arg)
|
||||
override inline fun tan(arg: Float): Float = kotlin.math.tan(arg)
|
||||
override inline fun acos(arg: Float): Float = kotlin.math.acos(arg)
|
||||
override inline fun asin(arg: Float): Float = kotlin.math.asin(arg)
|
||||
override inline fun atan(arg: Float): Float = kotlin.math.atan(arg)
|
||||
|
||||
public override inline fun sinh(arg: Float): Float = kotlin.math.sinh(arg)
|
||||
public override inline fun cosh(arg: Float): Float = kotlin.math.cosh(arg)
|
||||
public override inline fun tanh(arg: Float): Float = kotlin.math.tanh(arg)
|
||||
public override inline fun asinh(arg: Float): Float = kotlin.math.asinh(arg)
|
||||
public override inline fun acosh(arg: Float): Float = kotlin.math.acosh(arg)
|
||||
public override inline fun atanh(arg: Float): Float = kotlin.math.atanh(arg)
|
||||
override inline fun sinh(arg: Float): Float = kotlin.math.sinh(arg)
|
||||
override inline fun cosh(arg: Float): Float = kotlin.math.cosh(arg)
|
||||
override inline fun tanh(arg: Float): Float = kotlin.math.tanh(arg)
|
||||
override inline fun asinh(arg: Float): Float = kotlin.math.asinh(arg)
|
||||
override inline fun acosh(arg: Float): Float = kotlin.math.acosh(arg)
|
||||
override inline fun atanh(arg: Float): Float = kotlin.math.atanh(arg)
|
||||
|
||||
public override inline fun sqrt(arg: Float): Float = kotlin.math.sqrt(arg)
|
||||
public override inline fun power(arg: Float, pow: Number): Float = arg.kpow(pow.toFloat())
|
||||
public override inline fun exp(arg: Float): Float = kotlin.math.exp(arg)
|
||||
public override inline fun ln(arg: Float): Float = kotlin.math.ln(arg)
|
||||
override inline fun sqrt(arg: Float): Float = kotlin.math.sqrt(arg)
|
||||
override inline fun power(arg: Float, pow: Number): Float = arg.kpow(pow.toFloat())
|
||||
override inline fun exp(arg: Float): Float = kotlin.math.exp(arg)
|
||||
override inline fun ln(arg: Float): Float = kotlin.math.ln(arg)
|
||||
|
||||
public override inline fun norm(arg: Float): Float = abs(arg)
|
||||
override inline fun norm(arg: Float): Float = abs(arg)
|
||||
|
||||
public override inline fun Float.unaryMinus(): Float = -this
|
||||
public override inline fun Float.plus(b: Float): Float = this + b
|
||||
public override inline fun Float.minus(b: Float): Float = this - b
|
||||
public override inline fun Float.times(b: Float): Float = this * b
|
||||
public override inline fun Float.div(b: Float): Float = this / b
|
||||
override inline fun Float.unaryMinus(): Float = -this
|
||||
override inline fun Float.plus(b: Float): Float = this + b
|
||||
override inline fun Float.minus(b: Float): Float = this - b
|
||||
override inline fun Float.times(b: Float): Float = this * b
|
||||
override inline fun Float.div(b: Float): Float = this / b
|
||||
}
|
||||
|
||||
/**
|
||||
@ -163,21 +163,21 @@ public object FloatField : ExtendedField<Float>, Norm<Float, Float> {
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||
public object IntRing : Ring<Int>, Norm<Int, Int>, NumericAlgebra<Int> {
|
||||
public override inline val zero: Int
|
||||
override inline val zero: Int
|
||||
get() = 0
|
||||
|
||||
public override inline val one: Int
|
||||
override inline val one: Int
|
||||
get() = 1
|
||||
|
||||
public override fun number(value: Number): Int = value.toInt()
|
||||
public override inline fun add(a: Int, b: Int): Int = a + b
|
||||
public override inline fun multiply(a: Int, b: Int): Int = a * b
|
||||
public override inline fun norm(arg: Int): Int = abs(arg)
|
||||
override fun number(value: Number): Int = value.toInt()
|
||||
override inline fun add(a: Int, b: Int): Int = a + b
|
||||
override inline fun multiply(a: Int, b: Int): Int = a * b
|
||||
override inline fun norm(arg: Int): Int = abs(arg)
|
||||
|
||||
public override inline fun Int.unaryMinus(): Int = -this
|
||||
public override inline fun Int.plus(b: Int): Int = this + b
|
||||
public override inline fun Int.minus(b: Int): Int = this - b
|
||||
public override inline fun Int.times(b: Int): Int = this * b
|
||||
override inline fun Int.unaryMinus(): Int = -this
|
||||
override inline fun Int.plus(b: Int): Int = this + b
|
||||
override inline fun Int.minus(b: Int): Int = this - b
|
||||
override inline fun Int.times(b: Int): Int = this * b
|
||||
}
|
||||
|
||||
/**
|
||||
@ -185,21 +185,21 @@ public object IntRing : Ring<Int>, Norm<Int, Int>, NumericAlgebra<Int> {
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||
public object ShortRing : Ring<Short>, Norm<Short, Short>, NumericAlgebra<Short> {
|
||||
public override inline val zero: Short
|
||||
override inline val zero: Short
|
||||
get() = 0
|
||||
|
||||
public override inline val one: Short
|
||||
override inline val one: Short
|
||||
get() = 1
|
||||
|
||||
public override fun number(value: Number): Short = value.toShort()
|
||||
public override inline fun add(a: Short, b: Short): Short = (a + b).toShort()
|
||||
public override inline fun multiply(a: Short, b: Short): Short = (a * b).toShort()
|
||||
public override fun norm(arg: Short): Short = if (arg > 0) arg else (-arg).toShort()
|
||||
override fun number(value: Number): Short = value.toShort()
|
||||
override inline fun add(a: Short, b: Short): Short = (a + b).toShort()
|
||||
override inline fun multiply(a: Short, b: Short): Short = (a * b).toShort()
|
||||
override fun norm(arg: Short): Short = if (arg > 0) arg else (-arg).toShort()
|
||||
|
||||
public override inline fun Short.unaryMinus(): Short = (-this).toShort()
|
||||
public override inline fun Short.plus(b: Short): Short = (this + b).toShort()
|
||||
public override inline fun Short.minus(b: Short): Short = (this - b).toShort()
|
||||
public override inline fun Short.times(b: Short): Short = (this * b).toShort()
|
||||
override inline fun Short.unaryMinus(): Short = (-this).toShort()
|
||||
override inline fun Short.plus(b: Short): Short = (this + b).toShort()
|
||||
override inline fun Short.minus(b: Short): Short = (this - b).toShort()
|
||||
override inline fun Short.times(b: Short): Short = (this * b).toShort()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -207,21 +207,21 @@ public object ShortRing : Ring<Short>, Norm<Short, Short>, NumericAlgebra<Short>
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||
public object ByteRing : Ring<Byte>, Norm<Byte, Byte>, NumericAlgebra<Byte> {
|
||||
public override inline val zero: Byte
|
||||
override inline val zero: Byte
|
||||
get() = 0
|
||||
|
||||
public override inline val one: Byte
|
||||
override inline val one: Byte
|
||||
get() = 1
|
||||
|
||||
public override fun number(value: Number): Byte = value.toByte()
|
||||
public override inline fun add(a: Byte, b: Byte): Byte = (a + b).toByte()
|
||||
public override inline fun multiply(a: Byte, b: Byte): Byte = (a * b).toByte()
|
||||
public override fun norm(arg: Byte): Byte = if (arg > 0) arg else (-arg).toByte()
|
||||
override fun number(value: Number): Byte = value.toByte()
|
||||
override inline fun add(a: Byte, b: Byte): Byte = (a + b).toByte()
|
||||
override inline fun multiply(a: Byte, b: Byte): Byte = (a * b).toByte()
|
||||
override fun norm(arg: Byte): Byte = if (arg > 0) arg else (-arg).toByte()
|
||||
|
||||
public override inline fun Byte.unaryMinus(): Byte = (-this).toByte()
|
||||
public override inline fun Byte.plus(b: Byte): Byte = (this + b).toByte()
|
||||
public override inline fun Byte.minus(b: Byte): Byte = (this - b).toByte()
|
||||
public override inline fun Byte.times(b: Byte): Byte = (this * b).toByte()
|
||||
override inline fun Byte.unaryMinus(): Byte = (-this).toByte()
|
||||
override inline fun Byte.plus(b: Byte): Byte = (this + b).toByte()
|
||||
override inline fun Byte.minus(b: Byte): Byte = (this - b).toByte()
|
||||
override inline fun Byte.times(b: Byte): Byte = (this * b).toByte()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -229,19 +229,19 @@ public object ByteRing : Ring<Byte>, Norm<Byte, Byte>, NumericAlgebra<Byte> {
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||
public object LongRing : Ring<Long>, Norm<Long, Long>, NumericAlgebra<Long> {
|
||||
public override inline val zero: Long
|
||||
override inline val zero: Long
|
||||
get() = 0L
|
||||
|
||||
public override inline val one: Long
|
||||
override inline val one: Long
|
||||
get() = 1L
|
||||
|
||||
public override fun number(value: Number): Long = value.toLong()
|
||||
public override inline fun add(a: Long, b: Long): Long = a + b
|
||||
public override inline fun multiply(a: Long, b: Long): Long = a * b
|
||||
public override fun norm(arg: Long): Long = abs(arg)
|
||||
override fun number(value: Number): Long = value.toLong()
|
||||
override inline fun add(a: Long, b: Long): Long = a + b
|
||||
override inline fun multiply(a: Long, b: Long): Long = a * b
|
||||
override fun norm(arg: Long): Long = abs(arg)
|
||||
|
||||
public override inline fun Long.unaryMinus(): Long = (-this)
|
||||
public override inline fun Long.plus(b: Long): Long = (this + b)
|
||||
public override inline fun Long.minus(b: Long): Long = (this - b)
|
||||
public override inline fun Long.times(b: Long): Long = (this * b)
|
||||
override inline fun Long.unaryMinus(): Long = (-this)
|
||||
override inline fun Long.plus(b: Long): Long = (this + b)
|
||||
override inline fun Long.minus(b: Long): Long = (this - b)
|
||||
override inline fun Long.times(b: Long): Long = (this * b)
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ public object DoubleBufferFieldOperations : ExtendedFieldOperations<Buffer<Doubl
|
||||
DoubleBuffer(size) { -get(it) }
|
||||
}
|
||||
|
||||
public override fun add(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
override fun add(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
require(b.size == a.size) {
|
||||
"The size of the first buffer ${a.size} should be the same as for second one: ${b.size} "
|
||||
}
|
||||
@ -31,7 +31,7 @@ public object DoubleBufferFieldOperations : ExtendedFieldOperations<Buffer<Doubl
|
||||
} else DoubleBuffer(DoubleArray(a.size) { a[it] + b[it] })
|
||||
}
|
||||
//
|
||||
// public override fun multiply(a: Buffer<Double>, k: Number): RealBuffer {
|
||||
// override fun multiply(a: Buffer<Double>, k: Number): RealBuffer {
|
||||
// val kValue = k.toDouble()
|
||||
//
|
||||
// return if (a is RealBuffer) {
|
||||
@ -40,7 +40,7 @@ public object DoubleBufferFieldOperations : ExtendedFieldOperations<Buffer<Doubl
|
||||
// } else RealBuffer(DoubleArray(a.size) { a[it] * kValue })
|
||||
// }
|
||||
//
|
||||
// public override fun divide(a: Buffer<Double>, k: Number): RealBuffer {
|
||||
// override fun divide(a: Buffer<Double>, k: Number): RealBuffer {
|
||||
// val kValue = k.toDouble()
|
||||
//
|
||||
// return if (a is RealBuffer) {
|
||||
@ -49,7 +49,7 @@ public object DoubleBufferFieldOperations : ExtendedFieldOperations<Buffer<Doubl
|
||||
// } else RealBuffer(DoubleArray(a.size) { a[it] / kValue })
|
||||
// }
|
||||
|
||||
public override fun multiply(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
override fun multiply(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
require(b.size == a.size) {
|
||||
"The size of the first buffer ${a.size} should be the same as for second one: ${b.size} "
|
||||
}
|
||||
@ -62,7 +62,7 @@ public object DoubleBufferFieldOperations : ExtendedFieldOperations<Buffer<Doubl
|
||||
DoubleBuffer(DoubleArray(a.size) { a[it] * b[it] })
|
||||
}
|
||||
|
||||
public override fun divide(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
override fun divide(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
require(b.size == a.size) {
|
||||
"The size of the first buffer ${a.size} should be the same as for second one: ${b.size} "
|
||||
}
|
||||
@ -74,87 +74,87 @@ public object DoubleBufferFieldOperations : ExtendedFieldOperations<Buffer<Doubl
|
||||
} else DoubleBuffer(DoubleArray(a.size) { a[it] / b[it] })
|
||||
}
|
||||
|
||||
public override fun sin(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun sin(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { sin(array[it]) })
|
||||
} else DoubleBuffer(DoubleArray(arg.size) { sin(arg[it]) })
|
||||
|
||||
public override fun cos(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun cos(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { cos(array[it]) })
|
||||
} else DoubleBuffer(DoubleArray(arg.size) { cos(arg[it]) })
|
||||
|
||||
public override fun tan(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun tan(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { tan(array[it]) })
|
||||
} else DoubleBuffer(DoubleArray(arg.size) { tan(arg[it]) })
|
||||
|
||||
public override fun asin(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun asin(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { asin(array[it]) })
|
||||
} else
|
||||
DoubleBuffer(DoubleArray(arg.size) { asin(arg[it]) })
|
||||
|
||||
public override fun acos(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun acos(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { acos(array[it]) })
|
||||
} else
|
||||
DoubleBuffer(DoubleArray(arg.size) { acos(arg[it]) })
|
||||
|
||||
public override fun atan(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun atan(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { atan(array[it]) })
|
||||
} else
|
||||
DoubleBuffer(DoubleArray(arg.size) { atan(arg[it]) })
|
||||
|
||||
public override fun sinh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun sinh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { sinh(array[it]) })
|
||||
} else
|
||||
DoubleBuffer(DoubleArray(arg.size) { sinh(arg[it]) })
|
||||
|
||||
public override fun cosh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun cosh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { cosh(array[it]) })
|
||||
} else
|
||||
DoubleBuffer(DoubleArray(arg.size) { cosh(arg[it]) })
|
||||
|
||||
public override fun tanh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun tanh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { tanh(array[it]) })
|
||||
} else
|
||||
DoubleBuffer(DoubleArray(arg.size) { tanh(arg[it]) })
|
||||
|
||||
public override fun asinh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun asinh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { asinh(array[it]) })
|
||||
} else
|
||||
DoubleBuffer(DoubleArray(arg.size) { asinh(arg[it]) })
|
||||
|
||||
public override fun acosh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun acosh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { acosh(array[it]) })
|
||||
} else
|
||||
DoubleBuffer(DoubleArray(arg.size) { acosh(arg[it]) })
|
||||
|
||||
public override fun atanh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun atanh(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { atanh(array[it]) })
|
||||
} else
|
||||
DoubleBuffer(DoubleArray(arg.size) { atanh(arg[it]) })
|
||||
|
||||
public override fun power(arg: Buffer<Double>, pow: Number): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun power(arg: Buffer<Double>, pow: Number): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { array[it].pow(pow.toDouble()) })
|
||||
} else
|
||||
DoubleBuffer(DoubleArray(arg.size) { arg[it].pow(pow.toDouble()) })
|
||||
|
||||
public override fun exp(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun exp(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { exp(array[it]) })
|
||||
} else DoubleBuffer(DoubleArray(arg.size) { exp(arg[it]) })
|
||||
|
||||
public override fun ln(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
override fun ln(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
||||
val array = arg.array
|
||||
DoubleBuffer(DoubleArray(arg.size) { ln(array[it]) })
|
||||
} else
|
||||
@ -167,8 +167,8 @@ public object DoubleBufferFieldOperations : ExtendedFieldOperations<Buffer<Doubl
|
||||
* @property size the size of buffers to operate on.
|
||||
*/
|
||||
public class DoubleBufferField(public val size: Int) : ExtendedField<Buffer<Double>> {
|
||||
public override val zero: Buffer<Double> by lazy { DoubleBuffer(size) { 0.0 } }
|
||||
public override val one: Buffer<Double> by lazy { DoubleBuffer(size) { 1.0 } }
|
||||
override val zero: Buffer<Double> by lazy { DoubleBuffer(size) { 0.0 } }
|
||||
override val one: Buffer<Double> by lazy { DoubleBuffer(size) { 1.0 } }
|
||||
|
||||
override fun number(value: Number): Buffer<Double> = DoubleBuffer(size) { value.toDouble() }
|
||||
|
||||
@ -176,12 +176,12 @@ public class DoubleBufferField(public val size: Int) : ExtendedField<Buffer<Doub
|
||||
-this@unaryMinus
|
||||
}
|
||||
|
||||
public override fun add(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
override fun add(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
require(a.size == size) { "The buffer size ${a.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.add(a, b)
|
||||
}
|
||||
|
||||
public override fun scale(a: Buffer<Double>, value: Double): DoubleBuffer {
|
||||
override fun scale(a: Buffer<Double>, value: Double): DoubleBuffer {
|
||||
require(a.size == size) { "The buffer size ${a.size} does not match context size $size" }
|
||||
|
||||
return if (a is DoubleBuffer) {
|
||||
@ -190,87 +190,87 @@ public class DoubleBufferField(public val size: Int) : ExtendedField<Buffer<Doub
|
||||
} else DoubleBuffer(DoubleArray(a.size) { a[it] * value })
|
||||
}
|
||||
|
||||
public override fun multiply(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
override fun multiply(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
require(a.size == size) { "The buffer size ${a.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.multiply(a, b)
|
||||
}
|
||||
|
||||
public override fun divide(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
override fun divide(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
||||
require(a.size == size) { "The buffer size ${a.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.divide(a, b)
|
||||
}
|
||||
|
||||
public override fun sin(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun sin(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.sin(arg)
|
||||
}
|
||||
|
||||
public override fun cos(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun cos(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.cos(arg)
|
||||
}
|
||||
|
||||
public override fun tan(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun tan(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.tan(arg)
|
||||
}
|
||||
|
||||
public override fun asin(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun asin(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.asin(arg)
|
||||
}
|
||||
|
||||
public override fun acos(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun acos(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.acos(arg)
|
||||
}
|
||||
|
||||
public override fun atan(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun atan(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.atan(arg)
|
||||
}
|
||||
|
||||
public override fun sinh(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun sinh(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.sinh(arg)
|
||||
}
|
||||
|
||||
public override fun cosh(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun cosh(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.cosh(arg)
|
||||
}
|
||||
|
||||
public override fun tanh(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun tanh(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.tanh(arg)
|
||||
}
|
||||
|
||||
public override fun asinh(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun asinh(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.asinh(arg)
|
||||
}
|
||||
|
||||
public override fun acosh(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun acosh(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.acosh(arg)
|
||||
}
|
||||
|
||||
public override fun atanh(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun atanh(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.atanh(arg)
|
||||
}
|
||||
|
||||
public override fun power(arg: Buffer<Double>, pow: Number): DoubleBuffer {
|
||||
override fun power(arg: Buffer<Double>, pow: Number): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.power(arg, pow)
|
||||
}
|
||||
|
||||
public override fun exp(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun exp(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.exp(arg)
|
||||
}
|
||||
|
||||
public override fun ln(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun ln(arg: Buffer<Double>): DoubleBuffer {
|
||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||
return DoubleBufferFieldOperations.ln(arg)
|
||||
}
|
||||
|
@ -48,8 +48,8 @@ public class MutableMemoryBuffer<T : Any>(memory: Memory, spec: MemorySpec<T>) :
|
||||
|
||||
private val writer: MemoryWriter = memory.writer()
|
||||
|
||||
public override operator fun set(index: Int, value: T): Unit = writer.write(spec, spec.objectSize * index, value)
|
||||
public override fun copy(): MutableBuffer<T> = MutableMemoryBuffer(memory.copy(), spec)
|
||||
override operator fun set(index: Int, value: T): Unit = writer.write(spec, spec.objectSize * index, value)
|
||||
override fun copy(): MutableBuffer<T> = MutableMemoryBuffer(memory.copy(), spec)
|
||||
|
||||
public companion object {
|
||||
public fun <T : Any> create(spec: MemorySpec<T>, size: Int): MutableMemoryBuffer<T> =
|
||||
|
@ -14,16 +14,16 @@ import kotlin.jvm.JvmInline
|
||||
*/
|
||||
@JvmInline
|
||||
public value class ShortBuffer(public val array: ShortArray) : MutableBuffer<Short> {
|
||||
public override val size: Int get() = array.size
|
||||
override val size: Int get() = array.size
|
||||
|
||||
public override operator fun get(index: Int): Short = array[index]
|
||||
override operator fun get(index: Int): Short = array[index]
|
||||
|
||||
public override operator fun set(index: Int, value: Short) {
|
||||
override operator fun set(index: Int, value: Short) {
|
||||
array[index] = value
|
||||
}
|
||||
|
||||
public override operator fun iterator(): ShortIterator = array.iterator()
|
||||
public override fun copy(): MutableBuffer<Short> = ShortBuffer(array.copyOf())
|
||||
override operator fun iterator(): ShortIterator = array.iterator()
|
||||
override fun copy(): MutableBuffer<Short> = ShortBuffer(array.copyOf())
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -13,16 +13,16 @@ import java.math.MathContext
|
||||
* A field over [BigInteger].
|
||||
*/
|
||||
public object JBigIntegerField : Ring<BigInteger>, NumericAlgebra<BigInteger> {
|
||||
public override val zero: BigInteger get() = BigInteger.ZERO
|
||||
override val zero: BigInteger get() = BigInteger.ZERO
|
||||
|
||||
public override val one: BigInteger get() = BigInteger.ONE
|
||||
override val one: BigInteger get() = BigInteger.ONE
|
||||
|
||||
public override fun number(value: Number): BigInteger = BigInteger.valueOf(value.toLong())
|
||||
public override fun add(a: BigInteger, b: BigInteger): BigInteger = a.add(b)
|
||||
public override operator fun BigInteger.minus(b: BigInteger): BigInteger = subtract(b)
|
||||
public override fun multiply(a: BigInteger, b: BigInteger): BigInteger = a.multiply(b)
|
||||
override fun number(value: Number): BigInteger = BigInteger.valueOf(value.toLong())
|
||||
override fun add(a: BigInteger, b: BigInteger): BigInteger = a.add(b)
|
||||
override operator fun BigInteger.minus(b: BigInteger): BigInteger = subtract(b)
|
||||
override fun multiply(a: BigInteger, b: BigInteger): BigInteger = a.multiply(b)
|
||||
|
||||
public override operator fun BigInteger.unaryMinus(): BigInteger = negate()
|
||||
override operator fun BigInteger.unaryMinus(): BigInteger = negate()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,24 +33,24 @@ public object JBigIntegerField : Ring<BigInteger>, NumericAlgebra<BigInteger> {
|
||||
public abstract class JBigDecimalFieldBase internal constructor(
|
||||
private val mathContext: MathContext = MathContext.DECIMAL64,
|
||||
) : Field<BigDecimal>, PowerOperations<BigDecimal>, NumericAlgebra<BigDecimal>, ScaleOperations<BigDecimal> {
|
||||
public override val zero: BigDecimal
|
||||
override val zero: BigDecimal
|
||||
get() = BigDecimal.ZERO
|
||||
|
||||
public override val one: BigDecimal
|
||||
override val one: BigDecimal
|
||||
get() = BigDecimal.ONE
|
||||
|
||||
public override fun add(a: BigDecimal, b: BigDecimal): BigDecimal = a.add(b)
|
||||
public override operator fun BigDecimal.minus(b: BigDecimal): BigDecimal = subtract(b)
|
||||
public override fun number(value: Number): BigDecimal = BigDecimal.valueOf(value.toDouble())
|
||||
override fun add(a: BigDecimal, b: BigDecimal): BigDecimal = a.add(b)
|
||||
override operator fun BigDecimal.minus(b: BigDecimal): BigDecimal = subtract(b)
|
||||
override fun number(value: Number): BigDecimal = BigDecimal.valueOf(value.toDouble())
|
||||
|
||||
public override fun scale(a: BigDecimal, value: Double): BigDecimal =
|
||||
override fun scale(a: BigDecimal, value: Double): BigDecimal =
|
||||
a.multiply(value.toBigDecimal(mathContext), mathContext)
|
||||
|
||||
public override fun multiply(a: BigDecimal, b: BigDecimal): BigDecimal = a.multiply(b, mathContext)
|
||||
public override fun divide(a: BigDecimal, b: BigDecimal): BigDecimal = a.divide(b, mathContext)
|
||||
public override fun power(arg: BigDecimal, pow: Number): BigDecimal = arg.pow(pow.toInt(), mathContext)
|
||||
public override fun sqrt(arg: BigDecimal): BigDecimal = arg.sqrt(mathContext)
|
||||
public override operator fun BigDecimal.unaryMinus(): BigDecimal = negate(mathContext)
|
||||
override fun multiply(a: BigDecimal, b: BigDecimal): BigDecimal = a.multiply(b, mathContext)
|
||||
override fun divide(a: BigDecimal, b: BigDecimal): BigDecimal = a.divide(b, mathContext)
|
||||
override fun power(arg: BigDecimal, pow: Number): BigDecimal = arg.pow(pow.toInt(), mathContext)
|
||||
override fun sqrt(arg: BigDecimal): BigDecimal = arg.sqrt(mathContext)
|
||||
override operator fun BigDecimal.unaryMinus(): BigDecimal = negate(mathContext)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -32,9 +32,9 @@ public interface BlockingBufferChain<out T> : BlockingChain<T>, BufferChain<T> {
|
||||
|
||||
public fun nextBufferBlocking(size: Int): Buffer<T>
|
||||
|
||||
public override fun nextBlocking(): T = nextBufferBlocking(1)[0]
|
||||
override fun nextBlocking(): T = nextBufferBlocking(1)[0]
|
||||
|
||||
public override suspend fun nextBuffer(size: Int): Buffer<T> = nextBufferBlocking(size)
|
||||
override suspend fun nextBuffer(size: Int): Buffer<T> = nextBufferBlocking(size)
|
||||
|
||||
override suspend fun fork(): BlockingBufferChain<T>
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ public interface BlockingDoubleChain : BlockingBufferChain<Double> {
|
||||
/**
|
||||
* Returns an [DoubleArray] chunk of [size] values of [next].
|
||||
*/
|
||||
public override fun nextBufferBlocking(size: Int): DoubleBuffer
|
||||
override fun nextBufferBlocking(size: Int): DoubleBuffer
|
||||
|
||||
override suspend fun fork(): BlockingDoubleChain
|
||||
|
||||
|
@ -39,8 +39,8 @@ public fun <T> Sequence<T>.asChain(): Chain<T> = iterator().asChain()
|
||||
* A simple chain of independent tokens. [fork] returns the same chain.
|
||||
*/
|
||||
public class SimpleChain<out R>(private val gen: suspend () -> R) : Chain<R> {
|
||||
public override suspend fun next(): R = gen()
|
||||
public override suspend fun fork(): Chain<R> = this
|
||||
override suspend fun next(): R = gen()
|
||||
override suspend fun fork(): Chain<R> = this
|
||||
}
|
||||
|
||||
/**
|
||||
@ -52,13 +52,13 @@ public class MarkovChain<out R : Any>(private val seed: suspend () -> R, private
|
||||
|
||||
public fun value(): R? = value
|
||||
|
||||
public override suspend fun next(): R = mutex.withLock {
|
||||
override suspend fun next(): R = mutex.withLock {
|
||||
val newValue = gen(value ?: seed())
|
||||
value = newValue
|
||||
newValue
|
||||
}
|
||||
|
||||
public override suspend fun fork(): Chain<R> = MarkovChain(seed = { value ?: seed() }, gen = gen)
|
||||
override suspend fun fork(): Chain<R> = MarkovChain(seed = { value ?: seed() }, gen = gen)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -77,21 +77,21 @@ public class StatefulChain<S, out R>(
|
||||
|
||||
public fun value(): R? = value
|
||||
|
||||
public override suspend fun next(): R = mutex.withLock {
|
||||
override suspend fun next(): R = mutex.withLock {
|
||||
val newValue = state.gen(value ?: state.seed())
|
||||
value = newValue
|
||||
newValue
|
||||
}
|
||||
|
||||
public override suspend fun fork(): Chain<R> = StatefulChain(forkState(state), seed, forkState, gen)
|
||||
override suspend fun fork(): Chain<R> = StatefulChain(forkState(state), seed, forkState, gen)
|
||||
}
|
||||
|
||||
/**
|
||||
* A chain that repeats the same value
|
||||
*/
|
||||
public class ConstantChain<out T>(public val value: T) : Chain<T> {
|
||||
public override suspend fun next(): T = value
|
||||
public override suspend fun fork(): Chain<T> = this
|
||||
override suspend fun next(): T = value
|
||||
override suspend fun fork(): Chain<T> = this
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -22,10 +22,10 @@ public class RingBuffer<T>(
|
||||
) : Buffer<T> {
|
||||
private val mutex: Mutex = Mutex()
|
||||
|
||||
public override var size: Int = size
|
||||
override var size: Int = size
|
||||
private set
|
||||
|
||||
public override operator fun get(index: Int): T {
|
||||
override operator fun get(index: Int): T {
|
||||
require(index >= 0) { "Index must be positive" }
|
||||
require(index < size) { "Index $index is out of circular buffer size $size" }
|
||||
return buffer[startIndex.forward(index)] as T
|
||||
@ -36,7 +36,7 @@ public class RingBuffer<T>(
|
||||
/**
|
||||
* Iterator could provide wrong results if buffer is changed in initialization (iteration is safe)
|
||||
*/
|
||||
public override operator fun iterator(): Iterator<T> = object : AbstractIterator<T>() {
|
||||
override operator fun iterator(): Iterator<T> = object : AbstractIterator<T>() {
|
||||
private var count = size
|
||||
private var index = startIndex
|
||||
val copy = buffer.copy()
|
||||
|
@ -13,7 +13,7 @@ import space.kscience.kmath.nd.StructureND
|
||||
|
||||
public class LazyStructureND<out T>(
|
||||
public val scope: CoroutineScope,
|
||||
public override val shape: IntArray,
|
||||
override val shape: IntArray,
|
||||
public val function: suspend (IntArray) -> T,
|
||||
) : StructureND<T> {
|
||||
private val cache: MutableMap<IntArray, Deferred<T>> = HashMap()
|
||||
@ -23,10 +23,10 @@ public class LazyStructureND<out T>(
|
||||
}
|
||||
|
||||
public suspend fun await(index: IntArray): T = deferred(index).await()
|
||||
public override operator fun get(index: IntArray): T = runBlocking { deferred(index).await() }
|
||||
override operator fun get(index: IntArray): T = runBlocking { deferred(index).await() }
|
||||
|
||||
@OptIn(PerformancePitfall::class)
|
||||
public override fun elements(): Sequence<Pair<IntArray, T>> {
|
||||
override fun elements(): Sequence<Pair<IntArray, T>> {
|
||||
val strides = DefaultStrides(shape)
|
||||
val res = runBlocking { strides.indices().toList().map { index -> index to await(index) } }
|
||||
return res.asSequence()
|
||||
|
@ -17,6 +17,6 @@ import space.kscience.kmath.nd.Structure2D
|
||||
* @author Iaroslav Postovalov
|
||||
*/
|
||||
public abstract class EjmlMatrix<out T, out M : Matrix>(public open val origin: M) : Structure2D<T> {
|
||||
public override val rowNum: Int get() = origin.numRows
|
||||
public override val colNum: Int get() = origin.numCols
|
||||
override val rowNum: Int get() = origin.numRows
|
||||
override val colNum: Int get() = origin.numCols
|
||||
}
|
||||
|
@ -17,10 +17,10 @@ import space.kscience.kmath.linear.Point
|
||||
* @author Iaroslav Postovalov
|
||||
*/
|
||||
public abstract class EjmlVector<out T, out M : Matrix>(public open val origin: M) : Point<T> {
|
||||
public override val size: Int
|
||||
override val size: Int
|
||||
get() = origin.numCols
|
||||
|
||||
public override operator fun iterator(): Iterator<T> = object : Iterator<T> {
|
||||
override operator fun iterator(): Iterator<T> = object : Iterator<T> {
|
||||
private var cursor: Int = 0
|
||||
|
||||
override fun next(): T {
|
||||
@ -31,5 +31,5 @@ public abstract class EjmlVector<out T, out M : Matrix>(public open val origin:
|
||||
override fun hasNext(): Boolean = cursor < origin.numCols * origin.numRows
|
||||
}
|
||||
|
||||
public override fun toString(): String = "EjmlVector(origin=$origin)"
|
||||
override fun toString(): String = "EjmlVector(origin=$origin)"
|
||||
}
|
||||
|
@ -34,37 +34,37 @@ import kotlin.reflect.cast
|
||||
/**
|
||||
* [EjmlVector] specialization for [Double].
|
||||
*/
|
||||
public class EjmlDoubleVector<out M : DMatrix>(public override val origin: M) : EjmlVector<Double, M>(origin) {
|
||||
public class EjmlDoubleVector<out M : DMatrix>(override val origin: M) : EjmlVector<Double, M>(origin) {
|
||||
init {
|
||||
require(origin.numRows == 1) { "The origin matrix must have only one row to form a vector" }
|
||||
}
|
||||
|
||||
public override operator fun get(index: Int): Double = origin[0, index]
|
||||
override operator fun get(index: Int): Double = origin[0, index]
|
||||
}
|
||||
|
||||
/**
|
||||
* [EjmlVector] specialization for [Float].
|
||||
*/
|
||||
public class EjmlFloatVector<out M : FMatrix>(public override val origin: M) : EjmlVector<Float, M>(origin) {
|
||||
public class EjmlFloatVector<out M : FMatrix>(override val origin: M) : EjmlVector<Float, M>(origin) {
|
||||
init {
|
||||
require(origin.numRows == 1) { "The origin matrix must have only one row to form a vector" }
|
||||
}
|
||||
|
||||
public override operator fun get(index: Int): Float = origin[0, index]
|
||||
override operator fun get(index: Int): Float = origin[0, index]
|
||||
}
|
||||
|
||||
/**
|
||||
* [EjmlMatrix] specialization for [Double].
|
||||
*/
|
||||
public class EjmlDoubleMatrix<out M : DMatrix>(public override val origin: M) : EjmlMatrix<Double, M>(origin) {
|
||||
public override operator fun get(i: Int, j: Int): Double = origin[i, j]
|
||||
public class EjmlDoubleMatrix<out M : DMatrix>(override val origin: M) : EjmlMatrix<Double, M>(origin) {
|
||||
override operator fun get(i: Int, j: Int): Double = origin[i, j]
|
||||
}
|
||||
|
||||
/**
|
||||
* [EjmlMatrix] specialization for [Float].
|
||||
*/
|
||||
public class EjmlFloatMatrix<out M : FMatrix>(public override val origin: M) : EjmlMatrix<Float, M>(origin) {
|
||||
public override operator fun get(i: Int, j: Int): Float = origin[i, j]
|
||||
public class EjmlFloatMatrix<out M : FMatrix>(override val origin: M) : EjmlMatrix<Float, M>(origin) {
|
||||
override operator fun get(i: Int, j: Int): Float = origin[i, j]
|
||||
}
|
||||
|
||||
/**
|
||||
@ -75,23 +75,23 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
/**
|
||||
* The [DoubleField] reference.
|
||||
*/
|
||||
public override val elementAlgebra: DoubleField get() = DoubleField
|
||||
override val elementAlgebra: DoubleField get() = DoubleField
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override fun Matrix<Double>.toEjml(): EjmlDoubleMatrix<DMatrixRMaj> = when {
|
||||
override fun Matrix<Double>.toEjml(): EjmlDoubleMatrix<DMatrixRMaj> = when {
|
||||
this is EjmlDoubleMatrix<*> && origin is DMatrixRMaj -> this as EjmlDoubleMatrix<DMatrixRMaj>
|
||||
else -> buildMatrix(rowNum, colNum) { i, j -> get(i, j) }
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override fun Point<Double>.toEjml(): EjmlDoubleVector<DMatrixRMaj> = when {
|
||||
override fun Point<Double>.toEjml(): EjmlDoubleVector<DMatrixRMaj> = when {
|
||||
this is EjmlDoubleVector<*> && origin is DMatrixRMaj -> this as EjmlDoubleVector<DMatrixRMaj>
|
||||
else -> EjmlDoubleVector(DMatrixRMaj(size, 1).also {
|
||||
(0 until it.numRows).forEach { row -> it[row, 0] = get(row) }
|
||||
})
|
||||
}
|
||||
|
||||
public override fun buildMatrix(
|
||||
override fun buildMatrix(
|
||||
rows: Int,
|
||||
columns: Int,
|
||||
initializer: DoubleField.(i: Int, j: Int) -> Double,
|
||||
@ -101,7 +101,7 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
}
|
||||
}.wrapMatrix()
|
||||
|
||||
public override fun buildVector(
|
||||
override fun buildVector(
|
||||
size: Int,
|
||||
initializer: DoubleField.(Int) -> Double,
|
||||
): EjmlDoubleVector<DMatrixRMaj> = EjmlDoubleVector(DMatrixRMaj(size, 1).also {
|
||||
@ -111,21 +111,21 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
private fun <T : DMatrix> T.wrapMatrix() = EjmlDoubleMatrix(this)
|
||||
private fun <T : DMatrix> T.wrapVector() = EjmlDoubleVector(this)
|
||||
|
||||
public override fun Matrix<Double>.unaryMinus(): Matrix<Double> = this * elementAlgebra { -one }
|
||||
override fun Matrix<Double>.unaryMinus(): Matrix<Double> = this * elementAlgebra { -one }
|
||||
|
||||
public override fun Matrix<Double>.dot(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixRMaj> {
|
||||
override fun Matrix<Double>.dot(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixRMaj> {
|
||||
val out = DMatrixRMaj(1, 1)
|
||||
CommonOps_DDRM.mult(toEjml().origin, other.toEjml().origin, out)
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Matrix<Double>.dot(vector: Point<Double>): EjmlDoubleVector<DMatrixRMaj> {
|
||||
override fun Matrix<Double>.dot(vector: Point<Double>): EjmlDoubleVector<DMatrixRMaj> {
|
||||
val out = DMatrixRMaj(1, 1)
|
||||
CommonOps_DDRM.mult(toEjml().origin, vector.toEjml().origin, out)
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override operator fun Matrix<Double>.minus(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixRMaj> {
|
||||
override operator fun Matrix<Double>.minus(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixRMaj> {
|
||||
val out = DMatrixRMaj(1, 1)
|
||||
|
||||
CommonOps_DDRM.add(
|
||||
@ -139,19 +139,19 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override operator fun Matrix<Double>.times(value: Double): EjmlDoubleMatrix<DMatrixRMaj> {
|
||||
override operator fun Matrix<Double>.times(value: Double): EjmlDoubleMatrix<DMatrixRMaj> {
|
||||
val res = DMatrixRMaj(1, 1)
|
||||
CommonOps_DDRM.scale(value, toEjml().origin, res)
|
||||
return res.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Point<Double>.unaryMinus(): EjmlDoubleVector<DMatrixRMaj> {
|
||||
override fun Point<Double>.unaryMinus(): EjmlDoubleVector<DMatrixRMaj> {
|
||||
val res = DMatrixRMaj(1, 1)
|
||||
CommonOps_DDRM.changeSign(toEjml().origin, res)
|
||||
return res.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Matrix<Double>.plus(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixRMaj> {
|
||||
override fun Matrix<Double>.plus(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixRMaj> {
|
||||
val out = DMatrixRMaj(1, 1)
|
||||
|
||||
CommonOps_DDRM.add(
|
||||
@ -165,7 +165,7 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Point<Double>.plus(other: Point<Double>): EjmlDoubleVector<DMatrixRMaj> {
|
||||
override fun Point<Double>.plus(other: Point<Double>): EjmlDoubleVector<DMatrixRMaj> {
|
||||
val out = DMatrixRMaj(1, 1)
|
||||
|
||||
CommonOps_DDRM.add(
|
||||
@ -179,7 +179,7 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Point<Double>.minus(other: Point<Double>): EjmlDoubleVector<DMatrixRMaj> {
|
||||
override fun Point<Double>.minus(other: Point<Double>): EjmlDoubleVector<DMatrixRMaj> {
|
||||
val out = DMatrixRMaj(1, 1)
|
||||
|
||||
CommonOps_DDRM.add(
|
||||
@ -193,18 +193,18 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Double.times(m: Matrix<Double>): EjmlDoubleMatrix<DMatrixRMaj> = m * this
|
||||
override fun Double.times(m: Matrix<Double>): EjmlDoubleMatrix<DMatrixRMaj> = m * this
|
||||
|
||||
public override fun Point<Double>.times(value: Double): EjmlDoubleVector<DMatrixRMaj> {
|
||||
override fun Point<Double>.times(value: Double): EjmlDoubleVector<DMatrixRMaj> {
|
||||
val res = DMatrixRMaj(1, 1)
|
||||
CommonOps_DDRM.scale(value, toEjml().origin, res)
|
||||
return res.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Double.times(v: Point<Double>): EjmlDoubleVector<DMatrixRMaj> = v * this
|
||||
override fun Double.times(v: Point<Double>): EjmlDoubleVector<DMatrixRMaj> = v * this
|
||||
|
||||
@UnstableKMathAPI
|
||||
public override fun <F : StructureFeature> getFeature(structure: Matrix<Double>, type: KClass<out F>): F? {
|
||||
override fun <F : StructureFeature> getFeature(structure: Matrix<Double>, type: KClass<out F>): F? {
|
||||
structure.getFeature(type)?.let { return it }
|
||||
val origin = structure.toEjml().origin
|
||||
|
||||
@ -309,23 +309,23 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace<Float, FloatField, FMatrixRM
|
||||
/**
|
||||
* The [FloatField] reference.
|
||||
*/
|
||||
public override val elementAlgebra: FloatField get() = FloatField
|
||||
override val elementAlgebra: FloatField get() = FloatField
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override fun Matrix<Float>.toEjml(): EjmlFloatMatrix<FMatrixRMaj> = when {
|
||||
override fun Matrix<Float>.toEjml(): EjmlFloatMatrix<FMatrixRMaj> = when {
|
||||
this is EjmlFloatMatrix<*> && origin is FMatrixRMaj -> this as EjmlFloatMatrix<FMatrixRMaj>
|
||||
else -> buildMatrix(rowNum, colNum) { i, j -> get(i, j) }
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override fun Point<Float>.toEjml(): EjmlFloatVector<FMatrixRMaj> = when {
|
||||
override fun Point<Float>.toEjml(): EjmlFloatVector<FMatrixRMaj> = when {
|
||||
this is EjmlFloatVector<*> && origin is FMatrixRMaj -> this as EjmlFloatVector<FMatrixRMaj>
|
||||
else -> EjmlFloatVector(FMatrixRMaj(size, 1).also {
|
||||
(0 until it.numRows).forEach { row -> it[row, 0] = get(row) }
|
||||
})
|
||||
}
|
||||
|
||||
public override fun buildMatrix(
|
||||
override fun buildMatrix(
|
||||
rows: Int,
|
||||
columns: Int,
|
||||
initializer: FloatField.(i: Int, j: Int) -> Float,
|
||||
@ -335,7 +335,7 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace<Float, FloatField, FMatrixRM
|
||||
}
|
||||
}.wrapMatrix()
|
||||
|
||||
public override fun buildVector(
|
||||
override fun buildVector(
|
||||
size: Int,
|
||||
initializer: FloatField.(Int) -> Float,
|
||||
): EjmlFloatVector<FMatrixRMaj> = EjmlFloatVector(FMatrixRMaj(size, 1).also {
|
||||
@ -345,21 +345,21 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace<Float, FloatField, FMatrixRM
|
||||
private fun <T : FMatrix> T.wrapMatrix() = EjmlFloatMatrix(this)
|
||||
private fun <T : FMatrix> T.wrapVector() = EjmlFloatVector(this)
|
||||
|
||||
public override fun Matrix<Float>.unaryMinus(): Matrix<Float> = this * elementAlgebra { -one }
|
||||
override fun Matrix<Float>.unaryMinus(): Matrix<Float> = this * elementAlgebra { -one }
|
||||
|
||||
public override fun Matrix<Float>.dot(other: Matrix<Float>): EjmlFloatMatrix<FMatrixRMaj> {
|
||||
override fun Matrix<Float>.dot(other: Matrix<Float>): EjmlFloatMatrix<FMatrixRMaj> {
|
||||
val out = FMatrixRMaj(1, 1)
|
||||
CommonOps_FDRM.mult(toEjml().origin, other.toEjml().origin, out)
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Matrix<Float>.dot(vector: Point<Float>): EjmlFloatVector<FMatrixRMaj> {
|
||||
override fun Matrix<Float>.dot(vector: Point<Float>): EjmlFloatVector<FMatrixRMaj> {
|
||||
val out = FMatrixRMaj(1, 1)
|
||||
CommonOps_FDRM.mult(toEjml().origin, vector.toEjml().origin, out)
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override operator fun Matrix<Float>.minus(other: Matrix<Float>): EjmlFloatMatrix<FMatrixRMaj> {
|
||||
override operator fun Matrix<Float>.minus(other: Matrix<Float>): EjmlFloatMatrix<FMatrixRMaj> {
|
||||
val out = FMatrixRMaj(1, 1)
|
||||
|
||||
CommonOps_FDRM.add(
|
||||
@ -373,19 +373,19 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace<Float, FloatField, FMatrixRM
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override operator fun Matrix<Float>.times(value: Float): EjmlFloatMatrix<FMatrixRMaj> {
|
||||
override operator fun Matrix<Float>.times(value: Float): EjmlFloatMatrix<FMatrixRMaj> {
|
||||
val res = FMatrixRMaj(1, 1)
|
||||
CommonOps_FDRM.scale(value, toEjml().origin, res)
|
||||
return res.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Point<Float>.unaryMinus(): EjmlFloatVector<FMatrixRMaj> {
|
||||
override fun Point<Float>.unaryMinus(): EjmlFloatVector<FMatrixRMaj> {
|
||||
val res = FMatrixRMaj(1, 1)
|
||||
CommonOps_FDRM.changeSign(toEjml().origin, res)
|
||||
return res.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Matrix<Float>.plus(other: Matrix<Float>): EjmlFloatMatrix<FMatrixRMaj> {
|
||||
override fun Matrix<Float>.plus(other: Matrix<Float>): EjmlFloatMatrix<FMatrixRMaj> {
|
||||
val out = FMatrixRMaj(1, 1)
|
||||
|
||||
CommonOps_FDRM.add(
|
||||
@ -399,7 +399,7 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace<Float, FloatField, FMatrixRM
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Point<Float>.plus(other: Point<Float>): EjmlFloatVector<FMatrixRMaj> {
|
||||
override fun Point<Float>.plus(other: Point<Float>): EjmlFloatVector<FMatrixRMaj> {
|
||||
val out = FMatrixRMaj(1, 1)
|
||||
|
||||
CommonOps_FDRM.add(
|
||||
@ -413,7 +413,7 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace<Float, FloatField, FMatrixRM
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Point<Float>.minus(other: Point<Float>): EjmlFloatVector<FMatrixRMaj> {
|
||||
override fun Point<Float>.minus(other: Point<Float>): EjmlFloatVector<FMatrixRMaj> {
|
||||
val out = FMatrixRMaj(1, 1)
|
||||
|
||||
CommonOps_FDRM.add(
|
||||
@ -427,18 +427,18 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace<Float, FloatField, FMatrixRM
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Float.times(m: Matrix<Float>): EjmlFloatMatrix<FMatrixRMaj> = m * this
|
||||
override fun Float.times(m: Matrix<Float>): EjmlFloatMatrix<FMatrixRMaj> = m * this
|
||||
|
||||
public override fun Point<Float>.times(value: Float): EjmlFloatVector<FMatrixRMaj> {
|
||||
override fun Point<Float>.times(value: Float): EjmlFloatVector<FMatrixRMaj> {
|
||||
val res = FMatrixRMaj(1, 1)
|
||||
CommonOps_FDRM.scale(value, toEjml().origin, res)
|
||||
return res.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Float.times(v: Point<Float>): EjmlFloatVector<FMatrixRMaj> = v * this
|
||||
override fun Float.times(v: Point<Float>): EjmlFloatVector<FMatrixRMaj> = v * this
|
||||
|
||||
@UnstableKMathAPI
|
||||
public override fun <F : StructureFeature> getFeature(structure: Matrix<Float>, type: KClass<out F>): F? {
|
||||
override fun <F : StructureFeature> getFeature(structure: Matrix<Float>, type: KClass<out F>): F? {
|
||||
structure.getFeature(type)?.let { return it }
|
||||
val origin = structure.toEjml().origin
|
||||
|
||||
@ -543,23 +543,23 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
/**
|
||||
* The [DoubleField] reference.
|
||||
*/
|
||||
public override val elementAlgebra: DoubleField get() = DoubleField
|
||||
override val elementAlgebra: DoubleField get() = DoubleField
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override fun Matrix<Double>.toEjml(): EjmlDoubleMatrix<DMatrixSparseCSC> = when {
|
||||
override fun Matrix<Double>.toEjml(): EjmlDoubleMatrix<DMatrixSparseCSC> = when {
|
||||
this is EjmlDoubleMatrix<*> && origin is DMatrixSparseCSC -> this as EjmlDoubleMatrix<DMatrixSparseCSC>
|
||||
else -> buildMatrix(rowNum, colNum) { i, j -> get(i, j) }
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override fun Point<Double>.toEjml(): EjmlDoubleVector<DMatrixSparseCSC> = when {
|
||||
override fun Point<Double>.toEjml(): EjmlDoubleVector<DMatrixSparseCSC> = when {
|
||||
this is EjmlDoubleVector<*> && origin is DMatrixSparseCSC -> this as EjmlDoubleVector<DMatrixSparseCSC>
|
||||
else -> EjmlDoubleVector(DMatrixSparseCSC(size, 1).also {
|
||||
(0 until it.numRows).forEach { row -> it[row, 0] = get(row) }
|
||||
})
|
||||
}
|
||||
|
||||
public override fun buildMatrix(
|
||||
override fun buildMatrix(
|
||||
rows: Int,
|
||||
columns: Int,
|
||||
initializer: DoubleField.(i: Int, j: Int) -> Double,
|
||||
@ -569,7 +569,7 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
}
|
||||
}.wrapMatrix()
|
||||
|
||||
public override fun buildVector(
|
||||
override fun buildVector(
|
||||
size: Int,
|
||||
initializer: DoubleField.(Int) -> Double,
|
||||
): EjmlDoubleVector<DMatrixSparseCSC> = EjmlDoubleVector(DMatrixSparseCSC(size, 1).also {
|
||||
@ -579,21 +579,21 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
private fun <T : DMatrix> T.wrapMatrix() = EjmlDoubleMatrix(this)
|
||||
private fun <T : DMatrix> T.wrapVector() = EjmlDoubleVector(this)
|
||||
|
||||
public override fun Matrix<Double>.unaryMinus(): Matrix<Double> = this * elementAlgebra { -one }
|
||||
override fun Matrix<Double>.unaryMinus(): Matrix<Double> = this * elementAlgebra { -one }
|
||||
|
||||
public override fun Matrix<Double>.dot(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixSparseCSC> {
|
||||
override fun Matrix<Double>.dot(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixSparseCSC> {
|
||||
val out = DMatrixSparseCSC(1, 1)
|
||||
CommonOps_DSCC.mult(toEjml().origin, other.toEjml().origin, out)
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Matrix<Double>.dot(vector: Point<Double>): EjmlDoubleVector<DMatrixSparseCSC> {
|
||||
override fun Matrix<Double>.dot(vector: Point<Double>): EjmlDoubleVector<DMatrixSparseCSC> {
|
||||
val out = DMatrixSparseCSC(1, 1)
|
||||
CommonOps_DSCC.mult(toEjml().origin, vector.toEjml().origin, out)
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override operator fun Matrix<Double>.minus(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixSparseCSC> {
|
||||
override operator fun Matrix<Double>.minus(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixSparseCSC> {
|
||||
val out = DMatrixSparseCSC(1, 1)
|
||||
|
||||
CommonOps_DSCC.add(
|
||||
@ -609,19 +609,19 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override operator fun Matrix<Double>.times(value: Double): EjmlDoubleMatrix<DMatrixSparseCSC> {
|
||||
override operator fun Matrix<Double>.times(value: Double): EjmlDoubleMatrix<DMatrixSparseCSC> {
|
||||
val res = DMatrixSparseCSC(1, 1)
|
||||
CommonOps_DSCC.scale(value, toEjml().origin, res)
|
||||
return res.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Point<Double>.unaryMinus(): EjmlDoubleVector<DMatrixSparseCSC> {
|
||||
override fun Point<Double>.unaryMinus(): EjmlDoubleVector<DMatrixSparseCSC> {
|
||||
val res = DMatrixSparseCSC(1, 1)
|
||||
CommonOps_DSCC.changeSign(toEjml().origin, res)
|
||||
return res.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Matrix<Double>.plus(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixSparseCSC> {
|
||||
override fun Matrix<Double>.plus(other: Matrix<Double>): EjmlDoubleMatrix<DMatrixSparseCSC> {
|
||||
val out = DMatrixSparseCSC(1, 1)
|
||||
|
||||
CommonOps_DSCC.add(
|
||||
@ -637,7 +637,7 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Point<Double>.plus(other: Point<Double>): EjmlDoubleVector<DMatrixSparseCSC> {
|
||||
override fun Point<Double>.plus(other: Point<Double>): EjmlDoubleVector<DMatrixSparseCSC> {
|
||||
val out = DMatrixSparseCSC(1, 1)
|
||||
|
||||
CommonOps_DSCC.add(
|
||||
@ -653,7 +653,7 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Point<Double>.minus(other: Point<Double>): EjmlDoubleVector<DMatrixSparseCSC> {
|
||||
override fun Point<Double>.minus(other: Point<Double>): EjmlDoubleVector<DMatrixSparseCSC> {
|
||||
val out = DMatrixSparseCSC(1, 1)
|
||||
|
||||
CommonOps_DSCC.add(
|
||||
@ -669,18 +669,18 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace<Double, DoubleField, DMatrix
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Double.times(m: Matrix<Double>): EjmlDoubleMatrix<DMatrixSparseCSC> = m * this
|
||||
override fun Double.times(m: Matrix<Double>): EjmlDoubleMatrix<DMatrixSparseCSC> = m * this
|
||||
|
||||
public override fun Point<Double>.times(value: Double): EjmlDoubleVector<DMatrixSparseCSC> {
|
||||
override fun Point<Double>.times(value: Double): EjmlDoubleVector<DMatrixSparseCSC> {
|
||||
val res = DMatrixSparseCSC(1, 1)
|
||||
CommonOps_DSCC.scale(value, toEjml().origin, res)
|
||||
return res.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Double.times(v: Point<Double>): EjmlDoubleVector<DMatrixSparseCSC> = v * this
|
||||
override fun Double.times(v: Point<Double>): EjmlDoubleVector<DMatrixSparseCSC> = v * this
|
||||
|
||||
@UnstableKMathAPI
|
||||
public override fun <F : StructureFeature> getFeature(structure: Matrix<Double>, type: KClass<out F>): F? {
|
||||
override fun <F : StructureFeature> getFeature(structure: Matrix<Double>, type: KClass<out F>): F? {
|
||||
structure.getFeature(type)?.let { return it }
|
||||
val origin = structure.toEjml().origin
|
||||
|
||||
@ -772,23 +772,23 @@ public object EjmlLinearSpaceFSCC : EjmlLinearSpace<Float, FloatField, FMatrixSp
|
||||
/**
|
||||
* The [FloatField] reference.
|
||||
*/
|
||||
public override val elementAlgebra: FloatField get() = FloatField
|
||||
override val elementAlgebra: FloatField get() = FloatField
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override fun Matrix<Float>.toEjml(): EjmlFloatMatrix<FMatrixSparseCSC> = when {
|
||||
override fun Matrix<Float>.toEjml(): EjmlFloatMatrix<FMatrixSparseCSC> = when {
|
||||
this is EjmlFloatMatrix<*> && origin is FMatrixSparseCSC -> this as EjmlFloatMatrix<FMatrixSparseCSC>
|
||||
else -> buildMatrix(rowNum, colNum) { i, j -> get(i, j) }
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override fun Point<Float>.toEjml(): EjmlFloatVector<FMatrixSparseCSC> = when {
|
||||
override fun Point<Float>.toEjml(): EjmlFloatVector<FMatrixSparseCSC> = when {
|
||||
this is EjmlFloatVector<*> && origin is FMatrixSparseCSC -> this as EjmlFloatVector<FMatrixSparseCSC>
|
||||
else -> EjmlFloatVector(FMatrixSparseCSC(size, 1).also {
|
||||
(0 until it.numRows).forEach { row -> it[row, 0] = get(row) }
|
||||
})
|
||||
}
|
||||
|
||||
public override fun buildMatrix(
|
||||
override fun buildMatrix(
|
||||
rows: Int,
|
||||
columns: Int,
|
||||
initializer: FloatField.(i: Int, j: Int) -> Float,
|
||||
@ -798,7 +798,7 @@ public object EjmlLinearSpaceFSCC : EjmlLinearSpace<Float, FloatField, FMatrixSp
|
||||
}
|
||||
}.wrapMatrix()
|
||||
|
||||
public override fun buildVector(
|
||||
override fun buildVector(
|
||||
size: Int,
|
||||
initializer: FloatField.(Int) -> Float,
|
||||
): EjmlFloatVector<FMatrixSparseCSC> = EjmlFloatVector(FMatrixSparseCSC(size, 1).also {
|
||||
@ -808,21 +808,21 @@ public object EjmlLinearSpaceFSCC : EjmlLinearSpace<Float, FloatField, FMatrixSp
|
||||
private fun <T : FMatrix> T.wrapMatrix() = EjmlFloatMatrix(this)
|
||||
private fun <T : FMatrix> T.wrapVector() = EjmlFloatVector(this)
|
||||
|
||||
public override fun Matrix<Float>.unaryMinus(): Matrix<Float> = this * elementAlgebra { -one }
|
||||
override fun Matrix<Float>.unaryMinus(): Matrix<Float> = this * elementAlgebra { -one }
|
||||
|
||||
public override fun Matrix<Float>.dot(other: Matrix<Float>): EjmlFloatMatrix<FMatrixSparseCSC> {
|
||||
override fun Matrix<Float>.dot(other: Matrix<Float>): EjmlFloatMatrix<FMatrixSparseCSC> {
|
||||
val out = FMatrixSparseCSC(1, 1)
|
||||
CommonOps_FSCC.mult(toEjml().origin, other.toEjml().origin, out)
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Matrix<Float>.dot(vector: Point<Float>): EjmlFloatVector<FMatrixSparseCSC> {
|
||||
override fun Matrix<Float>.dot(vector: Point<Float>): EjmlFloatVector<FMatrixSparseCSC> {
|
||||
val out = FMatrixSparseCSC(1, 1)
|
||||
CommonOps_FSCC.mult(toEjml().origin, vector.toEjml().origin, out)
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override operator fun Matrix<Float>.minus(other: Matrix<Float>): EjmlFloatMatrix<FMatrixSparseCSC> {
|
||||
override operator fun Matrix<Float>.minus(other: Matrix<Float>): EjmlFloatMatrix<FMatrixSparseCSC> {
|
||||
val out = FMatrixSparseCSC(1, 1)
|
||||
|
||||
CommonOps_FSCC.add(
|
||||
@ -838,19 +838,19 @@ public object EjmlLinearSpaceFSCC : EjmlLinearSpace<Float, FloatField, FMatrixSp
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override operator fun Matrix<Float>.times(value: Float): EjmlFloatMatrix<FMatrixSparseCSC> {
|
||||
override operator fun Matrix<Float>.times(value: Float): EjmlFloatMatrix<FMatrixSparseCSC> {
|
||||
val res = FMatrixSparseCSC(1, 1)
|
||||
CommonOps_FSCC.scale(value, toEjml().origin, res)
|
||||
return res.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Point<Float>.unaryMinus(): EjmlFloatVector<FMatrixSparseCSC> {
|
||||
override fun Point<Float>.unaryMinus(): EjmlFloatVector<FMatrixSparseCSC> {
|
||||
val res = FMatrixSparseCSC(1, 1)
|
||||
CommonOps_FSCC.changeSign(toEjml().origin, res)
|
||||
return res.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Matrix<Float>.plus(other: Matrix<Float>): EjmlFloatMatrix<FMatrixSparseCSC> {
|
||||
override fun Matrix<Float>.plus(other: Matrix<Float>): EjmlFloatMatrix<FMatrixSparseCSC> {
|
||||
val out = FMatrixSparseCSC(1, 1)
|
||||
|
||||
CommonOps_FSCC.add(
|
||||
@ -866,7 +866,7 @@ public object EjmlLinearSpaceFSCC : EjmlLinearSpace<Float, FloatField, FMatrixSp
|
||||
return out.wrapMatrix()
|
||||
}
|
||||
|
||||
public override fun Point<Float>.plus(other: Point<Float>): EjmlFloatVector<FMatrixSparseCSC> {
|
||||
override fun Point<Float>.plus(other: Point<Float>): EjmlFloatVector<FMatrixSparseCSC> {
|
||||
val out = FMatrixSparseCSC(1, 1)
|
||||
|
||||
CommonOps_FSCC.add(
|
||||
@ -882,7 +882,7 @@ public object EjmlLinearSpaceFSCC : EjmlLinearSpace<Float, FloatField, FMatrixSp
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Point<Float>.minus(other: Point<Float>): EjmlFloatVector<FMatrixSparseCSC> {
|
||||
override fun Point<Float>.minus(other: Point<Float>): EjmlFloatVector<FMatrixSparseCSC> {
|
||||
val out = FMatrixSparseCSC(1, 1)
|
||||
|
||||
CommonOps_FSCC.add(
|
||||
@ -898,18 +898,18 @@ public object EjmlLinearSpaceFSCC : EjmlLinearSpace<Float, FloatField, FMatrixSp
|
||||
return out.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Float.times(m: Matrix<Float>): EjmlFloatMatrix<FMatrixSparseCSC> = m * this
|
||||
override fun Float.times(m: Matrix<Float>): EjmlFloatMatrix<FMatrixSparseCSC> = m * this
|
||||
|
||||
public override fun Point<Float>.times(value: Float): EjmlFloatVector<FMatrixSparseCSC> {
|
||||
override fun Point<Float>.times(value: Float): EjmlFloatVector<FMatrixSparseCSC> {
|
||||
val res = FMatrixSparseCSC(1, 1)
|
||||
CommonOps_FSCC.scale(value, toEjml().origin, res)
|
||||
return res.wrapVector()
|
||||
}
|
||||
|
||||
public override fun Float.times(v: Point<Float>): EjmlFloatVector<FMatrixSparseCSC> = v * this
|
||||
override fun Float.times(v: Point<Float>): EjmlFloatVector<FMatrixSparseCSC> = v * this
|
||||
|
||||
@UnstableKMathAPI
|
||||
public override fun <F : StructureFeature> getFeature(structure: Matrix<Float>, type: KClass<out F>): F? {
|
||||
override fun <F : StructureFeature> getFeature(structure: Matrix<Float>, type: KClass<out F>): F? {
|
||||
structure.getFeature(type)?.let { return it }
|
||||
val origin = structure.toEjml().origin
|
||||
|
||||
|
@ -30,7 +30,7 @@ public fun interface Piecewise<in T, out R> {
|
||||
public interface PiecewisePolynomial<T : Comparable<T>> : Piecewise<T, Polynomial<T>> {
|
||||
public val pieces: Collection<Pair<ClosedRange<T>, Polynomial<T>>>
|
||||
|
||||
public override fun findPiece(arg: T): Polynomial<T>?
|
||||
override fun findPiece(arg: T): Polynomial<T>?
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -98,13 +98,13 @@ public fun <T : Comparable<T>> Polynomial<T>.integrate(
|
||||
public class PolynomialSpace<T, C>(
|
||||
private val ring: C,
|
||||
) : Group<Polynomial<T>>, ScaleOperations<Polynomial<T>> where C : Ring<T>, C : ScaleOperations<T> {
|
||||
public override val zero: Polynomial<T> = Polynomial(emptyList())
|
||||
override val zero: Polynomial<T> = Polynomial(emptyList())
|
||||
|
||||
override fun Polynomial<T>.unaryMinus(): Polynomial<T> = ring {
|
||||
Polynomial(coefficients.map { -it })
|
||||
}
|
||||
|
||||
public override fun add(a: Polynomial<T>, b: Polynomial<T>): Polynomial<T> {
|
||||
override fun add(a: Polynomial<T>, b: Polynomial<T>): Polynomial<T> {
|
||||
val dim = max(a.coefficients.size, b.coefficients.size)
|
||||
|
||||
return ring {
|
||||
@ -114,7 +114,7 @@ public class PolynomialSpace<T, C>(
|
||||
}
|
||||
}
|
||||
|
||||
public override fun scale(a: Polynomial<T>, value: Double): Polynomial<T> =
|
||||
override fun scale(a: Polynomial<T>, value: Double): Polynomial<T> =
|
||||
ring { Polynomial(List(a.coefficients.size) { index -> a.coefficients[index] * value }) }
|
||||
|
||||
/**
|
||||
|
@ -21,9 +21,9 @@ internal fun <T : Comparable<T>> insureSorted(points: XYColumnarData<*, T, *>) {
|
||||
/**
|
||||
* Reference JVM implementation: https://github.com/apache/commons-math/blob/master/src/main/java/org/apache/commons/math4/analysis/interpolation/LinearInterpolator.java
|
||||
*/
|
||||
public class LinearInterpolator<T : Comparable<T>>(public override val algebra: Field<T>) : PolynomialInterpolator<T> {
|
||||
public class LinearInterpolator<T : Comparable<T>>(override val algebra: Field<T>) : PolynomialInterpolator<T> {
|
||||
@OptIn(UnstableKMathAPI::class)
|
||||
public override fun interpolatePolynomials(points: XYColumnarData<T, T, T>): PiecewisePolynomial<T> = algebra {
|
||||
override fun interpolatePolynomials(points: XYColumnarData<T, T, T>): PiecewisePolynomial<T> = algebra {
|
||||
require(points.size > 0) { "Point array should not be empty" }
|
||||
insureSorted(points)
|
||||
|
||||
|
@ -23,13 +23,13 @@ import space.kscience.kmath.structures.MutableBufferFactory
|
||||
* https://github.com/apache/commons-math/blob/eb57d6d457002a0bb5336d789a3381a24599affe/src/main/java/org/apache/commons/math4/analysis/interpolation/SplineInterpolator.java
|
||||
*/
|
||||
public class SplineInterpolator<T : Comparable<T>>(
|
||||
public override val algebra: Field<T>,
|
||||
override val algebra: Field<T>,
|
||||
public val bufferFactory: MutableBufferFactory<T>,
|
||||
) : PolynomialInterpolator<T> {
|
||||
//TODO possibly optimize zeroed buffers
|
||||
|
||||
@OptIn(UnstableKMathAPI::class)
|
||||
public override fun interpolatePolynomials(points: XYColumnarData<T, T, T>): PiecewisePolynomial<T> = algebra {
|
||||
override fun interpolatePolynomials(points: XYColumnarData<T, T, T>): PiecewisePolynomial<T> = algebra {
|
||||
require(points.size >= 3) { "Can't use spline interpolator with less than 3 points" }
|
||||
insureSorted(points)
|
||||
// Number of intervals. The number of data points is n + 1.
|
||||
|
@ -15,15 +15,15 @@ import kotlin.math.sqrt
|
||||
public interface Vector2D : Point<Double>, Vector{
|
||||
public val x: Double
|
||||
public val y: Double
|
||||
public override val size: Int get() = 2
|
||||
override val size: Int get() = 2
|
||||
|
||||
public override operator fun get(index: Int): Double = when (index) {
|
||||
override operator fun get(index: Int): Double = when (index) {
|
||||
1 -> x
|
||||
2 -> y
|
||||
else -> error("Accessing outside of point bounds")
|
||||
}
|
||||
|
||||
public override operator fun iterator(): Iterator<Double> = listOf(x, y).iterator()
|
||||
override operator fun iterator(): Iterator<Double> = listOf(x, y).iterator()
|
||||
}
|
||||
|
||||
public val Vector2D.r: Double
|
||||
@ -41,13 +41,13 @@ private data class Vector2DImpl(
|
||||
* 2D Euclidean space
|
||||
*/
|
||||
public object Euclidean2DSpace : GeometrySpace<Vector2D>, ScaleOperations<Vector2D> {
|
||||
public override val zero: Vector2D by lazy { Vector2D(0.0, 0.0) }
|
||||
override val zero: Vector2D by lazy { Vector2D(0.0, 0.0) }
|
||||
|
||||
public fun Vector2D.norm(): Double = sqrt(x * x + y * y)
|
||||
override fun Vector2D.unaryMinus(): Vector2D = Vector2D(-x, -y)
|
||||
|
||||
public override fun Vector2D.distanceTo(other: Vector2D): Double = (this - other).norm()
|
||||
public override fun add(a: Vector2D, b: Vector2D): Vector2D = Vector2D(a.x + b.x, a.y + b.y)
|
||||
public override fun scale(a: Vector2D, value: Double): Vector2D = Vector2D(a.x * value, a.y * value)
|
||||
public override fun Vector2D.dot(other: Vector2D): Double = x * other.x + y * other.y
|
||||
override fun Vector2D.distanceTo(other: Vector2D): Double = (this - other).norm()
|
||||
override fun add(a: Vector2D, b: Vector2D): Vector2D = Vector2D(a.x + b.x, a.y + b.y)
|
||||
override fun scale(a: Vector2D, value: Double): Vector2D = Vector2D(a.x * value, a.y * value)
|
||||
override fun Vector2D.dot(other: Vector2D): Double = x * other.x + y * other.y
|
||||
}
|
||||
|
@ -16,16 +16,16 @@ public interface Vector3D : Point<Double>, Vector {
|
||||
public val x: Double
|
||||
public val y: Double
|
||||
public val z: Double
|
||||
public override val size: Int get() = 3
|
||||
override val size: Int get() = 3
|
||||
|
||||
public override operator fun get(index: Int): Double = when (index) {
|
||||
override operator fun get(index: Int): Double = when (index) {
|
||||
1 -> x
|
||||
2 -> y
|
||||
3 -> z
|
||||
else -> error("Accessing outside of point bounds")
|
||||
}
|
||||
|
||||
public override operator fun iterator(): Iterator<Double> = listOf(x, y, z).iterator()
|
||||
override operator fun iterator(): Iterator<Double> = listOf(x, y, z).iterator()
|
||||
}
|
||||
|
||||
@Suppress("FunctionName")
|
||||
@ -40,19 +40,19 @@ private data class Vector3DImpl(
|
||||
) : Vector3D
|
||||
|
||||
public object Euclidean3DSpace : GeometrySpace<Vector3D>, ScaleOperations<Vector3D> {
|
||||
public override val zero: Vector3D by lazy { Vector3D(0.0, 0.0, 0.0) }
|
||||
override val zero: Vector3D by lazy { Vector3D(0.0, 0.0, 0.0) }
|
||||
|
||||
public fun Vector3D.norm(): Double = sqrt(x * x + y * y + z * z)
|
||||
override fun Vector3D.unaryMinus(): Vector3D = Vector3D(-x, -y, -z)
|
||||
|
||||
public override fun Vector3D.distanceTo(other: Vector3D): Double = (this - other).norm()
|
||||
override fun Vector3D.distanceTo(other: Vector3D): Double = (this - other).norm()
|
||||
|
||||
public override fun add(a: Vector3D, b: Vector3D): Vector3D =
|
||||
override fun add(a: Vector3D, b: Vector3D): Vector3D =
|
||||
Vector3D(a.x + b.x, a.y + b.y, a.z + b.z)
|
||||
|
||||
public override fun scale(a: Vector3D, value: Double): Vector3D =
|
||||
override fun scale(a: Vector3D, value: Double): Vector3D =
|
||||
Vector3D(a.x * value, a.y * value, a.z * value)
|
||||
|
||||
public override fun Vector3D.dot(other: Vector3D): Double =
|
||||
override fun Vector3D.dot(other: Vector3D): Double =
|
||||
x * other.x + y * other.y + z * other.z
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ import space.kscience.kmath.operations.invoke
|
||||
*/
|
||||
public data class DomainBin<in T : Comparable<T>>(
|
||||
public val domain: Domain<T>,
|
||||
public override val value: Number,
|
||||
override val value: Number,
|
||||
) : Bin<T>, Domain<T> by domain
|
||||
|
||||
@OptIn(UnstableKMathAPI::class)
|
||||
|
@ -27,15 +27,15 @@ public class UnivariateBin(
|
||||
public val standardDeviation: Double,
|
||||
) : Bin<Double>, ClosedFloatingPointRange<Double> by domain.range {
|
||||
|
||||
public override val dimension: Int get() = 1
|
||||
override val dimension: Int get() = 1
|
||||
|
||||
public override fun contains(point: Buffer<Double>): Boolean = point.size == 1 && contains(point[0])
|
||||
override fun contains(point: Buffer<Double>): Boolean = point.size == 1 && contains(point[0])
|
||||
}
|
||||
|
||||
@OptIn(UnstableKMathAPI::class)
|
||||
public interface UnivariateHistogram : Histogram<Double, UnivariateBin>{
|
||||
public operator fun get(value: Double): UnivariateBin?
|
||||
public override operator fun get(point: Buffer<Double>): UnivariateBin? = get(point[0])
|
||||
override operator fun get(point: Buffer<Double>): UnivariateBin? = get(point[0])
|
||||
|
||||
public companion object {
|
||||
/**
|
||||
|
@ -12,50 +12,50 @@ import space.kscience.kmath.operations.ScaleOperations
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||
public object JafamaDoubleField : ExtendedField<Double>, Norm<Double, Double>, ScaleOperations<Double> {
|
||||
public override inline val zero: Double get() = 0.0
|
||||
public override inline val one: Double get() = 1.0
|
||||
override inline val zero: Double get() = 0.0
|
||||
override inline val one: Double get() = 1.0
|
||||
|
||||
public override inline fun number(value: Number): Double = value.toDouble()
|
||||
override inline fun number(value: Number): Double = value.toDouble()
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: Double, right: Double) -> Double =
|
||||
override fun binaryOperationFunction(operation: String): (left: Double, right: Double) -> Double =
|
||||
when (operation) {
|
||||
PowerOperations.POW_OPERATION -> ::power
|
||||
else -> super<ExtendedField>.binaryOperationFunction(operation)
|
||||
}
|
||||
|
||||
public override inline fun add(a: Double, b: Double): Double = a + b
|
||||
override inline fun add(a: Double, b: Double): Double = a + b
|
||||
|
||||
public override inline fun multiply(a: Double, b: Double): Double = a * b
|
||||
public override inline fun divide(a: Double, b: Double): Double = a / b
|
||||
override inline fun multiply(a: Double, b: Double): Double = a * b
|
||||
override inline fun divide(a: Double, b: Double): Double = a / b
|
||||
|
||||
public override inline fun scale(a: Double, value: Double): Double = a * value
|
||||
override inline fun scale(a: Double, value: Double): Double = a * value
|
||||
|
||||
public override inline fun sin(arg: Double): Double = FastMath.sin(arg)
|
||||
public override inline fun cos(arg: Double): Double = FastMath.cos(arg)
|
||||
public override inline fun tan(arg: Double): Double = FastMath.tan(arg)
|
||||
public override inline fun acos(arg: Double): Double = FastMath.acos(arg)
|
||||
public override inline fun asin(arg: Double): Double = FastMath.asin(arg)
|
||||
public override inline fun atan(arg: Double): Double = FastMath.atan(arg)
|
||||
override inline fun sin(arg: Double): Double = FastMath.sin(arg)
|
||||
override inline fun cos(arg: Double): Double = FastMath.cos(arg)
|
||||
override inline fun tan(arg: Double): Double = FastMath.tan(arg)
|
||||
override inline fun acos(arg: Double): Double = FastMath.acos(arg)
|
||||
override inline fun asin(arg: Double): Double = FastMath.asin(arg)
|
||||
override inline fun atan(arg: Double): Double = FastMath.atan(arg)
|
||||
|
||||
public override inline fun sinh(arg: Double): Double = FastMath.sinh(arg)
|
||||
public override inline fun cosh(arg: Double): Double = FastMath.cosh(arg)
|
||||
public override inline fun tanh(arg: Double): Double = FastMath.tanh(arg)
|
||||
public override inline fun asinh(arg: Double): Double = FastMath.asinh(arg)
|
||||
public override inline fun acosh(arg: Double): Double = FastMath.acosh(arg)
|
||||
public override inline fun atanh(arg: Double): Double = FastMath.atanh(arg)
|
||||
override inline fun sinh(arg: Double): Double = FastMath.sinh(arg)
|
||||
override inline fun cosh(arg: Double): Double = FastMath.cosh(arg)
|
||||
override inline fun tanh(arg: Double): Double = FastMath.tanh(arg)
|
||||
override inline fun asinh(arg: Double): Double = FastMath.asinh(arg)
|
||||
override inline fun acosh(arg: Double): Double = FastMath.acosh(arg)
|
||||
override inline fun atanh(arg: Double): Double = FastMath.atanh(arg)
|
||||
|
||||
public override inline fun sqrt(arg: Double): Double = FastMath.sqrt(arg)
|
||||
public override inline fun power(arg: Double, pow: Number): Double = FastMath.pow(arg, pow.toDouble())
|
||||
public override inline fun exp(arg: Double): Double = FastMath.exp(arg)
|
||||
public override inline fun ln(arg: Double): Double = FastMath.log(arg)
|
||||
override inline fun sqrt(arg: Double): Double = FastMath.sqrt(arg)
|
||||
override inline fun power(arg: Double, pow: Number): Double = FastMath.pow(arg, pow.toDouble())
|
||||
override inline fun exp(arg: Double): Double = FastMath.exp(arg)
|
||||
override inline fun ln(arg: Double): Double = FastMath.log(arg)
|
||||
|
||||
public override inline fun norm(arg: Double): Double = FastMath.abs(arg)
|
||||
override inline fun norm(arg: Double): Double = FastMath.abs(arg)
|
||||
|
||||
public override inline fun Double.unaryMinus(): Double = -this
|
||||
public override inline fun Double.plus(b: Double): Double = this + b
|
||||
public override inline fun Double.minus(b: Double): Double = this - b
|
||||
public override inline fun Double.times(b: Double): Double = this * b
|
||||
public override inline fun Double.div(b: Double): Double = this / b
|
||||
override inline fun Double.unaryMinus(): Double = -this
|
||||
override inline fun Double.plus(b: Double): Double = this + b
|
||||
override inline fun Double.minus(b: Double): Double = this - b
|
||||
override inline fun Double.times(b: Double): Double = this * b
|
||||
override inline fun Double.div(b: Double): Double = this / b
|
||||
}
|
||||
|
||||
/**
|
||||
@ -63,48 +63,48 @@ public object JafamaDoubleField : ExtendedField<Double>, Norm<Double, Double>, S
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||
public object StrictJafamaDoubleField : ExtendedField<Double>, Norm<Double, Double>, ScaleOperations<Double> {
|
||||
public override inline val zero: Double get() = 0.0
|
||||
public override inline val one: Double get() = 1.0
|
||||
override inline val zero: Double get() = 0.0
|
||||
override inline val one: Double get() = 1.0
|
||||
|
||||
public override inline fun number(value: Number): Double = value.toDouble()
|
||||
override inline fun number(value: Number): Double = value.toDouble()
|
||||
|
||||
public override fun binaryOperationFunction(operation: String): (left: Double, right: Double) -> Double =
|
||||
override fun binaryOperationFunction(operation: String): (left: Double, right: Double) -> Double =
|
||||
when (operation) {
|
||||
PowerOperations.POW_OPERATION -> ::power
|
||||
else -> super<ExtendedField>.binaryOperationFunction(operation)
|
||||
}
|
||||
|
||||
public override inline fun add(a: Double, b: Double): Double = a + b
|
||||
override inline fun add(a: Double, b: Double): Double = a + b
|
||||
|
||||
public override inline fun multiply(a: Double, b: Double): Double = a * b
|
||||
public override inline fun divide(a: Double, b: Double): Double = a / b
|
||||
override inline fun multiply(a: Double, b: Double): Double = a * b
|
||||
override inline fun divide(a: Double, b: Double): Double = a / b
|
||||
|
||||
public override inline fun scale(a: Double, value: Double): Double = a * value
|
||||
override inline fun scale(a: Double, value: Double): Double = a * value
|
||||
|
||||
public override inline fun sin(arg: Double): Double = StrictFastMath.sin(arg)
|
||||
public override inline fun cos(arg: Double): Double = StrictFastMath.cos(arg)
|
||||
public override inline fun tan(arg: Double): Double = StrictFastMath.tan(arg)
|
||||
public override inline fun acos(arg: Double): Double = StrictFastMath.acos(arg)
|
||||
public override inline fun asin(arg: Double): Double = StrictFastMath.asin(arg)
|
||||
public override inline fun atan(arg: Double): Double = StrictFastMath.atan(arg)
|
||||
override inline fun sin(arg: Double): Double = StrictFastMath.sin(arg)
|
||||
override inline fun cos(arg: Double): Double = StrictFastMath.cos(arg)
|
||||
override inline fun tan(arg: Double): Double = StrictFastMath.tan(arg)
|
||||
override inline fun acos(arg: Double): Double = StrictFastMath.acos(arg)
|
||||
override inline fun asin(arg: Double): Double = StrictFastMath.asin(arg)
|
||||
override inline fun atan(arg: Double): Double = StrictFastMath.atan(arg)
|
||||
|
||||
public override inline fun sinh(arg: Double): Double = StrictFastMath.sinh(arg)
|
||||
public override inline fun cosh(arg: Double): Double = StrictFastMath.cosh(arg)
|
||||
public override inline fun tanh(arg: Double): Double = StrictFastMath.tanh(arg)
|
||||
public override inline fun asinh(arg: Double): Double = StrictFastMath.asinh(arg)
|
||||
public override inline fun acosh(arg: Double): Double = StrictFastMath.acosh(arg)
|
||||
public override inline fun atanh(arg: Double): Double = StrictFastMath.atanh(arg)
|
||||
override inline fun sinh(arg: Double): Double = StrictFastMath.sinh(arg)
|
||||
override inline fun cosh(arg: Double): Double = StrictFastMath.cosh(arg)
|
||||
override inline fun tanh(arg: Double): Double = StrictFastMath.tanh(arg)
|
||||
override inline fun asinh(arg: Double): Double = StrictFastMath.asinh(arg)
|
||||
override inline fun acosh(arg: Double): Double = StrictFastMath.acosh(arg)
|
||||
override inline fun atanh(arg: Double): Double = StrictFastMath.atanh(arg)
|
||||
|
||||
public override inline fun sqrt(arg: Double): Double = StrictFastMath.sqrt(arg)
|
||||
public override inline fun power(arg: Double, pow: Number): Double = StrictFastMath.pow(arg, pow.toDouble())
|
||||
public override inline fun exp(arg: Double): Double = StrictFastMath.exp(arg)
|
||||
public override inline fun ln(arg: Double): Double = StrictFastMath.log(arg)
|
||||
override inline fun sqrt(arg: Double): Double = StrictFastMath.sqrt(arg)
|
||||
override inline fun power(arg: Double, pow: Number): Double = StrictFastMath.pow(arg, pow.toDouble())
|
||||
override inline fun exp(arg: Double): Double = StrictFastMath.exp(arg)
|
||||
override inline fun ln(arg: Double): Double = StrictFastMath.log(arg)
|
||||
|
||||
public override inline fun norm(arg: Double): Double = StrictFastMath.abs(arg)
|
||||
override inline fun norm(arg: Double): Double = StrictFastMath.abs(arg)
|
||||
|
||||
public override inline fun Double.unaryMinus(): Double = -this
|
||||
public override inline fun Double.plus(b: Double): Double = this + b
|
||||
public override inline fun Double.minus(b: Double): Double = this - b
|
||||
public override inline fun Double.times(b: Double): Double = this * b
|
||||
public override inline fun Double.div(b: Double): Double = this / b
|
||||
override inline fun Double.unaryMinus(): Double = -this
|
||||
override inline fun Double.plus(b: Double): Double = this + b
|
||||
override inline fun Double.minus(b: Double): Double = this - b
|
||||
override inline fun Double.times(b: Double): Double = this * b
|
||||
override inline fun Double.div(b: Double): Double = this / b
|
||||
}
|
||||
|
@ -16,15 +16,15 @@ import space.kscience.kmath.operations.NumericAlgebra
|
||||
* @property algebra The algebra.
|
||||
* @property value The value of this number.
|
||||
*/
|
||||
public class KMathNumber<T, A>(public val algebra: A, public override val value: T) :
|
||||
public class KMathNumber<T, A>(public val algebra: A, override val value: T) :
|
||||
SConst<KMathNumber<T, A>>(value) where T : Number, A : NumericAlgebra<T> {
|
||||
/**
|
||||
* Returns a string representation of the [value].
|
||||
*/
|
||||
public override fun toString(): String = value.toString()
|
||||
override fun toString(): String = value.toString()
|
||||
|
||||
/**
|
||||
* Wraps [Number] to [KMathNumber].
|
||||
*/
|
||||
public override fun wrap(number: Number): KMathNumber<T, A> = KMathNumber(algebra, algebra.number(number))
|
||||
override fun wrap(number: Number): KMathNumber<T, A> = KMathNumber(algebra, algebra.number(number))
|
||||
}
|
||||
|
@ -25,9 +25,9 @@ public class KotlingradExpression<T : Number, A : NumericAlgebra<T>>(
|
||||
public val algebra: A,
|
||||
public val mst: MST,
|
||||
) : SpecialDifferentiableExpression<T, KotlingradExpression<T, A>> {
|
||||
public override fun invoke(arguments: Map<Symbol, T>): T = mst.interpret(algebra, arguments)
|
||||
override fun invoke(arguments: Map<Symbol, T>): T = mst.interpret(algebra, arguments)
|
||||
|
||||
public override fun derivativeOrNull(symbols: List<Symbol>): KotlingradExpression<T, A> =
|
||||
override fun derivativeOrNull(symbols: List<Symbol>): KotlingradExpression<T, A> =
|
||||
KotlingradExpression(
|
||||
algebra,
|
||||
symbols.map(Symbol::identity)
|
||||
|
@ -39,20 +39,20 @@ public sealed interface Nd4jArrayAlgebra<T, out C : Algebra<T>> : AlgebraND<T, C
|
||||
*/
|
||||
public val StructureND<T>.ndArray: INDArray
|
||||
|
||||
public override fun produce(initializer: C.(IntArray) -> T): Nd4jArrayStructure<T> {
|
||||
override fun produce(initializer: C.(IntArray) -> T): Nd4jArrayStructure<T> {
|
||||
val struct = Nd4j.create(*shape)!!.wrap()
|
||||
struct.indicesIterator().forEach { struct[it] = elementContext.initializer(it) }
|
||||
return struct
|
||||
}
|
||||
|
||||
@PerformancePitfall
|
||||
public override fun StructureND<T>.map(transform: C.(T) -> T): Nd4jArrayStructure<T> {
|
||||
override fun StructureND<T>.map(transform: C.(T) -> T): Nd4jArrayStructure<T> {
|
||||
val newStruct = ndArray.dup().wrap()
|
||||
newStruct.elements().forEach { (idx, value) -> newStruct[idx] = elementContext.transform(value) }
|
||||
return newStruct
|
||||
}
|
||||
|
||||
public override fun StructureND<T>.mapIndexed(
|
||||
override fun StructureND<T>.mapIndexed(
|
||||
transform: C.(index: IntArray, T) -> T,
|
||||
): Nd4jArrayStructure<T> {
|
||||
val new = Nd4j.create(*this@Nd4jArrayAlgebra.shape).wrap()
|
||||
@ -60,7 +60,7 @@ public sealed interface Nd4jArrayAlgebra<T, out C : Algebra<T>> : AlgebraND<T, C
|
||||
return new
|
||||
}
|
||||
|
||||
public override fun combine(
|
||||
override fun combine(
|
||||
a: StructureND<T>,
|
||||
b: StructureND<T>,
|
||||
transform: C.(T, T) -> T,
|
||||
@ -79,16 +79,16 @@ public sealed interface Nd4jArrayAlgebra<T, out C : Algebra<T>> : AlgebraND<T, C
|
||||
*/
|
||||
public sealed interface Nd4jArrayGroup<T, out S : Ring<T>> : GroupND<T, S>, Nd4jArrayAlgebra<T, S> {
|
||||
|
||||
public override val zero: Nd4jArrayStructure<T>
|
||||
override val zero: Nd4jArrayStructure<T>
|
||||
get() = Nd4j.zeros(*shape).wrap()
|
||||
|
||||
public override fun add(a: StructureND<T>, b: StructureND<T>): Nd4jArrayStructure<T> =
|
||||
override fun add(a: StructureND<T>, b: StructureND<T>): Nd4jArrayStructure<T> =
|
||||
a.ndArray.add(b.ndArray).wrap()
|
||||
|
||||
public override operator fun StructureND<T>.minus(b: StructureND<T>): Nd4jArrayStructure<T> =
|
||||
override operator fun StructureND<T>.minus(b: StructureND<T>): Nd4jArrayStructure<T> =
|
||||
ndArray.sub(b.ndArray).wrap()
|
||||
|
||||
public override operator fun StructureND<T>.unaryMinus(): Nd4jArrayStructure<T> =
|
||||
override operator fun StructureND<T>.unaryMinus(): Nd4jArrayStructure<T> =
|
||||
ndArray.neg().wrap()
|
||||
|
||||
public fun multiply(a: StructureND<T>, k: Number): Nd4jArrayStructure<T> =
|
||||
@ -104,23 +104,23 @@ public sealed interface Nd4jArrayGroup<T, out S : Ring<T>> : GroupND<T, S>, Nd4j
|
||||
@OptIn(UnstableKMathAPI::class)
|
||||
public sealed interface Nd4jArrayRing<T, out R : Ring<T>> : RingND<T, R>, Nd4jArrayGroup<T, R> {
|
||||
|
||||
public override val one: Nd4jArrayStructure<T>
|
||||
override val one: Nd4jArrayStructure<T>
|
||||
get() = Nd4j.ones(*shape).wrap()
|
||||
|
||||
public override fun multiply(a: StructureND<T>, b: StructureND<T>): Nd4jArrayStructure<T> =
|
||||
override fun multiply(a: StructureND<T>, b: StructureND<T>): Nd4jArrayStructure<T> =
|
||||
a.ndArray.mul(b.ndArray).wrap()
|
||||
//
|
||||
// public override operator fun Nd4jArrayStructure<T>.minus(b: Number): Nd4jArrayStructure<T> {
|
||||
// override operator fun Nd4jArrayStructure<T>.minus(b: Number): Nd4jArrayStructure<T> {
|
||||
// check(this)
|
||||
// return ndArray.sub(b).wrap()
|
||||
// }
|
||||
//
|
||||
// public override operator fun Nd4jArrayStructure<T>.plus(b: Number): Nd4jArrayStructure<T> {
|
||||
// override operator fun Nd4jArrayStructure<T>.plus(b: Number): Nd4jArrayStructure<T> {
|
||||
// check(this)
|
||||
// return ndArray.add(b).wrap()
|
||||
// }
|
||||
//
|
||||
// public override operator fun Number.minus(b: Nd4jArrayStructure<T>): Nd4jArrayStructure<T> {
|
||||
// override operator fun Number.minus(b: Nd4jArrayStructure<T>): Nd4jArrayStructure<T> {
|
||||
// check(b)
|
||||
// return b.ndArray.rsub(this).wrap()
|
||||
// }
|
||||
@ -153,7 +153,7 @@ public sealed interface Nd4jArrayRing<T, out R : Ring<T>> : RingND<T, R>, Nd4jAr
|
||||
* @param F the type field of structure elements.
|
||||
*/
|
||||
public sealed interface Nd4jArrayField<T, out F : Field<T>> : FieldND<T, F>, Nd4jArrayRing<T, F> {
|
||||
public override fun divide(a: StructureND<T>, b: StructureND<T>): Nd4jArrayStructure<T> =
|
||||
override fun divide(a: StructureND<T>, b: StructureND<T>): Nd4jArrayStructure<T> =
|
||||
a.ndArray.div(b.ndArray).wrap()
|
||||
|
||||
public operator fun Number.div(b: StructureND<T>): Nd4jArrayStructure<T> = b.ndArray.rdiv(this).wrap()
|
||||
@ -194,38 +194,38 @@ public sealed interface Nd4jArrayField<T, out F : Field<T>> : FieldND<T, F>, Nd4
|
||||
*/
|
||||
public sealed interface Nd4jArrayExtendedField<T, out F : ExtendedField<T>> : ExtendedField<StructureND<T>>,
|
||||
Nd4jArrayField<T, F> {
|
||||
public override fun sin(arg: StructureND<T>): StructureND<T> = Transforms.sin(arg.ndArray).wrap()
|
||||
public override fun cos(arg: StructureND<T>): StructureND<T> = Transforms.cos(arg.ndArray).wrap()
|
||||
public override fun asin(arg: StructureND<T>): StructureND<T> = Transforms.asin(arg.ndArray).wrap()
|
||||
public override fun acos(arg: StructureND<T>): StructureND<T> = Transforms.acos(arg.ndArray).wrap()
|
||||
public override fun atan(arg: StructureND<T>): StructureND<T> = Transforms.atan(arg.ndArray).wrap()
|
||||
override fun sin(arg: StructureND<T>): StructureND<T> = Transforms.sin(arg.ndArray).wrap()
|
||||
override fun cos(arg: StructureND<T>): StructureND<T> = Transforms.cos(arg.ndArray).wrap()
|
||||
override fun asin(arg: StructureND<T>): StructureND<T> = Transforms.asin(arg.ndArray).wrap()
|
||||
override fun acos(arg: StructureND<T>): StructureND<T> = Transforms.acos(arg.ndArray).wrap()
|
||||
override fun atan(arg: StructureND<T>): StructureND<T> = Transforms.atan(arg.ndArray).wrap()
|
||||
|
||||
public override fun power(arg: StructureND<T>, pow: Number): StructureND<T> =
|
||||
override fun power(arg: StructureND<T>, pow: Number): StructureND<T> =
|
||||
Transforms.pow(arg.ndArray, pow).wrap()
|
||||
|
||||
public override fun exp(arg: StructureND<T>): StructureND<T> = Transforms.exp(arg.ndArray).wrap()
|
||||
public override fun ln(arg: StructureND<T>): StructureND<T> = Transforms.log(arg.ndArray).wrap()
|
||||
public override fun sqrt(arg: StructureND<T>): StructureND<T> = Transforms.sqrt(arg.ndArray).wrap()
|
||||
public override fun sinh(arg: StructureND<T>): StructureND<T> = Transforms.sinh(arg.ndArray).wrap()
|
||||
public override fun cosh(arg: StructureND<T>): StructureND<T> = Transforms.cosh(arg.ndArray).wrap()
|
||||
public override fun tanh(arg: StructureND<T>): StructureND<T> = Transforms.tanh(arg.ndArray).wrap()
|
||||
override fun exp(arg: StructureND<T>): StructureND<T> = Transforms.exp(arg.ndArray).wrap()
|
||||
override fun ln(arg: StructureND<T>): StructureND<T> = Transforms.log(arg.ndArray).wrap()
|
||||
override fun sqrt(arg: StructureND<T>): StructureND<T> = Transforms.sqrt(arg.ndArray).wrap()
|
||||
override fun sinh(arg: StructureND<T>): StructureND<T> = Transforms.sinh(arg.ndArray).wrap()
|
||||
override fun cosh(arg: StructureND<T>): StructureND<T> = Transforms.cosh(arg.ndArray).wrap()
|
||||
override fun tanh(arg: StructureND<T>): StructureND<T> = Transforms.tanh(arg.ndArray).wrap()
|
||||
|
||||
public override fun asinh(arg: StructureND<T>): StructureND<T> =
|
||||
override fun asinh(arg: StructureND<T>): StructureND<T> =
|
||||
Nd4j.getExecutioner().exec(ASinh(arg.ndArray, arg.ndArray.ulike())).wrap()
|
||||
|
||||
public override fun acosh(arg: StructureND<T>): StructureND<T> =
|
||||
override fun acosh(arg: StructureND<T>): StructureND<T> =
|
||||
Nd4j.getExecutioner().exec(ACosh(arg.ndArray, arg.ndArray.ulike())).wrap()
|
||||
|
||||
public override fun atanh(arg: StructureND<T>): StructureND<T> = Transforms.atanh(arg.ndArray).wrap()
|
||||
override fun atanh(arg: StructureND<T>): StructureND<T> = Transforms.atanh(arg.ndArray).wrap()
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents [FieldND] over [Nd4jArrayDoubleStructure].
|
||||
*/
|
||||
public class DoubleNd4jArrayField(public override val shape: IntArray) : Nd4jArrayExtendedField<Double, DoubleField> {
|
||||
public override val elementContext: DoubleField get() = DoubleField
|
||||
public class DoubleNd4jArrayField(override val shape: IntArray) : Nd4jArrayExtendedField<Double, DoubleField> {
|
||||
override val elementContext: DoubleField get() = DoubleField
|
||||
|
||||
public override fun INDArray.wrap(): Nd4jArrayStructure<Double> = checkShape(this).asDoubleStructure()
|
||||
override fun INDArray.wrap(): Nd4jArrayStructure<Double> = checkShape(this).asDoubleStructure()
|
||||
|
||||
@OptIn(PerformancePitfall::class)
|
||||
override val StructureND<Double>.ndArray: INDArray
|
||||
@ -240,27 +240,27 @@ public class DoubleNd4jArrayField(public override val shape: IntArray) : Nd4jArr
|
||||
return a.ndArray.mul(value).wrap()
|
||||
}
|
||||
|
||||
public override operator fun StructureND<Double>.div(arg: Double): Nd4jArrayStructure<Double> {
|
||||
override operator fun StructureND<Double>.div(arg: Double): Nd4jArrayStructure<Double> {
|
||||
return ndArray.div(arg).wrap()
|
||||
}
|
||||
|
||||
public override operator fun StructureND<Double>.plus(arg: Double): Nd4jArrayStructure<Double> {
|
||||
override operator fun StructureND<Double>.plus(arg: Double): Nd4jArrayStructure<Double> {
|
||||
return ndArray.add(arg).wrap()
|
||||
}
|
||||
|
||||
public override operator fun StructureND<Double>.minus(arg: Double): Nd4jArrayStructure<Double> {
|
||||
override operator fun StructureND<Double>.minus(arg: Double): Nd4jArrayStructure<Double> {
|
||||
return ndArray.sub(arg).wrap()
|
||||
}
|
||||
|
||||
public override operator fun StructureND<Double>.times(arg: Double): Nd4jArrayStructure<Double> {
|
||||
override operator fun StructureND<Double>.times(arg: Double): Nd4jArrayStructure<Double> {
|
||||
return ndArray.mul(arg).wrap()
|
||||
}
|
||||
|
||||
public override operator fun Double.div(arg: StructureND<Double>): Nd4jArrayStructure<Double> {
|
||||
override operator fun Double.div(arg: StructureND<Double>): Nd4jArrayStructure<Double> {
|
||||
return arg.ndArray.rdiv(this).wrap()
|
||||
}
|
||||
|
||||
public override operator fun Double.minus(arg: StructureND<Double>): Nd4jArrayStructure<Double> {
|
||||
override operator fun Double.minus(arg: StructureND<Double>): Nd4jArrayStructure<Double> {
|
||||
return arg.ndArray.rsub(this).wrap()
|
||||
}
|
||||
}
|
||||
@ -268,13 +268,13 @@ public class DoubleNd4jArrayField(public override val shape: IntArray) : Nd4jArr
|
||||
/**
|
||||
* Represents [FieldND] over [Nd4jArrayStructure] of [Float].
|
||||
*/
|
||||
public class FloatNd4jArrayField(public override val shape: IntArray) : Nd4jArrayExtendedField<Float, FloatField> {
|
||||
public override val elementContext: FloatField get() = FloatField
|
||||
public class FloatNd4jArrayField(override val shape: IntArray) : Nd4jArrayExtendedField<Float, FloatField> {
|
||||
override val elementContext: FloatField get() = FloatField
|
||||
|
||||
public override fun INDArray.wrap(): Nd4jArrayStructure<Float> = checkShape(this).asFloatStructure()
|
||||
override fun INDArray.wrap(): Nd4jArrayStructure<Float> = checkShape(this).asFloatStructure()
|
||||
|
||||
@OptIn(PerformancePitfall::class)
|
||||
public override val StructureND<Float>.ndArray: INDArray
|
||||
override val StructureND<Float>.ndArray: INDArray
|
||||
get() = when (this) {
|
||||
is Nd4jArrayStructure<Float> -> checkShape(ndArray)
|
||||
else -> Nd4j.zeros(*shape).also {
|
||||
@ -285,36 +285,36 @@ public class FloatNd4jArrayField(public override val shape: IntArray) : Nd4jArra
|
||||
override fun scale(a: StructureND<Float>, value: Double): StructureND<Float> =
|
||||
a.ndArray.mul(value).wrap()
|
||||
|
||||
public override operator fun StructureND<Float>.div(arg: Float): Nd4jArrayStructure<Float> =
|
||||
override operator fun StructureND<Float>.div(arg: Float): Nd4jArrayStructure<Float> =
|
||||
ndArray.div(arg).wrap()
|
||||
|
||||
public override operator fun StructureND<Float>.plus(arg: Float): Nd4jArrayStructure<Float> =
|
||||
override operator fun StructureND<Float>.plus(arg: Float): Nd4jArrayStructure<Float> =
|
||||
ndArray.add(arg).wrap()
|
||||
|
||||
public override operator fun StructureND<Float>.minus(arg: Float): Nd4jArrayStructure<Float> =
|
||||
override operator fun StructureND<Float>.minus(arg: Float): Nd4jArrayStructure<Float> =
|
||||
ndArray.sub(arg).wrap()
|
||||
|
||||
public override operator fun StructureND<Float>.times(arg: Float): Nd4jArrayStructure<Float> =
|
||||
override operator fun StructureND<Float>.times(arg: Float): Nd4jArrayStructure<Float> =
|
||||
ndArray.mul(arg).wrap()
|
||||
|
||||
public override operator fun Float.div(arg: StructureND<Float>): Nd4jArrayStructure<Float> =
|
||||
override operator fun Float.div(arg: StructureND<Float>): Nd4jArrayStructure<Float> =
|
||||
arg.ndArray.rdiv(this).wrap()
|
||||
|
||||
public override operator fun Float.minus(arg: StructureND<Float>): Nd4jArrayStructure<Float> =
|
||||
override operator fun Float.minus(arg: StructureND<Float>): Nd4jArrayStructure<Float> =
|
||||
arg.ndArray.rsub(this).wrap()
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents [RingND] over [Nd4jArrayIntStructure].
|
||||
*/
|
||||
public class IntNd4jArrayRing(public override val shape: IntArray) : Nd4jArrayRing<Int, IntRing> {
|
||||
public override val elementContext: IntRing
|
||||
public class IntNd4jArrayRing(override val shape: IntArray) : Nd4jArrayRing<Int, IntRing> {
|
||||
override val elementContext: IntRing
|
||||
get() = IntRing
|
||||
|
||||
public override fun INDArray.wrap(): Nd4jArrayStructure<Int> = checkShape(this).asIntStructure()
|
||||
override fun INDArray.wrap(): Nd4jArrayStructure<Int> = checkShape(this).asIntStructure()
|
||||
|
||||
@OptIn(PerformancePitfall::class)
|
||||
public override val StructureND<Int>.ndArray: INDArray
|
||||
override val StructureND<Int>.ndArray: INDArray
|
||||
get() = when (this) {
|
||||
is Nd4jArrayStructure<Int> -> checkShape(ndArray)
|
||||
else -> Nd4j.zeros(*shape).also {
|
||||
@ -322,15 +322,15 @@ public class IntNd4jArrayRing(public override val shape: IntArray) : Nd4jArrayRi
|
||||
}
|
||||
}
|
||||
|
||||
public override operator fun StructureND<Int>.plus(arg: Int): Nd4jArrayStructure<Int> =
|
||||
override operator fun StructureND<Int>.plus(arg: Int): Nd4jArrayStructure<Int> =
|
||||
ndArray.add(arg).wrap()
|
||||
|
||||
public override operator fun StructureND<Int>.minus(arg: Int): Nd4jArrayStructure<Int> =
|
||||
override operator fun StructureND<Int>.minus(arg: Int): Nd4jArrayStructure<Int> =
|
||||
ndArray.sub(arg).wrap()
|
||||
|
||||
public override operator fun StructureND<Int>.times(arg: Int): Nd4jArrayStructure<Int> =
|
||||
override operator fun StructureND<Int>.times(arg: Int): Nd4jArrayStructure<Int> =
|
||||
ndArray.mul(arg).wrap()
|
||||
|
||||
public override operator fun Int.minus(arg: StructureND<Int>): Nd4jArrayStructure<Int> =
|
||||
override operator fun Int.minus(arg: StructureND<Int>): Nd4jArrayStructure<Int> =
|
||||
arg.ndArray.rsub(this).wrap()
|
||||
}
|
||||
|
@ -22,13 +22,13 @@ public sealed class Nd4jArrayStructure<T> : MutableStructureND<T> {
|
||||
*/
|
||||
public abstract val ndArray: INDArray
|
||||
|
||||
public override val shape: IntArray get() = ndArray.shape().toIntArray()
|
||||
override val shape: IntArray get() = ndArray.shape().toIntArray()
|
||||
|
||||
internal abstract fun elementsIterator(): Iterator<Pair<IntArray, T>>
|
||||
internal fun indicesIterator(): Iterator<IntArray> = ndArray.indicesIterator()
|
||||
|
||||
@PerformancePitfall
|
||||
public override fun elements(): Sequence<Pair<IntArray, T>> = Sequence(::elementsIterator)
|
||||
override fun elements(): Sequence<Pair<IntArray, T>> = Sequence(::elementsIterator)
|
||||
}
|
||||
|
||||
private data class Nd4jArrayIntStructure(override val ndArray: INDArray) : Nd4jArrayStructure<Int>() {
|
||||
|
@ -33,105 +33,105 @@ public sealed interface Nd4jTensorAlgebra<T : Number> : AnalyticTensorAlgebra<T>
|
||||
*/
|
||||
public val StructureND<T>.ndArray: INDArray
|
||||
|
||||
public override fun T.plus(other: Tensor<T>): Tensor<T> = other.ndArray.add(this).wrap()
|
||||
public override fun Tensor<T>.plus(value: T): Tensor<T> = ndArray.add(value).wrap()
|
||||
override fun T.plus(other: Tensor<T>): Tensor<T> = other.ndArray.add(this).wrap()
|
||||
override fun Tensor<T>.plus(value: T): Tensor<T> = ndArray.add(value).wrap()
|
||||
|
||||
public override fun Tensor<T>.plus(other: Tensor<T>): Tensor<T> = ndArray.add(other.ndArray).wrap()
|
||||
override fun Tensor<T>.plus(other: Tensor<T>): Tensor<T> = ndArray.add(other.ndArray).wrap()
|
||||
|
||||
public override fun Tensor<T>.plusAssign(value: T) {
|
||||
override fun Tensor<T>.plusAssign(value: T) {
|
||||
ndArray.addi(value)
|
||||
}
|
||||
|
||||
public override fun Tensor<T>.plusAssign(other: Tensor<T>) {
|
||||
override fun Tensor<T>.plusAssign(other: Tensor<T>) {
|
||||
ndArray.addi(other.ndArray)
|
||||
}
|
||||
|
||||
public override fun T.minus(other: Tensor<T>): Tensor<T> = other.ndArray.rsub(this).wrap()
|
||||
public override fun Tensor<T>.minus(value: T): Tensor<T> = ndArray.sub(value).wrap()
|
||||
public override fun Tensor<T>.minus(other: Tensor<T>): Tensor<T> = ndArray.sub(other.ndArray).wrap()
|
||||
override fun T.minus(other: Tensor<T>): Tensor<T> = other.ndArray.rsub(this).wrap()
|
||||
override fun Tensor<T>.minus(value: T): Tensor<T> = ndArray.sub(value).wrap()
|
||||
override fun Tensor<T>.minus(other: Tensor<T>): Tensor<T> = ndArray.sub(other.ndArray).wrap()
|
||||
|
||||
public override fun Tensor<T>.minusAssign(value: T) {
|
||||
override fun Tensor<T>.minusAssign(value: T) {
|
||||
ndArray.rsubi(value)
|
||||
}
|
||||
|
||||
public override fun Tensor<T>.minusAssign(other: Tensor<T>) {
|
||||
override fun Tensor<T>.minusAssign(other: Tensor<T>) {
|
||||
ndArray.subi(other.ndArray)
|
||||
}
|
||||
|
||||
public override fun T.times(other: Tensor<T>): Tensor<T> = other.ndArray.mul(this).wrap()
|
||||
override fun T.times(other: Tensor<T>): Tensor<T> = other.ndArray.mul(this).wrap()
|
||||
|
||||
public override fun Tensor<T>.times(value: T): Tensor<T> =
|
||||
override fun Tensor<T>.times(value: T): Tensor<T> =
|
||||
ndArray.mul(value).wrap()
|
||||
|
||||
public override fun Tensor<T>.times(other: Tensor<T>): Tensor<T> = ndArray.mul(other.ndArray).wrap()
|
||||
override fun Tensor<T>.times(other: Tensor<T>): Tensor<T> = ndArray.mul(other.ndArray).wrap()
|
||||
|
||||
public override fun Tensor<T>.timesAssign(value: T) {
|
||||
override fun Tensor<T>.timesAssign(value: T) {
|
||||
ndArray.muli(value)
|
||||
}
|
||||
|
||||
public override fun Tensor<T>.timesAssign(other: Tensor<T>) {
|
||||
override fun Tensor<T>.timesAssign(other: Tensor<T>) {
|
||||
ndArray.mmuli(other.ndArray)
|
||||
}
|
||||
|
||||
public override fun Tensor<T>.unaryMinus(): Tensor<T> = ndArray.neg().wrap()
|
||||
public override fun Tensor<T>.get(i: Int): Tensor<T> = ndArray.slice(i.toLong()).wrap()
|
||||
public override fun Tensor<T>.transpose(i: Int, j: Int): Tensor<T> = ndArray.swapAxes(i, j).wrap()
|
||||
public override fun Tensor<T>.dot(other: Tensor<T>): Tensor<T> = ndArray.mmul(other.ndArray).wrap()
|
||||
override fun Tensor<T>.unaryMinus(): Tensor<T> = ndArray.neg().wrap()
|
||||
override fun Tensor<T>.get(i: Int): Tensor<T> = ndArray.slice(i.toLong()).wrap()
|
||||
override fun Tensor<T>.transpose(i: Int, j: Int): Tensor<T> = ndArray.swapAxes(i, j).wrap()
|
||||
override fun Tensor<T>.dot(other: Tensor<T>): Tensor<T> = ndArray.mmul(other.ndArray).wrap()
|
||||
|
||||
public override fun Tensor<T>.min(dim: Int, keepDim: Boolean): Tensor<T> =
|
||||
override fun Tensor<T>.min(dim: Int, keepDim: Boolean): Tensor<T> =
|
||||
ndArray.min(keepDim, dim).wrap()
|
||||
|
||||
public override fun Tensor<T>.sum(dim: Int, keepDim: Boolean): Tensor<T> =
|
||||
override fun Tensor<T>.sum(dim: Int, keepDim: Boolean): Tensor<T> =
|
||||
ndArray.sum(keepDim, dim).wrap()
|
||||
|
||||
public override fun Tensor<T>.max(dim: Int, keepDim: Boolean): Tensor<T> =
|
||||
override fun Tensor<T>.max(dim: Int, keepDim: Boolean): Tensor<T> =
|
||||
ndArray.max(keepDim, dim).wrap()
|
||||
|
||||
public override fun Tensor<T>.view(shape: IntArray): Tensor<T> = ndArray.reshape(shape).wrap()
|
||||
public override fun Tensor<T>.viewAs(other: Tensor<T>): Tensor<T> = view(other.shape)
|
||||
override fun Tensor<T>.view(shape: IntArray): Tensor<T> = ndArray.reshape(shape).wrap()
|
||||
override fun Tensor<T>.viewAs(other: Tensor<T>): Tensor<T> = view(other.shape)
|
||||
|
||||
public override fun Tensor<T>.argMax(dim: Int, keepDim: Boolean): Tensor<T> =
|
||||
override fun Tensor<T>.argMax(dim: Int, keepDim: Boolean): Tensor<T> =
|
||||
ndBase.get().argmax(ndArray, keepDim, dim).wrap()
|
||||
|
||||
public override fun Tensor<T>.mean(dim: Int, keepDim: Boolean): Tensor<T> = ndArray.mean(keepDim, dim).wrap()
|
||||
override fun Tensor<T>.mean(dim: Int, keepDim: Boolean): Tensor<T> = ndArray.mean(keepDim, dim).wrap()
|
||||
|
||||
public override fun Tensor<T>.exp(): Tensor<T> = Transforms.exp(ndArray).wrap()
|
||||
public override fun Tensor<T>.ln(): Tensor<T> = Transforms.log(ndArray).wrap()
|
||||
public override fun Tensor<T>.sqrt(): Tensor<T> = Transforms.sqrt(ndArray).wrap()
|
||||
public override fun Tensor<T>.cos(): Tensor<T> = Transforms.cos(ndArray).wrap()
|
||||
public override fun Tensor<T>.acos(): Tensor<T> = Transforms.acos(ndArray).wrap()
|
||||
public override fun Tensor<T>.cosh(): Tensor<T> = Transforms.cosh(ndArray).wrap()
|
||||
override fun Tensor<T>.exp(): Tensor<T> = Transforms.exp(ndArray).wrap()
|
||||
override fun Tensor<T>.ln(): Tensor<T> = Transforms.log(ndArray).wrap()
|
||||
override fun Tensor<T>.sqrt(): Tensor<T> = Transforms.sqrt(ndArray).wrap()
|
||||
override fun Tensor<T>.cos(): Tensor<T> = Transforms.cos(ndArray).wrap()
|
||||
override fun Tensor<T>.acos(): Tensor<T> = Transforms.acos(ndArray).wrap()
|
||||
override fun Tensor<T>.cosh(): Tensor<T> = Transforms.cosh(ndArray).wrap()
|
||||
|
||||
public override fun Tensor<T>.acosh(): Tensor<T> =
|
||||
override fun Tensor<T>.acosh(): Tensor<T> =
|
||||
Nd4j.getExecutioner().exec(ACosh(ndArray, ndArray.ulike())).wrap()
|
||||
|
||||
public override fun Tensor<T>.sin(): Tensor<T> = Transforms.sin(ndArray).wrap()
|
||||
public override fun Tensor<T>.asin(): Tensor<T> = Transforms.asin(ndArray).wrap()
|
||||
public override fun Tensor<T>.sinh(): Tensor<T> = Transforms.sinh(ndArray).wrap()
|
||||
override fun Tensor<T>.sin(): Tensor<T> = Transforms.sin(ndArray).wrap()
|
||||
override fun Tensor<T>.asin(): Tensor<T> = Transforms.asin(ndArray).wrap()
|
||||
override fun Tensor<T>.sinh(): Tensor<T> = Transforms.sinh(ndArray).wrap()
|
||||
|
||||
public override fun Tensor<T>.asinh(): Tensor<T> =
|
||||
override fun Tensor<T>.asinh(): Tensor<T> =
|
||||
Nd4j.getExecutioner().exec(ASinh(ndArray, ndArray.ulike())).wrap()
|
||||
|
||||
public override fun Tensor<T>.tan(): Tensor<T> = Transforms.tan(ndArray).wrap()
|
||||
public override fun Tensor<T>.atan(): Tensor<T> = Transforms.atan(ndArray).wrap()
|
||||
public override fun Tensor<T>.tanh(): Tensor<T> = Transforms.tanh(ndArray).wrap()
|
||||
public override fun Tensor<T>.atanh(): Tensor<T> = Transforms.atanh(ndArray).wrap()
|
||||
public override fun Tensor<T>.ceil(): Tensor<T> = Transforms.ceil(ndArray).wrap()
|
||||
public override fun Tensor<T>.floor(): Tensor<T> = Transforms.floor(ndArray).wrap()
|
||||
public override fun Tensor<T>.std(dim: Int, keepDim: Boolean): Tensor<T> = ndArray.std(true, keepDim, dim).wrap()
|
||||
public override fun T.div(other: Tensor<T>): Tensor<T> = other.ndArray.rdiv(this).wrap()
|
||||
public override fun Tensor<T>.div(value: T): Tensor<T> = ndArray.div(value).wrap()
|
||||
public override fun Tensor<T>.div(other: Tensor<T>): Tensor<T> = ndArray.div(other.ndArray).wrap()
|
||||
override fun Tensor<T>.tan(): Tensor<T> = Transforms.tan(ndArray).wrap()
|
||||
override fun Tensor<T>.atan(): Tensor<T> = Transforms.atan(ndArray).wrap()
|
||||
override fun Tensor<T>.tanh(): Tensor<T> = Transforms.tanh(ndArray).wrap()
|
||||
override fun Tensor<T>.atanh(): Tensor<T> = Transforms.atanh(ndArray).wrap()
|
||||
override fun Tensor<T>.ceil(): Tensor<T> = Transforms.ceil(ndArray).wrap()
|
||||
override fun Tensor<T>.floor(): Tensor<T> = Transforms.floor(ndArray).wrap()
|
||||
override fun Tensor<T>.std(dim: Int, keepDim: Boolean): Tensor<T> = ndArray.std(true, keepDim, dim).wrap()
|
||||
override fun T.div(other: Tensor<T>): Tensor<T> = other.ndArray.rdiv(this).wrap()
|
||||
override fun Tensor<T>.div(value: T): Tensor<T> = ndArray.div(value).wrap()
|
||||
override fun Tensor<T>.div(other: Tensor<T>): Tensor<T> = ndArray.div(other.ndArray).wrap()
|
||||
|
||||
public override fun Tensor<T>.divAssign(value: T) {
|
||||
override fun Tensor<T>.divAssign(value: T) {
|
||||
ndArray.divi(value)
|
||||
}
|
||||
|
||||
public override fun Tensor<T>.divAssign(other: Tensor<T>) {
|
||||
override fun Tensor<T>.divAssign(other: Tensor<T>) {
|
||||
ndArray.divi(other.ndArray)
|
||||
}
|
||||
|
||||
public override fun Tensor<T>.variance(dim: Int, keepDim: Boolean): Tensor<T> =
|
||||
override fun Tensor<T>.variance(dim: Int, keepDim: Boolean): Tensor<T> =
|
||||
Nd4j.getExecutioner().exec(Variance(ndArray, true, true, dim)).wrap()
|
||||
|
||||
private companion object {
|
||||
@ -143,10 +143,10 @@ public sealed interface Nd4jTensorAlgebra<T : Number> : AnalyticTensorAlgebra<T>
|
||||
* [Double] specialization of [Nd4jTensorAlgebra].
|
||||
*/
|
||||
public object DoubleNd4jTensorAlgebra : Nd4jTensorAlgebra<Double> {
|
||||
public override fun INDArray.wrap(): Nd4jArrayStructure<Double> = asDoubleStructure()
|
||||
override fun INDArray.wrap(): Nd4jArrayStructure<Double> = asDoubleStructure()
|
||||
|
||||
@OptIn(PerformancePitfall::class)
|
||||
public override val StructureND<Double>.ndArray: INDArray
|
||||
override val StructureND<Double>.ndArray: INDArray
|
||||
get() = when (this) {
|
||||
is Nd4jArrayStructure<Double> -> ndArray
|
||||
else -> Nd4j.zeros(*shape).also {
|
||||
@ -154,22 +154,22 @@ public object DoubleNd4jTensorAlgebra : Nd4jTensorAlgebra<Double> {
|
||||
}
|
||||
}
|
||||
|
||||
public override fun Tensor<Double>.valueOrNull(): Double? =
|
||||
override fun Tensor<Double>.valueOrNull(): Double? =
|
||||
if (shape contentEquals intArrayOf(1)) ndArray.getDouble(0) else null
|
||||
|
||||
// TODO rewrite
|
||||
@PerformancePitfall
|
||||
public override fun diagonalEmbedding(
|
||||
override fun diagonalEmbedding(
|
||||
diagonalEntries: Tensor<Double>,
|
||||
offset: Int,
|
||||
dim1: Int,
|
||||
dim2: Int,
|
||||
): Tensor<Double> = DoubleTensorAlgebra.diagonalEmbedding(diagonalEntries, offset, dim1, dim2)
|
||||
|
||||
public override fun Tensor<Double>.sum(): Double = ndArray.sumNumber().toDouble()
|
||||
public override fun Tensor<Double>.min(): Double = ndArray.minNumber().toDouble()
|
||||
public override fun Tensor<Double>.max(): Double = ndArray.maxNumber().toDouble()
|
||||
public override fun Tensor<Double>.mean(): Double = ndArray.meanNumber().toDouble()
|
||||
public override fun Tensor<Double>.std(): Double = ndArray.stdNumber().toDouble()
|
||||
public override fun Tensor<Double>.variance(): Double = ndArray.varNumber().toDouble()
|
||||
override fun Tensor<Double>.sum(): Double = ndArray.sumNumber().toDouble()
|
||||
override fun Tensor<Double>.min(): Double = ndArray.minNumber().toDouble()
|
||||
override fun Tensor<Double>.max(): Double = ndArray.maxNumber().toDouble()
|
||||
override fun Tensor<Double>.mean(): Double = ndArray.meanNumber().toDouble()
|
||||
override fun Tensor<Double>.std(): Double = ndArray.stdNumber().toDouble()
|
||||
override fun Tensor<Double>.variance(): Double = ndArray.varNumber().toDouble()
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ public interface Distribution<T : Any> : Sampler<T> {
|
||||
*/
|
||||
public fun probability(arg: T): Double
|
||||
|
||||
public override fun sample(generator: RandomGenerator): Chain<T>
|
||||
override fun sample(generator: RandomGenerator): Chain<T>
|
||||
|
||||
/**
|
||||
* An empty companion. Distribution factories should be written as its extensions
|
||||
|
@ -23,14 +23,14 @@ public class NormalDistribution(public val sampler: GaussianSampler) : Univariat
|
||||
normalized: NormalizedGaussianSampler = ZigguratNormalizedGaussianSampler,
|
||||
) : this(GaussianSampler(mean, standardDeviation, normalized))
|
||||
|
||||
public override fun probability(arg: Double): Double {
|
||||
override fun probability(arg: Double): Double {
|
||||
val x1 = (arg - sampler.mean) / sampler.standardDeviation
|
||||
return exp(-0.5 * x1 * x1 - (ln(sampler.standardDeviation) + 0.5 * ln(2 * PI)))
|
||||
}
|
||||
|
||||
public override fun sample(generator: RandomGenerator): Chain<Double> = sampler.sample(generator)
|
||||
override fun sample(generator: RandomGenerator): Chain<Double> = sampler.sample(generator)
|
||||
|
||||
public override fun cumulative(arg: Double): Double {
|
||||
override fun cumulative(arg: Double): Double {
|
||||
val dev = arg - sampler.mean
|
||||
|
||||
return when {
|
||||
|
@ -24,7 +24,7 @@ public class AhrensDieterExponentialSampler(public val mean: Double) : Sampler<D
|
||||
require(mean > 0) { "mean is not strictly positive: $mean" }
|
||||
}
|
||||
|
||||
public override fun sample(generator: RandomGenerator): BlockingDoubleChain = object : BlockingDoubleChain {
|
||||
override fun sample(generator: RandomGenerator): BlockingDoubleChain = object : BlockingDoubleChain {
|
||||
override fun nextBlocking(): Double {
|
||||
// Step 1:
|
||||
var a = 0.0
|
||||
|
@ -113,8 +113,8 @@ public class AhrensDieterMarsagliaTsangGammaSampler private constructor(
|
||||
}
|
||||
}
|
||||
|
||||
public override fun sample(generator: RandomGenerator): Chain<Double> = delegate.sample(generator)
|
||||
public override fun toString(): String = delegate.toString()
|
||||
override fun sample(generator: RandomGenerator): Chain<Double> = delegate.sample(generator)
|
||||
override fun toString(): String = delegate.toString()
|
||||
|
||||
public companion object {
|
||||
public fun of(
|
||||
|
@ -76,7 +76,7 @@ public open class AliasMethodDiscreteSampler private constructor(
|
||||
}
|
||||
}
|
||||
|
||||
public override fun sample(generator: RandomGenerator): Chain<Int> = generator.chain {
|
||||
override fun sample(generator: RandomGenerator): Chain<Int> = generator.chain {
|
||||
// This implements the algorithm as per Vose (1991):
|
||||
// v = uniform() in [0, 1)
|
||||
// j = uniform(n) in [0, n)
|
||||
@ -107,7 +107,7 @@ public open class AliasMethodDiscreteSampler private constructor(
|
||||
if (generator.nextLong() ushr 11 < probability[j]) j else alias[j]
|
||||
}
|
||||
|
||||
public override fun toString(): String = "Alias method"
|
||||
override fun toString(): String = "Alias method"
|
||||
|
||||
public companion object {
|
||||
private const val DEFAULT_ALPHA = 0
|
||||
|
@ -28,7 +28,7 @@ public class GaussianSampler(
|
||||
require(standardDeviation > 0.0) { "standard deviation is not strictly positive: $standardDeviation" }
|
||||
}
|
||||
|
||||
public override fun sample(generator: RandomGenerator): BlockingDoubleChain = normalized
|
||||
override fun sample(generator: RandomGenerator): BlockingDoubleChain = normalized
|
||||
.sample(generator)
|
||||
.map { standardDeviation * it + mean }
|
||||
|
||||
|
@ -27,7 +27,7 @@ public class KempSmallMeanPoissonSampler internal constructor(
|
||||
private val p0: Double,
|
||||
private val mean: Double,
|
||||
) : Sampler<Int> {
|
||||
public override fun sample(generator: RandomGenerator): BlockingIntChain = object : BlockingIntChain {
|
||||
override fun sample(generator: RandomGenerator): BlockingIntChain = object : BlockingIntChain {
|
||||
override fun nextBlocking(): Int {
|
||||
//TODO move to nextBufferBlocking
|
||||
// Note on the algorithm:
|
||||
@ -60,7 +60,7 @@ public class KempSmallMeanPoissonSampler internal constructor(
|
||||
override suspend fun fork(): BlockingIntChain = sample(generator.fork())
|
||||
}
|
||||
|
||||
public override fun toString(): String = "Kemp Small Mean Poisson deviate"
|
||||
override fun toString(): String = "Kemp Small Mean Poisson deviate"
|
||||
}
|
||||
|
||||
public fun KempSmallMeanPoissonSampler(mean: Double): KempSmallMeanPoissonSampler {
|
||||
|
@ -58,7 +58,7 @@ public class SmallMeanPoissonSampler(public val mean: Double) : Sampler<Int> {
|
||||
throw IllegalArgumentException("No p(x=0) probability for mean: $mean")
|
||||
}.toInt()
|
||||
|
||||
public override fun sample(generator: RandomGenerator): BlockingIntChain = object : BlockingIntChain {
|
||||
override fun sample(generator: RandomGenerator): BlockingIntChain = object : BlockingIntChain {
|
||||
override fun nextBlocking(): Int {
|
||||
var n = 0
|
||||
var r = 1.0
|
||||
@ -76,7 +76,7 @@ public class SmallMeanPoissonSampler(public val mean: Double) : Sampler<Int> {
|
||||
override suspend fun fork(): BlockingIntChain = sample(generator.fork())
|
||||
}
|
||||
|
||||
public override fun toString(): String = "Small Mean Poisson deviate"
|
||||
override fun toString(): String = "Small Mean Poisson deviate"
|
||||
}
|
||||
|
||||
|
||||
@ -113,7 +113,7 @@ public class LargeMeanPoissonSampler(public val mean: Double) : Sampler<Int> {
|
||||
private val p1: Double = a1 / aSum
|
||||
private val p2: Double = a2 / aSum
|
||||
|
||||
public override fun sample(generator: RandomGenerator): BlockingIntChain = object : BlockingIntChain {
|
||||
override fun sample(generator: RandomGenerator): BlockingIntChain = object : BlockingIntChain {
|
||||
override fun nextBlocking(): Int {
|
||||
val exponential = AhrensDieterExponentialSampler(1.0).sample(generator)
|
||||
val gaussian = ZigguratNormalizedGaussianSampler.sample(generator)
|
||||
@ -197,7 +197,7 @@ public class LargeMeanPoissonSampler(public val mean: Double) : Sampler<Int> {
|
||||
}
|
||||
|
||||
private fun getFactorialLog(n: Int): Double = factorialLog.value(n)
|
||||
public override fun toString(): String = "Large Mean Poisson deviate"
|
||||
override fun toString(): String = "Large Mean Poisson deviate"
|
||||
|
||||
public companion object {
|
||||
private const val MAX_MEAN: Double = 0.5 * Int.MAX_VALUE
|
||||
|
@ -27,13 +27,13 @@ public class Mean<T>(
|
||||
|
||||
override suspend fun evaluate(data: Buffer<T>): T = super<ComposableStatistic>.evaluate(data)
|
||||
|
||||
public override suspend fun computeIntermediate(data: Buffer<T>): Pair<T, Int> =
|
||||
override suspend fun computeIntermediate(data: Buffer<T>): Pair<T, Int> =
|
||||
evaluateBlocking(data) to data.size
|
||||
|
||||
public override suspend fun composeIntermediate(first: Pair<T, Int>, second: Pair<T, Int>): Pair<T, Int> =
|
||||
override suspend fun composeIntermediate(first: Pair<T, Int>, second: Pair<T, Int>): Pair<T, Int> =
|
||||
group { first.first + second.first } to (first.second + second.second)
|
||||
|
||||
public override suspend fun toResult(intermediate: Pair<T, Int>): T = group {
|
||||
override suspend fun toResult(intermediate: Pair<T, Int>): T = group {
|
||||
division(intermediate.first, intermediate.second)
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ import space.kscience.kmath.structures.asSequence
|
||||
* Non-composable median
|
||||
*/
|
||||
public class Median<T>(private val comparator: Comparator<T>) : BlockingStatistic<T, T> {
|
||||
public override fun evaluateBlocking(data: Buffer<T>): T =
|
||||
override fun evaluateBlocking(data: Buffer<T>): T =
|
||||
data.asSequence().sortedWith(comparator).toList()[data.size / 2] //TODO check if this is correct
|
||||
|
||||
public companion object {
|
||||
|
@ -31,7 +31,7 @@ public fun <R> RandomGenerator.chain(generator: suspend RandomGenerator.() -> R)
|
||||
* A type-specific double chunk random chain
|
||||
*/
|
||||
public class UniformDoubleChain(public val generator: RandomGenerator) : BlockingDoubleChain {
|
||||
public override fun nextBufferBlocking(size: Int): DoubleBuffer = generator.nextDoubleBuffer(size)
|
||||
override fun nextBufferBlocking(size: Int): DoubleBuffer = generator.nextDoubleBuffer(size)
|
||||
override suspend fun nextBuffer(size: Int): DoubleBuffer = nextBufferBlocking(size)
|
||||
|
||||
override suspend fun fork(): UniformDoubleChain = UniformDoubleChain(generator.fork())
|
||||
|
@ -97,17 +97,17 @@ public interface RandomGenerator {
|
||||
* @property random the underlying [Random] object.
|
||||
*/
|
||||
public class DefaultGenerator(public val random: Random = Random) : RandomGenerator {
|
||||
public override fun nextBoolean(): Boolean = random.nextBoolean()
|
||||
public override fun nextDouble(): Double = random.nextDouble()
|
||||
public override fun nextInt(): Int = random.nextInt()
|
||||
public override fun nextInt(until: Int): Int = random.nextInt(until)
|
||||
public override fun nextLong(): Long = random.nextLong()
|
||||
public override fun nextLong(until: Long): Long = random.nextLong(until)
|
||||
override fun nextBoolean(): Boolean = random.nextBoolean()
|
||||
override fun nextDouble(): Double = random.nextDouble()
|
||||
override fun nextInt(): Int = random.nextInt()
|
||||
override fun nextInt(until: Int): Int = random.nextInt(until)
|
||||
override fun nextLong(): Long = random.nextLong()
|
||||
override fun nextLong(until: Long): Long = random.nextLong(until)
|
||||
|
||||
public override fun fillBytes(array: ByteArray, fromIndex: Int, toIndex: Int) {
|
||||
override fun fillBytes(array: ByteArray, fromIndex: Int, toIndex: Int) {
|
||||
random.nextBytes(array, fromIndex, toIndex)
|
||||
}
|
||||
|
||||
public override fun nextBytes(size: Int): ByteArray = random.nextBytes(size)
|
||||
public override fun fork(): RandomGenerator = RandomGenerator.default(random.nextLong())
|
||||
override fun nextBytes(size: Int): ByteArray = random.nextBytes(size)
|
||||
override fun fork(): RandomGenerator = RandomGenerator.default(random.nextLong())
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ import space.kscience.kmath.operations.invoke
|
||||
* @property value the value to sample.
|
||||
*/
|
||||
public class ConstantSampler<out T : Any>(public val value: T) : Sampler<T> {
|
||||
public override fun sample(generator: RandomGenerator): Chain<T> = ConstantChain(value)
|
||||
override fun sample(generator: RandomGenerator): Chain<T> = ConstantChain(value)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -28,7 +28,7 @@ public class ConstantSampler<out T : Any>(public val value: T) : Sampler<T> {
|
||||
* @property chainBuilder the provider of [Chain].
|
||||
*/
|
||||
public class BasicSampler<out T : Any>(public val chainBuilder: (RandomGenerator) -> Chain<T>) : Sampler<T> {
|
||||
public override fun sample(generator: RandomGenerator): Chain<T> = chainBuilder(generator)
|
||||
override fun sample(generator: RandomGenerator): Chain<T> = chainBuilder(generator)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,17 +39,17 @@ public class BasicSampler<out T : Any>(public val chainBuilder: (RandomGenerator
|
||||
public class SamplerSpace<T : Any, out S>(public val algebra: S) : Group<Sampler<T>>,
|
||||
ScaleOperations<Sampler<T>> where S : Group<T>, S : ScaleOperations<T> {
|
||||
|
||||
public override val zero: Sampler<T> = ConstantSampler(algebra.zero)
|
||||
override val zero: Sampler<T> = ConstantSampler(algebra.zero)
|
||||
|
||||
public override fun add(a: Sampler<T>, b: Sampler<T>): Sampler<T> = BasicSampler { generator ->
|
||||
override fun add(a: Sampler<T>, b: Sampler<T>): Sampler<T> = BasicSampler { generator ->
|
||||
a.sample(generator).zip(b.sample(generator)) { aValue, bValue -> algebra { aValue + bValue } }
|
||||
}
|
||||
|
||||
public override fun scale(a: Sampler<T>, value: Double): Sampler<T> = BasicSampler { generator ->
|
||||
override fun scale(a: Sampler<T>, value: Double): Sampler<T> = BasicSampler { generator ->
|
||||
a.sample(generator).map { a ->
|
||||
algebra { a * value }
|
||||
}
|
||||
}
|
||||
|
||||
public override fun Sampler<T>.unaryMinus(): Sampler<T> = scale(this, -1.0)
|
||||
override fun Sampler<T>.unaryMinus(): Sampler<T> = scale(this, -1.0)
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ public interface ComposableStatistic<in T, I, out R> : Statistic<T, R> {
|
||||
//Transform block to result
|
||||
public suspend fun toResult(intermediate: I): R
|
||||
|
||||
public override suspend fun evaluate(data: Buffer<T>): R = toResult(computeIntermediate(data))
|
||||
override suspend fun evaluate(data: Buffer<T>): R = toResult(computeIntermediate(data))
|
||||
}
|
||||
|
||||
@FlowPreview
|
||||
|
@ -17,19 +17,19 @@ public class RandomSourceGenerator internal constructor(public val source: Rando
|
||||
internal val random: UniformRandomProvider = seed?.let { RandomSource.create(source, seed) }
|
||||
?: RandomSource.create(source)
|
||||
|
||||
public override fun nextBoolean(): Boolean = random.nextBoolean()
|
||||
public override fun nextDouble(): Double = random.nextDouble()
|
||||
public override fun nextInt(): Int = random.nextInt()
|
||||
public override fun nextInt(until: Int): Int = random.nextInt(until)
|
||||
public override fun nextLong(): Long = random.nextLong()
|
||||
public override fun nextLong(until: Long): Long = random.nextLong(until)
|
||||
override fun nextBoolean(): Boolean = random.nextBoolean()
|
||||
override fun nextDouble(): Double = random.nextDouble()
|
||||
override fun nextInt(): Int = random.nextInt()
|
||||
override fun nextInt(until: Int): Int = random.nextInt(until)
|
||||
override fun nextLong(): Long = random.nextLong()
|
||||
override fun nextLong(until: Long): Long = random.nextLong(until)
|
||||
|
||||
public override fun fillBytes(array: ByteArray, fromIndex: Int, toIndex: Int) {
|
||||
override fun fillBytes(array: ByteArray, fromIndex: Int, toIndex: Int) {
|
||||
require(toIndex > fromIndex)
|
||||
random.nextBytes(array, fromIndex, toIndex - fromIndex)
|
||||
}
|
||||
|
||||
public override fun fork(): RandomGenerator = RandomSourceGenerator(source, nextLong())
|
||||
override fun fork(): RandomGenerator = RandomSourceGenerator(source, nextLong())
|
||||
}
|
||||
|
||||
/**
|
||||
@ -43,14 +43,14 @@ public class RandomGeneratorProvider(public val generator: RandomGenerator) : Un
|
||||
*
|
||||
* @return the next random value.
|
||||
*/
|
||||
public override fun nextBoolean(): Boolean = generator.nextBoolean()
|
||||
override fun nextBoolean(): Boolean = generator.nextBoolean()
|
||||
|
||||
/**
|
||||
* Generates a [Float] value between 0 and 1.
|
||||
*
|
||||
* @return the next random value between 0 and 1.
|
||||
*/
|
||||
public override fun nextFloat(): Float = generator.nextDouble().toFloat()
|
||||
override fun nextFloat(): Float = generator.nextDouble().toFloat()
|
||||
|
||||
/**
|
||||
* Generates [Byte] values and places them into a user-supplied array.
|
||||
@ -59,7 +59,7 @@ public class RandomGeneratorProvider(public val generator: RandomGenerator) : Un
|
||||
*
|
||||
* @param bytes byte array in which to put the random bytes.
|
||||
*/
|
||||
public override fun nextBytes(bytes: ByteArray): Unit = generator.fillBytes(bytes)
|
||||
override fun nextBytes(bytes: ByteArray): Unit = generator.fillBytes(bytes)
|
||||
|
||||
/**
|
||||
* Generates [Byte] values and places them into a user-supplied array.
|
||||
@ -71,7 +71,7 @@ public class RandomGeneratorProvider(public val generator: RandomGenerator) : Un
|
||||
* @param start the index at which to start inserting the generated bytes.
|
||||
* @param len the number of bytes to insert.
|
||||
*/
|
||||
public override fun nextBytes(bytes: ByteArray, start: Int, len: Int) {
|
||||
override fun nextBytes(bytes: ByteArray, start: Int, len: Int) {
|
||||
generator.fillBytes(bytes, start, start + len)
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ public class RandomGeneratorProvider(public val generator: RandomGenerator) : Un
|
||||
*
|
||||
* @return the next random value.
|
||||
*/
|
||||
public override fun nextInt(): Int = generator.nextInt()
|
||||
override fun nextInt(): Int = generator.nextInt()
|
||||
|
||||
/**
|
||||
* Generates an [Int] value between 0 (inclusive) and the specified value (exclusive).
|
||||
@ -88,21 +88,21 @@ public class RandomGeneratorProvider(public val generator: RandomGenerator) : Un
|
||||
* @param n the bound on the random number to be returned. Must be positive.
|
||||
* @return a random integer between 0 (inclusive) and [n] (exclusive).
|
||||
*/
|
||||
public override fun nextInt(n: Int): Int = generator.nextInt(n)
|
||||
override fun nextInt(n: Int): Int = generator.nextInt(n)
|
||||
|
||||
/**
|
||||
* Generates a [Double] value between 0 and 1.
|
||||
*
|
||||
* @return the next random value between 0 and 1.
|
||||
*/
|
||||
public override fun nextDouble(): Double = generator.nextDouble()
|
||||
override fun nextDouble(): Double = generator.nextDouble()
|
||||
|
||||
/**
|
||||
* Generates a [Long] value.
|
||||
*
|
||||
* @return the next random value.
|
||||
*/
|
||||
public override fun nextLong(): Long = generator.nextLong()
|
||||
override fun nextLong(): Long = generator.nextLong()
|
||||
|
||||
/**
|
||||
* Generates a [Long] value between 0 (inclusive) and the specified value (exclusive).
|
||||
@ -110,7 +110,7 @@ public class RandomGeneratorProvider(public val generator: RandomGenerator) : Un
|
||||
* @param n Bound on the random number to be returned. Must be positive.
|
||||
* @return a random long value between 0 (inclusive) and [n] (exclusive).
|
||||
*/
|
||||
public override fun nextLong(n: Long): Long = generator.nextLong(n)
|
||||
override fun nextLong(n: Long): Long = generator.nextLong(n)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -30,9 +30,9 @@ public class SymjaExpression<T : Number, A : NumericAlgebra<T>>(
|
||||
public val mst: MST,
|
||||
public val evaluator: ExprEvaluator = DEFAULT_EVALUATOR,
|
||||
) : SpecialDifferentiableExpression<T, SymjaExpression<T, A>> {
|
||||
public override fun invoke(arguments: Map<Symbol, T>): T = mst.interpret(algebra, arguments)
|
||||
override fun invoke(arguments: Map<Symbol, T>): T = mst.interpret(algebra, arguments)
|
||||
|
||||
public override fun derivativeOrNull(symbols: List<Symbol>): SymjaExpression<T, A> = SymjaExpression(
|
||||
override fun derivativeOrNull(symbols: List<Symbol>): SymjaExpression<T, A> = SymjaExpression(
|
||||
algebra,
|
||||
symbols.map(Symbol::toIExpr).fold(mst.toIExpr(), F::D).toMst(evaluator),
|
||||
evaluator,
|
||||
|
@ -10,15 +10,15 @@ import space.kscience.kmath.structures.MutableBuffer
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE", "OVERRIDE_BY_INLINE")
|
||||
public class ViktorBuffer(public val flatArray: F64FlatArray) : MutableBuffer<Double> {
|
||||
public override val size: Int
|
||||
override val size: Int
|
||||
get() = flatArray.size
|
||||
|
||||
public override inline fun get(index: Int): Double = flatArray[index]
|
||||
override inline fun get(index: Int): Double = flatArray[index]
|
||||
|
||||
public override inline fun set(index: Int, value: Double) {
|
||||
override inline fun set(index: Int, value: Double) {
|
||||
flatArray[index] = value
|
||||
}
|
||||
|
||||
public override fun copy(): MutableBuffer<Double> = ViktorBuffer(flatArray.copy().flatten())
|
||||
public override operator fun iterator(): Iterator<Double> = flatArray.data.iterator()
|
||||
override fun copy(): MutableBuffer<Double> = ViktorBuffer(flatArray.copy().flatten())
|
||||
override operator fun iterator(): Iterator<Double> = flatArray.data.iterator()
|
||||
}
|
||||
|
@ -16,16 +16,16 @@ import space.kscience.kmath.operations.ScaleOperations
|
||||
|
||||
@Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||
public class ViktorStructureND(public val f64Buffer: F64Array) : MutableStructureND<Double> {
|
||||
public override val shape: IntArray get() = f64Buffer.shape
|
||||
override val shape: IntArray get() = f64Buffer.shape
|
||||
|
||||
public override inline fun get(index: IntArray): Double = f64Buffer.get(*index)
|
||||
override inline fun get(index: IntArray): Double = f64Buffer.get(*index)
|
||||
|
||||
public override inline fun set(index: IntArray, value: Double) {
|
||||
override inline fun set(index: IntArray, value: Double) {
|
||||
f64Buffer.set(*index, value = value)
|
||||
}
|
||||
|
||||
@PerformancePitfall
|
||||
public override fun elements(): Sequence<Pair<IntArray, Double>> =
|
||||
override fun elements(): Sequence<Pair<IntArray, Double>> =
|
||||
DefaultStrides(shape).indices().map { it to get(it) }
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ public fun F64Array.asStructure(): ViktorStructureND = ViktorStructureND(this)
|
||||
|
||||
@OptIn(UnstableKMathAPI::class)
|
||||
@Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||
public class ViktorFieldND(public override val shape: IntArray) : FieldND<Double, DoubleField>,
|
||||
public class ViktorFieldND(override val shape: IntArray) : FieldND<Double, DoubleField>,
|
||||
NumbersAddOperations<StructureND<Double>>, ExtendedField<StructureND<Double>>,
|
||||
ScaleOperations<StructureND<Double>> {
|
||||
|
||||
@ -47,30 +47,30 @@ public class ViktorFieldND(public override val shape: IntArray) : FieldND<Double
|
||||
else -> produce { this@f64Buffer[it] }.f64Buffer
|
||||
}
|
||||
|
||||
public override val zero: ViktorStructureND by lazy { F64Array.full(init = 0.0, shape = shape).asStructure() }
|
||||
public override val one: ViktorStructureND by lazy { F64Array.full(init = 1.0, shape = shape).asStructure() }
|
||||
override val zero: ViktorStructureND by lazy { F64Array.full(init = 0.0, shape = shape).asStructure() }
|
||||
override val one: ViktorStructureND by lazy { F64Array.full(init = 1.0, shape = shape).asStructure() }
|
||||
|
||||
private val strides: Strides = DefaultStrides(shape)
|
||||
|
||||
public override val elementContext: DoubleField get() = DoubleField
|
||||
override val elementContext: DoubleField get() = DoubleField
|
||||
|
||||
public override fun produce(initializer: DoubleField.(IntArray) -> Double): ViktorStructureND =
|
||||
override fun produce(initializer: DoubleField.(IntArray) -> Double): ViktorStructureND =
|
||||
F64Array(*shape).apply {
|
||||
this@ViktorFieldND.strides.indices().forEach { index ->
|
||||
set(value = DoubleField.initializer(index), indices = index)
|
||||
}
|
||||
}.asStructure()
|
||||
|
||||
public override fun StructureND<Double>.unaryMinus(): StructureND<Double> = -1 * this
|
||||
override fun StructureND<Double>.unaryMinus(): StructureND<Double> = -1 * this
|
||||
|
||||
public override fun StructureND<Double>.map(transform: DoubleField.(Double) -> Double): ViktorStructureND =
|
||||
override fun StructureND<Double>.map(transform: DoubleField.(Double) -> Double): ViktorStructureND =
|
||||
F64Array(*this@ViktorFieldND.shape).apply {
|
||||
this@ViktorFieldND.strides.indices().forEach { index ->
|
||||
set(value = DoubleField.transform(this@map[index]), indices = index)
|
||||
}
|
||||
}.asStructure()
|
||||
|
||||
public override fun StructureND<Double>.mapIndexed(
|
||||
override fun StructureND<Double>.mapIndexed(
|
||||
transform: DoubleField.(index: IntArray, Double) -> Double,
|
||||
): ViktorStructureND = F64Array(*this@ViktorFieldND.shape).apply {
|
||||
this@ViktorFieldND.strides.indices().forEach { index ->
|
||||
@ -78,7 +78,7 @@ public class ViktorFieldND(public override val shape: IntArray) : FieldND<Double
|
||||
}
|
||||
}.asStructure()
|
||||
|
||||
public override fun combine(
|
||||
override fun combine(
|
||||
a: StructureND<Double>,
|
||||
b: StructureND<Double>,
|
||||
transform: DoubleField.(Double, Double) -> Double,
|
||||
@ -88,39 +88,39 @@ public class ViktorFieldND(public override val shape: IntArray) : FieldND<Double
|
||||
}
|
||||
}.asStructure()
|
||||
|
||||
public override fun add(a: StructureND<Double>, b: StructureND<Double>): ViktorStructureND =
|
||||
override fun add(a: StructureND<Double>, b: StructureND<Double>): ViktorStructureND =
|
||||
(a.f64Buffer + b.f64Buffer).asStructure()
|
||||
|
||||
public override fun scale(a: StructureND<Double>, value: Double): ViktorStructureND =
|
||||
override fun scale(a: StructureND<Double>, value: Double): ViktorStructureND =
|
||||
(a.f64Buffer * value).asStructure()
|
||||
|
||||
public override inline fun StructureND<Double>.plus(b: StructureND<Double>): ViktorStructureND =
|
||||
override inline fun StructureND<Double>.plus(b: StructureND<Double>): ViktorStructureND =
|
||||
(f64Buffer + b.f64Buffer).asStructure()
|
||||
|
||||
public override inline fun StructureND<Double>.minus(b: StructureND<Double>): ViktorStructureND =
|
||||
override inline fun StructureND<Double>.minus(b: StructureND<Double>): ViktorStructureND =
|
||||
(f64Buffer - b.f64Buffer).asStructure()
|
||||
|
||||
public override inline fun StructureND<Double>.times(k: Number): ViktorStructureND =
|
||||
override inline fun StructureND<Double>.times(k: Number): ViktorStructureND =
|
||||
(f64Buffer * k.toDouble()).asStructure()
|
||||
|
||||
public override inline fun StructureND<Double>.plus(arg: Double): ViktorStructureND =
|
||||
override inline fun StructureND<Double>.plus(arg: Double): ViktorStructureND =
|
||||
(f64Buffer.plus(arg)).asStructure()
|
||||
|
||||
public override fun number(value: Number): ViktorStructureND =
|
||||
override fun number(value: Number): ViktorStructureND =
|
||||
F64Array.full(init = value.toDouble(), shape = shape).asStructure()
|
||||
|
||||
public override fun sin(arg: StructureND<Double>): ViktorStructureND = arg.map { sin(it) }
|
||||
public override fun cos(arg: StructureND<Double>): ViktorStructureND = arg.map { cos(it) }
|
||||
public override fun tan(arg: StructureND<Double>): ViktorStructureND = arg.map { tan(it) }
|
||||
public override fun asin(arg: StructureND<Double>): ViktorStructureND = arg.map { asin(it) }
|
||||
public override fun acos(arg: StructureND<Double>): ViktorStructureND = arg.map { acos(it) }
|
||||
public override fun atan(arg: StructureND<Double>): ViktorStructureND = arg.map { atan(it) }
|
||||
override fun sin(arg: StructureND<Double>): ViktorStructureND = arg.map { sin(it) }
|
||||
override fun cos(arg: StructureND<Double>): ViktorStructureND = arg.map { cos(it) }
|
||||
override fun tan(arg: StructureND<Double>): ViktorStructureND = arg.map { tan(it) }
|
||||
override fun asin(arg: StructureND<Double>): ViktorStructureND = arg.map { asin(it) }
|
||||
override fun acos(arg: StructureND<Double>): ViktorStructureND = arg.map { acos(it) }
|
||||
override fun atan(arg: StructureND<Double>): ViktorStructureND = arg.map { atan(it) }
|
||||
|
||||
public override fun power(arg: StructureND<Double>, pow: Number): ViktorStructureND = arg.map { it.pow(pow) }
|
||||
override fun power(arg: StructureND<Double>, pow: Number): ViktorStructureND = arg.map { it.pow(pow) }
|
||||
|
||||
public override fun exp(arg: StructureND<Double>): ViktorStructureND = arg.f64Buffer.exp().asStructure()
|
||||
override fun exp(arg: StructureND<Double>): ViktorStructureND = arg.f64Buffer.exp().asStructure()
|
||||
|
||||
public override fun ln(arg: StructureND<Double>): ViktorStructureND = arg.f64Buffer.log().asStructure()
|
||||
override fun ln(arg: StructureND<Double>): ViktorStructureND = arg.f64Buffer.log().asStructure()
|
||||
}
|
||||
|
||||
public fun ViktorNDField(vararg shape: Int): ViktorFieldND = ViktorFieldND(shape)
|
||||
|
Loading…
Reference in New Issue
Block a user