Skip to content

Make Tensor names better #1950

Open
Open

Description

@jstoobysmith

The names of definitions in the Tensor machinery eg for contraction are not the best. They could be updated to make them easier to read and understand

Activity

  1. jstoobysmith commented on Oct 27, 2026

    @jstoobysmith
    MemberAuthor

    As a start, I think some of the contraction results could be renamed.

    Current New
    TensorSpecies.Tensor.contrT TensorSpecies.Tensor.contr
    TensorSpecies.Tensor.Pure.contrP TensorSpecies.Tensor.Pure.contr
    TensorSpecies.Tensor.Pure.contrPCoeff TensorSpecies.Tensor.Pure.contrCoeff
    TensorSpecies.Tensor.Pure.contrPMultilinear TensorSpecies.Tensor.Pure.contrMultilinear
    TensorSpecies.Tensor.crossToEnd TensorSpecies.Tensor.contrProd
    TensorSpecies.Tensor.crossToSlot TensorSpecies.Tensor.contrAtIndex
    TensorSpecies.Tensor.crossToSlotEquiv TensorSpecies.Tensor.contrAtIndexEquiv
    TensorSpecies.Tensor.crossToSlotInv remove

    @pariandrea What do you think about this?

  2. pariandrea commented on Oct 28, 2026

    @pariandrea
    Contributor

    The new names are certainly better.
    I believe we should try to realign the tensors with Mathlib; it seems there has been some work on it.
    I am going through the current tensor layer against Mathlib. The main difference is that Physlib's tensor indices are Fin n, while Mathlib's PiTensorProduct API works over general index types.
    Was there a specific reason to use Fin n rather than a general finite index type, for example the index notation or computing with concrete colours?

  3. jstoobysmith commented on Oct 28, 2026

    @jstoobysmith
    MemberAuthor

    @pariandrea Yes - it was essentially to make sure index notation worked. Basically so if someone wrote down a specific tensor, e.g. T | a b then it was clear what the indexing type would be. If there is a way around this though, I think this would be great, as I think a lot of the pain of working with tensors comes from having to deal with casting the indexing types.

  4. jstoobysmith commented on Oct 28, 2026

    @jstoobysmith
    MemberAuthor

    Want me to open a PR with the above changes in name?

  5. pariandrea commented on Oct 28, 2026

    @pariandrea
    Contributor

    Yes that would be great!

  6. pariandrea commented on Oct 28, 2026

    @pariandrea
    Contributor

    4. @pariandrea Yes - it was essentially to make sure index notation worked. Basically so if someone wrote down a specific tensor, e.g. T | a b then it was clear what the indexing type would be. If there is a way around this though, I think this would be great, as I think a lot of the pain of working with tensors comes from having to deal with casting the indexing types.

    Yes, absolutely, I'm happy to look into it. In the meantime, #1943, the last PR of the tensor series, lets us raise and lower indices for SUGRA. After that I'll stop building on top of the current layer and go back to the basics.

    On crossToSlotInv, I'd rather remove it as part of that rework than now. It is the ordinary contraction plus a cast that tells Lean the index list it gets back is the original one, so it only exists because of the casting you mention. Removing it today would just move that cast into the lemmas that use it. Once the indexing stops producing these casts, it can go and nothing needs to replace it.

  7. jstoobysmith commented on Oct 29, 2026

    @jstoobysmith
    MemberAuthor

    @pariandrea I wonder if defining a weaker form of equality, equality up-to-reindexing, would useful. I think this might actually be the only type of equality which matters

  8. jstoobysmith commented on Oct 30, 2026

    @jstoobysmith
    MemberAuthor

    I think we should explore here whether we are better of using Vector n to index the tensors compared to maps from Fin n -> C.

  9. pariandrea commented on Oct 30, 2026

    @pariandrea
    Contributor

    @jstoobysmith thanks, both points are close to what I have been looking at.

    The direction I am exploring is to move away from numbered slots altogether and index tensors by general finite types, built directly on Mathlib's PiTensorProduct. My impression is that Vector n would still number the slots, so the casts would come back in a different form.

    The two main ideas are these. The first is that a colour carries its dual: it records its own label and the label of its dual (and of its conjugate, where there is one), so the dual of the dual is the colour itself by definition and needs no cast.

    The second is contraction as linear maps: a tensor with some dual slots and some ordinary slots is read as a Mathlib linear map between tensors, and contracting two such tensors along their shared slots is composing the maps. Associativity of contraction is then Mathlib's associativity of composition, and the construction is built almost entirely from Mathlib's tensor product API rather than our own.

    On equality up to reindexing. I think the equality should say which relabelling it uses, not only that one exists. Otherwise swapping the two slots of a tensor would count as equality, so every tensor would equal its transpose and antisymmetry could not be stated.

    I am testing this locally and the results are encouraging.
    I can push this on a fork of mathlib on my own github for you to check it out.
    I am still not ready for a PR however feedback is always welcome.
    What is the best way to collaborate on some code which is still in test phase before the PR ?

  10. jstoobysmith commented on Oct 30, 2026

    @jstoobysmith
    MemberAuthor

    How would this interact with index notation?

    Best way to collaborate is to make a draft PR.

  11. jstoobysmith commented on Oct 30, 2026

    @jstoobysmith
    MemberAuthor

    One of the advantages of using something like Vector n apparently is that it has a normal form, which can make it easier for Lean to compare two sides.(Not sure if it is much help though)

  12. pariandrea commented on Nov 2, 2026

    @pariandrea
    Contributor

    @jstoobysmith I have opened a draft PR: #2014. It adds a new tensor library in Physlib/Mathematics/Tensors next to the existing one, so the two can be compared.

    The main ideas are three. Tensors are indexed by any type, directly on Mathlib's PiTensorProduct, so a product lives on the sum of the two index types and a contraction of any set of pairs is one operation. Nothing is renumbered along the way, so I did not try Vector n.

    Today the dual of a color comes from τ, and τ (τ c) = c holds only up to a proof, which is where many of the casts come from. In the PR a color is a kind, such as the Lorentz vector index, and a variance, up or down. The dual keeps the kind and flips the variance, so c.dual.dual = c holds by rfl and a statement that dualizes twice needs no cast. The basis labels depend only on the kind, so an index and its dual are labelled by the same μ, as in v^μ and α_μ. Saying that two bases are dual is then just contr (e_μ ⊗ e^ν) = if μ = ν then 1 else 0, with no matching of labels and no dual-basis class.

    The index notation keeps its syntax, {T | μ ν ⊗ U | ν ρ}ᵀ. Each product chain becomes one contraction of all its repeated names at once, rather than one contraction per pair with a renumbering in between, and the free indices come out numbered in the order they are written.

    What we obtain is the core of the existing generic layer in about a quarter of the code. Lines with casts or relabelings go from 563 to 17, lines with Fin arithmetic from 721 to 2, and set_option from 40 to 0. The components of a contraction are one formula for any bases.

    If the direction looks right, I would split it into a stack of smaller PRs. What is not ported yet is listed in the PR.

  13. jstoobysmith commented on Nov 2, 2026

    @jstoobysmith
    MemberAuthor

    @pariandrea What is the type of something like {T | μ ν ⊗ U | ν ρ}ᵀ?

  14. pariandrea commented on Nov 2, 2026

    @pariandrea
    Contributor

    @jstoobysmith The notation is still indexed by Fin n, as before. What changes is the operations underneath which are on general index types. The product is a tensor on Fin 2 ⊕ Fin 2, and one Tensor.contr contracts every repeated name and lands directly on Fin 2 with a literal list of colours.

  15. jstoobysmith commented on Nov 2, 2026

    @jstoobysmith
    MemberAuthor

    Maybe we could add some examples involving the Pauli matrices, to see how the proofs differ in specific cases?

  16. jstoobysmith commented on Nov 2, 2026

    @jstoobysmith
    MemberAuthor

    @pariandrea pinging for the above.

  17. pariandrea commented on Nov 7, 2026

    @pariandrea
    Contributor

    @jstoobysmith
    I've updated #2014 with the Pauli matrices as a test of the new machinery, and I'm quite excited about the result. They're in Bispinors/Pauli/Tensor.lean, next to the existing Relations.lean so you can compare.

    If you like the direction, I'd go on and split it into PRs and port the rest of the library.

  18. jstoobysmith commented on Nov 8, 2026

    @jstoobysmith
    MemberAuthor

    @pariandrea Ok, I think your Pauli matrix results look at lot better then the existing approach. I'm not sure about the notation ↓β though as one might not actually want to assign one color to be 'up' or 'down' (if this is what this means?).

  19. pariandrea commented on Nov 8, 2026

    @pariandrea
    Contributor

    @jstoobysmith Yes, that's what it means. A tensor is defined with each index up or down, and ↓β lowers β with the metric while ↑β raises it. To contract two indices they need opposite variance, so if both are upper you lower one of them with ↓. The only conventions are the index placement in the definition of the Pauli matrices, and which index of the metric is contracted when lowering. Lowering contracts with the first index of the metric, v_ν = v^μ g_μν, and for spinors ψ_β = ψ^α ε_αβ. This is fixed once in toDual and is the same as upstream, where toDualMapAtIndex contracts with slot 0 of the metric. Is this the convention you want?

    In the PR the Pauli matrices are defined once with upper indices, as pauliTensor, and lowered with ↓ where needed. Upstream defines each covariant and contravariant form by name:

    • σ^^^ is toTensor pauliMatrix, here pauliTensor | μ α β
    • σ_^^ is pauliCo, here pauliTensor | ↓μ α β
    • σ^__ is pauliContrDown, here pauliTensor | μ ↓α ↓β
    • σ___ is pauliCoDown, here pauliTensor | ↓μ ↓α ↓β

    I find this ad hoc and hard to scale, since every combination of indices would need its own name.

    The arrows do ask each color to be up or down. If you'd rather avoid that, the alternative I see is a single marker like τ(β) that flips the variance whichever way it points. Which would you prefer?

  20. jstoobysmith commented on Nov 9, 2026

    @jstoobysmith
    MemberAuthor

    I think I prefer τ(β) which is what we currently have.

    In the current set up what would e.g.

    pauliTensor | ↑μ α β
    

    do? Is it equivalent to

    pauliTensor | μ α β
    

    or would it call an error?

  21. pariandrea commented on Nov 9, 2026

    @pariandrea
    Contributor

    @jstoobysmith It is an error, ↑μ needs a visibly lower index. I'd keep both. τ(μ) flips the variance whichever way it points, so it works when a color is not assigned up or down. The arrows say which way the index goes and catch a wrong placement, which reads closer to the physics when the variance is known. Both keep the upstream lowering convention v_ν = v^μ g_μν (Is this the one we want ?). The Pauli matrices stay defined once as pauliTensor, and any other index placement is written with τ or an arrow, so we avoid naming every combination of indices.

  22. jstoobysmith commented on Nov 10, 2026

    @jstoobysmith
    MemberAuthor

    v_ν = g_νμ v^μ would probably be more standard.

  23. pariandrea commented on Nov 10, 2026

    @pariandrea
    Contributor

    Yes I agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help-wantedExtra attention is neededpriority: HighFor labelling high priority issues.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions