diff --git a/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/Noa.kt b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/Noa.kt new file mode 100644 index 000000000..20a5612d4 --- /dev/null +++ b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/Noa.kt @@ -0,0 +1,49 @@ +/* + * 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.noa + +import space.kscience.kmath.noa.memory.NoaScope +import space.kscience.kmath.noa.memory.withNoaScope + +public class NoaDouble +internal constructor(scope: NoaScope) : + NoaDoubleAlgebra(scope) + +public fun NoaDouble(block: NoaDouble.() -> R): R? = + withNoaScope { NoaDouble(this).block() } + +public fun NoaDouble(scope: NoaScope, block: NoaDouble.() -> R): R? = + withNoaScope(scope) { NoaDouble(this).block() } + +public class NoaFloat +internal constructor(scope: NoaScope) : + NoaFloatAlgebra(scope) + +public fun NoaFloat(block: NoaFloat.() -> R): R? = + withNoaScope { NoaFloat(this).block() } + +public fun NoaFloat(scope: NoaScope, block: NoaFloat.() -> R): R? = + withNoaScope(scope) { NoaFloat(this).block() } + +public class NoaLong +internal constructor(scope: NoaScope) : + NoaLongAlgebra(scope) + +public fun NoaLong(block: NoaLong.() -> R): R? = + withNoaScope { NoaLong(this).block() } + +public fun NoaLong(scope: NoaScope, block: NoaLong.() -> R): R? = + withNoaScope(scope) { NoaLong(this).block() } + +public class NoaInt +internal constructor(scope: NoaScope) : + NoaIntAlgebra(scope) + +public fun NoaInt(block: NoaInt.() -> R): R? = + withNoaScope { NoaInt(this).block() } + +public fun NoaInt(scope: NoaScope, block: NoaInt.() -> R): R? = + withNoaScope(scope) { NoaInt(this).block() } diff --git a/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/algebras.kt b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/algebras.kt index ef228d96a..b386b36ca 100644 --- a/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/algebras.kt +++ b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/algebras.kt @@ -15,7 +15,8 @@ import space.kscience.kmath.tensors.core.TensorLinearStructure public sealed class NoaAlgebra> -constructor(protected val scope: NoaScope) : TensorAlgebra { +protected constructor(protected val scope: NoaScope) : + TensorAlgebra { protected abstract val Tensor.tensor: TensorType @@ -132,8 +133,10 @@ constructor(protected val scope: NoaScope) : TensorAlgebra { } -public abstract class NoaPartialDivisionAlgebra> -internal constructor(scope: NoaScope) : NoaAlgebra(scope), LinearOpsTensorAlgebra, +public sealed class NoaPartialDivisionAlgebra> +protected constructor(scope: NoaScope) : + NoaAlgebra(scope), + LinearOpsTensorAlgebra, AnalyticTensorAlgebra { override operator fun Tensor.div(other: Tensor): TensorType { @@ -274,7 +277,8 @@ internal constructor(scope: NoaScope) : NoaAlgebra(scope), Linear } -public class NoaDoubleAlgebra(scope: NoaScope) : +public sealed class NoaDoubleAlgebra +protected constructor(scope: NoaScope) : NoaPartialDivisionAlgebra(scope) { private fun Tensor.castHelper(): NoaDoubleTensor = @@ -349,7 +353,8 @@ public class NoaDoubleAlgebra(scope: NoaScope) : } -public class NoaFloatAlgebra(scope: NoaScope) : +public sealed class NoaFloatAlgebra +protected constructor(scope: NoaScope) : NoaPartialDivisionAlgebra(scope) { private fun Tensor.castHelper(): NoaFloatTensor = @@ -424,7 +429,8 @@ public class NoaFloatAlgebra(scope: NoaScope) : } -public class NoaLongAlgebra(scope: NoaScope) : +public sealed class NoaLongAlgebra +protected constructor(scope: NoaScope) : NoaAlgebra(scope) { private fun Tensor.castHelper(): NoaLongTensor = @@ -484,7 +490,8 @@ public class NoaLongAlgebra(scope: NoaScope) : } -public class NoaIntAlgebra(scope: NoaScope) : +public sealed class NoaIntAlgebra +protected constructor(scope: NoaScope) : NoaAlgebra(scope) { private fun Tensor.castHelper(): NoaIntTensor = diff --git a/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaScope.kt b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaScope.kt index 08df2d79b..6dd2ed318 100644 --- a/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaScope.kt +++ b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaScope.kt @@ -30,14 +30,14 @@ public class NoaScope { } } -internal fun withNoaScope(block: NoaScope.() -> R): R? { +internal inline fun withNoaScope(block: NoaScope.() -> R): R? { val noaScope = NoaScope() val result = try { noaScope.block() } catch (e: Throwable) { null } noaScope.disposeAll() return result } -internal fun withNoaScope(scope: NoaScope, block: NoaScope.() -> R): R? { +internal inline fun withNoaScope(scope: NoaScope, block: NoaScope.() -> R): R? { val noaScope = NoaScope() val result = try { noaScope.block() } catch (e: Throwable) { null } if (result == null){