Dev #127
@ -7,7 +7,6 @@ import kotlin.math.pow as kpow
|
|||||||
* Advanced Number-like field that implements basic operations
|
* Advanced Number-like field that implements basic operations
|
||||||
*/
|
*/
|
||||||
interface ExtendedFieldOperations<T> :
|
interface ExtendedFieldOperations<T> :
|
||||||
FieldOperations<T>,
|
|
||||||
InverseTrigonometricOperations<T>,
|
InverseTrigonometricOperations<T>,
|
||||||
PowerOperations<T>,
|
PowerOperations<T>,
|
||||||
ExponentialOperations<T> {
|
ExponentialOperations<T> {
|
||||||
@ -24,9 +23,8 @@ interface ExtendedFieldOperations<T> :
|
|||||||
PowerOperations.SQRT_OPERATION -> sqrt(arg)
|
PowerOperations.SQRT_OPERATION -> sqrt(arg)
|
||||||
ExponentialOperations.EXP_OPERATION -> exp(arg)
|
ExponentialOperations.EXP_OPERATION -> exp(arg)
|
||||||
ExponentialOperations.LN_OPERATION -> ln(arg)
|
ExponentialOperations.LN_OPERATION -> ln(arg)
|
||||||
else -> super<InverseTrigonometricOperations>.unaryOperation(operation, arg)
|
else -> super.unaryOperation(operation, arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ExtendedField<T> : ExtendedFieldOperations<T>, Field<T> {
|
interface ExtendedField<T> : ExtendedFieldOperations<T>, Field<T> {
|
||||||
|
@ -64,7 +64,7 @@ fun <T : MathElement<out PowerOperations<T>>> sqr(arg: T): T = arg pow 2.0
|
|||||||
|
|
||||||
/* Exponential */
|
/* Exponential */
|
||||||
|
|
||||||
interface ExponentialOperations<T>: Algebra<T> {
|
interface ExponentialOperations<T> : Algebra<T> {
|
||||||
fun exp(arg: T): T
|
fun exp(arg: T): T
|
||||||
fun ln(arg: T): T
|
fun ln(arg: T): T
|
||||||
|
|
||||||
@ -81,4 +81,4 @@ interface Norm<in T : Any, out R> {
|
|||||||
fun norm(arg: T): R
|
fun norm(arg: T): R
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T : MathElement<out Norm<T, R>>, R> norm(arg: T): R = arg.context.norm(arg)
|
fun <T : MathElement<out Norm<T, R>>, R> norm(arg: T): R = arg.context.norm(arg)
|
||||||
|
@ -9,185 +9,171 @@ import kotlin.math.*
|
|||||||
* A simple field over linear buffers of [Double]
|
* A simple field over linear buffers of [Double]
|
||||||
*/
|
*/
|
||||||
object RealBufferFieldOperations : ExtendedFieldOperations<Buffer<Double>> {
|
object RealBufferFieldOperations : ExtendedFieldOperations<Buffer<Double>> {
|
||||||
|
override fun add(a: Buffer<Double>, b: Buffer<Double>): RealBuffer {
|
||||||
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} " }
|
require(b.size == a.size) { "The size of the first buffer ${a.size} should be the same as for second one: ${b.size} " }
|
||||||
|
|
||||||
return if (a is DoubleBuffer && b is DoubleBuffer) {
|
return if (a is RealBuffer && b is RealBuffer) {
|
||||||
val aArray = a.array
|
val aArray = a.array
|
||||||
val bArray = b.array
|
val bArray = b.array
|
||||||
DoubleBuffer(DoubleArray(a.size) { aArray[it] + bArray[it] })
|
RealBuffer(DoubleArray(a.size) { aArray[it] + bArray[it] })
|
||||||
} else {
|
} else
|
||||||
DoubleBuffer(DoubleArray(a.size) { a[it] + b[it] })
|
RealBuffer(DoubleArray(a.size) { a[it] + b[it] })
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun multiply(a: Buffer<Double>, k: Number): DoubleBuffer {
|
override fun multiply(a: Buffer<Double>, k: Number): RealBuffer {
|
||||||
val kValue = k.toDouble()
|
val kValue = k.toDouble()
|
||||||
|
|
||||||
return if (a is DoubleBuffer) {
|
return if (a is RealBuffer) {
|
||||||
val aArray = a.array
|
val aArray = a.array
|
||||||
DoubleBuffer(DoubleArray(a.size) { aArray[it] * kValue })
|
RealBuffer(DoubleArray(a.size) { aArray[it] * kValue })
|
||||||
} else {
|
} else
|
||||||
DoubleBuffer(DoubleArray(a.size) { a[it] * kValue })
|
RealBuffer(DoubleArray(a.size) { a[it] * kValue })
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun multiply(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
override fun multiply(a: Buffer<Double>, b: Buffer<Double>): RealBuffer {
|
||||||
require(b.size == a.size) { "The size of the first buffer ${a.size} should be the same as for second one: ${b.size} " }
|
require(b.size == a.size) { "The size of the first buffer ${a.size} should be the same as for second one: ${b.size} " }
|
||||||
|
|
||||||
return if (a is DoubleBuffer && b is DoubleBuffer) {
|
return if (a is RealBuffer && b is RealBuffer) {
|
||||||
val aArray = a.array
|
val aArray = a.array
|
||||||
val bArray = b.array
|
val bArray = b.array
|
||||||
DoubleBuffer(DoubleArray(a.size) { aArray[it] * bArray[it] })
|
RealBuffer(DoubleArray(a.size) { aArray[it] * bArray[it] })
|
||||||
} else {
|
} else
|
||||||
DoubleBuffer(DoubleArray(a.size) { a[it] * b[it] })
|
RealBuffer(DoubleArray(a.size) { a[it] * b[it] })
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun divide(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
override fun divide(a: Buffer<Double>, b: Buffer<Double>): RealBuffer {
|
||||||
require(b.size == a.size) { "The size of the first buffer ${a.size} should be the same as for second one: ${b.size} " }
|
require(b.size == a.size) { "The size of the first buffer ${a.size} should be the same as for second one: ${b.size} " }
|
||||||
|
|
||||||
return if (a is DoubleBuffer && b is DoubleBuffer) {
|
return if (a is RealBuffer && b is RealBuffer) {
|
||||||
val aArray = a.array
|
val aArray = a.array
|
||||||
val bArray = b.array
|
val bArray = b.array
|
||||||
DoubleBuffer(DoubleArray(a.size) { aArray[it] / bArray[it] })
|
RealBuffer(DoubleArray(a.size) { aArray[it] / bArray[it] })
|
||||||
} else {
|
} else
|
||||||
DoubleBuffer(DoubleArray(a.size) { a[it] / b[it] })
|
RealBuffer(DoubleArray(a.size) { a[it] / b[it] })
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun sin(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
override fun sin(arg: Buffer<Double>): RealBuffer = if (arg is RealBuffer) {
|
||||||
val array = arg.array
|
val array = arg.array
|
||||||
DoubleBuffer(DoubleArray(arg.size) { sin(array[it]) })
|
RealBuffer(DoubleArray(arg.size) { sin(array[it]) })
|
||||||
} else {
|
} else {
|
||||||
DoubleBuffer(DoubleArray(arg.size) { sin(arg[it]) })
|
RealBuffer(DoubleArray(arg.size) { sin(arg[it]) })
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cos(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
override fun cos(arg: Buffer<Double>): RealBuffer = if (arg is RealBuffer) {
|
||||||
val array = arg.array
|
val array = arg.array
|
||||||
DoubleBuffer(DoubleArray(arg.size) { cos(array[it]) })
|
RealBuffer(DoubleArray(arg.size) { cos(array[it]) })
|
||||||
|
} else
|
||||||
|
RealBuffer(DoubleArray(arg.size) { cos(arg[it]) })
|
||||||
|
|
||||||
|
override fun tan(arg: Buffer<Double>): RealBuffer = if (arg is RealBuffer) {
|
||||||
|
val array = arg.array
|
||||||
|
RealBuffer(DoubleArray(arg.size) { tan(array[it]) })
|
||||||
|
} else
|
||||||
|
RealBuffer(DoubleArray(arg.size) { tan(arg[it]) })
|
||||||
|
|
||||||
|
override fun asin(arg: Buffer<Double>): RealBuffer = if (arg is RealBuffer) {
|
||||||
|
val array = arg.array
|
||||||
|
RealBuffer(DoubleArray(arg.size) { asin(array[it]) })
|
||||||
} else {
|
} else {
|
||||||
DoubleBuffer(DoubleArray(arg.size) { cos(arg[it]) })
|
RealBuffer(DoubleArray(arg.size) { asin(arg[it]) })
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun tan(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
override fun acos(arg: Buffer<Double>): RealBuffer = if (arg is RealBuffer) {
|
||||||
val array = arg.array
|
val array = arg.array
|
||||||
DoubleBuffer(DoubleArray(arg.size) { tan(array[it]) })
|
RealBuffer(DoubleArray(arg.size) { acos(array[it]) })
|
||||||
} else {
|
} else
|
||||||
DoubleBuffer(DoubleArray(arg.size) { tan(arg[it]) })
|
RealBuffer(DoubleArray(arg.size) { acos(arg[it]) })
|
||||||
}
|
|
||||||
|
|
||||||
override fun asin(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
override fun atan(arg: Buffer<Double>): RealBuffer = if (arg is RealBuffer) {
|
||||||
val array = arg.array
|
val array = arg.array
|
||||||
DoubleBuffer(DoubleArray(arg.size) { asin(array[it]) })
|
RealBuffer(DoubleArray(arg.size) { atan(array[it]) })
|
||||||
} else {
|
} else
|
||||||
DoubleBuffer(DoubleArray(arg.size) { asin(arg[it]) })
|
RealBuffer(DoubleArray(arg.size) { atan(arg[it]) })
|
||||||
}
|
|
||||||
|
|
||||||
override fun acos(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
override fun power(arg: Buffer<Double>, pow: Number): RealBuffer = if (arg is RealBuffer) {
|
||||||
val array = arg.array
|
val array = arg.array
|
||||||
DoubleBuffer(DoubleArray(arg.size) { acos(array[it]) })
|
RealBuffer(DoubleArray(arg.size) { array[it].pow(pow.toDouble()) })
|
||||||
} else {
|
} else
|
||||||
DoubleBuffer(DoubleArray(arg.size) { acos(arg[it]) })
|
RealBuffer(DoubleArray(arg.size) { arg[it].pow(pow.toDouble()) })
|
||||||
}
|
|
||||||
|
|
||||||
override fun atan(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
override fun exp(arg: Buffer<Double>): RealBuffer = if (arg is RealBuffer) {
|
||||||
val array = arg.array
|
val array = arg.array
|
||||||
DoubleBuffer(DoubleArray(arg.size) { atan(array[it]) })
|
RealBuffer(DoubleArray(arg.size) { exp(array[it]) })
|
||||||
} else {
|
} else
|
||||||
DoubleBuffer(DoubleArray(arg.size) { atan(arg[it]) })
|
RealBuffer(DoubleArray(arg.size) { exp(arg[it]) })
|
||||||
}
|
|
||||||
|
|
||||||
override fun power(arg: Buffer<Double>, pow: Number): DoubleBuffer = if (arg is DoubleBuffer) {
|
override fun ln(arg: Buffer<Double>): RealBuffer = if (arg is RealBuffer) {
|
||||||
val array = arg.array
|
val array = arg.array
|
||||||
DoubleBuffer(DoubleArray(arg.size) { array[it].pow(pow.toDouble()) })
|
RealBuffer(DoubleArray(arg.size) { ln(array[it]) })
|
||||||
} else {
|
} else
|
||||||
DoubleBuffer(DoubleArray(arg.size) { arg[it].pow(pow.toDouble()) })
|
RealBuffer(DoubleArray(arg.size) { ln(arg[it]) })
|
||||||
}
|
|
||||||
|
|
||||||
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]) })
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun ln(arg: Buffer<Double>): DoubleBuffer = if (arg is DoubleBuffer) {
|
|
||||||
val array = arg.array
|
|
||||||
DoubleBuffer(DoubleArray(arg.size) { ln(array[it]) })
|
|
||||||
} else {
|
|
||||||
DoubleBuffer(DoubleArray(arg.size) { ln(arg[it]) })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class RealBufferField(val size: Int) : ExtendedField<Buffer<Double>> {
|
class RealBufferField(val size: Int) : ExtendedField<Buffer<Double>> {
|
||||||
|
override val zero: Buffer<Double> by lazy { RealBuffer(size) { 0.0 } }
|
||||||
|
override val one: Buffer<Double> by lazy { RealBuffer(size) { 1.0 } }
|
||||||
|
|
||||||
override val zero: Buffer<Double> by lazy { DoubleBuffer(size) { 0.0 } }
|
override fun add(a: Buffer<Double>, b: Buffer<Double>): RealBuffer {
|
||||||
|
|
||||||
override val one: Buffer<Double> by lazy { DoubleBuffer(size) { 1.0 } }
|
|
||||||
|
|
||||||
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" }
|
require(a.size == size) { "The buffer size ${a.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.add(a, b)
|
return RealBufferFieldOperations.add(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun multiply(a: Buffer<Double>, k: Number): DoubleBuffer {
|
override fun multiply(a: Buffer<Double>, k: Number): RealBuffer {
|
||||||
require(a.size == size) { "The buffer size ${a.size} does not match context size $size" }
|
require(a.size == size) { "The buffer size ${a.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.multiply(a, k)
|
return RealBufferFieldOperations.multiply(a, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun multiply(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
override fun multiply(a: Buffer<Double>, b: Buffer<Double>): RealBuffer {
|
||||||
require(a.size == size) { "The buffer size ${a.size} does not match context size $size" }
|
require(a.size == size) { "The buffer size ${a.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.multiply(a, b)
|
return RealBufferFieldOperations.multiply(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun divide(a: Buffer<Double>, b: Buffer<Double>): DoubleBuffer {
|
override fun divide(a: Buffer<Double>, b: Buffer<Double>): RealBuffer {
|
||||||
require(a.size == size) { "The buffer size ${a.size} does not match context size $size" }
|
require(a.size == size) { "The buffer size ${a.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.divide(a, b)
|
return RealBufferFieldOperations.divide(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun sin(arg: Buffer<Double>): DoubleBuffer {
|
override fun sin(arg: Buffer<Double>): RealBuffer {
|
||||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.sin(arg)
|
return RealBufferFieldOperations.sin(arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cos(arg: Buffer<Double>): DoubleBuffer {
|
override fun cos(arg: Buffer<Double>): RealBuffer {
|
||||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.cos(arg)
|
return RealBufferFieldOperations.cos(arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun tan(arg: Buffer<Double>): DoubleBuffer {
|
override fun tan(arg: Buffer<Double>): RealBuffer {
|
||||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.tan(arg)
|
return RealBufferFieldOperations.tan(arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun asin(arg: Buffer<Double>): DoubleBuffer {
|
override fun asin(arg: Buffer<Double>): RealBuffer {
|
||||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.asin(arg)
|
return RealBufferFieldOperations.asin(arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun acos(arg: Buffer<Double>): DoubleBuffer {
|
override fun acos(arg: Buffer<Double>): RealBuffer {
|
||||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.acos(arg)
|
return RealBufferFieldOperations.acos(arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun atan(arg: Buffer<Double>): DoubleBuffer {
|
override fun atan(arg: Buffer<Double>): RealBuffer {
|
||||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.atan(arg)
|
return RealBufferFieldOperations.atan(arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun power(arg: Buffer<Double>, pow: Number): DoubleBuffer {
|
override fun power(arg: Buffer<Double>, pow: Number): RealBuffer {
|
||||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.power(arg, pow)
|
return RealBufferFieldOperations.power(arg, pow)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun exp(arg: Buffer<Double>): DoubleBuffer {
|
override fun exp(arg: Buffer<Double>): RealBuffer {
|
||||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.exp(arg)
|
return RealBufferFieldOperations.exp(arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun ln(arg: Buffer<Double>): DoubleBuffer {
|
override fun ln(arg: Buffer<Double>): RealBuffer {
|
||||||
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
require(arg.size == size) { "The buffer size ${arg.size} does not match context size $size" }
|
||||||
return RealBufferFieldOperations.ln(arg)
|
return RealBufferFieldOperations.ln(arg)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user