Add contentEquals extension to ND algebra and LinearSpace

This commit is contained in:
Iaroslav Postovalov 2021-06-10 01:02:57 +07:00 committed by Iaroslav Postovalov
parent 21dd5ddd7d
commit 86a45504e3
2 changed files with 42 additions and 1 deletions

View File

@ -29,7 +29,7 @@ public typealias MutableMatrix<T> = MutableStructure2D<T>
public typealias Point<T> = Buffer<T>
/**
* Basic operations on matrices and vectors. Operates on [Matrix].
* Basic operations on matrices and vectors.
*
* @param T the type of items in the matrices.
* @param A the type of ring over [T].

View File

@ -5,8 +5,11 @@
package space.kscience.kmath.nd
import space.kscience.kmath.linear.LinearSpace
import space.kscience.kmath.misc.PerformancePitfall
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.Ring
import space.kscience.kmath.operations.invoke
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.BufferFactory
import kotlin.jvm.JvmName
@ -145,6 +148,44 @@ public interface StructureND<out T> {
}
}
/**
* Indicates whether some [StructureND] is equal to another one.
*/
@PerformancePitfall
public fun <T : Comparable<T>> AlgebraND<T, Ring<T>>.contentEquals(
st1: StructureND<T>,
st2: StructureND<T>,
): Boolean = StructureND.contentEquals(st1, st2)
/**
* Indicates whether some [StructureND] is equal to another one.
*/
@PerformancePitfall
public fun <T : Comparable<T>> LinearSpace<T, Ring<T>>.contentEquals(
st1: StructureND<T>,
st2: StructureND<T>,
): Boolean = StructureND.contentEquals(st1, st2)
/**
* Indicates whether some [StructureND] is equal to another one with [absoluteTolerance].
*/
@PerformancePitfall
public fun <T : Comparable<T>> GroupND<T, Ring<T>>.contentEquals(
st1: StructureND<T>,
st2: StructureND<T>,
absoluteTolerance: T,
): Boolean = st1.elements().all { (index, value) -> elementContext { (value - st2[index]) } < absoluteTolerance }
/**
* Indicates whether some [StructureND] is equal to another one with [absoluteTolerance].
*/
@PerformancePitfall
public fun <T : Comparable<T>> LinearSpace<T, Ring<T>>.contentEquals(
st1: StructureND<T>,
st2: StructureND<T>,
absoluteTolerance: T,
): Boolean = st1.elements().all { (index, value) -> elementAlgebra { (value - st2[index]) } < absoluteTolerance }
/**
* Returns the value at the specified indices.
*