Functionals API#
Functionals are real 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
\(\operatorname{Re}\langle \nabla f(x), v \rangle_X = D f(x)[v]\) when implemented.
Beck, First-Order Methods in Optimization, Remark 3.32; on complex domains
use the real inner product as in Geometry and Riesz Maps.
Differentials#
f.derivative_at(x) returns a LinearFunctional acting on directions:
df(h) evaluates \(Df(x)[h]\). f.value_and_derivative(x) returns the
value and differential together, reusing an existing fused
value_and_grad implementation when adapting a gradient-based functional.
import numpy as np
import spacecore as sc
ctx = sc.Context(sc.NumpyOps(), dtype=np.float64)
X = sc.DenseCoordinateSpace((2,), ctx,
geometry=sc.WeightedInnerProduct(ctx.asarray([2., 5.])))
f = sc.SquaredL2NormFunctional(X)
value, df = f.value_and_derivative(ctx.asarray([1., 2.]))
slope = df(ctx.asarray([3., 4.])) # 46.0
gradient = df.representer # [1., 2.], in X's metric
Existing grad implementations remain valid. New functionals can implement
derivative_at directly. The default grad then requests the derivative’s
representer; a callable-only differential raises NotImplementedError
instead of reconstructing a dense gradient. Implement both fused methods when
a native evaluator needs to share work in both interfaces.
Sums, scalar multiples, and pullbacks of linear functionals remain
LinearFunctional objects. Products of objectives use the product rule.
A callable differential can be pulled back through A using only forward
application: \((Df(x)\circ A)[h]=Df(x)[Ah]\). Retrieving its gradient may
additionally require a representer and the metric adjoint.
Functionals are real-valued. The codomain is always RealField; complex
domains remain supported. With checks on (cheap and above), a complex output
dtype, coefficient or offset raises TypeError, even with a zero imaginary
part; this covers batched and fused evaluations and subclass overrides.
Extended-real indicator values remain valid.
Functionals pair through the domain’s real inner product
real_inner(x, y) \(=\operatorname{Re}\langle x,y\rangle_X\): InnerProductFunctional
evaluates real_inner(c, h) and LinOpQuadraticForm evaluates
real_inner(x, Qx) / 2.
Matrix-free callables return real values themselves.
At a nonsmooth point, a chosen subgradient does not become a Frechet derivative; use these interfaces where the objective is differentiable.
This API composes with existing linear operators. General nonlinear-map linearization and manifold tangent spaces are separate extensions.
Base and composition#
Optional proximal methods#
Functional.prox(x, step) computes a registered closed-form prox in the
domain metric. conjugate_prox uses Hilbert-space Moreau decomposition.
IndicatorFunctional connects a closed convex set’s projection to this API.
See Metric proximal operations for conventions, supported rules,
compiled execution, and custom representations.
|
The extended-real indicator of a closed convex set. |
|
A closed subset of an ambient space, with optional membership and projection. |
|
Nonnegative real dense coordinates in Euclidean or diagonal geometry. |
Scalar maps#
A real-valued map |
|
Generic pull-back of a functional through a linear operator. |
|
Return the pull-back |
Algebra#
Lazy nodes for combining functionals. The operator overloads on Functional
(a * F, F + G, F - G, -F, F * G) delegate to the make_*
factories, which apply local, structural canonicalization — they read node
types, never values.
|
Lazy scalar multiple |
|
Lazy sum of finitely many functionals on a common domain. |
|
The zero functional: value |
|
The constant functional |
|
Lazy pointwise product |
|
Return |
|
Return the lazy sum of functionals, flattened, with zero terms dropped. |
|
Return |
|
Return the constant functional on |
|
Return |
ConstantFunctionalisx -> c, the embedding of a scalar into the algebra;make_constant_functionalcollapsesc = 0toZeroFunctional.ProductFunctionalis the pointwise productF(x) * G(x), with the product-rule Riesz gradient \(G(x)\, \nabla F(x) + F(x)\, \nabla G(x)\). Because functionals are scalar-valued, scaling is the constant-factor case of a product:make_functional_productfolds aConstantFunctionalfactor back into aScaledFunctional, and aZeroFunctionalfactor to zero.
The product rule follows from D(FG)[h] = G DF[h] + F DG[h] using the
real pairing. All values and scaling coefficients are real.
Mapping#
|
A map from |
Linear functionals#
A linear scalar-valued map on |
|
The functional \(x \mapsto \operatorname{Re}\langle c, x\rangle_X\) of a representer |
|
Linear functional given by an evaluation callable; no Riesz representer is stored. |
MatrixFreeLinearFunctionalwraps a callable evaluator; a stored gradient is optional and is not inferred from the callable.
Quadratic functionals#
A scalar quadratic objective on |
|
Quadratic form |
Norm functionals#
Coordinate norms (the ones the closed-form prox rules cover). Their gradients are metric (Riesz) gradients under the domain geometry.
Half the squared space norm |
|
Coordinate |
|
Coordinate 1-norm: |
The norms live in the spacecore.functional.tools subpackage and are
re-exported from spacecore.functional and the top-level spacecore namespace.
Least squares, entropy, Huber, spectral lifts, the realified view
(RealifiedFunctional, realify) and the shrinkage helpers are in
spacecore-optimize.
Autodoc#
- class spacecore.functional.Functional(*args, **kwargs)[source]#
Bases:
Mapping,Generic[Domain]A real-valued map
F : X -> R; subclasses implementvalueand optionallygrad.gradis the Riesz gradient, an element ofX. Batched evaluation falls back tovmap.- Parameters:
- property codomain: RealField#
The independently bound scalar field of this functional’s values.
- property domain: Domain#
Domain space of this scalar-valued map.
- value(x, *args, **kwargs)[source]#
Evaluate this functional at an element of
self.domain.Subclasses may accept extra positional/keyword arguments — auxiliary parameters such as data, temperature, or a penalty weight — that are not part of the domain. Overrides that take only
xremain valid.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- grad(x, *args, **kwargs)[source]#
Riesz gradient at an element of
self.domain.Implement
_grad_core, or_derivative_corewith an available Riesz representer; otherwise raisesNotImplementedError. Any auxiliary*args/**kwargsmirror those accepted byvalue().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- value_and_grad(x, *args, **kwargs)[source]#
Return
(value, gradient)atx.Implement
_value_and_grad_corefor a single-pass evaluator (e.g.jax.value_and_grad); it must return the same pair as the separate cores, with the gradient in the same geometry asgrad().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, Any]
- prox(x, step)[source]#
Minimize
f(y) + ||y-x||_X^2 / (2 step)for positivestep.This optional operation uses a closed-form rule registered with
register("prox", FunctionalType, GeometryType, rule). The rule takes(functional, geometry, x, step)and returns a native domain element. An unsupported functional/geometry pair raisesCapabilityError; sums and linear pullbacks do not acquire a prox automatically.Prox rules assert a proper, lower-semicontinuous convex real objective (also on complex spaces, using the real inner product). At check level
"none", traced steps must be finite and positive; their values are the caller’s responsibility. Concrete invalid Python steps still raise.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- conjugate_prox(x, step)[source]#
Return the conjugate prox by Hilbert-space Moreau decomposition.
The conjugate is
f*_X(p) = sup_z Re <p,z>_X - f(z): dual variables are Riesz representers in the same space, not raw coordinate covectors. Thusprox_(step f*_X)(x) = x - step prox_(f/step)(x/step). Requires a valid convex prox and a finite, strictly positive step.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- differential(x, *args, **kwargs)[source]#
Return the covector \(df(x)\) with \(Df(x)[h] = \langle df(x), h\rangle\).
The covector is a coordinate array paired with
domain.pairand involves no metric. The default reads it off the Riesz gradient,domain.riesz(grad(x)); functionals that compute coordinate partials implement_differential_core.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- derivative_at(x, *args, **kwargs)[source]#
Return the differential at
x, acting on domain directions.The default wraps the Riesz gradient,
Re <grad, h>. Implement_derivative_corefor a native directional evaluator. At nonsmooth points a supplied subgradient is not a Frechet derivative.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
- value_and_derivative(x, *args, **kwargs)[source]#
Return value and differential, preserving fused gradient evaluators.
Derivative-first subclasses may implement
_value_and_derivative_coreto share work. No representer is reconstructed from a callable differential.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, ‘LinearFunctional’]
- vgrad(xs)[source]#
Gradient over a leading batch axis; implement
_vgrad_coreto support it.- 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.
The level is stored on the bound object (
_check_level), seeded at construction. The fallback covers any construction path that assigns_ctxwithout going through__init__/_bind_context.
- convert(new_ctx=None)#
Return this object represented in
new_ctx.The object’s
check_levelis a property of the object, not of the backend/dtype context, so it is preserved across conversion rather than re-seeded from the ambient default.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- same_math(other)#
Tier-1 equality gate: same concrete type and same math context.
Combines a runtime-type identity check with
Context.same_math()(equal backend ops and dtype, ignoringcheck_level— a validation policy, not a mathematical or backend property). This is the mandatory first check in every__eq__: the type gate guaranteesotherexposes the same attributes this object’s__eq__will read, andsame_mathguarantees any laterops.allcloseruns only on same-backend, same-dtype arrays, making cross-backend objects compare unequal.Callers that fail this gate should return
NotImplementedso Python can try the reflected comparison and fall back to identity symmetrically.- Parameters:
other (Any)
- Return type:
bool
- class spacecore.functional.LinearFunctional(*args, **kwargs)[source]#
Bases:
Functional[Domain]A linear scalar-valued map on
dom.- Parameters:
- 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.
The level is stored on the bound object (
_check_level), seeded at construction. The fallback covers any construction path that assigns_ctxwithout going through__init__/_bind_context.
- property codomain: RealField#
The independently bound scalar field of this functional’s values.
- 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:
- conjugate_prox(x, step)#
Return the conjugate prox by Hilbert-space Moreau decomposition.
The conjugate is
f*_X(p) = sup_z Re <p,z>_X - f(z): dual variables are Riesz representers in the same space, not raw coordinate covectors. Thusprox_(step f*_X)(x) = x - step prox_(f/step)(x/step). Requires a valid convex prox and a finite, strictly positive step.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- convert(new_ctx=None)#
Return this object represented in
new_ctx.The object’s
check_levelis a property of the object, not of the backend/dtype context, so it is preserved across conversion rather than re-seeded from the ambient default.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- derivative_at(x, *args, **kwargs)#
Return the differential at
x, acting on domain directions.The default wraps the Riesz gradient,
Re <grad, h>. Implement_derivative_corefor a native directional evaluator. At nonsmooth points a supplied subgradient is not a Frechet derivative.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
- differential(x, *args, **kwargs)#
Return the covector \(df(x)\) with \(Df(x)[h] = \langle df(x), h\rangle\).
The covector is a coordinate array paired with
domain.pairand involves no metric. The default reads it off the Riesz gradient,domain.riesz(grad(x)); functionals that compute coordinate partials implement_differential_core.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x, *args, **kwargs)#
Riesz gradient at an element of
self.domain.Implement
_grad_core, or_derivative_corewith an available Riesz representer; otherwise raisesNotImplementedError. Any auxiliary*args/**kwargsmirror those accepted byvalue().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- prox(x, step)#
Minimize
f(y) + ||y-x||_X^2 / (2 step)for positivestep.This optional operation uses a closed-form rule registered with
register("prox", FunctionalType, GeometryType, rule). The rule takes(functional, geometry, x, step)and returns a native domain element. An unsupported functional/geometry pair raisesCapabilityError; sums and linear pullbacks do not acquire a prox automatically.Prox rules assert a proper, lower-semicontinuous convex real objective (also on complex spaces, using the real inner product). At check level
"none", traced steps must be finite and positive; their values are the caller’s responsibility. Concrete invalid Python steps still raise.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- same_math(other)#
Tier-1 equality gate: same concrete type and same math context.
Combines a runtime-type identity check with
Context.same_math()(equal backend ops and dtype, ignoringcheck_level— a validation policy, not a mathematical or backend property). This is the mandatory first check in every__eq__: the type gate guaranteesotherexposes the same attributes this object’s__eq__will read, andsame_mathguarantees any laterops.allcloseruns only on same-backend, same-dtype arrays, making cross-backend objects compare unequal.Callers that fail this gate should return
NotImplementedso Python can try the reflected comparison and fall back to identity symmetrically.- Parameters:
other (Any)
- Return type:
bool
- value(x, *args, **kwargs)#
Evaluate this functional at an element of
self.domain.Subclasses may accept extra positional/keyword arguments — auxiliary parameters such as data, temperature, or a penalty weight — that are not part of the domain. Overrides that take only
xremain valid.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- value_and_derivative(x, *args, **kwargs)#
Return value and differential, preserving fused gradient evaluators.
Derivative-first subclasses may implement
_value_and_derivative_coreto share work. No representer is reconstructed from a callable differential.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, ‘LinearFunctional’]
- value_and_grad(x, *args, **kwargs)#
Return
(value, gradient)atx.Implement
_value_and_grad_corefor a single-pass evaluator (e.g.jax.value_and_grad); it must return the same pair as the separate cores, with the gradient in the same geometry asgrad().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, Any]
- vgrad(xs)#
Gradient over a leading batch axis; implement
_vgrad_coreto support it.- 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.InnerProductFunctional(*args, **kwargs)[source]#
Bases:
LinearFunctional[Domain]The functional \(x \mapsto \operatorname{Re}\langle c, x\rangle_X\) of a representer
c.The real part of this pairing is always used. This represents real differentials on complex parameter spaces.
- Parameters:
- 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.
The level is stored on the bound object (
_check_level), seeded at construction. The fallback covers any construction path that assigns_ctxwithout going through__init__/_bind_context.
- property codomain: RealField#
The independently bound scalar field of this functional’s values.
- 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:
- conjugate_prox(x, step)#
Return the conjugate prox by Hilbert-space Moreau decomposition.
The conjugate is
f*_X(p) = sup_z Re <p,z>_X - f(z): dual variables are Riesz representers in the same space, not raw coordinate covectors. Thusprox_(step f*_X)(x) = x - step prox_(f/step)(x/step). Requires a valid convex prox and a finite, strictly positive step.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- convert(new_ctx=None)#
Return this object represented in
new_ctx.The object’s
check_levelis a property of the object, not of the backend/dtype context, so it is preserved across conversion rather than re-seeded from the ambient default.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- derivative_at(x, *args, **kwargs)#
Return the differential at
x, acting on domain directions.The default wraps the Riesz gradient,
Re <grad, h>. Implement_derivative_corefor a native directional evaluator. At nonsmooth points a supplied subgradient is not a Frechet derivative.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
- differential(x, *args, **kwargs)#
Return the covector \(df(x)\) with \(Df(x)[h] = \langle df(x), h\rangle\).
The covector is a coordinate array paired with
domain.pairand involves no metric. The default reads it off the Riesz gradient,domain.riesz(grad(x)); functionals that compute coordinate partials implement_differential_core.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x, *args, **kwargs)#
Riesz gradient at an element of
self.domain.Implement
_grad_core, or_derivative_corewith an available Riesz representer; otherwise raisesNotImplementedError. Any auxiliary*args/**kwargsmirror those accepted byvalue().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- prox(x, step)#
Minimize
f(y) + ||y-x||_X^2 / (2 step)for positivestep.This optional operation uses a closed-form rule registered with
register("prox", FunctionalType, GeometryType, rule). The rule takes(functional, geometry, x, step)and returns a native domain element. An unsupported functional/geometry pair raisesCapabilityError; sums and linear pullbacks do not acquire a prox automatically.Prox rules assert a proper, lower-semicontinuous convex real objective (also on complex spaces, using the real inner product). At check level
"none", traced steps must be finite and positive; their values are the caller’s responsibility. Concrete invalid Python steps still raise.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- same_math(other)#
Tier-1 equality gate: same concrete type and same math context.
Combines a runtime-type identity check with
Context.same_math()(equal backend ops and dtype, ignoringcheck_level— a validation policy, not a mathematical or backend property). This is the mandatory first check in every__eq__: the type gate guaranteesotherexposes the same attributes this object’s__eq__will read, andsame_mathguarantees any laterops.allcloseruns only on same-backend, same-dtype arrays, making cross-backend objects compare unequal.Callers that fail this gate should return
NotImplementedso Python can try the reflected comparison and fall back to identity symmetrically.- Parameters:
other (Any)
- Return type:
bool
- value(x, *args, **kwargs)#
Evaluate this functional at an element of
self.domain.Subclasses may accept extra positional/keyword arguments — auxiliary parameters such as data, temperature, or a penalty weight — that are not part of the domain. Overrides that take only
xremain valid.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- value_and_derivative(x, *args, **kwargs)#
Return value and differential, preserving fused gradient evaluators.
Derivative-first subclasses may implement
_value_and_derivative_coreto share work. No representer is reconstructed from a callable differential.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, ‘LinearFunctional’]
- value_and_grad(x, *args, **kwargs)#
Return
(value, gradient)atx.Implement
_value_and_grad_corefor a single-pass evaluator (e.g.jax.value_and_grad); it must return the same pair as the separate cores, with the gradient in the same geometry asgrad().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, Any]
- vgrad(xs)#
Gradient over a leading batch axis; implement
_vgrad_coreto support it.- 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.MatrixFreeLinearFunctional(*args, **kwargs)[source]#
Bases:
LinearFunctional[Domain]Linear functional given by an evaluation callable; no Riesz representer is stored.
- Parameters:
value (callable) –
value(x)returning a scalar forxindom.dom (Space) – Domain.
ctx (Context, str, or None, optional) – Backend context.
vvalue (callable or None, optional) – Batched evaluation;
vmapofvalueby default.check_level ({"none", "cheap", "standard", "strict"}, optional) – Validation level; the ambient default when omitted. The callable must return real values; no projection is applied.
- 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.
The level is stored on the bound object (
_check_level), seeded at construction. The fallback covers any construction path that assigns_ctxwithout going through__init__/_bind_context.
- property codomain: RealField#
The independently bound scalar field of this functional’s values.
- 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:
- conjugate_prox(x, step)#
Return the conjugate prox by Hilbert-space Moreau decomposition.
The conjugate is
f*_X(p) = sup_z Re <p,z>_X - f(z): dual variables are Riesz representers in the same space, not raw coordinate covectors. Thusprox_(step f*_X)(x) = x - step prox_(f/step)(x/step). Requires a valid convex prox and a finite, strictly positive step.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- convert(new_ctx=None)#
Return this object represented in
new_ctx.The object’s
check_levelis a property of the object, not of the backend/dtype context, so it is preserved across conversion rather than re-seeded from the ambient default.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- derivative_at(x, *args, **kwargs)#
Return the differential at
x, acting on domain directions.The default wraps the Riesz gradient,
Re <grad, h>. Implement_derivative_corefor a native directional evaluator. At nonsmooth points a supplied subgradient is not a Frechet derivative.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
- differential(x, *args, **kwargs)#
Return the covector \(df(x)\) with \(Df(x)[h] = \langle df(x), h\rangle\).
The covector is a coordinate array paired with
domain.pairand involves no metric. The default reads it off the Riesz gradient,domain.riesz(grad(x)); functionals that compute coordinate partials implement_differential_core.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x, *args, **kwargs)#
Riesz gradient at an element of
self.domain.Implement
_grad_core, or_derivative_corewith an available Riesz representer; otherwise raisesNotImplementedError. Any auxiliary*args/**kwargsmirror those accepted byvalue().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- prox(x, step)#
Minimize
f(y) + ||y-x||_X^2 / (2 step)for positivestep.This optional operation uses a closed-form rule registered with
register("prox", FunctionalType, GeometryType, rule). The rule takes(functional, geometry, x, step)and returns a native domain element. An unsupported functional/geometry pair raisesCapabilityError; sums and linear pullbacks do not acquire a prox automatically.Prox rules assert a proper, lower-semicontinuous convex real objective (also on complex spaces, using the real inner product). At check level
"none", traced steps must be finite and positive; their values are the caller’s responsibility. Concrete invalid Python steps still raise.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- same_math(other)#
Tier-1 equality gate: same concrete type and same math context.
Combines a runtime-type identity check with
Context.same_math()(equal backend ops and dtype, ignoringcheck_level— a validation policy, not a mathematical or backend property). This is the mandatory first check in every__eq__: the type gate guaranteesotherexposes the same attributes this object’s__eq__will read, andsame_mathguarantees any laterops.allcloseruns only on same-backend, same-dtype arrays, making cross-backend objects compare unequal.Callers that fail this gate should return
NotImplementedso Python can try the reflected comparison and fall back to identity symmetrically.- Parameters:
other (Any)
- Return type:
bool
- value(x, *args, **kwargs)#
Evaluate this functional at an element of
self.domain.Subclasses may accept extra positional/keyword arguments — auxiliary parameters such as data, temperature, or a penalty weight — that are not part of the domain. Overrides that take only
xremain valid.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- value_and_derivative(x, *args, **kwargs)#
Return value and differential, preserving fused gradient evaluators.
Derivative-first subclasses may implement
_value_and_derivative_coreto share work. No representer is reconstructed from a callable differential.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, ‘LinearFunctional’]
- value_and_grad(x, *args, **kwargs)#
Return
(value, gradient)atx.Implement
_value_and_grad_corefor a single-pass evaluator (e.g.jax.value_and_grad); it must return the same pair as the separate cores, with the gradient in the same geometry asgrad().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, Any]
- vgrad(xs)#
Gradient over a leading batch axis; implement
_vgrad_coreto support it.- Parameters:
xs (Any)
- Return type:
Any
- class spacecore.functional.QuadraticForm(*args, **kwargs)[source]#
Bases:
Functional[Domain]A scalar quadratic objective on
dom.- Parameters:
- hess_apply(x)[source]#
Apply the Hessian action at
x; implement_hess_apply_coreto support it.- 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.
The level is stored on the bound object (
_check_level), seeded at construction. The fallback covers any construction path that assigns_ctxwithout going through__init__/_bind_context.
- property codomain: RealField#
The independently bound scalar field of this functional’s values.
- 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:
- conjugate_prox(x, step)#
Return the conjugate prox by Hilbert-space Moreau decomposition.
The conjugate is
f*_X(p) = sup_z Re <p,z>_X - f(z): dual variables are Riesz representers in the same space, not raw coordinate covectors. Thusprox_(step f*_X)(x) = x - step prox_(f/step)(x/step). Requires a valid convex prox and a finite, strictly positive step.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- convert(new_ctx=None)#
Return this object represented in
new_ctx.The object’s
check_levelis a property of the object, not of the backend/dtype context, so it is preserved across conversion rather than re-seeded from the ambient default.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- derivative_at(x, *args, **kwargs)#
Return the differential at
x, acting on domain directions.The default wraps the Riesz gradient,
Re <grad, h>. Implement_derivative_corefor a native directional evaluator. At nonsmooth points a supplied subgradient is not a Frechet derivative.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
- differential(x, *args, **kwargs)#
Return the covector \(df(x)\) with \(Df(x)[h] = \langle df(x), h\rangle\).
The covector is a coordinate array paired with
domain.pairand involves no metric. The default reads it off the Riesz gradient,domain.riesz(grad(x)); functionals that compute coordinate partials implement_differential_core.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x, *args, **kwargs)#
Riesz gradient at an element of
self.domain.Implement
_grad_core, or_derivative_corewith an available Riesz representer; otherwise raisesNotImplementedError. Any auxiliary*args/**kwargsmirror those accepted byvalue().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- prox(x, step)#
Minimize
f(y) + ||y-x||_X^2 / (2 step)for positivestep.This optional operation uses a closed-form rule registered with
register("prox", FunctionalType, GeometryType, rule). The rule takes(functional, geometry, x, step)and returns a native domain element. An unsupported functional/geometry pair raisesCapabilityError; sums and linear pullbacks do not acquire a prox automatically.Prox rules assert a proper, lower-semicontinuous convex real objective (also on complex spaces, using the real inner product). At check level
"none", traced steps must be finite and positive; their values are the caller’s responsibility. Concrete invalid Python steps still raise.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- same_math(other)#
Tier-1 equality gate: same concrete type and same math context.
Combines a runtime-type identity check with
Context.same_math()(equal backend ops and dtype, ignoringcheck_level— a validation policy, not a mathematical or backend property). This is the mandatory first check in every__eq__: the type gate guaranteesotherexposes the same attributes this object’s__eq__will read, andsame_mathguarantees any laterops.allcloseruns only on same-backend, same-dtype arrays, making cross-backend objects compare unequal.Callers that fail this gate should return
NotImplementedso Python can try the reflected comparison and fall back to identity symmetrically.- Parameters:
other (Any)
- Return type:
bool
- value(x, *args, **kwargs)#
Evaluate this functional at an element of
self.domain.Subclasses may accept extra positional/keyword arguments — auxiliary parameters such as data, temperature, or a penalty weight — that are not part of the domain. Overrides that take only
xremain valid.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- value_and_derivative(x, *args, **kwargs)#
Return value and differential, preserving fused gradient evaluators.
Derivative-first subclasses may implement
_value_and_derivative_coreto share work. No representer is reconstructed from a callable differential.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, ‘LinearFunctional’]
- value_and_grad(x, *args, **kwargs)#
Return
(value, gradient)atx.Implement
_value_and_grad_corefor a single-pass evaluator (e.g.jax.value_and_grad); it must return the same pair as the separate cores, with the gradient in the same geometry asgrad().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, Any]
- vgrad(xs)#
Gradient over a leading batch axis; implement
_vgrad_coreto support it.- 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.LinOpQuadraticForm(*args, **kwargs)[source]#
Bases:
QuadraticForm[Domain]Quadratic form
1/2 <x, Q x> + linear(x) + afor a HermitianQ, sograd = Q x + ....Hermiticity is checked where the operator’s structure allows; matrix-free
Qis trusted. For a non-HermitianQ, pass its Hermitian part0.5 * (Q + Q.H). Expanding the quadratic givesD(1/2 <x,Qx>)[h] = Re<Qx,h>; for a real-valued objective on a complex space the linear term must also be real. Beck, First-Order Methods in Optimization, Thm 3.29, with the real pairing.- Parameters:
Q (LinOp) – Hermitian operator from a space to itself.
linear (LinearFunctional or None, optional) – Linear term on
Q.domain.a (scalar-like, optional) – Constant offset.
ctx (Context, str, or None, optional) – Backend context; resolved from
Qandlinearwhen omitted.check_level ({"none", "cheap", "standard", "strict"}, optional) – Validation level; the ambient default when omitted.
- 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.
The level is stored on the bound object (
_check_level), seeded at construction. The fallback covers any construction path that assigns_ctxwithout going through__init__/_bind_context.
- property codomain: RealField#
The independently bound scalar field of this functional’s values.
- 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:
- conjugate_prox(x, step)#
Return the conjugate prox by Hilbert-space Moreau decomposition.
The conjugate is
f*_X(p) = sup_z Re <p,z>_X - f(z): dual variables are Riesz representers in the same space, not raw coordinate covectors. Thusprox_(step f*_X)(x) = x - step prox_(f/step)(x/step). Requires a valid convex prox and a finite, strictly positive step.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- convert(new_ctx=None)#
Return this object represented in
new_ctx.The object’s
check_levelis a property of the object, not of the backend/dtype context, so it is preserved across conversion rather than re-seeded from the ambient default.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- derivative_at(x, *args, **kwargs)#
Return the differential at
x, acting on domain directions.The default wraps the Riesz gradient,
Re <grad, h>. Implement_derivative_corefor a native directional evaluator. At nonsmooth points a supplied subgradient is not a Frechet derivative.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
- differential(x, *args, **kwargs)#
Return the covector \(df(x)\) with \(Df(x)[h] = \langle df(x), h\rangle\).
The covector is a coordinate array paired with
domain.pairand involves no metric. The default reads it off the Riesz gradient,domain.riesz(grad(x)); functionals that compute coordinate partials implement_differential_core.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x, *args, **kwargs)#
Riesz gradient at an element of
self.domain.Implement
_grad_core, or_derivative_corewith an available Riesz representer; otherwise raisesNotImplementedError. Any auxiliary*args/**kwargsmirror those accepted byvalue().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- hess_apply(x)#
Apply the Hessian action at
x; implement_hess_apply_coreto support it.- Parameters:
x (Any)
- Return type:
Any
- prox(x, step)#
Minimize
f(y) + ||y-x||_X^2 / (2 step)for positivestep.This optional operation uses a closed-form rule registered with
register("prox", FunctionalType, GeometryType, rule). The rule takes(functional, geometry, x, step)and returns a native domain element. An unsupported functional/geometry pair raisesCapabilityError; sums and linear pullbacks do not acquire a prox automatically.Prox rules assert a proper, lower-semicontinuous convex real objective (also on complex spaces, using the real inner product). At check level
"none", traced steps must be finite and positive; their values are the caller’s responsibility. Concrete invalid Python steps still raise.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- same_math(other)#
Tier-1 equality gate: same concrete type and same math context.
Combines a runtime-type identity check with
Context.same_math()(equal backend ops and dtype, ignoringcheck_level— a validation policy, not a mathematical or backend property). This is the mandatory first check in every__eq__: the type gate guaranteesotherexposes the same attributes this object’s__eq__will read, andsame_mathguarantees any laterops.allcloseruns only on same-backend, same-dtype arrays, making cross-backend objects compare unequal.Callers that fail this gate should return
NotImplementedso Python can try the reflected comparison and fall back to identity symmetrically.- Parameters:
other (Any)
- Return type:
bool
- value(x, *args, **kwargs)#
Evaluate this functional at an element of
self.domain.Subclasses may accept extra positional/keyword arguments — auxiliary parameters such as data, temperature, or a penalty weight — that are not part of the domain. Overrides that take only
xremain valid.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- value_and_derivative(x, *args, **kwargs)#
Return value and differential, preserving fused gradient evaluators.
Derivative-first subclasses may implement
_value_and_derivative_coreto share work. No representer is reconstructed from a callable differential.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, ‘LinearFunctional’]
- value_and_grad(x, *args, **kwargs)#
Return
(value, gradient)atx.Implement
_value_and_grad_corefor a single-pass evaluator (e.g.jax.value_and_grad); it must return the same pair as the separate cores, with the gradient in the same geometry asgrad().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, Any]
- vgrad(xs)#
Gradient over a leading batch axis; implement
_vgrad_coreto support it.- 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.ComposedFunctional(*args, **kwargs)[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.check_level ({"none", "cheap", "standard", "strict"}, optional) – Validation level; the ambient default when omitted.
- 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.
The level is stored on the bound object (
_check_level), seeded at construction. The fallback covers any construction path that assigns_ctxwithout going through__init__/_bind_context.
- property codomain: RealField#
The independently bound scalar field of this functional’s values.
- 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:
- conjugate_prox(x, step)#
Return the conjugate prox by Hilbert-space Moreau decomposition.
The conjugate is
f*_X(p) = sup_z Re <p,z>_X - f(z): dual variables are Riesz representers in the same space, not raw coordinate covectors. Thusprox_(step f*_X)(x) = x - step prox_(f/step)(x/step). Requires a valid convex prox and a finite, strictly positive step.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- convert(new_ctx=None)#
Return this object represented in
new_ctx.The object’s
check_levelis a property of the object, not of the backend/dtype context, so it is preserved across conversion rather than re-seeded from the ambient default.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- derivative_at(x, *args, **kwargs)#
Return the differential at
x, acting on domain directions.The default wraps the Riesz gradient,
Re <grad, h>. Implement_derivative_corefor a native directional evaluator. At nonsmooth points a supplied subgradient is not a Frechet derivative.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
- differential(x, *args, **kwargs)#
Return the covector \(df(x)\) with \(Df(x)[h] = \langle df(x), h\rangle\).
The covector is a coordinate array paired with
domain.pairand involves no metric. The default reads it off the Riesz gradient,domain.riesz(grad(x)); functionals that compute coordinate partials implement_differential_core.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x, *args, **kwargs)#
Riesz gradient at an element of
self.domain.Implement
_grad_core, or_derivative_corewith an available Riesz representer; otherwise raisesNotImplementedError. Any auxiliary*args/**kwargsmirror those accepted byvalue().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- prox(x, step)#
Minimize
f(y) + ||y-x||_X^2 / (2 step)for positivestep.This optional operation uses a closed-form rule registered with
register("prox", FunctionalType, GeometryType, rule). The rule takes(functional, geometry, x, step)and returns a native domain element. An unsupported functional/geometry pair raisesCapabilityError; sums and linear pullbacks do not acquire a prox automatically.Prox rules assert a proper, lower-semicontinuous convex real objective (also on complex spaces, using the real inner product). At check level
"none", traced steps must be finite and positive; their values are the caller’s responsibility. Concrete invalid Python steps still raise.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- same_math(other)#
Tier-1 equality gate: same concrete type and same math context.
Combines a runtime-type identity check with
Context.same_math()(equal backend ops and dtype, ignoringcheck_level— a validation policy, not a mathematical or backend property). This is the mandatory first check in every__eq__: the type gate guaranteesotherexposes the same attributes this object’s__eq__will read, andsame_mathguarantees any laterops.allcloseruns only on same-backend, same-dtype arrays, making cross-backend objects compare unequal.Callers that fail this gate should return
NotImplementedso Python can try the reflected comparison and fall back to identity symmetrically.- Parameters:
other (Any)
- Return type:
bool
- value(x, *args, **kwargs)#
Evaluate this functional at an element of
self.domain.Subclasses may accept extra positional/keyword arguments — auxiliary parameters such as data, temperature, or a penalty weight — that are not part of the domain. Overrides that take only
xremain valid.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- value_and_derivative(x, *args, **kwargs)#
Return value and differential, preserving fused gradient evaluators.
Derivative-first subclasses may implement
_value_and_derivative_coreto share work. No representer is reconstructed from a callable differential.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, ‘LinearFunctional’]
- value_and_grad(x, *args, **kwargs)#
Return
(value, gradient)atx.Implement
_value_and_grad_corefor a single-pass evaluator (e.g.jax.value_and_grad); it must return the same pair as the separate cores, with the gradient in the same geometry asgrad().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, Any]
- vgrad(xs)#
Gradient over a leading batch axis; implement
_vgrad_coreto support it.- 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 A, specialized where a closed form exists.- Parameters:
F (Functional) – Functional on
A.codomain.A (LinOp) – Operator into
F.domain.
- Returns:
The pull-back.
- Return type:
- class spacecore.functional.SquaredL2NormFunctional(*args, **kwargs)[source]#
Bases:
_CoordinateFunctional[Domain]Half the squared space norm
<x, x>_X / 2; its gradient isx.Not
LpNormFunctional(X, 2), which is the unsquared coordinate norm.- 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.
The level is stored on the bound object (
_check_level), seeded at construction. The fallback covers any construction path that assigns_ctxwithout going through__init__/_bind_context.
- property codomain: RealField#
The independently bound scalar field of this functional’s values.
- 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:
- conjugate_prox(x, step)#
Return the conjugate prox by Hilbert-space Moreau decomposition.
The conjugate is
f*_X(p) = sup_z Re <p,z>_X - f(z): dual variables are Riesz representers in the same space, not raw coordinate covectors. Thusprox_(step f*_X)(x) = x - step prox_(f/step)(x/step). Requires a valid convex prox and a finite, strictly positive step.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- convert(new_ctx=None)#
Return this object represented in
new_ctx.The object’s
check_levelis a property of the object, not of the backend/dtype context, so it is preserved across conversion rather than re-seeded from the ambient default.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- derivative_at(x, *args, **kwargs)#
Return the differential at
x, acting on domain directions.The default wraps the Riesz gradient,
Re <grad, h>. Implement_derivative_corefor a native directional evaluator. At nonsmooth points a supplied subgradient is not a Frechet derivative.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
- differential(x, *args, **kwargs)#
Return the covector \(df(x)\) with \(Df(x)[h] = \langle df(x), h\rangle\).
The covector is a coordinate array paired with
domain.pairand involves no metric. The default reads it off the Riesz gradient,domain.riesz(grad(x)); functionals that compute coordinate partials implement_differential_core.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x, *args, **kwargs)#
Riesz gradient at an element of
self.domain.Implement
_grad_core, or_derivative_corewith an available Riesz representer; otherwise raisesNotImplementedError. Any auxiliary*args/**kwargsmirror those accepted byvalue().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- prox(x, step)#
Minimize
f(y) + ||y-x||_X^2 / (2 step)for positivestep.This optional operation uses a closed-form rule registered with
register("prox", FunctionalType, GeometryType, rule). The rule takes(functional, geometry, x, step)and returns a native domain element. An unsupported functional/geometry pair raisesCapabilityError; sums and linear pullbacks do not acquire a prox automatically.Prox rules assert a proper, lower-semicontinuous convex real objective (also on complex spaces, using the real inner product). At check level
"none", traced steps must be finite and positive; their values are the caller’s responsibility. Concrete invalid Python steps still raise.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- same_math(other)#
Tier-1 equality gate: same concrete type and same math context.
Combines a runtime-type identity check with
Context.same_math()(equal backend ops and dtype, ignoringcheck_level— a validation policy, not a mathematical or backend property). This is the mandatory first check in every__eq__: the type gate guaranteesotherexposes the same attributes this object’s__eq__will read, andsame_mathguarantees any laterops.allcloseruns only on same-backend, same-dtype arrays, making cross-backend objects compare unequal.Callers that fail this gate should return
NotImplementedso Python can try the reflected comparison and fall back to identity symmetrically.- Parameters:
other (Any)
- Return type:
bool
- value(x, *args, **kwargs)#
Evaluate this functional at an element of
self.domain.Subclasses may accept extra positional/keyword arguments — auxiliary parameters such as data, temperature, or a penalty weight — that are not part of the domain. Overrides that take only
xremain valid.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- value_and_derivative(x, *args, **kwargs)#
Return value and differential, preserving fused gradient evaluators.
Derivative-first subclasses may implement
_value_and_derivative_coreto share work. No representer is reconstructed from a callable differential.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, ‘LinearFunctional’]
- value_and_grad(x, *args, **kwargs)#
Return
(value, gradient)atx.Implement
_value_and_grad_corefor a single-pass evaluator (e.g.jax.value_and_grad); it must return the same pair as the separate cores, with the gradient in the same geometry asgrad().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, Any]
- vgrad(xs)#
Gradient over a leading batch axis; implement
_vgrad_coreto support it.- 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(*args, **kwargs)[source]#
Bases:
_CoordinateFunctional[Domain]Coordinate
p-norm(sum_i |x_i|^p)^(1/p)forp >= 1.The gradient is
sign(x_i) |x_i|^(p-1) / ||x||_p^(p-1), Riesz-corrected; at the origin the zero subgradient is returned (Beck, First-Order Methods in Optimization, Example 3.3). Forp = 1, zero coordinates also use zero subgradient components (Example 3.4); elsewhere differentiate the displayed norm.- Parameters:
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.
The level is stored on the bound object (
_check_level), seeded at construction. The fallback covers any construction path that assigns_ctxwithout going through__init__/_bind_context.
- property codomain: RealField#
The independently bound scalar field of this functional’s values.
- 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:
- conjugate_prox(x, step)#
Return the conjugate prox by Hilbert-space Moreau decomposition.
The conjugate is
f*_X(p) = sup_z Re <p,z>_X - f(z): dual variables are Riesz representers in the same space, not raw coordinate covectors. Thusprox_(step f*_X)(x) = x - step prox_(f/step)(x/step). Requires a valid convex prox and a finite, strictly positive step.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- convert(new_ctx=None)#
Return this object represented in
new_ctx.The object’s
check_levelis a property of the object, not of the backend/dtype context, so it is preserved across conversion rather than re-seeded from the ambient default.- Parameters:
new_ctx (Context | BackendFamily | str | None)
- Return type:
Self
- derivative_at(x, *args, **kwargs)#
Return the differential at
x, acting on domain directions.The default wraps the Riesz gradient,
Re <grad, h>. Implement_derivative_corefor a native directional evaluator. At nonsmooth points a supplied subgradient is not a Frechet derivative.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
- differential(x, *args, **kwargs)#
Return the covector \(df(x)\) with \(Df(x)[h] = \langle df(x), h\rangle\).
The covector is a coordinate array paired with
domain.pairand involves no metric. The default reads it off the Riesz gradient,domain.riesz(grad(x)); functionals that compute coordinate partials implement_differential_core.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- property domain: Domain#
Domain space of this scalar-valued map.
- grad(x, *args, **kwargs)#
Riesz gradient at an element of
self.domain.Implement
_grad_core, or_derivative_corewith an available Riesz representer; otherwise raisesNotImplementedError. Any auxiliary*args/**kwargsmirror those accepted byvalue().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- prox(x, step)#
Minimize
f(y) + ||y-x||_X^2 / (2 step)for positivestep.This optional operation uses a closed-form rule registered with
register("prox", FunctionalType, GeometryType, rule). The rule takes(functional, geometry, x, step)and returns a native domain element. An unsupported functional/geometry pair raisesCapabilityError; sums and linear pullbacks do not acquire a prox automatically.Prox rules assert a proper, lower-semicontinuous convex real objective (also on complex spaces, using the real inner product). At check level
"none", traced steps must be finite and positive; their values are the caller’s responsibility. Concrete invalid Python steps still raise.- Parameters:
x (Any)
step (Any)
- Return type:
Any
- same_math(other)#
Tier-1 equality gate: same concrete type and same math context.
Combines a runtime-type identity check with
Context.same_math()(equal backend ops and dtype, ignoringcheck_level— a validation policy, not a mathematical or backend property). This is the mandatory first check in every__eq__: the type gate guaranteesotherexposes the same attributes this object’s__eq__will read, andsame_mathguarantees any laterops.allcloseruns only on same-backend, same-dtype arrays, making cross-backend objects compare unequal.Callers that fail this gate should return
NotImplementedso Python can try the reflected comparison and fall back to identity symmetrically.- Parameters:
other (Any)
- Return type:
bool
- value(x, *args, **kwargs)#
Evaluate this functional at an element of
self.domain.Subclasses may accept extra positional/keyword arguments — auxiliary parameters such as data, temperature, or a penalty weight — that are not part of the domain. Overrides that take only
xremain valid.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
Any
- value_and_derivative(x, *args, **kwargs)#
Return value and differential, preserving fused gradient evaluators.
Derivative-first subclasses may implement
_value_and_derivative_coreto share work. No representer is reconstructed from a callable differential.- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, ‘LinearFunctional’]
- value_and_grad(x, *args, **kwargs)#
Return
(value, gradient)atx.Implement
_value_and_grad_corefor a single-pass evaluator (e.g.jax.value_and_grad); it must return the same pair as the separate cores, with the gradient in the same geometry asgrad().- Parameters:
x (Any)
args (Any)
kwargs (Any)
- Return type:
tuple[Any, Any]
- vgrad(xs)#
Gradient over a leading batch axis; implement
_vgrad_coreto support it.- 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