diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 000000000..2c99c7bc1 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,32 @@ +# KMath + +## [Unreleased] + +### Added +- Functional Expressions API +- Mathematical Syntax Tree, its interpreter and API +- String to MST parser (https://github.com/mipt-npm/kmath/pull/120) +- MST to JVM bytecode translator (https://github.com/mipt-npm/kmath/pull/94) +- FloatBuffer (specialized MutableBuffer over FloatArray) +- FlaggedBuffer to associate primitive numbers buffer with flags (to mark values infinite or missing, etc.) +- Specialized builder functions for all primitive buffers like `IntBuffer(25) { it + 1 }` (https://github.com/mipt-npm/kmath/pull/125) +- Interface `NumericAlgebra` where `number` operation is available to convert numbers to algebraic elements +- Inverse trigonometric functions support in ExtendedField (`asin`, `acos`, `atan`) (https://github.com/mipt-npm/kmath/pull/114) +- New space extensions: `average` and `averageWith` +- Local coding conventions +- Geometric Domains API in `kmath-core` +- Blocking chains in `kmath-coroutines` + +### Changed +- BigInteger and BigDecimal algebra: JBigDecimalField has companion object with default math context; minor optimizations +- `power(T, Int)` extension function has preconditions and supports `Field` +- Memory objects have more preconditions (overflow checking) +- `tg` function is renamed to `tan` (https://github.com/mipt-npm/kmath/pull/114) +- Gradle version: 6.3 -> 6.5.1 +- Moved probability distributions to commons-rng and to `kmath-prob`. + +### Fixed +- Missing copy method in Memory implementation on JS (https://github.com/mipt-npm/kmath/pull/106) +- D3.dim value in `kmath-dimensions` +- Multiplication in integer rings in `kmath-core` (https://github.com/mipt-npm/kmath/pull/101) +- Commons RNG compatibility (https://github.com/mipt-npm/kmath/issues/93) diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt index 9abd43c8f..f18bde597 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt @@ -61,7 +61,7 @@ inline operator fun , R> A.invoke(block: A.() -> R): R = run(bloc * Represents "semispace", i.e. algebraic structure with associative binary operation called "addition" as well as * multiplication by scalars. * - * @param T the type of element of this "semispace". + * @param T the type of element of this semispace. */ interface SpaceOperations : Algebra { /** diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/AlgebraExtensions.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/AlgebraExtensions.kt index 6712d3baa..00b16dc98 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/AlgebraExtensions.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/AlgebraExtensions.kt @@ -4,7 +4,7 @@ package scientifik.kmath.operations * Returns the sum of all elements in the iterable in this [Space]. * * @receiver the algebra that provides addition. - * @param data the collection to sum up. + * @param data the iterable to sum up. * @return the sum. */ fun Space.sum(data: Iterable): T = data.fold(zero) { left, right -> add(left, right) } @@ -13,11 +13,29 @@ fun Space.sum(data: Iterable): T = data.fold(zero) { left, right -> ad * Returns the sum of all elements in the sequence in this [Space]. * * @receiver the algebra that provides addition. - * @param data the collection to sum up. + * @param data the sequence to sum up. * @return the sum. */ fun Space.sum(data: Sequence): T = data.fold(zero) { left, right -> add(left, right) } +/** + * Returns an average value of elements in the iterable in this [Space]. + * + * @receiver the algebra that provides addition and division. + * @param data the iterable to find average. + * @return the average value. + */ +fun Space.average(data: Iterable): T = sum(data) / data.count() + +/** + * Returns an average value of elements in the sequence in this [Space]. + * + * @receiver the algebra that provides addition and division. + * @param data the sequence to find average. + * @return the average value. + */ +fun Space.average(data: Sequence): T = sum(data) / data.count() + /** * Returns the sum of all elements in the iterable in provided space. * @@ -25,7 +43,34 @@ fun Space.sum(data: Sequence): T = data.fold(zero) { left, right -> ad * @param space the algebra that provides addition. * @return the sum. */ -fun > Iterable.sumWith(space: S): T = space.sum(this) +fun Iterable.sumWith(space: Space): T = space.sum(this) + +/** + * Returns the sum of all elements in the sequence in provided space. + * + * @receiver the collection to sum up. + * @param space the algebra that provides addition. + * @return the sum. + */ +fun Sequence.sumWith(space: Space): T = space.sum(this) + +/** + * Returns an average value of elements in the iterable in this [Space]. + * + * @receiver the iterable to find average. + * @param space the algebra that provides addition and division. + * @return the average value. + */ +fun Iterable.averageWith(space: Space): T = space.average(this) + +/** + * Returns an average value of elements in the sequence in this [Space]. + * + * @receiver the sequence to find average. + * @param space the algebra that provides addition and division. + * @return the average value. + */ +fun Sequence.averageWith(space: Space): T = space.average(this) //TODO optimized power operation diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt index f1535e212..ef4c9bcf1 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt @@ -40,7 +40,7 @@ private val PI_DIV_2 = Complex(PI / 2, 0) /** * A field of [Complex]. */ -object ComplexField : ExtendedField { +object ComplexField : ExtendedField, Norm { override val zero: Complex = 0.0.toComplex() override val one: Complex = 1.0.toComplex() @@ -153,6 +153,8 @@ object ComplexField : ExtendedField { */ operator fun Double.times(c: Complex): Complex = Complex(c.re * this, c.im * this) + override fun norm(arg: Complex): Complex = arg.conjugate * arg + override fun symbol(value: String): Complex = if (value == "i") i else super.symbol(value) } diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/ShortBuffer.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/ShortBuffer.kt index a67cbec62..c6f19feaf 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/ShortBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/ShortBuffer.kt @@ -1,7 +1,7 @@ package scientifik.kmath.structures /** - * Specialized [MutableBuffer] implementation over [ShortBuffer]. + * Specialized [MutableBuffer] implementation over [ShortArray]. * * @property array the underlying array. */ diff --git a/kmath-core/src/commonTest/kotlin/scientifik/kmath/operations/ComplexTest.kt b/kmath-core/src/commonTest/kotlin/scientifik/kmath/operations/ComplexTest.kt new file mode 100644 index 000000000..e2132b75f --- /dev/null +++ b/kmath-core/src/commonTest/kotlin/scientifik/kmath/operations/ComplexTest.kt @@ -0,0 +1,38 @@ +package scientifik.kmath.operations + +import kotlin.test.Test +import kotlin.test.assertEquals + +internal class ComplexTest { + @Test + fun conjugate() { + assertEquals( + Complex(0, -42), (ComplexField.i * 42).conjugate + ) + } + + @Test + fun reciprocal() { + assertEquals(Complex(0.5, -0.0), 2.toComplex().reciprocal) + } + + @Test + fun r() { + assertEquals(kotlin.math.sqrt(2.0), (ComplexField.i + 1.0.toComplex()).r) + } + + @Test + fun theta() { + assertEquals(0.0, 1.toComplex().theta) + } + + @Test + fun toComplex() { + assertEquals(Complex(42, 0), 42.toComplex()) + assertEquals(Complex(42.0, 0), 42.0.toComplex()) + assertEquals(Complex(42f, 0), 42f.toComplex()) + assertEquals(Complex(42.0, 0), 42.0.toComplex()) + assertEquals(Complex(42.toByte(), 0), 42.toByte().toComplex()) + assertEquals(Complex(42.toShort(), 0), 42.toShort().toComplex()) + } +} \ No newline at end of file