Affine Pattern Matching¶
- affine_inputs(fn)[source]¶
Returns a [sound sub]set of real inputs of
fnwrt whichfnis known to be affine.
- extract_affine(fn)[source]¶
Extracts an affine representation of a funsor, satisfying:
x = ... const, coeffs = extract_affine(x) y = sum(Einsum(eqn, coeff, Variable(var, coeff.output)) for var, (coeff, eqn) in coeffs.items()) assert_close(y, x) assert frozenset(coeffs) == affine_inputs(x)
The
coeffswill have one key per input wrt whichfnis known to be affine (viaaffine_inputs()), andconstandcoeffs.valueswill all be constant wrt these inputs.The affine approximation is computed by ev evaluating
fnat zero and each basis vector. To improve performance, users may want to run under theMemoize()interpretation.