# \[ANN\] TensorAlgebra.jl: Taking covariance seriously

**URL:** <https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587>\
**Category:** Package Announcements\
**Tags:** package, announcement, algebra, tensors\
**Created:** [July 23, 2020, 9:26pm UTC](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587 "2020-07-23T21:26:16Z")\
**Posts on this page:** 10\
**Page:** 3

<div class="post-metadata">

**Author:** ![thisrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisrod/32/10642_2.png) [@thisrod](https://discourse.julialang.org/u/thisrod)\
**Post date:** [August 7, 2020, 6:31am UTC](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587/42 "2020-08-07T06:31:35Z")

</div>

> [@juthohaegeman](#):
>
> However, to get a vector with a barred upper index in the first place, you indeed need to e.g. conjugate a regular vector with an unbarred upper index: conj:v→w\mathrm{conj}: v \to w such that w¯ı=conj(vı)w^{\bar{\imath}} = \mathrm{conj}(v^{\imath}) .

Isn’t conj basis dependent? I.e., i·(1\ 0) + 1·(0\ i) = ie₁ + ie₂, but -i·(1\ 0) + 1·(0\ i) = -ie₁ + ie₂.

There’s an obvious answer in C², which has a special basis e\_j with a defined phase. That doesn’t work in a general unitary space, when you only have i|ψ〉 + 1|φ〉, |ψ〉 = |1〉, |φ〉 = i|2〉, and 〈1|2〉 = 0.

I’ve thought for a while that it would be interesting to hear differential geometry and quantum mechanics people talk about tensors.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [August 7, 2020, 6:49am UTC](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587/43 "2020-08-07T06:49:04Z")

</div>

Conjugation of a complex unit (bilinear tensor) is the reversal of the composition of two reflection tensors, a’la Cartan-Dieudonne. Anti-linear maps are expressible with the Grassmann.jl geometric algebra as tensor operators, similar to quaternions but different (odd) rank.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [August 7, 2020, 6:55am UTC](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587/44 "2020-08-07T06:55:09Z")

</div>

> [@thisrod](#):
>
> Isn’t conj basis dependent?

Sure, but the metric itself is also basis dependant.

---

<div class="post-metadata">

**Author:** ![anon67531922](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@anon67531922](https://discourse.julialang.org/u/anon67531922)\
**Post date:** [August 7, 2020, 11:41am UTC](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587/45 "2020-08-07T11:41:02Z")

</div>

Here are some notes, mostly for my future self when I need to refer back to this 🙂

The [tensor Hom adjunction](https://en.wikipedia.org/wiki/Tensor-hom_adjunction) is pretty cool.

Vect\_K is a category whose objects are vector spaces over a field K and whose morphisms are linear maps.

Think of two linear maps between vector spaces: F: U\to V and G: U\to V. These can be combined linearly

H = \alpha F + \beta G

to get a new linear map H: U\to V. Therefore, the set of all linear maps U\to V forms a vector space we denote as [U,V]. This vector space is referred to as an [internal hom](https://ncatlab.org/nlab/show/internal+hom).

Also, the field K itself is a vector space.

Hence, K, U, V and [U,V] are all objects (vector spaces) in Vect\_K.

The tensor Hom adjunction says that for any three vector spaces U, V and W, we have

[V\otimes U, W]\cong[V,[U,W]].

If we let L\_U: Vect\_K\to Vect\_K denote a functor given by

V\mapsto V\otimes U

and R\_U: Vect\_K\to Vect\_K denote a functor given by

W\mapsto [U,W],

the tensor Hom adjunction is a little more suggestive

[L\_U(V),W] \cong [V,R\_U(W)]

and we see that “tensor product with U” and “maps from U” are adjoint (or adjunct).

Given a vector space U, its dual U^\* is defined as the internal hom [U,K], i.e. all linear maps from U to K, which as we saw above is also a vector space.

Now consider the tensor Hom adjunction with V = K and W = K. Then we have

[K\otimes U,K] \cong [K, [U,K]] = [K,U^\*].

However, K\otimes U \cong U so we can rewrite the tensor Hom adjunction simply as

U^\* = [U,K] \cong [K,U^\*].

Replacing U with U^\* and noting that U^{\*\*}\cong U, we have

U \cong [U^\*,K] \cong [K,U].

This says the vector space of maps from U^\* to K is equivalent to the vector space of maps from K to U and both vector spaces are equivalent to U.

This 👆was the final missing piece for me.

So if I summarize,

TensorKit.jl defines a tensor as \tau\in [K,U].

TensorAlgebra.jl defines a tensor as \tau\in [U^\*,K].

Since [K,U] and [U^\*,K] are equivalent, both packages are mathematically correct, but the choice leads to different implementations.

One advantage of defining tensors as maps U^\*\to K is that it seems to be easier to support partial evaluation / currying, which is an important operation for tensor algebra.

---

<div class="post-metadata">

**Author:** ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)\
**Post date:** [February 11, 2021, 9:54am UTC](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587/46 "2021-02-11T09:54:07Z")

</div>

I returned here after reading a particular discussion on Zulip on `isa(M[1,:],Vector)` where you link to this post. While it is true that TensorKit.jl defines a `Tensor` as an element from [K,U], that’s actually just a specific type constant corresponding to a more general type `TensorMap` which describes general elements from [U,V] .So that type can certainly also describe maps [U^\*,K] .

However, TensorKit.jl can also describe morphisms in other categories than Vect, which have similar properties, (but where currying does not necessarily makes sense).

---

<div class="post-metadata">

**Author:** ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)\
**Post date:** [February 12, 2021, 8:18am UTC](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587/47 "2021-02-12T08:18:37Z")

</div>

Actually, currying probably makes sense, I don’t use this word very often so I was a bit confused by its meaning. Rather, the mean reason to consider general morphisms in [U,V] is when you want to decompose a tensor using some linear algebra factorization. In that case, you want to have a clear domain and codomain, in order to apply standard matrix factorization algorithms.

---

<div class="post-metadata">

**Author:** ![anon67531922](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@anon67531922](https://discourse.julialang.org/u/anon67531922)\
**Post date:** [February 12, 2021, 9:03am UTC](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587/48 "2021-02-12T09:03:00Z")

</div>

Hi @juthohaegeman ,

Please don’t take my slowness to respond as lack of interest. I’m in the middle of something now, but I would much rather be discussing this 🙂

My memory is notoriously bad so I’ll need to review our discussion before asking questions (and potentially repeating myself). I’d love to learn more about the applications you have in mind. Just FYI, TensorAlgebra.jl is just a POC. I sat down and implemented tensors in a way that felt natural to me. Hopefully there can be some shared ideas 👍

---

<div class="post-metadata">

**Author:** ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)\
**Post date:** [March 12, 2021, 4:50pm UTC](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587/49 "2021-03-12T16:50:54Z")

</div>

How do you generally handle change of basis (or any specification of basis) in tensor spaces? We have a vector bundle/basis/charts design mostly ready in Manifolds.jl and I wonder if it could be somehow integrated with some tensor library. One point that turned out to be important is that, with the exception of some operations on tangent and cotangent space, we can only operate on coefficients of tensors in some basis.

---

<div class="post-metadata">

**Author:** ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)\
**Post date:** [March 20, 2021, 9:27pm UTC](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587/50 "2021-03-20T21:27:01Z")

</div>

Any recommend source to read about what you’ve been working on @mateuszbaran ?

---

<div class="post-metadata">

**Author:** ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)\
**Post date:** [March 22, 2021, 11:52am UTC](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587/51 "2021-03-22T11:52:06Z")

</div>

This is just motivated by some changes to Manifolds.jl I’m working on: [Atlases and charts by mateuszbaran · Pull Request #325 · JuliaManifolds/Manifolds.jl · GitHub](https://github.com/JuliaManifolds/Manifolds.jl/pull/325) , which in turn is motivated by me being uncomfortable about the way metrics are handled in Manifolds.jl. This is just basic differential geometry stuff, nothing fancy.

[Previous page](https://discourse.julialang.org/t/ann-tensoralgebra-jl-taking-covariance-seriously/43587.md?page=2)
