From 0e8eb44670181a828ac4b9858386b8f3204bef8b Mon Sep 17 00:00:00 2001 From: "anna.pomytkina" Date: Sun, 5 Jun 2022 13:36:00 +0300 Subject: [PATCH] new TensorFlow operations are added. Binary Logic functions were moved into separate interface from LinearAlgebra --- .../kscience/kmath/expressions/MstAlgebra.kt | 4 +-- .../kscience/kmath/operations/BinaryLogic.kt | 36 +++++++++++++++++++ .../kscience/kmath/operations/LogicAlgebra.kt | 31 +--------------- .../kscience/kmath/tensorflow/tfOperations.kt | 10 +++++- 4 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BinaryLogic.kt diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt index 4bd2a6c53..b7ee695d7 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt @@ -167,9 +167,9 @@ public object MstLogicAlgebra : LogicAlgebra { override fun bindSymbolOrNull(value: String): MST = super.bindSymbolOrNull(value) ?: StringSymbol(value) override fun const(boolean: Boolean): Symbol = if (boolean) { - LogicAlgebra.TRUE + BinaryLogic.TRUE } else { - LogicAlgebra.FALSE + BinaryLogic.FALSE } override fun MST.not(): MST = MST.Unary(Boolean::not.name, this) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BinaryLogic.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BinaryLogic.kt new file mode 100644 index 000000000..75cbeca05 --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BinaryLogic.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2018-2021 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.operations + +import space.kscience.kmath.expressions.Symbol +import space.kscience.kmath.expressions.symbol + + interface BinaryLogic { + /** + * Logic 'not' + */ + public operator fun T.not(): T + + /** + * Logic 'and' + */ + public infix fun T.and(other: T): T + + /** + * Logic 'or' + */ + public infix fun T.or(other: T): T + + /** + * Logic 'xor' + */ + public infix fun T.xor(other: T): T + + companion object { + public val TRUE: Symbol by symbol + public val FALSE: Symbol by symbol + } +} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt index 9037525e1..1af6afd87 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt @@ -5,16 +5,13 @@ package space.kscience.kmath.operations -import space.kscience.kmath.expressions.Symbol -import space.kscience.kmath.expressions.symbol import space.kscience.kmath.misc.UnstableKMathAPI /** * An algebra for generic boolean logic */ @UnstableKMathAPI -public interface LogicAlgebra : Algebra { - +public interface LogicAlgebra : Algebra, BinaryLogic { /** * Represent constant [Boolean] as [T] */ @@ -38,32 +35,6 @@ public interface LogicAlgebra : Algebra { override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = { l, r -> binaryOperation(operation, l, r) } - - /** - * Logic 'not' - */ - public operator fun T.not(): T - - /** - * Logic 'and' - */ - public infix fun T.and(other: T): T - - /** - * Logic 'or' - */ - public infix fun T.or(other: T): T - - /** - * Logic 'xor' - */ - public infix fun T.xor(other: T): T - - - public companion object { - public val TRUE: Symbol by symbol - public val FALSE: Symbol by symbol - } } /** diff --git a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt index f67c333ce..e37608adf 100644 --- a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt +++ b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt @@ -20,4 +20,12 @@ public fun TensorFlowAlgebra.sin( public fun TensorFlowAlgebra.cos( arg: StructureND, -): TensorFlowOutput where A : TrigonometricOperations, A : Ring = arg.operate { ops.math.cos(it) } \ No newline at end of file +): TensorFlowOutput where A : TrigonometricOperations, A : Ring = arg.operate { ops.math.cos(it) } + +public fun TensorFlowAlgebra.tan( + arg: StructureND, +): TensorFlowOutput where A : TrigonometricOperations, A : Ring = arg.operate { ops.math.tan(it) } + +public fun TensorFlowAlgebra.abs( + arg: StructureND, +): TensorFlowOutput where A : TrigonometricOperations, A : Ring = arg.operate { ops.math.abs(it) } \ No newline at end of file