Implementation:1kbgz Tributary Streaming Operators
| Knowledge Sources | |
|---|---|
| Domains | Streaming, Mathematical_Operators |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete streaming operator overloads for composing mathematical operations on data streams with dual-number autodiff support, provided by the Tributary library.
Description
The streaming operators are created via unary, binary, and n_ary factory functions that produce dunder methods for the streaming Node class. Each factory accepts a tuple of lambdas: funcs[0] for standard mode and funcs[1] for dual-number mode (selected when self._use_dual is True).
Operators are monkey-patched onto Node as dunder methods at module load time. When two stream nodes are combined (e.g., stream1 + stream2), a new downstream Node is created with inputs=2, and both upstream nodes are wired via >>.
Supported operators include arithmetic (+, -, *, /, **, %), comparison (==, !=, <, <=, >, >=), logical (and, or, not), math functions (sin, cos, tan, log, exp, sqrt, abs, erf, asin, acos, atan), converters (int, float, bool, str), and numpy/scipy array protocol support.
Usage
These operators are used implicitly via Python's operator syntax (e.g., stream1 + stream2, stream.sin()).
Code Reference
Source Location
- Repository: Tributary
- File: tributary/streaming/calculations/ops.py
Signature
# Factory functions
def unary(funcs, name):
"""Create a unary operator from a tuple of (normal_lambda, dual_lambda)."""
def binary(funcs, name):
"""Create a binary operator from a tuple of (normal_lambda, dual_lambda)."""
def n_ary(funcs, name):
"""Create an n-ary operator from a tuple of (normal_lambda, dual_lambda)."""
# Arithmetic operators
Add = binary((lambda x, y: x + y, lambda x, y: (x[0]+y[0], x[1]+y[1])), name="Add")
Sub = binary((lambda x, y: x - y, lambda x, y: (x[0]-y[0], x[1]-y[1])), name="Sub")
Mult = binary((lambda x, y: x*y, lambda x, y: (x[0]*y[0], x[0]*y[1]+x[1]*y[0])), name="Mult")
Div = binary((lambda x, y: x/y, lambda x, y: (x[0]/y[0], (x[1]*y[0]-x[0]*y[1])/y[0]**2)), name="Div")
Pow = binary((lambda x, y: x**y, lambda x, y: (x[0]**y, y*x[1]*x[0]**(y-1))), name="Pow")
# Math functions
Log = unary((lambda x: math.log(x), lambda x: (math.log(x[0]), x[1]/x[0])), name="Log")
Sin = unary((lambda x: math.sin(x), lambda x: (math.sin(x[0]), math.cos(x[0])*x[1])), name="Sin")
Cos = unary((lambda x: math.cos(x), lambda x: (math.cos(x[0]), -math.sin(x[0])*x[1])), name="Cos")
Sqrt = unary((lambda x: math.sqrt(x), lambda x: (math.sqrt(x[0]), x[1]*0.5/math.sqrt(x[0]))), name="Sqrt")
Exp = unary((lambda x: math.exp(x), lambda x: (math.exp(x[0]), x[1]*math.exp(x[0]))), name="Exp")
# Operator bindings
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
Import
# Operators are automatically available when importing tributary.streaming
import tributary.streaming as ts
# Importing tributary.streaming.calculations.ops binds all operators to Node
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| self | Node | Yes | Left operand (a streaming Node) |
| other | Node/any | Yes (binary) | Right operand (auto-wrapped to Node via _gen_node) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Node | A new downstream streaming Node wired to both upstream nodes |
Usage Examples
Stream Arithmetic
import tributary.streaming as ts
# Multiply two streams element-by-element
s1 = ts.Curve(value=[1, 2, 3, 4])
s2 = ts.Curve(value=[10, 20, 30, 40])
product = s1 * s2
out = ts.Print(product)
ts.run(out)
# Output: 10, 40, 90, 160
Stream Math Functions
import tributary.streaming as ts
import math
# Compute sin of each value in a stream
source = ts.Curve(value=[0, math.pi/2, math.pi])
sine = source.sin()
out = ts.Print(sine)
ts.run(out)
# Output: 0.0, 1.0, ~0.0
Dual-Number Autodiff on Streams
import tributary.streaming as ts
# f(x) = x^2, f'(x) = 2x
# Create dual-number source: (value, seed)
source = ts.Node(
func=lambda: (3.0, 1.0),
name="x",
use_dual=True
)
f = source * source # x^2
out = ts.Print(f)
result = ts.run(out)
print(result[0]) # 9.0 (function value)
print(result[1]) # 6.0 (derivative: 2*3 = 6)