Functionals API#
Functionals are scalar-valued maps on a domain space \(f : X \to R\).
Gradients are represented as elements of X using the domain geometry: for a
differentiable functional, grad(x) satisfies
\(\langle \nabla f(x), v \rangle_X = D f(x)[v]\) when implemented.
Base and composition#
Scalar-valued map on a space. |
|
Generic pull-back of a functional through a linear operator. |
|
Return the pull-back |
Functionalis the base scalar-valued map contract.ComposedFunctionalrepresents pullbackf o Afor a linear operatorA.make_functional_composedconstructs the same pullback with simplifications.
Linear functionals#
Represent a linear scalar-valued map. |
|
Linear functional represented by a domain element. |
|
Linear functional defined by user-supplied evaluation callables. |
LinearFunctionalis the base class for linear maps to scalars.InnerProductFunctionalstores a Riesz representercand evaluates<c, x>.MatrixFreeLinearFunctionalwraps callable value and gradient logic.
Quadratic functionals#
Represent a scalar quadratic objective on a space. |
|
Represent a quadratic form backed by a linear operator. |
QuadraticFormmodels objectives with value, gradient, and Hessian-vector action.LinOpQuadraticFormrepresents0.5 <x, Qx> + ell(x) + a.
Battery functionals#
Named constructors over the existing machinery (ADR-019). Their gradients are metric (Riesz) gradients under the domain geometry.
Build the least-squares objective |
|
Half the squared space norm |
|
Coordinate |
|
Coordinate 1-norm |
|
Schatten |
|
Nuclear (trace) norm, a thin wrapper for |
|
Negative (Shannon) entropy |
|
Kullback--Leibler divergence to a fixed positive |
|
Separable Huber loss |
least_squaresbuilds thescale ||A x - b||^2objective as aLinOpQuadraticForm.SquaredL2NormFunctionalis1/2 ||x||_X^2(gradientx, clean shrinkage prox).LpNormFunctional/L1NormFunctionalare coordinatep-norms.SpectralLpNormFunctional/NuclearNormFunctionalare the Schattenp-norm and nuclear norm of a Jordan spectrum (e.g. Hermitian eigenvalues).NegativeEntropyFunctionalandKLDivergenceFunctionalare the entropy objectives.HuberFunctionalis the separable Huber loss.
These constructors live in the spacecore.functional.tools subpackage and are
re-exported from spacecore.functional and the top-level spacecore namespace.
Proximal and projection#
A closed-form, metric-aware proximal primitive and its named wrappers (ADR-019). Valid on Euclidean and diagonal metrics; a non-diagonal metric raises.
Solve the separable forward--backward subproblem in the space metric. |
|
Proximal operator of |
|
Proximal operator of |
|
Metric projection onto the nonnegative orthant: coordinatewise |
generalized_shrinkagesolves<c, x>_X + eps ||x - x0||^2_X + lam ||x||_1(optionallyx >= 0).prox_l1is the metric soft-threshold;prox_l2sqis the shrinkagev / (1 + t).project_nonnegis the metric projection onto the nonnegative orthant.
Autodoc#
- class spacecore.functional.Functional(dom, ctx=None)[source]#
Bases:
ContextBound,Generic[Domain]Scalar-valued map on a space.
Functionalrepresents a mapF : X -> Kwithout assuming any storage model. It mirrors the minimalLinOpcontract: the domain is converted into the resolved context, value checks followctx.check_level, and batched evaluation is implemented by a backendvmapfallback.- Parameters:
- property domain: Domain#
Domain space of this scalar-valued map.
- abstractmethod value(x)[source]#
Evaluate this functional at an element of
self.domain.- Parameters:
x (Any)
- Return type:
Any
- grad(x)[source]#
Gradient at an element of
self.domain.Override in subclasses that support differentiation; the base raises
NotImplementedError.- Parameters:
x (Any)
- Return type:
Any
- vgrad(xs)[source]#
Gradient over a leading batch axis.
Override in subclasses that support differentiation; the base raises
NotImplementedError.- Parameters:
xs (Any)
- Return type:
Any
- compose(A)[source]#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- vvalue(xs)[source]#
Evaluate over a leading batch axis. Input must have shape
(N,) + domain.shape; usemoveaxisfor other layouts.- Parameters:
xs (Any)
- Return type:
Any
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- class spacecore.functional.LinearFunctional(dom, ctx=None)[source]#
Bases:
Functional[Domain]Represent a linear scalar-valued map.
- Parameters:
- grad(x)[source]#
Return the constant Riesz gradient of this linear functional.
For
ell(x) = <c, x>_X, the gradient is the space elementc. Matrix-free functionals without a stored representer inherit theNotImplementedErrorraised byrepresenter.- Parameters:
x (Any)
- Return type:
Any
- vgrad(xs)[source]#
Return the constant Riesz gradient over a leading batch axis.
- Parameters:
xs (Any)
- Return type:
Any
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- abstractmethod value(x)#
Evaluate this functional at an element of
self.domain.- Parameters:
x (Any)
- Return type:
Any
- vvalue(xs)#
Evaluate over a leading batch axis. Input must have shape
(N,) + domain.shape; usemoveaxisfor other layouts.- Parameters:
xs (Any)
- Return type:
Any
- class spacecore.functional.InnerProductFunctional(c, dom, ctx=None)[source]#
Bases:
LinearFunctional[Domain]Linear functional represented by a domain element.
InnerProductFunctional(c, X)evaluates \(\ell_c(x) = \langle c, x\rangle_X\).- Parameters:
- representer#
Stored domain element
c.- Type:
array-like
- vvalue(xs)[source]#
Evaluate
domain.inner(representer, xs[i])without a Python loop.- Parameters:
xs (Any)
- Return type:
Any
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x)#
Return the constant Riesz gradient of this linear functional.
For
ell(x) = <c, x>_X, the gradient is the space elementc. Matrix-free functionals without a stored representer inherit theNotImplementedErrorraised byrepresenter.- Parameters:
x (Any)
- Return type:
Any
- vgrad(xs)#
Return the constant Riesz gradient over a leading batch axis.
- Parameters:
xs (Any)
- Return type:
Any
- class spacecore.functional.MatrixFreeLinearFunctional(value, dom, ctx=None, vvalue=None)[source]#
Bases:
LinearFunctional[Domain]Linear functional defined by user-supplied evaluation callables.
MatrixFreeLinearFunctional(value, X)represents a linear scalar-valued map onXwithout storing or materializing a Riesz representer.- Parameters:
value (callable) – Callable with signature
value(x: Any) -> Anyaccepting an element ofdomand returning a scalar-like backend value.dom (Space) – Domain space of the functional.
ctx (Context, str, or None, optional) – Optional context specification. An explicit context wins over inferred and default contexts.
vvalue (callable or None, optional) – Optional callable with signature
vvalue(xs: Any) -> Anyfor batched evaluation. If omitted, backendvmapfallback is used.
- Returns:
Functional using the supplied callable for scalar evaluation and, optionally, batched scalar evaluation.
- Return type:
- __init__(value, dom, ctx=None, vvalue=None)[source]#
Initialize a matrix-free linear functional.
- Parameters:
value (Callable[[Any], Any]) – Callable
value(x)accepting an element ofdomand returning a scalar-like value.dom (Domain) – Domain space of the functional.
ctx (Context | str | None) – Optional context specification for the functional and converted domain.
vvalue (Callable[[Any], Any] | None) – Optional callable
vvalue(xs)accepting a batch of domain elements and returning a batch of scalar-like values.
- Returns:
The initializer stores the callables and converted domain on
self.- Return type:
None
- value(x)[source]#
Evaluate the scalar functional.
- Parameters:
x (Any) – Element of
self.domainpassed tovalue_fn.- Returns:
Scalar-like backend value returned by
value_fn.- Return type:
Any
- vvalue(xs)[source]#
Evaluate the scalar functional over a batch of domain elements.
- Parameters:
xs (Any) – Batched element of
self.domain.- Returns:
Backend array of scalar-like values with shape matching the leading batch shape.
- Return type:
Any
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x)#
Return the constant Riesz gradient of this linear functional.
For
ell(x) = <c, x>_X, the gradient is the space elementc. Matrix-free functionals without a stored representer inherit theNotImplementedErrorraised byrepresenter.- Parameters:
x (Any)
- Return type:
Any
- vgrad(xs)#
Return the constant Riesz gradient over a leading batch axis.
- Parameters:
xs (Any)
- Return type:
Any
- class spacecore.functional.QuadraticForm(dom, ctx=None)[source]#
Bases:
Functional[Domain]Represent a scalar quadratic objective on a space.
- Parameters:
- hess_apply(x)[source]#
Apply the Hessian action at
xwhen available.- Parameters:
x (Any)
- Return type:
Any
- grad(x)[source]#
Return the gradient with respect to
domain.innerwhen available.- Parameters:
x (Any)
- Return type:
Any
- vgrad(xs)[source]#
Evaluate
gradindependently over leading batch axes.- Parameters:
xs (Any)
- Return type:
Any
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- abstractmethod value(x)#
Evaluate this functional at an element of
self.domain.- Parameters:
x (Any)
- Return type:
Any
- vvalue(xs)#
Evaluate over a leading batch axis. Input must have shape
(N,) + domain.shape; usemoveaxisfor other layouts.- Parameters:
xs (Any)
- Return type:
Any
- class spacecore.functional.LinOpQuadraticForm(Q, linear=None, a=0, ctx=None)[source]#
Bases:
QuadraticForm[Domain]Represent a quadratic form backed by a linear operator.
- Assumption:
Q is Hermitian/self-adjoint. Under this assumption, grad f(x) = Q x.
Non-Hermitian operators are not supported here. If users need the Hermitian part, they must construct 0.5 * (Q + Q.H) explicitly.
The full objective is
q(x) = 1/2 * <x, Qx> + linear(x) + awithQ : X -> X. Structurally available dense and diagonal operators are checked at construction. Matrix-free operators are not validated; correctness is the caller’s responsibility.- Parameters:
Q (LinOp) – Hermitian operator from a space to itself.
linear (LinearFunctional or None, optional) – Optional linear term on
Q.domain.a (scalar-like, optional) – Constant scalar offset. Default is 0.
ctx (Context, str, or None, optional) – Backend context specification. Default is resolved from
Qandlinear.
- linear#
Stored linear term.
- Type:
LinearFunctional or None
- a#
Stored scalar offset.
- Type:
scalar-like
- grad(x)[source]#
Return the gradient with respect to
domain.inner.This is the Riesz gradient: for Euclidean geometry it is the ordinary coordinate gradient, while for non-Euclidean geometry it is corrected by the domain inner product.
LinOpQuadraticFormassumesQis Hermitian/self-adjoint, so the quadratic contribution is exactlyQ.apply(x).- Parameters:
x (Any)
- Return type:
Any
- hess_apply(x)[source]#
Return the Hessian action
Q xunder the Hermitian assumption.- Parameters:
x (Any)
- Return type:
Any
- vvalue(xs)[source]#
Evaluate the quadratic objective over a leading batch axis.
- Parameters:
xs (Any)
- Return type:
Any
- vgrad(xs)[source]#
Evaluate the Riesz gradient over a leading batch axis.
- Parameters:
xs (Any)
- Return type:
Any
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- class spacecore.functional.ComposedFunctional(F, A)[source]#
Bases:
FunctionalGeneric pull-back of a functional through a linear operator.
ComposedFunctional(F, A)representsx -> F(A x)onA.domain.- Parameters:
F (Functional) – Functional defined on
A.codomain.A (LinOp) – Linear operator whose codomain is
F.domain.
- value(x)[source]#
Evaluate
F(A x).- Parameters:
x (Any) – Element of
A.domain.- Returns:
Scalar-like value returned by the composed functional.
- Return type:
Any
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x)#
Gradient at an element of
self.domain.Override in subclasses that support differentiation; the base raises
NotImplementedError.- Parameters:
x (Any)
- Return type:
Any
- vgrad(xs)#
Gradient over a leading batch axis.
Override in subclasses that support differentiation; the base raises
NotImplementedError.- Parameters:
xs (Any)
- Return type:
Any
- vvalue(xs)#
Evaluate over a leading batch axis. Input must have shape
(N,) + domain.shape; usemoveaxisfor other layouts.- Parameters:
xs (Any)
- Return type:
Any
- spacecore.functional.make_functional_composed(F, A)[source]#
Return the pull-back
F o Awith local specializations.- Parameters:
F (Functional) – Functional defined on
A.codomain.A (LinOp) – Linear operator whose codomain is
F.domain.
- Returns:
Specialized pull-back when available, otherwise
ComposedFunctional.- Return type:
- spacecore.functional.least_squares(A, b, *, weights=None, scale=0.5)[source]#
Build the least-squares objective
scale * ||A x - b||^2as a quadratic form.The objective is expanded into the canonical
LinOpQuadraticFormq(x) = 1/2 <x, Q x>_X + <c, x>_X + awithQ = 2 scale * (A.H @ A)(the normal operator, Hermitian by construction),c = -2 scale * A.H(b)(a Riesz/metric gradient, the linear term), anda = scale * <b, b>_Y(the constant residual energy),
so that
q(x)equalsscale * ||A x - b||_Y^2exactly on real spaces. Withscale = 0.5(the default) this is the textbook1/2 ||A x - b||^2andQis exactlyA.H @ A.Adjoints and inner products are taken in the operator’s declared geometry (ADR-009), so the result is metric-correct on non-Euclidean domains and codomains without any extra work by the caller.
- Parameters:
A (LinOp) – Forward operator
A : X -> Y.b (array-like) – Observation in the codomain
Y = A.codomain.weights (array-like or None, optional) – Diagonal residual weights
wwith the codomain shape and strictly positive, finite entries. When given, the objective is the weighted least squaresscale * <A x - b, W (A x - b)>_YwithW = diag(w). DefaultNoneis the unweighted objective.scale (float, optional) – Positive scalar multiplying the squared residual. Default
0.5.
- Returns:
Quadratic form whose
valueis the (weighted) least-squares objective and whosegradis the metric gradient2 scale * A.H(W (A x - b)).- Return type:
Examples
>>> import numpy as np >>> import spacecore as sc >>> ctx = sc.Context(sc.NumpyOps(), dtype=np.float64) >>> X = sc.DenseCoordinateSpace((2,), ctx) >>> Y = sc.DenseCoordinateSpace((2,), ctx) >>> A = sc.DenseLinOp(ctx.asarray([[1.0, 0.0], [0.0, 2.0]]), X, Y, ctx) >>> f = sc.least_squares(A, ctx.asarray([1.0, 4.0])) >>> float(f.value(ctx.asarray([1.0, 2.0]))) 0.0
- class spacecore.functional.SquaredL2NormFunctional(dom, ctx=None)[source]#
Bases:
_CoordinateFunctional[Domain]Half the squared space norm
F(x) = 1/2 ||x||_X^2 = 1/2 <x, x>_X.This is the smooth quadratic energy whose Riesz gradient is
xand whose proximal operator is the clean shrinkagev / (1 + t)(seeprox_l2sq()). It is intentionally distinct fromLpNormFunctional(X, 2), which is the un-squared coordinate 2-norm(sum_i |x_i|^2)^{1/2}.- Parameters:
Examples
>>> import numpy as np >>> import spacecore as sc >>> ctx = sc.Context(sc.NumpyOps(), dtype=np.float64) >>> X = sc.DenseCoordinateSpace((2,), ctx) >>> f = sc.SquaredL2NormFunctional(X) >>> float(f.value(ctx.asarray([3.0, 4.0]))) 12.5 >>> np.asarray(f.grad(ctx.asarray([3.0, 4.0]))) array([3., 4.])
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- vgrad(xs)#
Evaluate
grad()independently over a leading batch axis.- Parameters:
xs (Any)
- Return type:
Any
- vvalue(xs)#
Evaluate over a leading batch axis. Input must have shape
(N,) + domain.shape; usemoveaxisfor other layouts.- Parameters:
xs (Any)
- Return type:
Any
- class spacecore.functional.LpNormFunctional(dom, p, ctx=None)[source]#
Bases:
_CoordinateFunctional[Domain]Coordinate
p-normF(x) = (sum_i |x_i|^p)^{1/p}forp >= 1.- Parameters:
Notes
The gradient at
x != 0isd/dx_i ||x||_p = sign(x_i) |x_i|^{p-1} / ||x||_p^{p-1}(Riesz-corrected). At the origin the function is not differentiable; this returns the zero subgradient there. Forp = 1the gradient issign(x).Examples
>>> import numpy as np >>> import spacecore as sc >>> ctx = sc.Context(sc.NumpyOps(), dtype=np.float64) >>> X = sc.DenseCoordinateSpace((3,), ctx) >>> f = sc.LpNormFunctional(X, 1) >>> float(f.value(ctx.asarray([1.0, -2.0, 3.0]))) 6.0
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x)#
Return the Riesz gradient under the domain geometry.
- Parameters:
x (Any)
- Return type:
Any
- vgrad(xs)#
Evaluate
grad()independently over a leading batch axis.- Parameters:
xs (Any)
- Return type:
Any
- vvalue(xs)#
Evaluate over a leading batch axis. Input must have shape
(N,) + domain.shape; usemoveaxisfor other layouts.- Parameters:
xs (Any)
- Return type:
Any
- spacecore.functional.L1NormFunctional(dom, ctx=None)[source]#
Coordinate 1-norm
||x||_1– a thin wrapper forLpNormFunctional(X, 1).- Parameters:
- Returns:
The
p = 1instance ofLpNormFunctional.- Return type:
- class spacecore.functional.SpectralLpNormFunctional(dom, p, ctx=None)[source]#
Bases:
_CoordinateFunctional[Domain]Schatten
p-normF(X) = (sum_i |lambda_i(X)|^p)^{1/p}forp >= 1.lambda(X)is the Jordan-algebraic spectrum ofX(eigenvalues for a Hermitian matrix). The gradient is the spectral function gradient: withX = U diag(lambda) U^*, it isU diag(g) U^*wheregis the coordinatep-norm gradient oflambda, reconstructed through the space’sfrom_spectrum.p = 1is the nuclear / trace norm (seeNuclearNormFunctional());p = 2is the Frobenius norm.- Parameters:
dom (JordanAlgebraSpace) – Domain space with a spectral decomposition (e.g.
HermitianSpace). A space without a Jordan spectrum raisesTypeError.p (float) – Norm order; must be finite and
>= 1.ctx (Context, str, or None, optional) – Backend context specification. Default is resolved from
dom.
Examples
>>> import numpy as np >>> import spacecore as sc >>> ctx = sc.Context(sc.NumpyOps(), dtype=np.float64) >>> X = sc.HermitianSpace(2, ctx=ctx) >>> A = ctx.asarray([[2.0, 0.0], [0.0, -3.0]]) >>> f = sc.SpectralLpNormFunctional(X, 1) # nuclear norm |2| + |-3| >>> float(f.value(A)) 5.0
- value(x)[source]#
Return the Schatten-
pnorm(sum_i |lambda_i|^p)^{1/p}.- Parameters:
x (Any)
- Return type:
Any
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x)#
Return the Riesz gradient under the domain geometry.
- Parameters:
x (Any)
- Return type:
Any
- vgrad(xs)#
Evaluate
grad()independently over a leading batch axis.- Parameters:
xs (Any)
- Return type:
Any
- vvalue(xs)#
Evaluate over a leading batch axis. Input must have shape
(N,) + domain.shape; usemoveaxisfor other layouts.- Parameters:
xs (Any)
- Return type:
Any
- spacecore.functional.NuclearNormFunctional(dom, ctx=None)[source]#
Nuclear (trace) norm, a thin wrapper for
SpectralLpNormFunctional(X, 1).Computes
sum_i |lambda_i(X)|, the Schatten-1 norm of the Jordan spectrum.- Parameters:
dom (JordanAlgebraSpace) – Domain space with a spectral decomposition.
ctx (Context, str, or None, optional) – Backend context specification. Default is resolved from
dom.
- Returns:
The
p = 1instance ofSpectralLpNormFunctional.- Return type:
- class spacecore.functional.NegativeEntropyFunctional(dom, ctx=None)[source]#
Bases:
_CoordinateFunctional[Domain]Negative (Shannon) entropy
F(x) = sum_i x_i log x_i.The natural domain is the positive orthant
x_i > 0; the value uses the convention0 log 0 = 0so the origin and zero coordinates evaluate cleanly, but the gradientlog x_i + 1is only defined forx_i > 0.- Parameters:
Examples
>>> import numpy as np >>> import spacecore as sc >>> ctx = sc.Context(sc.NumpyOps(), dtype=np.float64) >>> X = sc.DenseCoordinateSpace((2,), ctx) >>> f = sc.NegativeEntropyFunctional(X) >>> float(f.value(ctx.asarray([1.0, 1.0]))) 0.0 >>> np.asarray(f.grad(ctx.asarray([1.0, 1.0]))) # log(x) + 1 array([1., 1.])
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x)#
Return the Riesz gradient under the domain geometry.
- Parameters:
x (Any)
- Return type:
Any
- vgrad(xs)#
Evaluate
grad()independently over a leading batch axis.- Parameters:
xs (Any)
- Return type:
Any
- vvalue(xs)#
Evaluate over a leading batch axis. Input must have shape
(N,) + domain.shape; usemoveaxisfor other layouts.- Parameters:
xs (Any)
- Return type:
Any
- class spacecore.functional.KLDivergenceFunctional(target, dom, ctx=None)[source]#
Bases:
_CoordinateFunctional[Domain]Kullback–Leibler divergence to a fixed positive
target.F(x) = sum_i x_i log(x_i / t_i)against a strictly positive targett. The natural domain isx_i >= 0(with0 log 0 = 0) andt_i > 0. Withtargetequal to the all-ones element this reduces exactly toNegativeEntropyFunctional, and the gradientlog(x_i / t_i) + 1reduces accordingly.- Parameters:
target (array-like) – Reference element
tindomwith strictly positive coordinates.dom (Space) – Domain space
X. ADR-019 writesKLDivergenceFunctional(target); the explicit space follows the(data, dom, ctx)constructor shape used byInnerProductFunctional, because a bare element does not carry its space.ctx (Context, str, or None, optional) – Backend context specification. Default is resolved from
dom.
Examples
>>> import numpy as np >>> import spacecore as sc >>> ctx = sc.Context(sc.NumpyOps(), dtype=np.float64) >>> X = sc.DenseCoordinateSpace((2,), ctx) >>> f = sc.KLDivergenceFunctional(ctx.asarray([1.0, 1.0]), X) >>> float(f.value(ctx.asarray([1.0, 1.0]))) # zero divergence at x == target 0.0 >>> np.asarray(f.grad(ctx.asarray([1.0, 1.0]))) # log(x / t) + 1 array([1., 1.])
- property target: Any#
Stored reference element
t.
- value(x)[source]#
Return
sum_i x_i log(x_i / t_i)with0 log 0 = 0.- Parameters:
x (Any)
- Return type:
Any
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x)#
Return the Riesz gradient under the domain geometry.
- Parameters:
x (Any)
- Return type:
Any
- vgrad(xs)#
Evaluate
grad()independently over a leading batch axis.- Parameters:
xs (Any)
- Return type:
Any
- vvalue(xs)#
Evaluate over a leading batch axis. Input must have shape
(N,) + domain.shape; usemoveaxisfor other layouts.- Parameters:
xs (Any)
- Return type:
Any
- class spacecore.functional.HuberFunctional(dom, delta, ctx=None)[source]#
Bases:
_CoordinateFunctional[Domain]Separable Huber loss
F(x) = sum_i h_delta(x_i).The per-coordinate loss is quadratic near the origin and linear in the tails:
h_delta(r) = 1/2 r^2for|r| <= deltaanddelta (|r| - delta/2)otherwise. It is everywhere differentiable, with gradientrin the quadratic region anddelta sign(r)in the tails.- Parameters:
Examples
>>> import numpy as np >>> import spacecore as sc >>> ctx = sc.Context(sc.NumpyOps(), dtype=np.float64) >>> X = sc.DenseCoordinateSpace((2,), ctx) >>> f = sc.HuberFunctional(X, 1.0) >>> float(f.value(ctx.asarray([0.5, 3.0]))) # 0.125 + (3 - 0.5) 2.625
- assert_domain(x)#
Raise if
xis not in the domain.- Parameters:
x (Any)
- Return type:
None
- property check_level: Literal['none', 'cheap', 'standard', 'strict']#
Return this object’s runtime validation level.
- compose(A)#
Return the pull-back
self o A.- Parameters:
A (LinOp) – Linear operator whose codomain matches this functional’s domain.
- Returns:
Functional on
A.domainevaluatingself.value(A.apply(x)).- Return type:
- convert(new_ctx=None)#
Return this object represented in
new_ctx.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x)#
Return the Riesz gradient under the domain geometry.
- Parameters:
x (Any)
- Return type:
Any
- vgrad(xs)#
Evaluate
grad()independently over a leading batch axis.- Parameters:
xs (Any)
- Return type:
Any
- vvalue(xs)#
Evaluate over a leading batch axis. Input must have shape
(N,) + domain.shape; usemoveaxisfor other layouts.- Parameters:
xs (Any)
- Return type:
Any
- spacecore.functional.generalized_shrinkage(X, *, c, x0, eps, lam=0.0, nonneg=False)[source]#
Solve the separable forward–backward subproblem in the space metric.
Returns the coordinatewise minimizer of
<c, x>_X + eps * ||x - x0||^2_X + lam * ||x||_1 (optionally x >= 0)
The smooth part has unconstrained minimizer
v = x0 - c / (2 eps)(the metric weight cancels, becausecis a metric gradient). Thel1term then applies a soft-threshold with the metric-aware widthtau_i = lam / (2 eps w_i)for a diagonal weightw(w_i = 1on a Euclidean space); withnonneg=Truethe nonnegative-orthant constraint turns the step intomax(v - tau, 0).- Parameters:
X (Space) – Ambient inner-product space. Must be Euclidean or have a diagonal (weighted) metric; a non-diagonal metric raises.
c (array-like) – Metric gradient (linear-term coefficient) in
X.x0 (array-like) – Proximal center in
X.eps (float) – Strictly positive, finite quadratic weight.
lam (float, optional) – Nonnegative, finite
l1weight. Default0.0(no thresholding).nonneg (bool, optional) – Constrain the solution to
x >= 0(real spaces only). DefaultFalse.
- Returns:
The minimizer, an element of
X.- Return type:
Element
- spacecore.functional.prox_l1(v, t, X)[source]#
Proximal operator of
t * ||.||_1in the space metric (soft-threshold).Returns
argmin_x 1/2 ||x - v||^2_X + t ||x||_1, i.e. the metric-aware soft-threshold with per-coordinate widtht / w_ion a diagonal metric.- Parameters:
v (array-like) – Point in
Xto be thresholded.t (float) – Nonnegative threshold / step size.
X (Space) – Ambient inner-product space (Euclidean or diagonal metric).
- Returns:
The soft-thresholded element.
- Return type:
Element
- spacecore.functional.prox_l2sq(v, t, X)[source]#
Proximal operator of
t * (1/2) ||.||_X^2(linear shrinkagev / (1 + t)).Returns
argmin_x 1/2 ||x - v||^2_X + t (1/2) ||x||^2_X = v / (1 + t).Completing the square casts this into the primitive’s form: it is the minimizer of
<-v, x>_X + ((1 + t)/2) ||x||^2_X, i.e.generalized_shrinkagewithc = -v,x0 = 0andeps = (1 + t)/2.- Parameters:
v (array-like) – Point in
Xto be shrunk.t (float) – Nonnegative step size.
X (Space) – Ambient inner-product space (Euclidean or diagonal metric).
- Returns:
The shrunk element
v / (1 + t).- Return type:
Element
- spacecore.functional.project_nonneg(v, X)[source]#
Metric projection onto the nonnegative orthant: coordinatewise
max(v, 0).This is the proximal operator of the indicator of
{x : x >= 0}. On a diagonal metric the projection is still coordinatewise (the orthant is separable), and a non-diagonal metric raises via the shared primitive.- Parameters:
v (array-like) – Point in
Xto project.X (Space) – Ambient inner-product space (Euclidean or diagonal metric).
- Returns:
The projected element
max(v, 0).- Return type:
Element