View Source Nx.Constants (Nx v0.9.0)

Common constants used in computations.

This module can be used in defn.

Summary

Functions

e()

Returns $e$ in f32.

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

Returns a scalar with the machine epsilon 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.

i()

Returns the imaginary constant in c64.

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 negative 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 smallest positive value for the given type.

Functions

Returns $e$ in f32.

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

Options

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

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({:f, 8})
#Nx.Tensor<
  f8
  2.5
>

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

epsilon(type, opts \\ [])

View Source

Returns a scalar with the machine epsilon for the given type.

The values are compatible with a IEEE 754 floating point standard.

Options

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

Examples

iex> Nx.Constants.epsilon({:f, 64})
#Nx.Tensor<
  f64
  2.220446049250313e-16
>

iex> Nx.Constants.epsilon({:f, 32})
#Nx.Tensor<
  f32
  1.1920928955078125e-7
>

iex> Nx.Constants.epsilon({:f, 16})
#Nx.Tensor<
  f16
  9.765625e-4
>

iex> Nx.Constants.epsilon(:bf16)
#Nx.Tensor<
  bf16
  0.0078125
>

iex> Nx.Constants.epsilon(:f8)
#Nx.Tensor<
  f8
  0.25
>

iex> Nx.Constants.epsilon({: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

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

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({:f, 8})
#Nx.Tensor<
  f8
  0.5
>

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

Returns the imaginary constant in c64.

Returns the imaginary constant.

Accepts the same options as Nx.tensor/2

Examples

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

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

Error cases

iex> Nx.Constants.i({: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

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

Examples

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

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

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

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

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

Examples

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

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

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

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

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

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

Examples

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

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

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

Returns NaN in f32.

Returns NaN (Not a Number).

Options

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

Examples

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

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 negative infinity in f32.

Link to this function

neg_infinity(type, opts \\ [])

View Source

Returns negative infinity.

Options

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

Examples

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

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

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

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({:f, 8})
#Nx.Tensor<
  f8
  3.0
>

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 smallest positive value for the given type.

Options

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

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(:f8)
#Nx.Tensor<
  f8
  6.103515625e-5
>

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