numerics module

Warning

The FloatX/IntX types here are Python wrappers around numpy arrays. They are glacially slow and should only be used for convenience inside of Python scope and well outside of any high-traffic loops.

The numerics module offers shading-language-like syntax for float and int vector data types, as well as a few utility functions. These types are an extension of numpy arrays, and you can use them as such. Matrices are not natively supported.

from tinycio.numerics import *
import numpy as np
import torch

# Several possible inputs
Float4(4,3,2,1)             # Float4([4., 3., 2., 1.])
Float4(1.23)                # Float4([1.23, 1.23, 1.23, 1.23])
Float4([4,3,2,1])           # Float4([4., 3., 2., 1.])
Float4((4,3,2,1))           # Float4([4., 3., 2., 1.])
Float4(np.array([3,2,1,0])) # Float4([3., 2., 1., 0.])
Float4(torch.rand(4))       # Float4([0.22407186, 0.26193792, 0.89055574, 0.57386285])
Float4(torch.rand(4,1,1))   # Float4([0.99545109, 0.46160549, 0.78145427, 0.02302521])

# Swizzling
foo = Float3(1,2,3)
foo.y                       # 2.0 (float)
foo.rg                      # Float2([1., 2.])
foo.bgr                     # Float3([3., 2., 1.])
foo.xxyx                    # Float4([1., 1., 2., 1.])

# Utility functions
bar = Float3.y_axis()       # Float3([0., 1., 0.])
bar.list()                  # [0., 1., 0.]
bar.tuple()                 # (0., 1., 0.)
lerp(foo.bbb, bar, 0.5)     # Float3([1.5, 2. , 1.5])
saturate(Float2(-2,5))      # Float2([0., 1.])
sign(Float2(-2,5))          # Float2([-1.,  1.])
Float4(1) == Float4.one()   # Float4([ True,  True,  True,  True])

Float2(*args)

Float2 type using numpy.ndarray.

Float3(*args)

Float3 type using numpy.ndarray.

Float4(*args)

Float4 type using numpy.ndarray.

Int2(*args)

Int2 type using numpy.ndarray.

Int3(*args)

Int3 type using numpy.ndarray.

Int4(*args)

Int4 type using numpy.ndarray.

lerp(a, b, w)

Linearly interpolate two values.

saturate(x)

Saturate x, such that x is clamped to range [0, 1].

sign(x)

Sign of x as 1, -1 or 0.

normalize(v)

Normalize vector v to a unit vector.

reflect(n, l)

Reflect vector l over n.

class tinycio.numerics.Float2(*args)[source]

Bases: ndarray

Float2 type using numpy.ndarray.

list()[source]

Returns values as Python list

Return type:

list

static one()[source]

Returns numeric type filled with one values

tuple()[source]

Returns values as Python tuple

Return type:

tuple

static x_axis()[source]

Returns numeric type with x-axis set to 1 and all others to 0

static y_axis()[source]

Returns numeric type with y-axis set to 1 and all others to 0

static zero()[source]

Returns numeric type filled with zero values

class tinycio.numerics.Float3(*args)[source]

Bases: ndarray

Float3 type using numpy.ndarray.

list()[source]

Returns values as Python list

Return type:

list

static one()[source]

Returns numeric type filled with one values

tuple()[source]

Returns values as Python tuple

Return type:

tuple

static x_axis()[source]

Returns numeric type with x-axis set to 1 and all others to 0

static y_axis()[source]

Returns numeric type with y-axis set to 1 and all others to 0

static z_axis()[source]

Returns numeric type with z-axis set to 1 and all others to 0

static zero()[source]

Returns numeric type filled with zero values

class tinycio.numerics.Float4(*args)[source]

Bases: ndarray

Float4 type using numpy.ndarray.

list()[source]

Returns values as Python list

Return type:

list

static one()[source]

Returns numeric type filled with one values

tuple()[source]

Returns values as Python tuple

Return type:

tuple

static x_axis()[source]

Returns numeric type with x-axis set to 1 and all others to 0

static y_axis()[source]

Returns numeric type with y-axis set to 1 and all others to 0

static z_axis()[source]

Returns numeric type with z-axis set to 1 and all others to 0

static zero()[source]

Returns numeric type filled with zero values

class tinycio.numerics.Int2(*args)[source]

Bases: ndarray

Int2 type using numpy.ndarray.

list()[source]

Returns values as Python list

Return type:

list

tuple()[source]

Returns values as Python tuple

Return type:

tuple

class tinycio.numerics.Int3(*args)[source]

Bases: ndarray

Int3 type using numpy.ndarray.

list()[source]

Returns values as Python list

Return type:

list

tuple()[source]

Returns values as Python tuple

Return type:

tuple

class tinycio.numerics.Int4(*args)[source]

Bases: ndarray

Int4 type using numpy.ndarray.

list()[source]

Returns values as Python list

Return type:

list

tuple()[source]

Returns values as Python tuple

Return type:

tuple

tinycio.numerics.lerp(a, b, w)[source]

Linearly interpolate two values.

Parameters:
  • a (float | list | numpy.ndarray | torch.Tensor) – first value or list to interpolate

  • b (float | list | numpy.ndarray | torch.Tensor) – second value or list to interpolate

  • w (float | list | numpy.ndarray | torch.Tensor) – 0-1 weight ratio of first to second value

Returns:

linearly interpolated value(s)

Return type:

float | list | numpy.ndarray | torch.Tensor

tinycio.numerics.saturate(x)[source]

Saturate x, such that x is clamped to range [0, 1].

Parameters:

x (float | list | numpy.ndarray | torch.Tensor) – input value

Returns:

saturated input value(s)

Return type:

float | list | numpy.ndarray | torch.Tensor

tinycio.numerics.sign(x)[source]

Sign of x as 1, -1 or 0. Returns:

  • 0 if/where x == 0

  • 1 if/where x > 0

  • -1 if/where x < 0

Parameters:

x (float | list | numpy.ndarray | torch.Tensor) – input value

Returns:

sign(s) of input values

Return type:

float | list | numpy.ndarray | torch.Tensor

tinycio.numerics.normalize(v)[source]

Normalize vector v to a unit vector.

Parameters:

v (numpy.ndarray | torch.Tensor) – input vector

Returns:

normalized vector

Return type:

numpy.ndarray | torch.Tensor

tinycio.numerics.reflect(n, l)[source]

Reflect vector l over n.

Parameters:
  • l (numpy.ndarray or torch.Tensor) – input “light” vector

  • n (numpy.ndarray | torch.Tensor) – input normal vector

Returns:

reflected vector

Return type:

numpy.ndarray | torch.Tensor