Fork me on GitHub
Arraymancer Technical reference Tutorial Spellbook (How-To's) Under the hood

Module higher_order_applymap

Procs

proc map[T; U: not (ref | string | seq)](t: Tensor[T]; f: T -> U): Tensor[U] {.
noInit
.}
Apply a unary function in an element-wise manner on Tensor[T], returning a new Tensor.
Usage with Nim's future module:
a.map(x => x+1) # Map the anonymous function x => x+1
Usage with named functions:
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 :
a .+ 1

map is especially useful to do multiple element-wise operations on a tensor in a single loop over the data.

For OpenMP compatibility, this map doesn't allow ref types as result like seq or string

  Source Edit
proc map[T; U: ref | string | seq](t: Tensor[T]; f: T -> U): Tensor[U] {.
noInit, noSideEffect
.}

Apply a unary function in an element-wise manner on Tensor[T], 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
proc apply[T](t: var Tensor[T]; f: T -> T)
Apply a unary function in an element-wise manner on Tensor[T], 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 future module:
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:
proc plusone[T](x: T): T =
  x + 1
a.apply(plusone) # Apply the function plusone in-place
  Source Edit
proc apply[T](t: var Tensor[T]; f: proc (x: var T))
Apply a unary function in an element-wise manner on Tensor[T], 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 future module:
  • Future module has a functional programming paradigm, anonymous function cannot mutate the arguments
Usage with named functions:
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 map2[T, U; V: not (ref | string | seq)](t1: Tensor[T]; f: (T, U) -> V; t2: Tensor[U]): Tensor[
    V] {.
noInit
.}

Apply a binary function in an element-wise manner on two Tensor[T], 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:
proc `**`[T](x, y: 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: ref | string | seq](t1: Tensor[T]; f: (T, U) -> V; t2: Tensor[U]): Tensor[V] {.
noInit, noSideEffect
.}

Apply a binary function in an element-wise manner on two Tensor[T], 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
proc apply2[T, U](a: var Tensor[T]; f: proc (x: var T; y: T); b: Tensor[U])

Apply a binary in-place function in an element-wise manner on two Tensor[T], returning a new Tensor.

The function is applied on the elements with the same coordinates.

Input:
  • A var tensor
  • A function
  • A tensor
Result:
  • Nothing, the var``Tensor is modified in-place Usage with named functions: .. code:: nim proc `**=`[T](x, y: 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

Templates

template apply_inline(t: var Tensor; op: untyped): untyped
  Source Edit
template apply2_inline[T; U](dest: var Tensor[T]; src: Tensor[U]; op: untyped): untyped
  Source Edit
template apply3_inline[T; U; V](dest: var Tensor[T]; src1: Tensor[U]; src2: Tensor[V];
                             op: untyped): untyped
  Source Edit
template map_inline[T](t: Tensor[T]; op: untyped): untyped
  Source Edit
template map2_inline[T; U](t1: Tensor[T]; t2: Tensor[U]; op: untyped): untyped
  Source Edit
template map3_inline[T; U; V](t1: Tensor[T]; t2: Tensor[U]; t3: Tensor[V]; op: untyped): untyped
  Source Edit