Basic Funsors

class Approximate(*args, **kwargs)[source]

Bases: Funsor

Interpretation-specific approximation wrt a set of variables.

The default eager interpretation should be exact. The user-facing interface is the Funsor.approximate() method.

  • op (AssociativeOp) – An associative operator.

  • model (Funsor) – An exact funsor depending on approx_vars.

  • guide (Funsor) – A proposal funsor guiding optional approximation.

  • approx_vars (frozenset) – A set of variables over which to approximate.

class Binary(*args, **kwargs)[source]

Bases: Funsor

Lazy binary operation.

  • op (Op) – A binary operator.

  • lhs (Funsor) – A left hand side argument.

  • rhs (Funsor) – A right hand side argument.

class Cat(name, parts, part_name=None)[source]

Bases: Funsor

Concatenate funsors along an existing input dimension.

  • name (str) – The name of the input variable along which to concatenate.

  • parts (tuple) – A tuple of Funsors of homogenous output domain.

class Funsor(*args, **kwargs)[source]

Bases: object

Abstract base class for immutable functional tensors.

Concrete derived classes must implement __init__() methods taking hashable *args and no optional **kwargs so as to support cons hashing.

Derived classes with .fresh variables must implement an eager_subs() method. Derived classes with .bound variables must implement an _alpha_convert() method.

  • inputs (OrderedDict) – A mapping from input name to domain. This can be viewed as a typed context or a mapping from free variables to domains.

  • output (Domain) – An output domain.

property dtype
property shape
pretty(*args, **kwargs)[source]
property requires_grad
reduce(op, reduced_vars=None)[source]

Reduce along all or a subset of inputs.

  • op (AssociativeOp or ReductionOp) – A reduction operation.

  • reduced_vars (str, Variable, or set or frozenset thereof.) – An optional input name or set of names to reduce. If unspecified, all inputs will be reduced.

approximate(op, guide, approx_vars=None)[source]

Approximate wrt and all or a subset of inputs.

  • op (AssociativeOp) – A reduction operation.

  • guide (Funsor) – A guide funsor (e.g. a proposal distribution).

  • approx_vars (str, Variable, or set or frozenset thereof.) – An optional input name or set of names to reduce. If unspecified, all inputs will be reduced.

sample(sampled_vars, sample_inputs=None, rng_key=None)[source]

Create a Monte Carlo approximation to this funsor by replacing functions of sampled_vars with Delta s.

The result is a Funsor with the same .inputs and .output as the original funsor (plus sample_inputs if provided), so that self can be replaced by the sample in expectation computations:

y = x.sample(sampled_vars)
assert y.inputs == x.inputs
assert y.output == x.output
exact = (x.exp() * integrand).reduce(ops.add)
approx = (y.exp() * integrand).reduce(ops.add)

If sample_inputs is provided, this creates a batch of samples.

  • sampled_vars (str, Variable, or set or frozenset thereof.) – A set of input variables to sample.

  • sample_inputs (OrderedDict) – An optional mapping from variable name to Domain over which samples will be batched.

  • rng_key (None or JAX's random.PRNGKey) – a PRNG state to be used by JAX backend to generate random samples


Align this funsor to match given names. This is mainly useful in preparation for extracting .data of a funsor.tensor.Tensor.


names (tuple) – A tuple of strings representing all names but in a new order.


A permuted funsor equivalent to self.

Return type



Internal substitution function. This relies on the user-facing __call__() method to coerce non-Funsors to Funsors. Once all inputs are Funsors, eager_subs() implementations can recurse to call Subs.

eager_reduce(op, reduced_vars)[source]
sequential_reduce(op, reduced_vars)[source]
moment_matching_reduce(op, reduced_vars)[source]
all(axis=None, keepdims=False)[source]
any(axis=None, keepdims=False)[source]
argmax(axis=None, keepdims=False)[source]
argmin(axis=None, keepdims=False)[source]
max(axis=None, keepdims=False)[source]
min(axis=None, keepdims=False)[source]
sum(axis=None, keepdims=False)[source]
prod(axis=None, keepdims=False)[source]
logsumexp(axis=None, keepdims=False)[source]
mean(axis=None, keepdims=False)[source]
std(axis=None, ddof=0, keepdims=False)[source]
var(axis=None, ddof=0, keepdims=False)[source]
class Independent(*args, **kwargs)[source]

Bases: Funsor

Creates an independent diagonal distribution.

This is equivalent to substitution followed by reduction:

f = ...  # a batched distribution
assert f.inputs['x_i'] == Reals[4, 5]
assert f.inputs['i'] == Bint[3]

g = Independent(f, 'x', 'i', 'x_i')
assert g.inputs['x'] == Reals[3, 4, 5]
assert 'x_i' not in g.inputs
assert 'i' not in g.inputs

x = Variable('x', Reals[3, 4, 5])
g == f(x_i=x['i']).reduce(ops.add, 'i')
  • fn (Funsor) – A funsor.

  • reals_var (str) – The name of a real-tensor input.

  • bint_var (str) – The name of a new batch input of fn.

  • diag_var – The name of a smaller-shape real input of fn.

class Lambda(*args, **kwargs)[source]

Bases: Funsor

Lazy inverse to ops.getitem.

This is useful to simulate higher-order functions of integers by representing those functions as arrays.

  • var (Variable) – A variable to bind.

  • expr (funsor) – A funsor.

class Number(data, dtype=None)[source]

Bases: Funsor

Funsor backed by a Python number.

  • data (numbers.Number) – A python number.

  • dtype – A nonnegative integer or the string “real”.

class Reduce(*args, **kwargs)[source]

Bases: Funsor

Lazy reduction over multiple variables.

The user-facing interface is the Funsor.reduce() method.

  • op (AssociativeOp) – An associative operator.

  • arg (funsor) – An argument to be reduced.

  • reduced_vars (frozenset) – A set of variables over which to reduce.

class Scatter(*args, **kwargs)[source]

Bases: Funsor

Transpose of structurally linear Subs, followed by Reduce.

For injective scatter operations this should satisfy the equation:

if destin = Scatter(op, subs, source, frozenset())
then source = Subs(destin, subs)

The reduced_vars is merely for computational efficiency, and could always be split out into a separate .reduce(). For example in the following equation, the left hand side uses much less memory than the right hand side:

Scatter(op, subs, source, reduced_vars) ==
  Scatter(op, subs, source, frozenset()).reduce(op, reduced_vars)


This is currently implemented only for injective scatter operations. In particular, this does not allow accumulation behavior like scatter-add.


Scatter(ops.add, ...) is the funsor analog of or torch.index_put() or jax.lax.scatter_add(). For injective substitutions, Scatter(ops.add, ...) is roughly equivalent to the tensor operation:

result = zeros(...)  # since zero is the additive unit
result[subs] = source
  • op (AssociativeOp) – An op. The unit of this op will be used as default value.

  • subs (tuple) – A substitution.

  • source (Funsor) – A source for data to be scattered from.

  • reduced_vars (frozenset) – A set of variables over which to reduce.

class Stack(*args, **kwargs)[source]

Bases: Funsor

Stack of funsors along a new input dimension.

  • name (str) – The name of the new input variable along which to stack.

  • parts (tuple) – A tuple of Funsors of homogenous output domain.

eager_reduce(op, reduced_vars)[source]
class Slice(name, *args, **kwargs)[source]

Bases: Funsor

Symbolic representation of a Python slice object.

  • name (str) – A name for the new slice dimension.

  • start (int) –

  • stop (int) –

  • step (int) – Three args following slice semantics.

  • dtype (int) – An optional bounded integer type of this slice.

class Subs(arg, subs)[source]

Bases: Funsor

Lazy substitution of the form x(u=y, v=z).

  • arg (Funsor) – A funsor being substituted into.

  • subs (tuple) – A tuple of (name, value) pairs, where name is a string and value can be coerced to a Funsor via to_funsor().

class Unary(*args, **kwargs)[source]

Bases: Funsor

Lazy unary operation.

  • op (Op) – A unary operator.

  • arg (Funsor) – An argument.

class Variable(*args, **kwargs)[source]

Bases: Funsor

Funsor representing a single free variable.

  • name (str) – A variable name.

  • output ( – A domain.

to_data(x, name_to_dim=None, **kwargs)[source]
to_data(x: Funsor, name_to_dim=None)
to_data(x: Number, name_to_dim=None)
to_data(x: Tensor, name_to_dim=None)
to_data(funsor_dist: Distribution, name_to_dim=None)
to_data(funsor_dist: Independent[Union[Independent, Distribution], str, str, str], name_to_dim=None)
to_data(funsor_dist: Gaussian, name_to_dim=None)
to_data(funsor_dist: Contraction[Union[LogaddexpOp, NullOp], AddOp, frozenset, Tuple[Union[Tensor, Number], Gaussian]], name_to_dim=None)
to_data(funsor_dist: Multinomial, name_to_dim=None)
to_data(funsor_dist: Delta, name_to_dim=None)
to_data(expr: Unary[TransformOp, Union[Unary, Variable]], name_to_dim=None)
to_data(x: Constant, name_to_dim=None)

Extract a python object from a Funsor.

Raises a ValueError if free variables remain or if the funsor is lazy.

  • x – An object, possibly a Funsor.

  • name_to_dim (OrderedDict) – An optional inputs hint.


A non-funsor equivalent to x.


ValueError if any free variables remain.


PatternMissingError if funsor is not fully evaluated.

to_funsor(x, output=None, dim_to_name=None, **kwargs)[source]
to_funsor(x: Funsor, output=None, dim_to_name=None)
to_funsor(name: str, output=None)
to_funsor(x: Number, output=None, dim_to_name=None)
to_funsor(s: slice, output=None, dim_to_name=None)
to_funsor(args: tuple, output=None, dim_to_name=None)
to_funsor(x: generic, output=None, dim_to_name=None)
to_funsor(x: ndarray, output=None, dim_to_name=None)
to_funsor(backend_dist: Beta, output=None, dim_to_name=None)
to_funsor(backend_dist: Cauchy, output=None, dim_to_name=None)
to_funsor(backend_dist: Chi2, output=None, dim_to_name=None)
to_funsor(backend_dist: _PyroWrapper_BernoulliProbs, output=None, dim_to_name=None)
to_funsor(backend_dist: _PyroWrapper_BernoulliLogits, output=None, dim_to_name=None)
to_funsor(backend_dist: Binomial, output=None, dim_to_name=None)
to_funsor(backend_dist: Categorical, output=None, dim_to_name=None)
to_funsor(backend_dist: _PyroWrapper_CategoricalLogits, output=None, dim_to_name=None)
to_funsor(pyro_dist: Delta, output=None, dim_to_name=None)
to_funsor(backend_dist: Dirichlet, output=None, dim_to_name=None)
to_funsor(backend_dist: DirichletMultinomial, output=None, dim_to_name=None)
to_funsor(backend_dist: Exponential, output=None, dim_to_name=None)
to_funsor(backend_dist: Gamma, output=None, dim_to_name=None)
to_funsor(backend_dist: GammaPoisson, output=None, dim_to_name=None)
to_funsor(backend_dist: Geometric, output=None, dim_to_name=None)
to_funsor(backend_dist: Gumbel, output=None, dim_to_name=None)
to_funsor(backend_dist: HalfCauchy, output=None, dim_to_name=None)
to_funsor(backend_dist: HalfNormal, output=None, dim_to_name=None)
to_funsor(backend_dist: Laplace, output=None, dim_to_name=None)
to_funsor(backend_dist: Logistic, output=None, dim_to_name=None)
to_funsor(backend_dist: LowRankMultivariateNormal, output=None, dim_to_name=None)
to_funsor(backend_dist: Multinomial, output=None, dim_to_name=None)
to_funsor(backend_dist: MultivariateNormal, output=None, dim_to_name=None)
to_funsor(backend_dist: NonreparameterizedBeta, output=None, dim_to_name=None)
to_funsor(backend_dist: NonreparameterizedDirichlet, output=None, dim_to_name=None)
to_funsor(backend_dist: NonreparameterizedGamma, output=None, dim_to_name=None)
to_funsor(backend_dist: NonreparameterizedNormal, output=None, dim_to_name=None)
to_funsor(backend_dist: Normal, output=None, dim_to_name=None)
to_funsor(backend_dist: Pareto, output=None, dim_to_name=None)
to_funsor(backend_dist: Poisson, output=None, dim_to_name=None)
to_funsor(backend_dist: StudentT, output=None, dim_to_name=None)
to_funsor(backend_dist: Uniform, output=None, dim_to_name=None)
to_funsor(backend_dist: VonMises, output=None, dim_to_name=None)
to_funsor(backend_dist: ContinuousBernoulli, output=None, dim_to_name=None)
to_funsor(backend_dist: FisherSnedecor, output=None, dim_to_name=None)
to_funsor(backend_dist: NegativeBinomial, output=None, dim_to_name=None)
to_funsor(backend_dist: OneHotCategorical, output=None, dim_to_name=None)
to_funsor(backend_dist: RelaxedBernoulli, output=None, dim_to_name=None)
to_funsor(backend_dist: Weibull, output=None, dim_to_name=None)
to_funsor(tfm: Transform, output=None, dim_to_name=None, real_inputs=None)
to_funsor(tfm: ExpTransform, output=None, dim_to_name=None, real_inputs=None)
to_funsor(tfm: TanhTransform, output=None, dim_to_name=None, real_inputs=None)
to_funsor(tfm: SigmoidTransform, output=None, dim_to_name=None, real_inputs=None)
to_funsor(tfm: _InverseTransform, output=None, dim_to_name=None, real_inputs=None)
to_funsor(tfm: ComposeTransform, output=None, dim_to_name=None, real_inputs=None)
to_funsor(backend_dist: ExpandedDistribution, output=None, dim_to_name=None)
to_funsor(backend_dist: Independent, output=None, dim_to_name=None)
to_funsor(backend_dist: MaskedDistribution, output=None, dim_to_name=None)
to_funsor(backend_dist: TransformedDistribution, output=None, dim_to_name=None)
to_funsor(pyro_dist: Bernoulli, output=None, dim_to_name=None)
to_funsor(x: ProvenanceTensor, output=None, dim_to_name=None)
to_funsor(x: Tensor, output=None, dim_to_name=None)
to_funsor(pyro_dist: FunsorDistribution, output=None, dim_to_name=None)

Convert to a Funsor . Only Funsor s and scalars are accepted.

  • x – An object.

  • output ( – An optional output hint.

  • dim_to_name (OrderedDict) – An optional mapping from negative batch dimensions to name strings.


A Funsor equivalent to x.

Return type





class Delta(*args)[source]

Bases: Funsor

Normalized delta distribution binding multiple variables.

There are three syntaxes supported for constructing Deltas:

Delta(((name1, (point1, log_density1)),
       (name2, (point2, log_density2)),
       (name3, (point3, log_density3))))

or for a single name:

Delta(name, point, log_density)

or for default log_density == 0:

Delta(name, point)

terms (tuple) – A tuple of tuples of the form (name, (point, log_density)).

eager_reduce(op, reduced_vars)[source]
solve(expr, value)[source]

Tries to solve for free inputs of an expr such that expr == value, and computes the log-abs-det-Jacobian of the resulting substitution.

  • expr (Funsor) – An expression with a free variable.

  • value (Funsor) – A target value.


A tuple (name, point, log_abs_det_jacobian)

Return type





Einsum(equation, *operands)[source]

Wrapper around torch.einsum() or np.einsum() to operate on real-valued Funsors.

Note this operates only on the output tensor. To perform sum-product contractions on named dimensions, instead use + and Reduce.

  • equation (str) – An torch.einsum() or np.einsum() equation.

  • operands (tuple) – A tuple of input funsors.

class Function(*args, **kwargs)[source]

Bases: Funsor

Funsor wrapped by a native PyTorch or NumPy function.

Functions are assumed to support broadcasting and can be eagerly evaluated on funsors with free variables of int type (i.e. batch dimensions).

Function s are usually created via the function() decorator.

  • fn (callable) – A native PyTorch or NumPy function to wrap.

  • output (type) – An output domain.

  • args (Funsor) – Funsor arguments.

class Tensor(data, inputs=None, dtype='real')[source]

Bases: Funsor

Funsor backed by a PyTorch Tensor or a NumPy ndarray.

This follows the torch.distributions convention of arranging named “batch” dimensions on the left and remaining “event” dimensions on the right. The output shape is determined by all remaining dims. For example:

data = torch.zeros(5,4,3,2)
x = Tensor(data, {"i": Bint[5], "j": Bint[4]})
assert x.output == Reals[3, 2]

Operators like matmul and .sum() operate only on the output shape, and will not change the named inputs.

  • data (numeric_array) – A PyTorch tensor or NumPy ndarray.

  • inputs (dict) – An optional mapping from input name (str) to datatype ( Defaults to empty.

  • dtype (int or the string "real".) – optional output datatype. Defaults to “real”.

property requires_grad
eager_reduce(op, reduced_vars)[source]
new_arange(name, *args, **kwargs)[source]

Helper to create a named torch.arange() or np.arange() funsor. In some cases this can be replaced by a symbolic Slice .

  • name (str) – A variable name.

  • start (int) –

  • stop (int) –

  • step (int) – Three args following slice semantics.

  • dtype (int) – An optional bounded integer type of this slice.

Return type



Attempt to convert a Funsor to a Number or Tensor by substituting arange() s into its free variables.


x (Funsor) – A funsor.

Return type


align_tensor(new_inputs, x, expand=False)[source]

Permute and add dims to a tensor to match desired new_inputs.

  • new_inputs (OrderedDict) – A target set of inputs.

  • x (funsor.terms.Funsor) – A Tensor or Number .

  • expand (bool) – If False (default), set result size to 1 for any input of x not in new_inputs; if True expand to new_inputs size.


a number or torch.Tensor or np.ndarray that can be broadcast to other tensors with inputs new_inputs.

Return type

int or float or torch.Tensor or np.ndarray

align_tensors(*args, **kwargs)[source]

Permute multiple tensors before applying a broadcasted op.

This is mainly useful for implementing eager funsor operations.


a pair (inputs, tensors) where tensors are all torch.Tensor s or np.ndarray s that can be broadcast together to a single data with given inputs.

Return type



Decorator to wrap a PyTorch/NumPy function, using either type hints or explicit type annotations.


# Using type hints:
def matmul(x: Reals[3, 4], y: Reals[4, 5]) -> Reals[3, 5]:
    return torch.matmul(x, y)

# Using explicit type annotations:
@funsor.tensor.function(Reals[3, 4], Reals[4, 5], Reals[3, 5])
def matmul(x, y):
    return torch.matmul(x, y)

@funsor.tensor.function(Reals[10], Reals[10, 10], Reals[10], Real)
def mvn_log_prob(loc, scale_tril, x):
    d = torch.distributions.MultivariateNormal(loc, scale_tril)
    return d.log_prob(x)

To support functions that output nested tuples of tensors, specify a nested Tuple of output types, for example:

def max_and_argmax(x: Reals[8]) -> Tuple[Real, Bint[8]]:
    return torch.max(x, dim=-1)

*signature – A sequence if input domains followed by a final output domain or nested tuple of output domains.

tensordot(x, y, dims)[source]

Wrapper around torch.tensordot() or np.tensordot() to operate on real-valued Funsors.

Note this operates only on the output tensor. To perform sum-product contractions on named dimensions, instead use + and Reduce.

Arguments should satisfy:

len(x.shape) >= dims
len(y.shape) >= dims
dims == 0 or x.shape[-dims:] == y.shape[:dims]
  • x (Funsor) – A left hand argument.

  • y (Funsor) – A y hand argument.

  • dims (int) – The number of dimension of overlap of output shape.

Return type



class BlockMatrix(shape)[source]

Bases: object

Jit-compatible helper to build blockwise matrices. Syntax is similar to torch.zeros()

x = BlockMatrix((100, 20, 20))
x[..., 0:4, 0:4] = x11
x[..., 0:4, 6:10] = x12
x[..., 6:10, 0:4] = x12.transpose(-1, -2)
x[..., 6:10, 6:10] = x22
x = x.as_tensor()
assert x.shape == (100, 20, 20)
class BlockVector(shape)[source]

Bases: object

Jit-compatible helper to build blockwise vectors. Syntax is similar to torch.zeros()

x = BlockVector((100, 20))
x[..., 0:4] = x1
x[..., 6:10] = x2
x = x.as_tensor()
assert x.shape == (100, 20)
class Gaussian(white_vec=None, prec_sqrt=None, inputs=None, *, mean=None, info_vec=None, precision=None, scale_tril=None, covariance=None)[source]

Bases: Funsor

Funsor representing a batched Gaussian log-density function.

Gaussians are the internal representation for joint and conditional multivariate normal distributions and multivariate normal likelihoods. Mathematically, a Gaussian represents the quadratic log density function:

f(x) = -0.5 * || x @ prec_sqrt - white_vec ||^2
     = -0.5 * < x @ prec_sqrt - white_vec | x @ prec_sqrt - white_vec >
     = -0.5 * < x | prec_sqrt @ prec_sqrt.T | x>
       + < x | prec_sqrt | white_vec > - 0.5 ||white_vec||^2

Internally Gaussians use a square root information filter (SRIF) representation consisting of a square root of the precision matrix prec_sqrt and a vector in the whitened space white_vec. This representation allows space-efficient construction of Gaussians with incomplete information, i.e. with zero eigenvalues in the precision matrix. These incomplete log densities arise when making low-dimensional observations of higher-dimensional hidden state. Sampling and marginalization are supported only for full-rank Gaussians or full-rank subsets of Gaussians. See the rank() and is_full_rank() properties.


Gaussian s are not normalized probability distributions, rather they are canonicalized to evaluate to zero log density at their maximum: f(prec_sqrt \ white_vec) = 0. Not only are Gaussians non-normalized, but they may be rank deficient and non-normalizable, in which case sampling and marginalization are supported only un full-rank subsets of variables.

  • white_vec (torch.Tensor) – An batched white noise vector, where white_vec = prec_sqrt.T @ mean. Alternatively you can specify one of the kwargs mean or info_vec, which will be converted to white_vec.

  • prec_sqrt (torch.Tensor) – A batched square root of the positive semidefinite precision matrix. This need not be square, and typically has shape prec_sqrt.shape == white_vec.shape[:-1] + (dim, rank), where dim is the total flattened size of real inputs and rank = white_vec.shape[-1]. Alternatively you can specify one of the kwargs precision, covariance, or scale_tril, which will be converted to prec_sqrt.

  • inputs (OrderedDict) – Mapping from name to Domain .

compression_threshold = 2
classmethod set_compression_threshold(threshold: float)[source]

Context manager to set rank compression threshold.

To save space Gaussians compress wide prec_sqrt matrices down to square. However compression uses a QR decomposition which can be expensive and which has unstable gradients when the resulting precision matrix is rank deficient. To balance space and time costs and numerical stability, compression is trigger only on prec_sqrt matrices whose width to height ratio is greater than threshold.


threshold (float) – Defaults to 2. To optimize for space and deterministic computations, set threshold = 1. To optimize for fewest QR decompositions and numerical stability, set threshold = math.inf.

property rank
property is_full_rank
eager_reduce(op, reduced_vars)[source]
align_gaussian(new_inputs, old, expand=False)[source]

Align data of a Gaussian distribution to a new inputs shape.


moment_matching_contract_joint(red_op, bin_op, reduced_vars, discrete, gaussian)[source]
eager_reduce_exp(op, arg, reduced_vars)[source]
eager_independent_joint(joint, reals_var, bint_var, diag_var)[source]


class Contraction(*args, **kwargs)[source]

Bases: Funsor

Declarative representation of a finitary sum-product operation.

After normalization via the normalize() interpretation contractions will canonically order their terms by type:

Delta, Number, Tensor, Gaussian

alias of Contraction

eager_contraction_generic_to_tuple(red_op, bin_op, reduced_vars, *terms)[source]
eager_contraction_generic_recursive(red_op, bin_op, reduced_vars, terms)[source]
eager_contraction_to_reduce(red_op, bin_op, reduced_vars, term)[source]
eager_contraction_to_binary(red_op, bin_op, reduced_vars, lhs, rhs)[source]
eager_contraction_tensor(red_op, bin_op, reduced_vars, *terms)[source]
eager_contraction_gaussian(red_op, bin_op, reduced_vars, x, y)[source]
normalize_contraction_commutative_canonical_order(red_op, bin_op, reduced_vars, *terms)[source]
normalize_contraction_commute_joint(red_op, bin_op, reduced_vars, other, mixture)[source]
normalize_contraction_generic_args(red_op, bin_op, reduced_vars, *terms)[source]
normalize_trivial(red_op, bin_op, reduced_vars, term)[source]
normalize_contraction_generic_tuple(red_op, bin_op, reduced_vars, terms)[source]
binary_to_contract(op, lhs, rhs)[source]
reduce_funsor(op, arg, reduced_vars)[source]
unary_neg_variable(op, arg)[source]
do_fresh_subs(arg, subs)[source]
distribute_subs_contraction(arg, subs)[source]
normalize_fuse_subs(arg, subs)[source]
binary_subtract(op, lhs, rhs)[source]
binary_divide(op, lhs, rhs)[source]
unary_log_exp(op, arg)[source]
unary_contract(op, arg)[source]


class Integrate(log_measure, integrand, reduced_vars)[source]

Bases: Funsor

Funsor representing an integral wrt a log density funsor.

  • log_measure (Funsor) – A log density funsor treated as a measure.

  • integrand (Funsor) – An integrand funsor.

  • reduced_vars (str, Variable, or set or frozenset thereof.) – An input name or set of names to reduce.


class ConstantMeta(name, bases, dct)[source]

Bases: FunsorMeta

Wrapper to convert const_inputs to a tuple.

class Constant(const_inputs, arg)[source]

Bases: Funsor

Funsor that is constant wrt const_inputs.

Constant can be used for provenance tracking.


a = Constant(OrderedDict(x=Real, y=Bint[3]), Number(0))
a(y=1)  # returns Constant(OrderedDict(x=Real), Number(0))
a(x=2, y=1)  # returns Number(0)

d = Tensor(torch.tensor([1, 2, 3]))["y"]
a + d  # returns Constant(OrderedDict(x=Real), d)

c = Constant(OrderedDict(x=Bint[3]), Number(1))
c.reduce(ops.add, "x")  # returns Number(3)
  • const_inputs (dict) – A mapping from input name (str) to datatype (funsor.domain.Domain).

  • arg (funsor) – A funsor that is constant wrt to const_inputs.

eager_reduce(op, reduced_vars)[source]

Attempt to convert a Funsor to a Number or Tensor by substituting arange() s into its free variables.


x (Funsor) – A funsor.

Return type


eager_reduce_add(op, arg, reduced_vars)[source]
eager_binary_constant_constant(op, lhs, rhs)[source]
eager_binary_constant_tensor(op, lhs, rhs)[source]
eager_binary_tensor_constant(op, lhs, rhs)[source]
eager_unary(op, arg)[source]