View Source Nx.Constants (Nx v0.5.3)

Common constants used in computations.

This module can be used in defn.

Link to this section Summary

Functions

e()

Returns $e$ in f32.

Returns a scalar tensor with the value of $e$ for the given type.

Returns $\gamma$ (Euler-Mascheroni constant) in f32.

Returns a scalar tensor with the value of $\gamma$ (Euler-Mascheroni constant) for the given type.

Returns the imaginary constant.

Returns infinity in f32.

Returns infinity.

Returns a scalar tensor with the maximum value for the given type.

Returns a scalar tensor with the maximum finite value for the given type.

Returns a scalar tensor with the minimum value for the given type.

Returns a scalar tensor with the minimum finite value for the given type.

Returns NaN in f32.

Returns NaN (Not a Number).

Returns infinity in f32.

Returns negative infinity.

Returns $\pi$ in f32.

Returns a scalar tensor with the value of $\pi$ for the given type.

Returns a scalar tensor with the maximum finite value for the given type.

Link to this section Functions

Returns $e$ in f32.

Returns a scalar tensor with the value of $e$ for the given type.

options

Options

  • :backend - a backend to allocate the tensor on.

examples

Examples

iex> Nx.Constants.e({:f, 64})
#Nx.Tensor<
  f64
  2.718281828459045
>

iex> Nx.Constants.e({:f, 32})
#Nx.Tensor<
  f32
  2.7182817459106445
>

iex> Nx.Constants.e({:f, 16})
#Nx.Tensor<
  f16
  2.71875
>

iex> Nx.Constants.e({:bf, 16})
#Nx.Tensor<
  bf16
  2.703125
>

iex> Nx.Constants.e({:s, 32})
** (ArgumentError) only floating types are supported, got: {:s, 32}

Returns $\gamma$ (Euler-Mascheroni constant) in f32.

Link to this function

euler_gamma(type, opts \\ [])

View Source

Returns a scalar tensor with the value of $\gamma$ (Euler-Mascheroni constant) for the given type.

options

Options

  • :backend - a backend to allocate the tensor on.

examples

Examples

iex> Nx.Constants.euler_gamma({:f, 64})
#Nx.Tensor<
  f64
  0.5772156649015329
>

iex> Nx.Constants.euler_gamma({:f, 32})
#Nx.Tensor<
  f32
  0.5772156715393066
>

iex> Nx.Constants.euler_gamma({:f, 16})
#Nx.Tensor<
  f16
  0.5771484375
>

iex> Nx.Constants.euler_gamma({:bf, 16})
#Nx.Tensor<
  bf16
  0.57421875
>

iex> Nx.Constants.euler_gamma({:s, 32})
** (ArgumentError) only floating types are supported, got: {:s, 32}

Returns the imaginary constant.

Accepts the same options as Nx.tensor/2

examples

Examples

iex> Nx.Constants.i()
#Nx.Tensor<
  c64
  0.0+1.0i
>

iex> Nx.Constants.i(type: {:c, 128})
#Nx.Tensor<
  c128
  0.0+1.0i
>

error-cases

Error cases

iex> Nx.Constants.i(type: {:f, 32})
** (ArgumentError) invalid type for complex number. Expected {:c, 64} or {:c, 128}, got: {:f, 32}

Returns infinity in f32.

Link to this function

infinity(type, opts \\ [])

View Source

Returns infinity.

options

Options

  • :backend - a backend to allocate the tensor on.

examples

Examples

iex> Nx.Constants.infinity({:bf, 16})
#Nx.Tensor<
  bf16
  Inf
>

iex> Nx.Constants.infinity({:f, 16})
#Nx.Tensor<
  f16
  Inf
>

iex> Nx.Constants.infinity({:f, 32})
#Nx.Tensor<
  f32
  Inf
>

iex> Nx.Constants.infinity({:f, 64})
#Nx.Tensor<
  f64
  Inf
>

Returns a scalar tensor with the maximum value for the given type.

It is infinity for floating point tensors.

options

Options

  • :backend - a backend to allocate the tensor on.

examples

Examples

iex> Nx.Constants.max({:u, 8})
#Nx.Tensor<
  u8
  255
>

iex> Nx.Constants.max({:f, 32})
#Nx.Tensor<
  f32
  Inf
>
Link to this function

max_finite(type, opts \\ [])

View Source

Returns a scalar tensor with the maximum finite value for the given type.

options

Options

  • :backend - a backend to allocate the tensor on.

examples

Examples

iex> Nx.Constants.max_finite({:u, 8})
#Nx.Tensor<
  u8
  255
>

iex> Nx.Constants.max_finite({:s, 16})
#Nx.Tensor<
  s16
  32677
>

iex> Nx.Constants.max_finite({:f, 32})
#Nx.Tensor<
  f32
  3.4028234663852886e38
>

Returns a scalar tensor with the minimum value for the given type.

It is negative infinity for floating point tensors.

options

Options

  • :backend - a backend to allocate the tensor on.

examples

Examples

iex> Nx.Constants.min({:u, 8})
#Nx.Tensor<
  u8
  0
>

iex> Nx.Constants.min({:f, 32})
#Nx.Tensor<
  f32
  -Inf
>
Link to this function

min_finite(type, opts \\ [])

View Source

Returns a scalar tensor with the minimum finite value for the given type.

options

Options

  • :backend - a backend to allocate the tensor on.

examples

Examples

iex> Nx.Constants.min_finite({:u, 8})
#Nx.Tensor<
  u8
  0
>

iex> Nx.Constants.min_finite({:s, 16})
#Nx.Tensor<
  s16
  -32678
>

iex> Nx.Constants.min_finite({:f, 32})
#Nx.Tensor<
  f32
  -3.4028234663852886e38
>

Returns NaN in f32.

Returns NaN (Not a Number).

options

Options

  • :backend - a backend to allocate the tensor on.

examples

Examples

iex> Nx.Constants.nan({:bf, 16})
#Nx.Tensor<
  bf16
  NaN
>

iex> Nx.Constants.nan({:f, 16})
#Nx.Tensor<
  f16
  NaN
>

iex> Nx.Constants.nan({:f, 32})
#Nx.Tensor<
  f32
  NaN
>

iex> Nx.Constants.nan({:f, 64})
#Nx.Tensor<
  f64
  NaN
>

Returns infinity in f32.

Link to this function

neg_infinity(type, opts \\ [])

View Source

Returns negative infinity.

options

Options

  • :backend - a backend to allocate the tensor on.

examples

Examples

iex> Nx.Constants.neg_infinity({:bf, 16})
#Nx.Tensor<
  bf16
  -Inf
>

iex> Nx.Constants.neg_infinity({:f, 16})
#Nx.Tensor<
  f16
  -Inf
>

iex> Nx.Constants.neg_infinity({:f, 32})
#Nx.Tensor<
  f32
  -Inf
>

iex> Nx.Constants.neg_infinity({:f, 64})
#Nx.Tensor<
  f64
  -Inf
>

Returns $\pi$ in f32.

Returns a scalar tensor with the value of $\pi$ for the given type.

options

Options

  • :backend - a backend to allocate the tensor on.

examples

Examples

iex> Nx.Constants.pi({:f, 64})
#Nx.Tensor<
  f64
  3.141592653589793
>

iex> Nx.Constants.pi({:f, 32})
#Nx.Tensor<
  f32
  3.1415927410125732
>

iex> Nx.Constants.pi({:f, 16})
#Nx.Tensor<
  f16
  3.140625
>

iex> Nx.Constants.pi({:bf, 16})
#Nx.Tensor<
  bf16
  3.140625
>

iex> Nx.Constants.pi({:s, 32})
** (ArgumentError) only floating types are supported, got: {:s, 32}
Link to this function

smallest_positive_normal(type, opts \\ [])

View Source

Returns a scalar tensor with the maximum finite value for the given type.

options

Options

  • :backend - a backend to allocate the tensor on.

examples

Examples

iex> Nx.Constants.smallest_positive_normal({:f, 64})
#Nx.Tensor<
  f64
  2.2250738585072014e-308
>

iex> Nx.Constants.smallest_positive_normal({:f, 32})
#Nx.Tensor<
  f32
  1.1754943508222875e-38
>

iex> Nx.Constants.smallest_positive_normal({:f, 16})
#Nx.Tensor<
  f16
  6.103515625e-5
>

iex> Nx.Constants.smallest_positive_normal(:bf16)
#Nx.Tensor<
  bf16
  1.1754943508222875e-38
>

iex> Nx.Constants.smallest_positive_normal({:s, 32})
** (ArgumentError) only floating types are supported, got: {:s, 32}