tt_zipper_tangent_left_to_right#
- t3toolbox.backend.tt_operations.tt_zipper_tangent_left_to_right(coresA, coresB, coresA_dot, coresB_dot, zippers=None)#
def tt_zipper_tangent_left_to_right( coresA: typ.Union[typ.Sequence[NDArray], NDArray], # len=d, elm_shape=C+(rAi, ni, rA(i+1)) coresB: typ.Union[typ.Sequence[NDArray], NDArray], # len=d, elm_shape=C+(rBi, ni, rB(i+1)) coresA_dot: typ.Union[typ.Sequence[NDArray], NDArray], # len=d, elm_shape=K+C+(rAi, ni, rA(i+1)) coresB_dot: typ.Union[typ.Sequence[NDArray], NDArray], # len=d, elm_shape=K+C+(rBi, ni, rB(i+1)) zippers: typ.Optional[typ.Sequence[NDArray]] = None, # len=d+1, elm_shape=C+(rAi, rBi); None -> computed ) -> typ.Tuple[NDArray, ...]: # zipper tangents, len=d+1, elm_shape=K+C+(rAi, rBi); the first is 0
The directional derivative of
tt_zipper_left_to_right()along both chains.With
Z = tt_zipper_left_to_right(A, B)(Z_0the boundary ones,Z_(i+1) = Z_i A_i B_i), this returnsZdot_i = d/ds Z_i(A + s Adot, B + s Bdot)ats = 0:Zdot_0 = 0and, by the product rule,Zdot_(i+1) = Zdot_i A_i B_i + Z_i Adot_i B_i + Z_i A_i Bdot_i– one scan, three grouped terms per core. It is the “exactly one core replaced” companion of a zipper that a two-hole (second directional derivative) sweep carries beside the one-hole environment.Batching: the chains and the zippers carry
C; the two direction chains carry the sameK + C(a stack of directions at one point), which the tangents inherit.zippersmay be passed when the caller already holds them (a precomputed sweep);Nonecomputes them.Polymorphic over the representation, like its parent: a ragged core tuple or a uniform TT supercore (
(d,)+stack+(r, n, r)). Named like its parent too – the chain helpers keep descriptive names (a cataloged exception,docs/naming_conventions.md).Examples
The tangent agrees with a central difference of the zipper (to the difference’s
O(h^2)):>>> import numpy as np >>> import t3toolbox.backend.tt_operations as tto >>> np.random.seed(0) >>> shapes_A, shapes_B = [(1, 3, 2), (2, 4, 3), (3, 2, 1)], [(1, 3, 3), (3, 4, 2), (2, 2, 1)] >>> A = tuple(np.random.randn(*s) for s in shapes_A) >>> B = tuple(np.random.randn(*s) for s in shapes_B) >>> Ad = tuple(np.random.randn(5, *s) for s in shapes_A) # K = (5,) directions >>> Bd = tuple(np.random.randn(5, *s) for s in shapes_B) >>> Zdot = tto.tt_zipper_tangent_left_to_right(A, B, Ad, Bd) >>> print([z.shape for z in Zdot]) [(5, 1, 1), (5, 2, 3), (5, 3, 2), (5, 1, 1)] >>> h = 1e-3 >>> zipper_at = lambda t: tto.tt_zipper_left_to_right([a + t * ad[0] for a, ad in zip(A, Ad)], ... [b + t * bd[0] for b, bd in zip(B, Bd)]) >>> print(all(np.allclose(zd[0], (p - m) / (2 * h)) ... for zd, p, m in zip(Zdot, zipper_at(h), zipper_at(-h)))) True
- Parameters:
coresA (t3toolbox.backend.common.typ.Union[t3toolbox.backend.common.typ.Sequence[NDArray], NDArray])
coresB (t3toolbox.backend.common.typ.Union[t3toolbox.backend.common.typ.Sequence[NDArray], NDArray])
coresA_dot (t3toolbox.backend.common.typ.Union[t3toolbox.backend.common.typ.Sequence[NDArray], NDArray])
coresB_dot (t3toolbox.backend.common.typ.Union[t3toolbox.backend.common.typ.Sequence[NDArray], NDArray])
zippers (t3toolbox.backend.common.typ.Optional[t3toolbox.backend.common.typ.Sequence[NDArray]])
- Return type:
t3toolbox.backend.common.typ.Tuple[NDArray, …]