Implementation:1kbgz Tributary Lazy Operators
| Knowledge Sources | |
|---|---|
| Domains | Lazy_Evaluation, Mathematical_Operators |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete lazy operator overloads for composing mathematical computation graphs with dual-number autodiff support, provided by the Tributary library.
Description
The lazy operators are functions that create new lazy Node instances from upstream nodes. They are monkey-patched onto the Node class as dunder methods at module load time. Each operator uses a binary or unary helper that calls _gennode to create a downstream node wired to its upstream dependencies.
Every operator has two code paths:
- Non-dual: Standard arithmetic (e.g., x + y)
- Dual: Dual-number arithmetic (e.g., (x[0]+y[0], x[1]+y[1])) selected when use_dual=True
Supported operators include arithmetic (+, -, *, /, **, %), comparison (==, !=, <, <=, >, >=), logical (and, or, not), math functions (sin, cos, tan, log, exp, sqrt, abs, erf, asin, acos, atan), and converters (int, float, bool, str, floor, ceil, round).
Usage
These operators are used implicitly via Python's operator syntax (e.g., node1 + node2, node.sin()). They are also accessible as module-level functions (e.g., Add(node1, node2)).
Code Reference
Source Location
- Repository: Tributary
- File: tributary/lazy/calculations/ops.py
Signature
# Helper functions
def unary(node, name, lam):
"""Create a unary operation node."""
def binary(node1, other, name, lam):
"""Create a binary operation node."""
# Arithmetic operators (dual-number aware)
def Add(self, other): ...
def Sub(self, other): ...
def Mult(self, other): ...
def Div(self, other): ...
def Pow(self, other): ...
# Math functions (dual-number aware)
def Sin(self): ... # lambda x: (sin(x[0]), cos(x[0]) * x[1])
def Cos(self): ... # lambda x: (cos(x[0]), -sin(x[0]) * x[1])
def Log(self): ... # lambda x: (log(x[0]), x[1] / x[0])
def Exp(self): ... # lambda x: (exp(x[0]), x[1] * exp(x[0]))
def Sqrt(self): ... # lambda x: (sqrt(x[0]), x[1] * 0.5 / sqrt(x[0]))
def Abs(self): ... # lambda x: (abs(x[0]), x[1] * x[0] / abs(x[0]))
# Operator bindings (applied at module load)
Node.__add__ = Add
Node.__sub__ = Sub
Node.__mul__ = Mult
Node.__pow__ = Pow
Node.sin = Sin
Node.cos = Cos
Node.log = Log
Node.exp = Exp
Node.sqrt = Sqrt
Node.abs = Abs
Import
# Operators are automatically available when importing tributary.lazy
from tributary.lazy import Node
# Importing tributary.lazy.calculations.ops binds all operators to Node
import tributary.lazy.calculations.ops # noqa
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| self | Node | Yes | Left operand (a lazy Node) |
| other | Node/any | Yes (binary) | Right operand (auto-wrapped to Node via _tonode) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Node | A new lazy Node whose value is computed from upstream dependencies |
Usage Examples
Arithmetic Composition
from tributary.lazy import Node
x = Node(value=10, name="x")
y = Node(value=3, name="y")
z = x + y # Add: 13
w = x * y # Mult: 30
v = x ** 2 # Pow: 100
print(z()) # 13
print(w()) # 30
print(v()) # 100
Math Functions
from tributary.lazy import Node
import math
x = Node(value=math.pi / 4, name="x")
s = x.sin() # sin(pi/4) = 0.707...
c = x.cos() # cos(pi/4) = 0.707...
e = x.exp() # exp(pi/4) = 2.193...
print(s()) # 0.7071067811865476
print(c()) # 0.7071067811865475
print(e()) # 2.193280050738015
Dual-Number Autodiff with Operators
from tributary.lazy import Node
# f(x) = sin(x) * x^2
# f'(x) = cos(x)*x^2 + sin(x)*2x
x = Node(value=(2.0, 1.0), name="x", use_dual=True)
f = x.sin() * (x ** 2)
result = f()
print("f(2) =", result[0]) # sin(2) * 4 ≈ 3.637
print("f'(2) =", result[1]) # cos(2)*4 + sin(2)*4 ≈ 0.655