Skip to content

Math Functions

These are element-wise mathematical functions with full gradient support. Each one wraps a NumPy primitive inside a Function subclass so that gradients flow correctly through the operation during backpropagation. They are useful when building custom loss functions or architectures that require explicit mathematical transformations.

import simplegrad as sg

x = sg.Tensor([1.0, 2.0, 3.0], requires_grad=True)
y = sg.log(x) + sg.exp(x)
y.sum().backward()
print(x.grad)

log

\[ f(x) = \ln(x) \]

log(x: Tensor) -> Tensor

Compute element-wise natural logarithm.

Parameters:

  • x (Tensor) –

    Input tensor. All values must be positive.

Raises:

  • ValueError

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

exp

\[ f(x) = e^x \]

exp(x: Tensor) -> Tensor

Compute element-wise exponential (e^x).

sin

\[ f(x) = \sin(x) \]

sin(x: Tensor) -> Tensor

Compute element-wise sine (input in radians).

cos

\[ f(x) = \cos(x) \]

cos(x: Tensor) -> Tensor

Compute element-wise cosine (input in radians).

tan

\[ f(x) = \tan(x) \]

tan(x: Tensor) -> Tensor

Compute element-wise tangent (input in radians).