Tartalomjegyzék

Script anal3_diffgeom_tenzor_spherical_1

TELJES MATLAB SCRIPT KIEGÉSZÍTŐ FÜGGVÉNYEKKEL

file:   anal3_diffgeom_tenzor_spherical_1.m
author: Peter Polcz <ppolcz@gmail.com>
Created on 2017.08.08. Tuesday, 14:34:51
Output:
┌anal3_diffgeom_tenzor_spherical_1
│   - Persistence for `anal3_diffgeom_tenzor_spherical_1` initialized [run ID: 2247, 2017.08.27. Sunday, 05:03:42]
│   - Script `anal3_diffgeom_tenzor_spherical_1` backuped

Index notation during the script

The name of a symbolic object $T^{i}_{j} is chosen to be Ti_j

Ti_j = sym('T%d_%d',[2 2])
Output:
Ti_j =
[ T1_1, T1_2]
[ T2_1, T2_2]

The name of a symbolic object $T^{ij} is chosen to be Tij

Tij = sym('T%d%d',[2 2])
Output:
Tij =
[ T11, T12]
[ T21, T22]

The name of a symbolic object $T_{ij} is chosen to be T_ij

T_ij = sym('T_%d%d',[2 2])
Output:
T_ij =
[ T_11, T_12]
[ T_21, T_22]

Variables of spherical coordinate system

Dimension of the vector space

n = 3;

Coordinates $Z^i$. During the script the name of variable representing coordinates $Z^i$ will be denoted by lower case letter 'z'. Coordinates: $Z^1 = r$, $Z^2 = 'theta$, $Z^3 = \phi$.

syms r t p real
zi = [ r ; t ; p ];
% zi = sym('z%d',[n 1])

Mapping from Cartesian coordinate system to spherical.

R = [
    r*cos(t)*sin(p)
    r*sin(t)*sin(p)
    r*cos(p)
    ];

Covariant basis vectors $\vec Z_i$.

Zr = diff(R,r)
Zt = diff(R,t)
Zp = diff(R,p)
Output:
Zr =
 cos(t)*sin(p)
 sin(p)*sin(t)
        cos(p)
Zt =
 -r*sin(p)*sin(t)
  r*cos(t)*sin(p)
                0
Zp =
 r*cos(p)*cos(t)
 r*cos(p)*sin(t)
       -r*sin(p)

Covariant basis vectors in a single operation.

Z_i = jacobian(R,zi)
Output:
Z_i =
[ cos(t)*sin(p), -r*sin(p)*sin(t), r*cos(p)*cos(t)]
[ sin(p)*sin(t),  r*cos(t)*sin(p), r*cos(p)*sin(t)]
[        cos(p),                0,       -r*sin(p)]

Covariant metric tensor $Z_{ij}$.

Z_ij = simplify(Z_i'*Z_i)
Output:
Z_ij =
[ 1,            0,   0]
[ 0, r^2*sin(p)^2,   0]
[ 0,            0, r^2]

Contravariant metric tensor $Z^{ij}$.

Zij = inv(Z_ij)
Output:
Zij =
[ 1,                0,     0]
[ 0, 1/(r^2*sin(p)^2),     0]
[ 0,                0, 1/r^2]

Contravariant components $V^i$ of vector field $\vec V = V^i \vec Z_i$.

Vi = sym('V%d',[n 1])

fVi = [
    sym('V1(r,t,p)')
    sym('V2(r,t,p)')
    sym('V3(r,t,p)')
    ];

assume(in(fVi, 'real'))
Output:
Vi =
 V1
 V2
 V3

Convariant components $V_i$ of vector field $\vec V = V_i \vec Z^i$.

V_i = sym('V_%d',[n 1])

fV_i = [
    sym('V_1(r,t,p)')
    sym('V_2(r,t,p)')
    sym('V_3(r,t,p)')
    ];

assume(in(fV_i, 'real') & in(fVi, 'real'))
Output:
V_i =
 V_1
 V_2
 V_3

Christoffel symbol $\Gamma_{ij}^k$

Denoted by 3-dimensional symbolic array Gamma(i,j,k).

Gamma = sym(zeros(n,n,n));
for i = 1:n
    for j = 1:n
        for k = 1:n
            for m = 1:n
                Gamma(i,j,k) = Gamma(i,j,k) + 0.5 * Zij(k,m) * ( ...
                    diff(Z_ij(m,i), zi(j)) + diff(Z_ij(m,j), zi(i)) - diff(Z_ij(i,j), zi(m)) ...
                    );
            end
        end
    end
end
Gamma
Output:
Gamma(:,:,1) = 
[ 0,           0,  0]
[ 0, -r*sin(p)^2,  0]
[ 0,           0, -r]
Gamma(:,:,2) = 
[   0,           1/r,             0]
[ 1/r,             0, cos(p)/sin(p)]
[   0, cos(p)/sin(p),             0]
Gamma(:,:,3) = 
[   0,              0, 1/r]
[   0, -cos(p)*sin(p),   0]
[ 1/r,              0,   0]

Christoffel symbol demo for reshape operations

m = 3;
comb = allcomb(1:m,1:m,1:m);
comb = comb(:,[3 2 1]);

G = reshape(cellfun(@(a) {sprintf('(%d,%d,%d)', a)}, num2cell(comb,2)),[m,m,m])
Output:
G(:,:,1) = 
    '(1,1,1)'    '(1,2,1)'    '(1,3,1)'
    '(2,1,1)'    '(2,2,1)'    '(2,3,1)'
    '(3,1,1)'    '(3,2,1)'    '(3,3,1)'
G(:,:,2) = 
    '(1,1,2)'    '(1,2,2)'    '(1,3,2)'
    '(2,1,2)'    '(2,2,2)'    '(2,3,2)'
    '(3,1,2)'    '(3,2,2)'    '(3,3,2)'
G(:,:,3) = 
    '(1,1,3)'    '(1,2,3)'    '(1,3,3)'
    '(2,1,3)'    '(2,2,3)'    '(2,3,3)'
    '(3,1,3)'    '(3,2,3)'    '(3,3,3)'

Reshaped Christoffel symbol $\Gamma_{ij}^k$ for multiplication along index $k$.

  • First dimension: $i$, $j$
  • Second simension: $k$
G2D_ij_k = reshape(G,[4 2])
Gamma2D_ij_k = reshape(Gamma,[n^2 n])

Restore after multiplication along index $k$.

reshape(G2D_ij_k(:,1),[2 2])

Covariant derivative

Covariant derivative of covariant tensor field $V_i$.

$$\nabla_j V_i = \frac{\partial V_i}{\partial Z^j} - \Gamma^k_{ij} V_k$$

Using for loops (more transparent, clear-cut)

D_jfV_i = sym(zeros(n,n));

tic
for i = 1:n
    for j = 1:n
        D_jfV_i(i,j) = diff(fV_i(i),zi(j));
        for k = 1:n
            D_jfV_i(i,j) = D_jfV_i(i,j) + Gamma(i,j,k) * fV_i(k);
        end
    end
end
toc

Using reshape and matrix multiplication (less transparent, ~10x faster)

tic
D_jfV_i_fast = reshape(Gamma2D_ij_k * fV_i, [n n]) + jacobian(fV_i,zi);
toc

pcz_info(pcz_symzero(D_jfV_i - D_jfV_i_fast), 'The two operations gave the same result')

Permutation symbol

e_ijk = reshape(levicivita(comb,2), ones(1,n)*n)

End of the script.