OjAlgo support module #145

Closed
CommanderTvis wants to merge 1 commits from ojalgo into dev
7 changed files with 125 additions and 1 deletions

View File

@ -19,6 +19,7 @@
- Complex power
- Separate methods for UInt, Int and Number powers. NaN safety.
- Tensorflow prototype
- OjAlgo support
### Changed
- Exponential operations merged with hyperbolic functions

View File

@ -0,0 +1,15 @@
plugins {
kotlin("jvm")
id("ru.mipt.npm.gradle.common")
}
dependencies {
api("org.ojalgo:ojalgo:48.4.1")
api(project(":kmath-complex"))
}
readme {
description = "OjAlgo bindings"
maturity = ru.mipt.npm.gradle.Maturity.PROTOTYPE
propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md"))
}

View File

@ -0,0 +1,42 @@
package space.kscience.kmath.ojalgo
import org.ojalgo.algebra.ScalarOperation
import space.kscience.kmath.operations.Field
import space.kscience.kmath.operations.Group
import space.kscience.kmath.operations.Ring
import space.kscience.kmath.operations.invoke
public open class OjalgoGroup<T>(override val zero: T) : Group<T>
where T : org.ojalgo.algebra.Group.Additive<T> {
public override fun add(a: T, b: T): T = a.add(b)
public override fun T.unaryMinus(): T = negate()
}
public open class OjalgoRing<T>(zero: T, override val one: T) : OjalgoGroup<T>(zero),
Ring<T> where T : org.ojalgo.algebra.Ring<T> {
public override fun multiply(a: T, b: T): T = a.multiply(b)
}
public open class OjalgoField<T>(zero: T, one: T) : OjalgoRing<T>(zero, one),
Field<T> where T : org.ojalgo.algebra.Field<T>, T : ScalarOperation.Multiplication<T, *> {
public override fun divide(a: T, b: T): T = a.divide(b)
public override fun scale(a: T, value: Double): T = a.multiply(value)
}
internal inline fun <T, R> Field<T>.convert(crossinline tToR: (T) -> R, crossinline rToT: (R) -> T): Field<R> =
object : Field<R> {
override val zero: R
get() = tToR(this@convert.zero)
override val one: R
get() = tToR(this@convert.one)
override fun add(a: R, b: R): R = tToR(this@convert.add(rToT(a), rToT(b)))
override fun multiply(a: R, b: R): R = tToR(this@convert.multiply(rToT(a), rToT(b)))
override fun divide(a: R, b: R): R = tToR(this@convert.divide(rToT(a), rToT(b)))
override fun R.unaryMinus(): R = tToR(this@convert { -rToT(this@unaryMinus) })
override fun scale(a: R, value: Double): R {
return tToR(this@convert.scale(rToT(a), value))
}
}

View File

@ -0,0 +1,34 @@
package space.kscience.kmath.ojalgo
import org.ojalgo.structure.Access1D
import org.ojalgo.structure.Access2D
import space.kscience.kmath.linear.Matrix
import space.kscience.kmath.linear.Point
public class OjalgoMatrix<T : Comparable<T>>(public val origin: Access2D<T>) : Matrix<T> {
public override val rowNum: Int
get() = Math.toIntExact(origin.countRows())
public override val colNum: Int
get() = Math.toIntExact(origin.countColumns())
public override fun get(i: Int, j: Int): T = origin[i.toLong(), j.toLong()]
}
public class OjalgoVector<T : Comparable<T>>(public val origin: Access1D<T>) : Point<T> {
public override val size: Int
get() = origin.size()
override fun get(index: Int): T = origin[index.toLong()]
public override operator fun iterator(): Iterator<T> = object : Iterator<T> {
private var cursor: Int = 0
override fun next(): T {
cursor += 1
return this@OjalgoVector[cursor - 1]
}
override fun hasNext(): Boolean = cursor < size
}
}

View File

@ -0,0 +1,31 @@
package space.kscience.kmath.ojalgo
import org.ojalgo.scalar.*
import space.kscience.kmath.complex.Complex
import space.kscience.kmath.operations.Field
public val OjalgoMoneyField: Field<Scalar<Money>> = OjalgoField<Scalar<Money>>(Money.ZERO, Money.ONE)
public val OjalgoComplexField: Field<Complex> =
OjalgoField<Scalar<ComplexNumber>>(ComplexNumber.ZERO, ComplexNumber.ONE).convert(
{ Complex(it.get().real, it.get().imaginary) },
{ ComplexNumber.of(it.re, it.im) },
)
public val OjalgoRationalField: Field<Double> =
OjalgoField<Scalar<RationalNumber>>(RationalNumber.ZERO, RationalNumber.ONE).convert(
Scalar<RationalNumber>::doubleValue,
RationalNumber::valueOf,
)
public val OjalgoQuaternionField: Field<space.kscience.kmath.complex.Quaternion> =
OjalgoField<Scalar<Quaternion>>(Quaternion.ZERO, Quaternion.ONE).convert(
{ x -> space.kscience.kmath.complex.Quaternion(x.get().scalar(), x.get().i, x.get().j, x.get().k) },
{ (s, i, j, k) -> Quaternion.of(s, i, j, k) },
)
public val OjalgoPrimitiveField: Field<Double> =
OjalgoField<Scalar<Double>>(PrimitiveScalar.ZERO, PrimitiveScalar.ONE).convert(
Scalar<Double>::doubleValue,
PrimitiveScalar::valueOf,
)

View File

@ -26,5 +26,6 @@ include(
":kmath-symja",
":kmath-jafama",
":examples",
":kmath-ojalgo",
":benchmarks",
)