
% Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
%     - Redistributions of source code must retain the above copyright
%       notice, this list of conditions and the following disclaimer.
%
%     - Redistributions in binary form must reproduce the above copyright
%       notice, this list of conditions and the following disclaimer in
%       the documentation and/or other materials provided with the
%       distribution.
%
%     - Neither the name of The Numerical ALgorithms Group Ltd. nor the
%       names of its contributors may be used to endorse or promote products
%       derived from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
% OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES-- LOSS OF USE, DATA, OR
% PROFITS-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



% carten.htex
\spadtype{CartesianTensor(i0,dim,R)}
provides Cartesian tensors with
components belonging to a commutative ring \spad{R}.
Tensors can be described as a generalization of vectors and matrices.
\index{tensor!Cartesian}
This gives a concise {\it tensor algebra} for multilinear objects
supported by the \spadtype{CartesianTensor} domain.
You can form the inner or outer product of any two tensors and you can add
or subtract tensors with the same number of components.
Additionally, various forms of traces and transpositions are useful.

The \spadtype{CartesianTensor} constructor allows you to specify the
minimum index for subscripting.
In what follows we discuss in detail how to manipulate tensors.

\xtc{
Here we construct the domain of Cartesian tensors of dimension 2 over the
integers, with indices starting at 1.
}{
\spadcommand{CT := CARTEN(i0 := 1, 2, Integer) \bound{CT i0}}
}

% ----------------------------------------------------------------------
\subsubsection{Forming tensors}
% ----------------------------------------------------------------------

\xtc{
Scalars can be converted to tensors of rank zero.
}{
\spadcommand{t0: CT := 8 \free{CT}\bound{t0}}
}
\xtc{
}{
\spadcommand{rank t0 \free{t0}}
}
\xtc{
Vectors (mathematical direct products, rather than one dimensional array
structures) can be converted to tensors of rank one.
}{
\spadcommand{v: DirectProduct(2, Integer) := directProduct [3,4] \bound{v}}
}
\xtc{
}{
\spadcommand{Tv: CT := v \free{v CT}\bound{Tv}}
}
\xtc{
Matrices can be converted to tensors of rank two.
}{
\spadcommand{m: SquareMatrix(2, Integer) := matrix [[1,2],[4,5]] \bound{m}}
}
\xtc{
}{
\spadcommand{Tm: CT := m \bound{Tm}\free{CT m}}
}
\xtc{
}{
\spadcommand{n: SquareMatrix(2, Integer) := matrix [[2,3],[0,1]] \bound{n}}
}
\xtc{
}{
\spadcommand{Tn: CT := n \bound{Tn}\free{CT n}}
}
\xtc{
In general, a tensor of rank \spad{k} can be formed by making a
list of rank \spad{k-1} tensors or, alternatively, a \spad{k}-deep nested
list of lists.
}{
\spadcommand{t1: CT := [2, 3] \free{CT}\bound{t1}}
}
\xtc{
}{
\spadcommand{rank t1 \free{t1}}
}
\xtc{
}{
\spadcommand{t2: CT := [t1, t1] \free{CT t1}\bound{t2}}
}
\xtc{
}{
\spadcommand{t3: CT := [t2, t2] \free{CT t2}\bound{t3}}
}
\xtc{
}{
\spadcommand{tt: CT := [t3, t3]; tt := [tt, tt] \free{CT t3}\bound{tt}}
}
\xtc{
}{
\spadcommand{rank tt \free{tt}}
}

% ----------------------------------------------------------------------
\subsubsection{Multiplication}
%\head{subsection}{Multiplication}{ugxCartenMult}
% ----------------------------------------------------------------------

Given two tensors of rank \spad{k1} and \spad{k2}, the outer
\index{tensor!outer product}
\spadfunFrom{product}{CartesianTensor} forms a new tensor of rank
\spad{k1+k2}.
\xtc{
Here
\texht{$T_{mn}(i,j,k,l) = T_m(i,j) \  T_n(k,l).$}{\spad{Tmn(i,j,k,l) = Tm(i,j)*Tn(k,l).}}
}{
\spadcommand{Tmn := product(Tm, Tn)\free{Tm Tn}\bound{Tmn}}
}

The inner product (\spadfunFrom{contract}{CartesianTensor}) forms a tensor
\index{tensor!inner product}
of rank \spad{k1+k2-2}.
This product generalizes the vector dot product and matrix-vector product
by summing component products along two indices.
\xtc{
Here we sum along the second index of \texht{$T_m$}{\spad{Tm}} and the
first index of \texht{$T_v$}{\spad{Tv}}.
Here
\texht{$T_{mv} = \sum_{j=1}^{\hbox{\tiny\rm dim}} T_m(i,j) \  T_v(j)$}{\spad{Tmv(i) = sum Tm(i,j) * Tv(j) for j in 1..dim.}}
}{
\spadcommand{Tmv := contract(Tm,2,Tv,1)\free{Tm Tv}\bound{Tmv}}
}

The multiplication operator \spadopFrom{*}{CartesianTensor} is scalar
multiplication or an inner product depending on the ranks of the
arguments.
\xtc{
If either argument is rank zero it is treated as scalar multiplication.
Otherwise, \spad{a*b} is the inner product summing the last index of
\spad{a} with the first index of \spad{b}.
}{
\spadcommand{Tm*Tv \free{Tm Tv}}
}
\xtc{
This definition is consistent with the inner product on matrices
and vectors.
}{
\spadcommand{Tmv = m * v \free{Tmv m v}}
}

% ----------------------------------------------------------------------
\subsubsection{Selecting Components}
%\head{subsection}{Selecting Components}{ugxCartenSelect}
% ----------------------------------------------------------------------

\xtc{
For tensors of low rank (that is, four or less), components can be selected
by applying the tensor to its indices.
}{
\spadcommand{t0()      \free{t0}}
}
\xtc{
}{
\spadcommand{t1(1+1)   \free{t1}}
}
\xtc{
}{
\spadcommand{t2(2,1)   \free{t2}}
}
\xtc{
}{
\spadcommand{t3(2,1,2) \free{t3}}
}
\xtc{
}{
\spadcommand{Tmn(2,1,2,1) \free{Tmn}}
}
\xtc{
A general indexing mechanism is provided for a list of indices.
}{
\spadcommand{t0[]        \free{t0}}
}
\xtc{
}{
\spadcommand{t1[2]        \free{t1}}
}
\xtc{
}{
\spadcommand{t2[2,1]      \free{t2}}
}
\xtc{
The general mechanism works for tensors of arbitrary rank, but is
somewhat less efficient since the intermediate index list must be created.
}{
\spadcommand{t3[2,1,2]    \free{t3}}
}
\xtc{
}{
\spadcommand{Tmn[2,1,2,1] \free{Tmn}}
}

% ----------------------------------------------------------------------
\subsubsection{Contraction}
%\head{subsection}{Contraction}{ugxCartenContract}
% ----------------------------------------------------------------------

A ``contraction'' between two tensors is an inner product, as we have
\index{tensor!contraction}
seen above.
You can also contract a pair of indices of a single tensor.
This corresponds to a ``trace'' in linear algebra.
The expression \spad{contract(t,k1,k2)} forms a new tensor by summing the
diagonal given by indices in position \spad{k1} and \spad{k2}.
\xtc{
This is the tensor given by
\texht{$xT_{mn} = \sum_{k=1}^{\hbox{\tiny\rm dim}} T_{mn}(k,k,i,j).$}{\spad{cTmn(i,j) = sum Tmn(k,k,i,j) for k in 1..dim.}}
}{
\spadcommand{cTmn := contract(Tmn,1,2) \free{Tmn}\bound{cTmn}}
}
\xtc{
Since \spad{Tmn} is the outer product of matrix \spad{m} and matrix \spad{n},
the above is equivalent to this.
}{
\spadcommand{trace(m) * n \free{m n}}
}
\xtc{
In this and the next few examples,
we show all possible contractions of \spad{Tmn} and their matrix
algebra equivalents.
}{
\spadcommand{contract(Tmn,1,2) = trace(m) * n      \free{Tmn m n}}
}
\xtc{
}{
\spadcommand{contract(Tmn,1,3) = transpose(m) * n  \free{Tmn m n}}
}
\xtc{
}{
\spadcommand{contract(Tmn,1,4) = transpose(m) * transpose(n) \free{Tmn m n}}
}
\xtc{
}{
\spadcommand{contract(Tmn,2,3) = m * n             \free{Tmn m n}}
}
\xtc{
}{
\spadcommand{contract(Tmn,2,4) = m * transpose(n)  \free{Tmn m n}}
}
\xtc{
}{
\spadcommand{contract(Tmn,3,4) = trace(n) * m      \free{Tmn m n}}
}

% ----------------------------------------------------------------------
\subsubsection{Transpositions}
%\head{subsection}{Transpositions}{ugxCartenTranspos}
% ----------------------------------------------------------------------

You can exchange any desired pair of indices using the
\spadfunFrom{transpose}{CartesianTensor} operation.

\xtc{
Here the indices in positions one and three are exchanged, that is,
\texht{$tT_{mn}(i,j,k,l) = T_{mn}(k,j,i,l).$}{\spad{tTmn(i,j,k,l) = Tmn(k,j,i,l).}}
}{
\spadcommand{tTmn := transpose(Tmn,1,3) \free{tTmn}}
}
\xtc{
If no indices are specified, the first and last index are exchanged.
}{
\spadcommand{transpose Tmn \free{Tmn}}
}
\xtc{
This is consistent with the matrix transpose.
}{
\spadcommand{transpose Tm = transpose m \free{Tm m}}
}

If a more complicated reordering of the indices is required, then the
\spadfunFrom{reindex}{CartesianTensor} operation can be used.
This operation allows the indices to be arbitrarily permuted.
\xtc{
This defines
\texht{$rT_{mn}(i,j,k,l) = \allowbreak T_{mn}(i,l,j,k).$}{\spad{rTmn(i,j,k,l) = Tmn(i,l,j,k).}}
}{
\spadcommand{rTmn := reindex(Tmn, [1,4,2,3]) \free{Tmn}\bound{rTmn}}
}

% ----------------------------------------------------------------------
\subsubsection{Arithmetic}
%\head{subsection}{Arithmetic}{ugxCartenArith}
% ----------------------------------------------------------------------

\xtc{
Tensors of equal rank can be added or subtracted so arithmetic
expressions can be used to produce new tensors.
}{
\spadcommand{tt := transpose(Tm)*Tn - Tn*transpose(Tm) \free{Tm Tn}\bound{tt2}}
}
\xtc{
}{
\spadcommand{Tv*(tt+Tn) \free{tt2 Tv Tn}}
}
\xtc{
}{
\spadcommand{reindex(product(Tn,Tn),[4,3,2,1])+3*Tn*product(Tm,Tm) \free{Tm Tn}}
}

% ----------------------------------------------------------------------
\subsubsection{Specific Tensors}
%\head{subsection}{Specific Tensors}{ugxCartenSpecific}
% ----------------------------------------------------------------------

Two specific tensors have properties which depend only on the
dimension.

\xtc{
The Kronecker delta satisfies
\index{Kronecker delta}
\begin{texonly}
$$
\hbox{delta}(i,j) = \left\{
\begin{array}{ll}
1 & \hbox{if } i = j \\
0 & \hbox{if } i \neq j \\
\end{array}
\right.
$$
\end{texonly}
\begin{htonly}
\begin{verbatim}
             +-
             |   1  if i  = j
delta(i,j) = |
             |   0  if i ^= j
             +-
\end{verbatim}
\end{htonly}
}{
\spadcommand{delta:  CT := kroneckerDelta()   \free{CT}\bound{delta}}
}
\xtc{
This can be used to reindex via contraction.
}{
\spadcommand{contract(Tmn, 2, delta, 1) = reindex(Tmn, [1,3,4,2]) \free{Tmn delta}}
}

\xtc{
The Levi Civita symbol determines the sign of a permutation of indices.
}{
\spadcommand{epsilon:CT := leviCivitaSymbol() \free{CT}\bound{epsilon}}
}
Here we have:
\begin{texonly}
\begin{displaymath}
\hbox{epsilon}(i_1,\ldots,i_{\hbox{\tiny\rm dim}}) =
\left\{
\begin{array}{rl}
+1& \hbox{if }  i_1,\ldots,i_{\hbox{\tiny\rm dim}} \
\hbox{ is an even permutation of } \\ &
 i_0,\ldots,i_0 + \hbox{dim}-1 \\
-1& \hbox{if }  i_1,\ldots,i_{\hbox{\tiny\rm dim}} \
\hbox{ is an  odd permutation of } \\ &
i_0,\ldots,i_0 + \hbox{dim}-1 \\
 0& \hbox{if }  i_1,\ldots,i_{\hbox{\tiny\rm dim}}  \
\hbox{ is {\it not}   a permutation of } \\ &
 i_0,\ldots,i_0 + \hbox{dim}-1
\end{array}
\right.
\end{displaymath}
\end{texonly}%
\begin{htonly}
\begin{verbatim}
epsilon(i1,...,idim)
     = +1  if i1,...,idim is an even permutation of i0,...,i0+dim-1
     = -1  if i1,...,idim is an  odd permutation of i0,...,i0+dim-1
     =  0  if i1,...,idim is not   a permutation of i0,...,i0+dim-1
\end{verbatim}
\end{htonly}
\xtc{
This property can be used to form determinants.
}{
\spadcommand{contract(epsilon*Tm*epsilon, 1,2) = 2 * determinant m \free{epsilon Tm m}}
}

% ----------------------------------------------------------------------
\subsubsection{Properties of the CartesianTensor domain}
%\head{subsection}{Properties of the CartesianTensor domain}{ugxCartenProp}
% ----------------------------------------------------------------------

\spadtype{GradedModule(R,E)} denotes ``\spad{E}-graded \spad{R}-module'',
that is, a collection of \spad{R}-modules indexed by an abelian monoid
\spad{E.}
\exptypeindex{GradedModule}
An element \spad{g} of \spad{G[s]} for some specific \spad{s}
in \spad{E} is said to be an element of \spad{G} with
\spadfunFrom{degree}{GradedModule} \spad{s}.
Sums are defined in each module \spad{G[s]} so two elements of
\spad{G} can be added if they have the same degree.
Morphisms can be defined and composed by degree to give the
mathematical category of graded modules.

\spadtype{GradedAlgebra(R,E)} denotes ``\spad{E}-graded \spad{R}-algebra.''
\exptypeindex{GradedAlgebra}
A graded algebra is a graded module together with a degree preserving
\spad{R}-bilinear map, called the \spadfunFrom{product}{GradedAlgebra}.

\begin{verbatim}
degree(product(a,b))= degree(a) + degree(b)

product(r*a,b) = product(a,r*b) = r*product(a,b)
product(a1+a2,b) = product(a1,b) + product(a2,b)
product(a,b1+b2) = product(a,b1) + product(a,b2)
product(a,product(b,c)) = product(product(a,b),c)
\end{verbatim}

The domain \spadtype{CartesianTensor(i0, dim, R)} belongs
to the category \spadtype{GradedAlgebra(R, NonNegativeInteger)}.
The non-negative integer \spadfunFrom{degree}{GradedAlgebra}
is the tensor rank
and the graded algebra \spadfunFrom{product}{GradedAlgebra}
is the tensor outer product.
The graded module addition captures the notion that
only tensors of equal rank can be added.

If \spad{V} is a vector space of dimension \spad{dim} over \spad{R},
then the tensor module \spad{T[k](V)} is defined as
\begin{verbatim}
T[0](V) = R
T[k](V) = T[k-1](V) * V
\end{verbatim}
where \spadop{*} denotes the \spad{R}-module tensor
\spadfunFrom{product}{GradedAlgebra}.
\spadtype{CartesianTensor(i0,dim,R)} is the graded algebra in which
the degree \spad{k} module is \spad{T[k](V)}.

% ----------------------------------------------------------------------
\subsubsection{Tensor Calculus}
%\head{subsection}{Tensor Calculus}{ugxCartenCalculus}
% ----------------------------------------------------------------------

It should be noted here that often tensors are used in the context of
tensor-valued manifold maps.
This leads to the notion of covariant and contravariant bases with tensor
component functions transforming in specific ways under a change of
coordinates on the manifold.
This is no more directly supported by the \spadtype{CartesianTensor}
domain than it is by the \spadtype{Vector} domain.
However, it is possible to have the components implicitly represent
component maps by choosing a polynomial or expression type for the
components.
In this case, it is up to the user to satisfy any constraints which arise
on the basis of this interpretation.
