Fork me on GitHub
Nothing Special   »   [go: up one dir, main page]

src/arraymancer/tensor/math_functions

Search:
Group by:
  Source Edit

Types

ConvolveMode = enum
  full, same, valid
  Source Edit

Procs

proc `-`[T: SomeNumber](t: Tensor[T]): Tensor[T] {.noinit.}
Negate all values of a Tensor   Source Edit
proc abs(t: Tensor[Complex[float32]]): Tensor[float32] {.noinit, ...raises: [],
    tags: [], forbids: [].}
Return a Tensor with absolute values of all elements   Source Edit
proc abs(t: Tensor[Complex[float64]]): Tensor[float64] {.noinit, ...raises: [],
    tags: [], forbids: [].}
Return a Tensor with absolute values of all elements   Source Edit
proc abs[T: SomeNumber](t: Tensor[T]): Tensor[T] {.noinit.}
Return a Tensor with absolute values of all elements   Source Edit
proc almostEqual[T: SomeFloat | Complex32 | Complex64](t1, t2: Tensor[T];
    unitsInLastPlace: Natural = 4): Tensor[bool] {.noinit.}

Element-wise almostEqual function

Checks whether pairs of elements of two tensors are almost equal, using the machine epsilon.

For more details check the section covering the almostEqual procedure in nim's standard library documentation.

Inputs:

  • t1, t2: Input (floating point or complex) tensors of the same shape.
  • unitsInLastPlace: The max number of units in the last place difference tolerated when comparing two numbers. The larger the value, the more error is allowed. A 0 value means that two numbers must be exactly the same to be considered equal.

Result:

  • A new boolean tensor of the same shape as the inputs, in which elements are true if the two values in the same position on the two input tensors are almost equal (and false if they are not).

Note:

  • You can combine this function with all to check if two real tensors are almost equal.
  Source Edit
proc clamp[T](t: Tensor[T]; min, max: T): Tensor[T] {.noinit.}
Return a Tensor with all elements clamped to the interval min, max.   Source Edit
proc classify[T: SomeFloat](t: Tensor[T]): Tensor[FloatClass] {.noinit.}

Element-wise classify function (returns a tensor with the float class of each element).

Returns: A FloatClass tensor where each value is one of the following:

  • fcNormal: value is an ordinary nonzero floating point value
  • fcSubnormal: value is a subnormal (a very small) floating point value
  • fcZero: value is zero
  • fcNegZero: value is the negative zero
  • fcNan: value is Not a Number (NaN)
  • fcInf: value is positive infinity
  • fcNegInf: value is negative infinity
  Source Edit
proc convolve[T: SomeNumber | Complex32 | Complex64](t1, t2: Tensor[T];
    mode = ConvolveMode.full): Tensor[T] {.noinit.}

Returns the discrete, linear convolution of two one-dimensional tensors.

The convolution operator is often seen in signal processing, where it models the effect of a linear time-invariant system on a signal (Wikipedia, “Convolution”, https://en.wikipedia.org/wiki/Convolution).

The convolution is defined as the integral of the product of the two tensors after one is reflected about the y-axis and shifted n positions, for all values of n in which the tensors overlap (since the integral will be zero outside of that window).

Inputs:

  • t1, t2: Input tensors of size N and M respectively.
  • mode: Convolution mode (full, same, valid):
    • full: This is the default mode. It returns the convolution at each point of overlap, with an output shape of (N+M-1,). At the end-points of the convolution, the signals do not overlap completely, and boundary effects may be seen.
    • same: Returns an output of length max(M, N). Boundary effects are still visible.
    • valid: Returns output of length max(M, N) - min(M, N) + 1. The convolution is only given for points where the signals overlap completely. Values outside the signal boundary have no effect.

Output:

  • Convolution tensor of same type as the inputs and size according to the mode.

Notes:

  • The API of this function is the same as the one of numpy.convolve.
  Source Edit
proc copySign[T: SomeFloat](t1, t2: Tensor[T]): Tensor[T] {.noinit.}

Element-wise copySign function (combines 2 tensors, taking the magnitudes from t1 and the signs from t2)

This uses nim's copySign under the hood, and thus has the same properties. That is, it works for values which are NaN, infinity or zero (all of which can carry a sign) but does not work for integers.

  Source Edit
proc correlate[T: Complex32 | Complex64](t1, t2: Tensor[T];
    mode = CorrelateMode.valid): Tensor[T] {.noinit.}

Returns the cross-correlation of two one-dimensional complex tensors.

The correlation is defined as the integral of the product of the two tensors after the second one is conjugated and shifted n positions, for all values of n in which the tensors overlap (since the integral will be zero outside of that window).

Inputs:

  • t1, t2: Input tensors of size N and M respectively.
  • mode: Correlation mode (full, same, valid):
    • full: It returns the correlation at each point of overlap, with an output shape of (N+M-1,). At the end-points of the correlation, the signals do not overlap completely, and boundary effects may be seen.
    • same: Returns an output of length max(M, N). Boundary effects are still visible.
    • valid: This is the default mode. Returns output of length max(M, N) - min(M, N) + 1. The correlation is only given for points where the signals overlap completely. Values outside the signal boundary have no effect.

Output:

  • Correlation tensor of same type as the inputs and size according to the mode.

Notes:

  • The API of this function is the same as the one of numpy.correlate.
  • Note that (as with np.correlate) the default correlation mode is valid, which is different than the default convolution mode (full).
  Source Edit
proc correlate[T: SomeNumber](t1, t2: Tensor[T]; mode = CorrelateMode.valid): Tensor[
    T] {.noinit.}

Returns the cross-correlation of two one-dimensional real tensors.

The correlation is defined as the integral of the product of the two tensors after the second one is shifted n positions, for all values of n in which the tensors overlap (since the integral will be zero outside of that window).

Inputs:

  • t1, t2: Input tensors of size N and M respectively.
  • mode: Correlation mode (full, same, valid):
    • full: It returns the correlation at each point of overlap, with an output shape of (N+M-1,). At the end-points of the correlation, the signals do not overlap completely, and boundary effects may be seen.
    • same: Returns an output of length max(M, N). Boundary effects are still visible.
    • valid: This is the default mode. Returns output of length max(M, N) - min(M, N) + 1. The correlation is only given for points where the signals overlap completely. Values outside the signal boundary have no effect.

Output:

  • Correlation tensor of same type as the inputs and size according to the mode.

Notes:

  • The API of this function is the same as the one of numpy.correlate.
  • Note that (as with np.correlate) the default correlation mode is valid, which is different than the default convolution mode (full).
  Source Edit
proc elwise_div[T: SomeFloat](a, b: Tensor[T]): Tensor[T] {.noinit.}
Element-wise division   Source Edit
proc elwise_div[T: SomeInteger](a, b: Tensor[T]): Tensor[T] {.noinit.}
Element-wise division   Source Edit
proc elwise_mul[T](a, b: Tensor[T]): Tensor[T] {.noinit.}
Element-wise multiply   Source Edit
proc floorMod[T: SomeNumber](t1, t2: Tensor[T]): Tensor[T] {.noinit.}
Broadcasted floorMod operation: floorMod(tensor, tensor).   Source Edit
proc floorMod[T: SomeNumber](t: Tensor[T]; val: T): Tensor[T] {.noinit.}
Broadcasted floorMod operation: floorMod(tensor, scalar).   Source Edit
proc floorMod[T: SomeNumber](val: T; t: Tensor[T]): Tensor[T] {.noinit.}
Broadcasted floorMod operation: floorMod(scalar, tensor).   Source Edit
proc mabs[T](t: var Tensor[T])
Return a Tensor with absolute values of all elements   Source Edit
proc max[T: SomeNumber](args: varargs[Tensor[T]]): Tensor[T] {.noinit.}

Compare any number of arrays and return a new array containing the element-wise maxima.

As in nim's built-in max procedure if one of the elements being compared is a NaN, then the non NaN element is returned.

  Source Edit
proc max[T: SomeNumber](t1, t2: Tensor[T]): Tensor[T] {.noinit.}

Compare two arrays and return a new array containing the element-wise maxima.

As in nim's built-in max procedure if one of the elements being compared is a NaN, then the non NaN element is returned.

  Source Edit
proc mclamp[T](t: var Tensor[T]; min, max: T)
Update the Tensor with all elements clamped to the interval min, max.   Source Edit
proc mcopySign[T: SomeFloat](t1: var Tensor[T]; t2: Tensor[T])

In-place element-wise copySign function (changes the signs of the elements of t1 to match those of t2)

This uses nim's copySign under the hood, and thus has the same properties. That is, it works for values which are NaN, infinity or zero (all of which can carry a sign) but does not work for integers.

  Source Edit
proc melwise_div[T: SomeFloat](a: var Tensor[T]; b: Tensor[T])
Element-wise division (in-place)   Source Edit
proc melwise_div[T: SomeInteger](a: var Tensor[T]; b: Tensor[T])
Element-wise division (in-place)   Source Edit
proc melwise_mul[T](a: var Tensor[T]; b: Tensor[T])
Element-wise multiply   Source Edit
proc min[T: SomeNumber](args: varargs[Tensor[T]]): Tensor[T] {.noinit.}

Compare any number of arrays and return a new array containing the element-wise minima.

As in nim's built-in min procedure if one of the elements being compared is a NaN, then the non NaN element is returned.

  Source Edit
proc min[T: SomeNumber](t1, t2: Tensor[T]): Tensor[T] {.noinit.}

Compare two arrays and return a new array containing the element-wise minima.

As in nim's built-in min procedure if one of the elements being compared is a NaN, then the non NaN element is returned.

  Source Edit
proc mmax[T: SomeNumber](t1: var Tensor[T]; args: varargs[Tensor[T]])

In-place element-wise maxima of N tensors.

As in nim's built-in max procedure if one of the elements being compared is a NaN, then the non NaN element is returned.

  Source Edit
proc mmax[T: SomeNumber](t1: var Tensor[T]; t2: Tensor[T])

In-place element-wise maxima of two tensors.

As in nim's built-in max procedure if one of the elements being compared is a NaN, then the non NaN element is returned.

  Source Edit
proc mmin[T: SomeNumber](t1: var Tensor[T]; args: varargs[Tensor[T]])

In-place element-wise minima of N tensors.

As in nim's built-in min procedure if one of the elements being compared is a NaN, then the non NaN element is returned.

  Source Edit
proc mmin[T: SomeNumber](t1: var Tensor[T]; t2: Tensor[T])

In-place element-wise minima of two tensors.

As in nim's built-in min procedure if one of the elements being compared is a NaN, then the non NaN element is returned.

  Source Edit
proc mnegate[T: SomeSignedInt | SomeFloat](t: var Tensor[T])
Negate in-place all elements of the tensor (10 -> -10)   Source Edit
proc mreciprocal[T: Complex[float32] or Complex[float64]](t: var Tensor[T])
Apply the reciprocal 1/x in-place to all elements of the Tensor   Source Edit
proc mreciprocal[T: SomeFloat](t: var Tensor[T])
Apply the reciprocal 1/x in-place to all elements of the Tensor   Source Edit
proc negate[T: SomeSignedInt | SomeFloat](t: Tensor[T]): Tensor[T] {.noinit.}
Return a tensor with all elements negated (10 -> -10)   Source Edit
proc phase(t: Tensor[Complex[float32]]): Tensor[float32] {.noinit, ...raises: [],
    tags: [], forbids: [].}
Return a Tensor with phase values of all elements   Source Edit
proc phase(t: Tensor[Complex[float64]]): Tensor[float64] {.noinit, ...raises: [],
    tags: [], forbids: [].}
Return a Tensor with phase values of all elements   Source Edit
proc reciprocal[T: Complex[float32] or Complex[float64]](t: Tensor[T]): Tensor[T] {.
    noinit.}
Return a tensor with the reciprocal 1/x of all elements   Source Edit
proc reciprocal[T: SomeFloat](t: Tensor[T]): Tensor[T] {.noinit.}
Return a tensor with the reciprocal 1/x of all elements   Source Edit
proc sgn[T: SomeNumber](t: Tensor[T]): Tensor[int] {.noinit.}

Element-wise sgn function (returns a tensor with the sign of each element)

Returns:

  • -1 for negative numbers and NegInf,
  • 1 for positive numbers and Inf,
  • 0 for positive zero, negative zero and NaN
  Source Edit
proc sinc[T: SomeFloat](t: Tensor[T]; normalized: static bool = true): Tensor[T] {.
    noinit.}

Return the normalized or non-normalized sinc function of a Tensor

For values other than 0, the normalized sinc function is equal to sin(PI * x) / (PI * x), while the non-normalized sync function is equal to sin(x) / x. sinc(0) takes the limit value 1 in both cases, making sinc not only everywhere continuous but also infinitely differentiable.

Inputs:

  • t: Input real tensor.
  • normalized: Select whether to return the normalized or non-normalized sync. This argument is static so it must be set at compile time. The default is true (i.e. to return the normalized sync).

Result:

  • New tensor with the sinc values of all the input tensor elements.
  Source Edit
proc sinc[T: SomeFloat](x: T; normalized: static bool = true): T {.inline.}

Return the normalized or non-normalized sinc function.

For values other than 0, the normalized sinc function is equal to sin(PI * x) / (PI * x), while the non-normalized sync function is equal to sin(x) / x. sinc(0) takes the limit value 1 in both cases, making sinc not only everywhere continuous but also infinitely differentiable.

Inputs:

  • t: Real input value.
  • normalized: Select whether to return the normalized or non-normalized sync. This argument is static so it must be set at compile time. The default is true (i.e. to return the normalized sync).

Result:

  • Calculated sinc value
  Source Edit
proc square[T](t`gensym101: Tensor[T]): Tensor[T] {.noinit.}
Applies square to every element of the input tensor t and returns a copy.   Source Edit
proc square[T](t`gensym101: Tensor[T]): Tensor[T] {.noinit.}
Applies square to every element of the input tensor t and returns a copy.   Source Edit
proc square[T](x: T): T {.inline.}
Return x*x   Source Edit
proc square[T](x: T): T {.inline.}
Return x*x   Source Edit
Arraymancer Technical reference Tutorial Spellbook (How-To's) Under the hood