Fork me on GitHub

src/arraymancer/tensor/init_cpu

Search:
Group by:
  Source Edit

Procs

proc arange[T: SomeNumber](start, stop, step: T): Tensor[T] {.noinit.}

Creates a new 1d-tensor with values evenly spaced by step in the half-open interval [start, stop)

Resulting size is ceil((stop - start) / step)

⚠️ Warnings: To limit floating point rounding issues, size is computed by converting to float64.

  • It is recommended to add a small epsilon for non-integer steps
  • float64 cannot represent exactly integers over 2^32 (~4.3 billions)
  Source Edit
proc geomspace[T: SomeFloat](start, stop: T; num: int; endpoint = true): Tensor[
    float] {.noinit, inline.}

Creates a new 1d-tensor with num values linearly spaced in a log scale (i.e. a geometric progression). This is similar to logspace, but with the interval endpoints specified directly as start, stop (endpoint == true) or in the half open interval [start, stop) (endpoint == false).

Resulting size is num.

  Source Edit
proc linspace[T: SomeNumber](start, stop: T; num: int; endpoint = true): Tensor[
    float] {.noinit.}

Creates a new 1d-tensor with num values linearly spaced between the closed interval start, stop (endpoint == true) or in the half open interval [start, stop) (endpoint == false).

Resulting size is num.

  Source Edit
proc logspace[T: SomeNumber](start, stop: T; num: int; base = 10.0;
                             endpoint = true): Tensor[float] {.noinit.}

Creates a new 1d-tensor with num values linearly spaced in log space of base base either in the closed interval start, stop (endpoint == true) or in the half open interval [start, stop) (endpoint == false).

Note that the given start, stop arguments refer to the exponents of base!

Resulting size is num.

  Source Edit
proc newTensorUninit[T](shape: Metadata): Tensor[T] {.noinit, inline.}
Creates a new uninitialized Tensor of type T on the Cpu backend Input:
- Shape of the Tensor

Result:

- A Tensor of the proper shape with NO initialization

Warning ⚠:

- Tensor data is uninitialized and contains garbage.
  Source Edit
proc newTensorUninit[T](shape: varargs[int] = [0]): Tensor[T] {.noinit, inline.}
Creates a new uninitialized Tensor of type T on the Cpu backend Input:
- Shape of the Tensor (defaults to an empty rank-1 tensor)

Result:

- A Tensor of the proper shape with NO initialization

Warnings ⚠:

- Tensor data is uninitialized and contains garbage.
- If no shape is provided, a 1D tensor of size 0 is created.
  It is possible to create a rank-0 tensor by explicitly
  providing an empty shape `[]` (e.g. `newTensorUninit[float]([])`).
  Source Edit
proc newTensorUninit[T](size: int): Tensor[T] {.noinit, inline.}
Overload above taking varargs[int] to avoid "expression * cannot be called" errors if called in a template. Warning: This will create a 1D tensor!   Source Edit
proc newTensorWith[T](shape: Metadata; value: T): Tensor[T] {.noinit.}
Creates a new Tensor filled with the given value Input:
- Shape of the Tensor
- Type of its elements
- Value to initialize its elements

Result:

- A Tensor of the proper shape initialized with
  the given value
  Source Edit
proc newTensorWith[T](shape: varargs[int]; value: T): Tensor[T] {.noinit.}
Creates a new Tensor filled with the given value Input:
- Shape of the Tensor
- Type of its elements
- Value to initialize its elements

Result:

- A Tensor of the proper shape initialized with
  the given value
  Source Edit
proc ones[T: SomeNumber | Complex[float32] | Complex[float64] | bool](
    shape: varargs[int]): Tensor[T] {.noinit, inline.}
Creates a new Tensor filled with 1 Input:
- Shape of the Tensor
- Type of its elements

Result:

- A one-ed Tensor of the same shape
  Source Edit
proc ones[T: SomeNumber | Complex[float32] | Complex[float64] | bool](
    shape: Metadata): Tensor[T] {.noinit, inline.}
Creates a new Tensor filled with 1 Input:
- Shape of the Tensor
- Type of its elements

Result:

- A one-ed Tensor of the same shape
  Source Edit
proc ones_like[T: SomeNumber | Complex[float32] | Complex[float64] | bool](
    t: Tensor[T]): Tensor[T] {.noinit, inline.}
Creates a new Tensor filled with 1 with the same shape as the input and filled with 1 Input:
- Tensor

Result:

- A one-ed Tensor of the same shape
  Source Edit
proc randomNormalTensor[T: SomeFloat](shape: varargs[int]; mean: T = 0;
                                      std: T = 1): Tensor[T] {.noinit.}

Creates a new Tensor filled with values in the normal distribution

Random seed can be set by importing random and randomize(seed) Input:

- a shape
- the mean (default 0)
- the standard deviation (default 1)

Result:

- A tensor of the input shape filled with random values in the normal distribution
  Source Edit
proc randomTensor(shape: varargs[int]; max: int): Tensor[int] {.noinit,
    ...raises: [], tags: [], forbids: [].}

Creates a new int Tensor filled with values between 0 and max (inclusive).

Random seed can be set by importing random and randomize(seed) Input:

- a shape
- the max value possible (integer, inclusive)
- a tensor backend

Result:

- A tensor of the input shape filled with random values between 0 and max input value (excluded)
  Source Edit
proc randomTensor[T: bool](shape: varargs[int]): Tensor[T] {.noinit.}

Creates a new bool Tensor.

Random seed can be set by importing random and randomize(seed) Input:

- a shape

Result:

- A tensor of the input shape filled with random bool values
  Source Edit
proc randomTensor[T: SomeFloat](shape: varargs[int]; max: T): Tensor[T] {.noinit.}

Creates a new float Tensor filled with values between 0 and max.

Random seed can be set by importing random and randomize(seed) Input:

- a shape
- the max value possible (float)
- a tensor backend

Result:

- A tensor of the input shape filled with random values between 0 and max input value
  Source Edit
proc randomTensor[T](shape: varargs[int]; sample_source: openArray[T]): Tensor[T] {.
    noinit.}

Creates a new Tensor filled with values uniformly sampled from sample_source

Random seed can be set by importing random and randomize(seed) Input:

- a shape
- a sample_source

Result:

- A tensor of the input shape filled with random values from ``sample_source``
  Source Edit
proc randomTensor[T](shape: varargs[int]; slice: Slice[T]): Tensor[T] {.noinit.}

Creates a new int Tensor filled with values in the Slice range (inclusive).

Random seed can be set by importing random and randomize(seed) Input:

- a shape
- a range/slice
- a tensor backend

Result:

- A tensor of the input shape filled with random values in the slice range
  Source Edit
proc zeros[T: SomeNumber | Complex[float32] | Complex[float64] | bool](
    shape: Metadata): Tensor[T] {.noinit, inline.}

Creates a new Tensor filled with 0

Input:

- Shape of the Tensor
- Type of its elements

Result:

- A zero-ed Tensor of the input shape on backend Cpu
  Source Edit
proc zeros[T: SomeNumber | Complex[float32] | Complex[float64] | bool](
    shape: varargs[int]): Tensor[T] {.noinit, inline.}

Creates a new Tensor filled with 0

Input:

- Shape of the Tensor
- Type of its elements

Result:

- A zero-ed Tensor of the input shape on backend Cpu
  Source Edit
proc zeros_like[T: SomeNumber | Complex[float32] | Complex[float64] | bool](
    t: Tensor[T]): Tensor[T] {.noinit, inline.}
Creates a new Tensor filled with 0 with the same shape as the input Input:
- Shape of the Tensor
- Type of its elements

Result:

- A zero-ed Tensor of the same shape
  Source Edit

Templates

template arange[T: SomeNumber](start, stop: T): Tensor[T]
  Source Edit
template arange[T: SomeNumber](stop: T): Tensor[T]
  Source Edit
Arraymancer Technical reference Tutorial Spellbook (How-To's) Under the hood