Skip to content

Commit 4f1be3e

Browse files
committed
new vignette: tensorprod.Rmd
1 parent 6f53b23 commit 4f1be3e

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: stokes
22
Type: Package
33
Title: The Exterior Calculus
4-
Version: 1.2-1
4+
Version: 1.2-2
55
Depends: R (>= 3.5.0)
66
Suggests:
77
knitr,

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# stokes 1.2-2
2+
3+
- new vignette on tensor products
4+
5+
6+
17
# stokes 1.2-1
28

39
- edge-cases for `vector_cross_product()`, now works with a single 2D vector

vignettes/tensorprod.Rmd

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
title: "Function `tensorprod()` function in the `stokes` package"
3+
author: "Robin K. S. Hankin"
4+
output: html_vignette
5+
bibliography: stokes.bib
6+
link-citations: true
7+
vignette: >
8+
%\VignetteEngine{knitr::rmarkdown}
9+
%\VignetteIndexEntry{spraycross}
10+
%\usepackage[utf8]{inputenc}
11+
---
12+
13+
```{r setup, include=FALSE}
14+
set.seed(0)
15+
library("spray")
16+
library("stokes")
17+
options(rmarkdown.html_vignette.check_title = FALSE)
18+
knitr::opts_chunk$set(echo = TRUE)
19+
knit_print.function <- function(x, ...){dput(x)}
20+
registerS3method(
21+
"knit_print", "function", knit_print.function,
22+
envir = asNamespace("knitr")
23+
)
24+
```
25+
26+
```{r out.width='20%', out.extra='style="float:right; padding:10px"',echo=FALSE}
27+
knitr::include_graphics(system.file("help/figures/spray.png", package = "spray"))
28+
```
29+
30+
```{r, label=showAlt,comment=""}
31+
tensorprod
32+
tensorprod2
33+
```
34+
35+
To cite the `stokes` package in publications, please use
36+
@hankin2022_stokes. Function `tensorprod()` returns the tensor cross
37+
product of any number of `ktensor` objects; `tensorprod2()` is a
38+
lower-level helper function that returns the product of two such
39+
objects. These functions use `spraycross()` from the `spray` package
40+
[@hankin2022_spray].
41+
42+
### The tensor cross product
43+
44+
In a memorable passage, @spivak1965 states:
45+
46+
<div class="warning" style='padding:0.1em; background-color:#E9D8FD; color:#69337A'>
47+
<span>
48+
<p style='margin-top:1em; text-align:center'>
49+
<b>Integration on chains</b></p>
50+
<p style='margin-left:1em;'>
51+
52+
If $V$ is a vector space over $\mathbb{R}$, we denote the $k$-fold
53+
product $V\times\cdots\times V$ by $V^k$. A function $T\colon
54+
V^k\longrightarrow\mathbb{R}$ is called *multilinear* if for each $i$
55+
with $1\leqslant i\leqslant k$ we have
56+
57+
$$
58+
T\left(v_1,\ldots, v_i + {v'}_i,\ldots, v_k\right)=
59+
T\left(v_1,\ldots,v_i,\ldots,v_k\right)+
60+
T\left(v_1,\ldots,{v'}_i,\ldots,v_k\right),\\
61+
T\left(v_1,\ldots,av_i,\ldots,v_k\right)=aT\left(v_1,\ldots,v_i,\ldots,v_k\right)
62+
$$
63+
64+
A multilinear function $T\colon v^k\longrightarrow\mathbb{R}$ is
65+
called a *$k$-tensor* on $V$ and the set of all $k$-tensors, denoted
66+
by $\mathcal{J}^k(V)$, becomes a vector space (over $\mathbb{R}$) if
67+
for $S,T\in\mathcal{J}^k(V)$ and $a\in\mathbb{R}$ we define
68+
69+
$$
70+
(S+T)(v_1,\ldots,v_k) = S(v_1,\ldots,v_k) + T(v_1,\ldots,v_k)
71+
(aS)(v_1,\ldots,v_k) = a\cdot S(v_1,\ldots,v_k)
72+
$$
73+
74+
There is also an operation connecting the various spaces
75+
$\mathcal{J}(V)$. If $S\in\mathcal{J}^k(V)$ and
76+
$T\in\mathcal{J}^l(V)$, we define the *tensor product* $S\otimes
77+
T\in\mathcal{J}^{k+l}(V)$ by
78+
79+
$$
80+
S\otimes T(v_1,\ldots,v_k,v_{k+1},\ldots,v_{k+l})=
81+
S(v_1,\ldots,v_k)\cdot T(v_{k+1},\ldots,v_{k+l}).
82+
$$
83+
84+
85+
</p>
86+
<p style='margin-bottom:1em; margin-right:1em; text-align:right; font-family:Georgia'> <b>- Michael Spivak, 1969</b> <i>(Calculus on Manifolds, Perseus books). Page 75</i>
87+
</p></span>
88+
</div>
89+
90+
Spivak goes on to observe that the tensor product is distributive and
91+
associative but not commutative. He then proves that the set of all
92+
$k$-fold tensor products
93+
94+
$$
95+
\phi_{i_1}\otimes\cdots\otimes\phi_{i_k},\qquad 1\leqslant
96+
i_1,\ldots,i_k\leqslant n
97+
$$
98+
99+
[where $\phi_i(v_j)=\delta_{ij}$,$v_1,\ldots,v_k$ being a basis for
100+
$V$] is a basis for $\mathcal{J}^k(V)$, which therefore has dimension
101+
$n^k$. Function `spraycross2()` evaluates the tensor product and I
102+
give examples here.
103+
104+
```{r}
105+
(a <- ktensor(spray(matrix(c(1,1,2,1),2,2),3:4)))
106+
(b <- ktensor(spray(matrix(c(3,4,7,5,4,3),3,2),7:9)))
107+
```
108+
109+
Thus $a=4\phi_1\otimes\phi_1+3\phi_1\otimes\phi_2$ and
110+
$b=7\phi_3\otimes\phi_5+8\phi_4\otimes\phi_4+9\phi_7\otimes\phi_3$.
111+
Now the cross product $a\otimes b$ is given by `spraycross()`:
112+
113+
```{r}
114+
tensorprod(a,b)
115+
```
116+
117+
We can see that the product includes the term
118+
$21\phi_1\otimes\phi_2\otimes\phi_3\otimes\phi_5$ and five others.
119+
120+
## Verification
121+
122+
Spivak proves that the tensor product is associative and distributive,
123+
which are demonstrated here.
124+
125+
```{r}
126+
S <- rtensor()
127+
T <- rtensor()
128+
U <- rtensor()
129+
c( left_distributive = S %X% (T+U) == S*T + S*U,
130+
right_distributive = (S+T) %X% U == S %X% U + T %X% U,
131+
associative = S %X% (T %X% U) == (S %X% T) %X% U
132+
)
133+
```
134+
135+
### Note on associativity
136+
137+
It is interesting to note that, while the tensor product is
138+
associative, disord discipline obscures this fact. Consider the
139+
following:
140+
141+
142+
```{r}
143+
x <- ktensor(spray(matrix(c(1,1,2,1),2,2),1:2))
144+
y <- ktensor(spray(matrix(c(3,4,7,5,4,3),3,2),1:3))
145+
z <- ktensor(spray(matrix(c(1,1,2,1),2,2),1:2))
146+
tensorprod(x, tensorprod(y, z))
147+
tensorprod(tensorprod(x, y), z)
148+
```
149+
150+
The two products are algebraically identical but the terms appear in a
151+
different order.
152+
153+
```{r echo=FALSE}
154+
rm(T) # tidyup
155+
```
156+
157+
# References

0 commit comments

Comments
 (0)