Skip to content

Math Functions

simplegrad.functions.math.log(x: Tensor) -> Tensor

Compute element-wise natural logarithm.

Parameters:

Name Type Description Default
x Tensor

Input tensor. All values must be positive.

required

Returns:

Type Description
Tensor

Tensor of ln(x).

Raises:

Type Description
ValueError

If any value in x is <= 0 (checked in eager mode only).

Source code in simplegrad/functions/math.py
def log(x: Tensor) -> Tensor:
    """Compute element-wise natural logarithm.

    Args:
        x: Input tensor. All values must be positive.

    Returns:
        Tensor of ln(x).

    Raises:
        ValueError: If any value in x is <= 0 (checked in eager mode only).
    """
    if not is_lazy() and np.any(x.values <= 0):
        raise ValueError("Log of negative value is undefined")
    return _Log.apply(x)

simplegrad.functions.math.exp(x: Tensor) -> Tensor

Compute element-wise exponential.

Parameters:

Name Type Description Default
x Tensor

Input tensor.

required

Returns:

Type Description
Tensor

Tensor of e^x.

Source code in simplegrad/functions/math.py
def exp(x: Tensor) -> Tensor:
    """Compute element-wise exponential.

    Args:
        x: Input tensor.

    Returns:
        Tensor of e^x.
    """
    return _Exp.apply(x)

simplegrad.functions.math.sin(x: Tensor) -> Tensor

Compute element-wise sine.

Parameters:

Name Type Description Default
x Tensor

Input tensor (radians).

required

Returns:

Type Description
Tensor

Tensor of sin(x).

Source code in simplegrad/functions/math.py
def sin(x: Tensor) -> Tensor:
    """Compute element-wise sine.

    Args:
        x: Input tensor (radians).

    Returns:
        Tensor of sin(x).
    """
    return _Sin.apply(x)

simplegrad.functions.math.cos(x: Tensor) -> Tensor

Compute element-wise cosine.

Parameters:

Name Type Description Default
x Tensor

Input tensor (radians).

required

Returns:

Type Description
Tensor

Tensor of cos(x).

Source code in simplegrad/functions/math.py
def cos(x: Tensor) -> Tensor:
    """Compute element-wise cosine.

    Args:
        x: Input tensor (radians).

    Returns:
        Tensor of cos(x).
    """
    return _Cos.apply(x)

simplegrad.functions.math.tan(x: Tensor) -> Tensor

Compute element-wise tangent.

Parameters:

Name Type Description Default
x Tensor

Input tensor (radians).

required

Returns:

Type Description
Tensor

Tensor of tan(x).

Source code in simplegrad/functions/math.py
def tan(x: Tensor) -> Tensor:
    """Compute element-wise tangent.

    Args:
        x: Input tensor (radians).

    Returns:
        Tensor of tan(x).
    """
    return _Tan.apply(x)