Weighted Limits

Limits and colimits weighted by enrichment in V-categories.

weighted_limits

Weighted (co)limits for V-enriched categories.

In a V-enriched category, the correct notion of limit is the weighted limit. Given a V-functor D: J → C (diagram) and a V-functor W: J → V (weight), the weighted limit {W, D} is characterized by:

C(X, {W, D}) ≅ [J, V](W, C(X, D-))

and the weighted colimit W ⊗_J D by:

C(W ⊗_J D, X) ≅ [J, V](W, C(D-, X))

In our finite setting, these reduce to end/coend computations:

{W, D} = ∫_j [W(j), D(j)]     (weighted limit)
W ⊗_J D = ∫^j W(j) ⊗ D(j)    (weighted colimit)

where [-, -] denotes the internal hom in V and ⊗ the tensor.

This module provides:

Weight                  — a V-valued presheaf (weight functor)
Diagram                 — a finite diagram of objects/morphisms
weighted_limit()        — compute {W, D}
weighted_colimit()      — compute W ⊗_J D
representable_weight()  — weight represented by an object
terminal_weight()       — constant weight at the unit

Weight dataclass

Weight(values: Tensor, quantale: Quantale | None = None)

A V-valued presheaf on a finite indexing category.

Represents a weight functor W: J → V where J is a finite set of indices, and W assigns a V-value (scalar in the quantale's lattice) to each index.

Holds a torch.Tensor of values; not a value type.

PARAMETER DESCRIPTION
values

A 1D tensor of shape (|J|,) with values in L (the quantale's lattice). W(j) = values[j].

TYPE: Tensor

quantale

The enrichment algebra. Defaults to PRODUCT_FUZZY.

TYPE: Quantale or None DEFAULT: None

size property

size: int

Number of indices in J.

Diagram

Bases: Model

A finite diagram of morphisms sharing a common domain or codomain.

For weighted limits, this is a collection of morphisms D(j): X → A_j for each index j (cone-shaped). For weighted colimits, this is a collection of morphisms D(j): A_j → X for each index j (cocone-shaped).

In the simplest case (discrete diagram), this is just a tuple of objects with no connecting morphisms.

ATTRIBUTE DESCRIPTION
objects

The objects A_j in the diagram, one per index j.

TYPE: tuple[SetObject, ...]

size property

size: int

Number of objects in the diagram.

weighted_limit

weighted_limit(weight: Weight, diagram: Diagram, quantale: Quantale | None = None) -> Tensor

Compute the weighted limit {W, D} for a discrete diagram.

For a discrete diagram (no connecting morphisms), the weighted limit reduces to:

{W, D} = ∫_j [W(j), D(j)] = ⋀_j [W(j), D(j)]

where [w, x] is the internal hom in the quantale (residuation):

[w, x] = sup{v : w ⊗ v ≤ x}

For the product-fuzzy quantale, [w, x] = min(1, x/w) when w > 0, and the unit when w = 0.

For discrete diagrams with objects A_0, ..., A_{n-1}, this computes a tensor of shape (∏_j |A_j|,) representing the weighted product.

PARAMETER DESCRIPTION
weight

The weight W with values W(j) for each j.

TYPE: Weight

diagram

The diagram of objects.

TYPE: Diagram

quantale

The enrichment algebra. Defaults to PRODUCT_FUZZY.

TYPE: Quantale or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

The weighted limit tensor. For discrete diagrams, shape is the meet of identity tensors scaled by the weights.

Source code in src/quivers/enriched/weighted_limits.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
def weighted_limit(
    weight: Weight,
    diagram: Diagram,
    quantale: Quantale | None = None,
) -> torch.Tensor:
    """Compute the weighted limit {W, D} for a discrete diagram.

    For a discrete diagram (no connecting morphisms), the weighted
    limit reduces to:

        {W, D} = ∫_j [W(j), D(j)] = ⋀_j [W(j), D(j)]

    where [w, x] is the internal hom in the quantale (residuation):

        [w, x] = sup{v : w ⊗ v ≤ x}

    For the product-fuzzy quantale, [w, x] = min(1, x/w) when w > 0,
    and the unit when w = 0.

    For discrete diagrams with objects A_0, ..., A_{n-1}, this
    computes a tensor of shape (∏_j |A_j|,) representing the
    weighted product.

    Parameters
    ----------
    weight : Weight
        The weight W with values W(j) for each j.
    diagram : Diagram
        The diagram of objects.
    quantale : Quantale or None
        The enrichment algebra. Defaults to PRODUCT_FUZZY.

    Returns
    -------
    torch.Tensor
        The weighted limit tensor. For discrete diagrams, shape is
        the meet of identity tensors scaled by the weights.
    """
    q = quantale if quantale is not None else PRODUCT_FUZZY

    if weight.size != diagram.size:
        raise ValueError(f"weight size {weight.size} != diagram size {diagram.size}")

    n = weight.size

    if n == 0:
        return torch.tensor(q.unit)

    # for a discrete diagram, the weighted limit is the meet
    # (product) of the identities scaled by internal homs
    # {W, D}_j gives a "weighted identity" for each component
    components: list[torch.Tensor] = []

    for j in range(n):
        obj = diagram.objects[j]
        w_j = weight.values[j]

        # identity tensor for object j, scaled by the weight
        id_j = q.identity_tensor(obj.shape)
        scaled = _internal_hom_scalar(w_j, id_j, q)
        components.append(scaled)

    # the weighted limit is the meet over all components
    # for independent objects, this is just the collection
    # return as a list of component tensors
    # for the product, stack and meet over the first dim
    if len(components) == 1:
        return components[0]

    # for a discrete diagram, return the component-wise meet
    # the result represents the product weighted by W
    # we return as a dictionary-like structure encoded as a tuple
    # but for simplicity, we return the tensor for the product
    # which is the outer-meet of all scaled identities
    result = components[0]

    for comp in components[1:]:
        # outer product via tensor_op, then reshape
        n_r = result.ndim
        n_c = comp.ndim

        r_exp = result.reshape(*result.shape, *([1] * n_c))
        c_exp = comp.reshape(*([1] * n_r), *comp.shape)

        result = q.tensor_op(r_exp, c_exp)

    return result

weighted_colimit

weighted_colimit(weight: Weight, diagram: Diagram, quantale: Quantale | None = None) -> Tensor

Compute the weighted colimit W ⊗_J D for a discrete diagram.

For a discrete diagram, the weighted colimit reduces to:

W ⊗_J D = ∫^j W(j) ⊗ D(j) = ⋁_j W(j) ⊗ D(j)

This is the join (existential) over j of the tensor product of the weight value with the identity at each object.

PARAMETER DESCRIPTION
weight

The weight W with values W(j) for each j.

TYPE: Weight

diagram

The diagram of objects.

TYPE: Diagram

quantale

The enrichment algebra. Defaults to PRODUCT_FUZZY.

TYPE: Quantale or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

The weighted colimit tensor.

Source code in src/quivers/enriched/weighted_limits.py
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
def weighted_colimit(
    weight: Weight,
    diagram: Diagram,
    quantale: Quantale | None = None,
) -> torch.Tensor:
    """Compute the weighted colimit W ⊗_J D for a discrete diagram.

    For a discrete diagram, the weighted colimit reduces to:

        W ⊗_J D = ∫^j W(j) ⊗ D(j) = ⋁_j W(j) ⊗ D(j)

    This is the join (existential) over j of the tensor product of
    the weight value with the identity at each object.

    Parameters
    ----------
    weight : Weight
        The weight W with values W(j) for each j.
    diagram : Diagram
        The diagram of objects.
    quantale : Quantale or None
        The enrichment algebra. Defaults to PRODUCT_FUZZY.

    Returns
    -------
    torch.Tensor
        The weighted colimit tensor.
    """
    q = quantale if quantale is not None else PRODUCT_FUZZY

    if weight.size != diagram.size:
        raise ValueError(f"weight size {weight.size} != diagram size {diagram.size}")

    n = weight.size

    if n == 0:
        return torch.tensor(q.zero)

    components: list[torch.Tensor] = []

    for j in range(n):
        obj = diagram.objects[j]
        w_j = weight.values[j]

        # identity tensor scaled by weight
        id_j = q.identity_tensor(obj.shape)
        scaled = q.tensor_op(id_j, w_j)
        components.append(scaled)

    if len(components) == 1:
        return components[0]

    # join over all components (outer-join)
    result = components[0]

    for comp in components[1:]:
        n_r = result.ndim
        n_c = comp.ndim

        r_exp = result.reshape(*result.shape, *([1] * n_c))
        c_exp = comp.reshape(*([1] * n_r), *comp.shape)

        # join of tensor products
        result = q.tensor_op(r_exp, c_exp)

    return result

weighted_limit_morphisms

weighted_limit_morphisms(weight: Weight, morphisms: Sequence[Morphism], quantale: Quantale | None = None) -> Tensor

Compute a weighted limit from a family of morphisms.

Given morphisms f_j: X → A_j and weights W(j), computes the weighted meet:

result[x, ...] = ⋀_j [W(j), f_j(x, ...)]

This is the tensor-level computation of the weighted limit of a cone.

PARAMETER DESCRIPTION
weight

The weight W.

TYPE: Weight

morphisms

The morphisms f_j comprising the cone.

TYPE: Sequence[Morphism]

quantale

The enrichment algebra. Defaults to PRODUCT_FUZZY.

TYPE: Quantale or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

The weighted limit tensor.

Source code in src/quivers/enriched/weighted_limits.py
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
def weighted_limit_morphisms(
    weight: Weight,
    morphisms: Sequence[Morphism],
    quantale: Quantale | None = None,
) -> torch.Tensor:
    """Compute a weighted limit from a family of morphisms.

    Given morphisms f_j: X → A_j and weights W(j), computes the
    weighted meet:

        result[x, ...] = ⋀_j [W(j), f_j(x, ...)]

    This is the tensor-level computation of the weighted limit
    of a cone.

    Parameters
    ----------
    weight : Weight
        The weight W.
    morphisms : Sequence[Morphism]
        The morphisms f_j comprising the cone.
    quantale : Quantale or None
        The enrichment algebra. Defaults to PRODUCT_FUZZY.

    Returns
    -------
    torch.Tensor
        The weighted limit tensor.
    """
    q = quantale if quantale is not None else PRODUCT_FUZZY

    if weight.size != len(morphisms):
        raise ValueError(
            f"weight size {weight.size} != number of morphisms {len(morphisms)}"
        )

    n = len(morphisms)

    if n == 0:
        raise ValueError("need at least one morphism")

    # compute [W(j), f_j] for each j and meet over j
    components: list[torch.Tensor] = []

    for j in range(n):
        w_j = weight.values[j]
        f_j = morphisms[j].tensor
        hom = _internal_hom_scalar(w_j, f_j, q)
        components.append(hom)

    # meet over all components (they should all have the same shape)
    stacked = torch.stack(components, dim=0)
    return q.meet(stacked, dim=0)

weighted_colimit_morphisms

weighted_colimit_morphisms(weight: Weight, morphisms: Sequence[Morphism], quantale: Quantale | None = None) -> Tensor

Compute a weighted colimit from a family of morphisms.

Given morphisms f_j: A_j → X and weights W(j), computes the weighted join:

result[..., x] = ⋁_j W(j) ⊗ f_j(..., x)
PARAMETER DESCRIPTION
weight

The weight W.

TYPE: Weight

morphisms

The morphisms f_j comprising the cocone.

TYPE: Sequence[Morphism]

quantale

The enrichment algebra. Defaults to PRODUCT_FUZZY.

TYPE: Quantale or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

The weighted colimit tensor.

Source code in src/quivers/enriched/weighted_limits.py
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
def weighted_colimit_morphisms(
    weight: Weight,
    morphisms: Sequence[Morphism],
    quantale: Quantale | None = None,
) -> torch.Tensor:
    """Compute a weighted colimit from a family of morphisms.

    Given morphisms f_j: A_j → X and weights W(j), computes the
    weighted join:

        result[..., x] = ⋁_j W(j) ⊗ f_j(..., x)

    Parameters
    ----------
    weight : Weight
        The weight W.
    morphisms : Sequence[Morphism]
        The morphisms f_j comprising the cocone.
    quantale : Quantale or None
        The enrichment algebra. Defaults to PRODUCT_FUZZY.

    Returns
    -------
    torch.Tensor
        The weighted colimit tensor.
    """
    q = quantale if quantale is not None else PRODUCT_FUZZY

    if weight.size != len(morphisms):
        raise ValueError(
            f"weight size {weight.size} != number of morphisms {len(morphisms)}"
        )

    n = len(morphisms)

    if n == 0:
        raise ValueError("need at least one morphism")

    components: list[torch.Tensor] = []

    for j in range(n):
        w_j = weight.values[j]
        f_j = morphisms[j].tensor
        scaled = q.tensor_op(f_j, w_j)
        components.append(scaled)

    stacked = torch.stack(components, dim=0)
    return q.join(stacked, dim=0)

representable_weight

representable_weight(index_set: FinSet, represented_at: int, quantale: Quantale | None = None) -> Weight

Create a representable weight (Yoneda-style).

The representable weight at index k is W(j) = I if j == k, and W(j) = ⊥ otherwise. Weighted limits with representable weights recover evaluation: {y(k), D} ≅ D(k).

PARAMETER DESCRIPTION
index_set

The indexing set J.

TYPE: FinSet

represented_at

The representing index k.

TYPE: int

quantale

The enrichment algebra. Defaults to PRODUCT_FUZZY.

TYPE: Quantale or None DEFAULT: None

RETURNS DESCRIPTION
Weight

The representable weight at k.

Source code in src/quivers/enriched/weighted_limits.py
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
def representable_weight(
    index_set: FinSet,
    represented_at: int,
    quantale: Quantale | None = None,
) -> Weight:
    """Create a representable weight (Yoneda-style).

    The representable weight at index k is W(j) = I if j == k,
    and W(j) = ⊥ otherwise. Weighted limits with representable
    weights recover evaluation: {y(k), D} ≅ D(k).

    Parameters
    ----------
    index_set : FinSet
        The indexing set J.
    represented_at : int
        The representing index k.
    quantale : Quantale or None
        The enrichment algebra. Defaults to PRODUCT_FUZZY.

    Returns
    -------
    Weight
        The representable weight at k.
    """
    q = quantale if quantale is not None else PRODUCT_FUZZY

    values = torch.full((index_set.cardinality,), q.zero)
    values[represented_at] = q.unit

    return Weight(values=values, quantale=q)

terminal_weight

terminal_weight(index_set: FinSet, quantale: Quantale | None = None) -> Weight

Create the terminal (constant unit) weight.

W(j) = I for all j. Weighted limits with the terminal weight recover ordinary (conical) limits.

PARAMETER DESCRIPTION
index_set

The indexing set J.

TYPE: FinSet

quantale

The enrichment algebra. Defaults to PRODUCT_FUZZY.

TYPE: Quantale or None DEFAULT: None

RETURNS DESCRIPTION
Weight

The terminal weight.

Source code in src/quivers/enriched/weighted_limits.py
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
def terminal_weight(
    index_set: FinSet,
    quantale: Quantale | None = None,
) -> Weight:
    """Create the terminal (constant unit) weight.

    W(j) = I for all j. Weighted limits with the terminal weight
    recover ordinary (conical) limits.

    Parameters
    ----------
    index_set : FinSet
        The indexing set J.
    quantale : Quantale or None
        The enrichment algebra. Defaults to PRODUCT_FUZZY.

    Returns
    -------
    Weight
        The terminal weight.
    """
    q = quantale if quantale is not None else PRODUCT_FUZZY
    values = torch.full((index_set.cardinality,), q.unit)

    return Weight(values=values, quantale=q)