Procs
proc apply[T: KnownSupportsCopyMem](t: var Tensor[T]; f: proc (x: var T)) {. effectsOf: f.}
-
Apply a unary function in an element-wise manner on TensorT, in-place.
Input:
- a var Tensor
- An in-place function that returns no value
Result:
- Nothing, the var Tensor is modified in-place
Usage with Nim's sugar module:
- The sugar module has a functional programming paradigm, anonymous functions cannot mutate the arguments
Usage with named functions: .. code:: nim proc pluseqone[T](x: var T) = x += 1 a.apply(pluseqone) # Apply the in-place function pluseqone apply is especially useful to do multiple element-wise operations on a tensor in a single loop over the data.
Source Edit proc apply[T](t: var Tensor[T]; f: T -> T) {.effectsOf: f.}
-
Apply a unary function in an element-wise manner on TensorT, in-place.
Input:
- a var Tensor
- A function or anonymous function that returns a value
Result:
- Nothing, the var Tensor is modified in-place
Usage with Nim's sugar module: .. code:: nim var a = newTensor(5,5, int) # a must be var a.apply(x => x+1) # Map the anonymous function x => x+1 Usage with named functions: .. code:: nim proc plusone[T](x: T): T = x + 1 a.apply(plusone) # Apply the function plusone in-place
Source Edit proc apply2[T: KnownSupportsCopyMem; U](a: var Tensor[T]; f: proc (x: var T; y: T); b: Tensor[U]) {. effectsOf: f.}
-
Apply a binary in-place function in an element-wise manner on two TensorT, returning a new Tensor. Overload for types that are not mem-copyable.
The function is applied on the elements with the same coordinates.
Input:
- A var tensor
- A function
- A tensor
Result:
- Nothing, the varTensor is modified in-place
Usage with named functions: .. code:: nim proc **=T = # We create a new in-place power **= function that works on 2 scalars x = pow(x, y) a.apply2(**=, b) # Or apply2(a, **=, b)
apply2 is especially useful to do multiple element-wise operations on a two tensors in a single loop over the data. for example A += alpha * sin(A) + B
Source Edit proc apply2[T: not KnownSupportsCopyMem; U](a: var Tensor[T]; f: proc (x: var T; y: T); b: Tensor[U]) {.effectsOf: f.}
-
Apply a binary in-place function in an element-wise manner on two TensorT, returning a new Tensor. Overload for types that are not mem-copyable.
The function is applied on the elements with the same coordinates.
Input:
- A var tensor
- A function
- A tensor
Result:
- Nothing, the varTensor is modified in-place
Usage with named functions: .. code:: nim proc **=T = # We create a new in-place power **= function that works on 2 scalars x = pow(x, y) a.apply2(**=, b) # Or apply2(a, **=, b) apply2 is especially useful to do multiple element-wise operations on a two tensors in a single loop over the data. for example A += alpha * sin(A) + B
Source Edit proc map[T; U](t: Tensor[T]; f: T -> U): Tensor[U] {.noinit, effectsOf: f.}
-
Apply a unary function in an element-wise manner on TensorT, returning a new Tensor. Usage with Nim's sugar module: .. code:: nim a.map(x => x+1) # Map the anonymous function x => x+1 Usage with named functions: .. code:: nim proc plusone[T](x: T): T = x + 1 a.map(plusone) # Map the function plusone Note: for basic operation, you can use implicit broadcasting instead with operators prefixed by a dot : .. code:: nim a +. 1 map is especially useful to do multiple element-wise operations on a tensor in a single loop over the data.
For types that are not mem-copyable types (ref, string, etc.) a non OpenMP accelerated version of apply2_inline is used internally!
Source Edit proc map2[T, U; V: KnownSupportsCopyMem](t1: Tensor[T]; f: (T, U) -> V; t2: Tensor[U]): Tensor[V] {.noinit, effectsOf: f.}
-
Apply a binary function in an element-wise manner on two TensorT, returning a new Tensor.
The function is applied on the elements with the same coordinates.
Input:
- A tensor
- A function
- A tensor
Result:
- A new tensor
Usage with named functions: .. code:: nim proc **T: T = # We create a new power ** function that works on 2 scalars pow(x, y) a.map2(**, b) # Or map2(a, **, b) map2 is especially useful to do multiple element-wise operations on a two tensors in a single loop over the data. for example alpha * sin(A) + B
For OpenMP compatibility, this map2 doesn't allow ref types as result like seq or string
Source Edit proc map2[T, U; V: not KnownSupportsCopyMem](t1: Tensor[T]; f: (T, U) -> V; t2: Tensor[U]): Tensor[V] {.noinit, noSideEffect, effectsOf: f.}
-
Apply a binary function in an element-wise manner on two TensorT, returning a new Tensor.
This is a fallback for ref types as OpenMP will not work with if the results allocate memory managed by GC.
Source Edit
Templates
template apply2_inline[T: KnownSupportsCopyMem; U](dest: var Tensor[T]; src: Tensor[U]; op: untyped): untyped
- Source Edit
template apply2_inline[T: not KnownSupportsCopyMem; U](dest: var Tensor[T]; src: Tensor[U]; op: untyped): untyped
- NOTE: this is an overload of apply2_inline, which also works with Source Edit
template apply3_inline[T: KnownSupportsCopyMem; U, V](dest: var Tensor[T]; src1: Tensor[U]; src2: Tensor[V]; op: untyped): untyped
- Source Edit
template apply_inline[T: KnownSupportsCopyMem](t: var Tensor[T]; op: untyped): untyped
- Source Edit
template apply_inline[T: not KnownSupportsCopyMem](t: var Tensor[T]; op: untyped): untyped
- Source Edit
template map2_inline[T, U](t1: Tensor[T]; t2: Tensor[U]; op: untyped): untyped
- Source Edit
template map_inline[T](t: Tensor[T]; op: untyped): untyped
- Source Edit