Edit or run this notebook

Two by Two Matrix Jacobians

168 μs

This notebook emphasizes the multiple views of Jacobians with examples of 2x2 matrix functions.

In particular we will see the

  • Symbolic "vec" format producing 4x4 matrices (generally n² by n² or mn by mn)

  • Numerical formats

  • The important Linear Transformation view

  • Kronecker notation

  • An example using ForwardDiff automatic differentiation

We also emphasize that matrix factorizations are also matrix functions, just as much as the square and the cube.

2.4 ms
3.6 s
4.3 ms

Symbolic Matrices

134 μs
101 ms

[rcos(θ)rsin(θ)rsin(θ)rcos(θ)]

10.8 s
X

[prqs]

18.1 ms

vec

The vec command in Julia and in standard mathematics flattens a matrix column by column.

186 μs
8.7 μs

1) The matrix square function

135 μs

[p2+qrpr+rspq+qsqr+s2]

674 ms
2.5 ms

Symbolic Jacobian

The Jacobian of the (flattened) matrix function X² symbolically

166 μs
jac (generic function with 1 method)
292 μs
J

[2prq0qp+s0qr0p+sr0rq2s]

1.2 s

Numerical Jacobian

125 μs

[2230350320520238]

985 ms
92.4 ms
2×2 Matrix{Float64}:
 0.00140007  0.0020001
 0.00300015  0.00440022
67.0 ms

Linear Transformation Jacobian

Notice: there is no flattening; this is just matrix to matrix.

160 μs
linear_transformation (generic function with 1 method)
270 μs
2×2 Matrix{Float64}:
 0.0014  0.002
 0.003   0.0044
738 ms

Kronecker product or ⊗ notation

Notation that kind of lets you think "flattened" or "not flattened" at the same time

161 μs
64.1 μs
26.0 μs

Notice all possible products with the first matrix and the second

130 μs

[apaqarasbpbqbrbs]

5.1 ms

[apaqcpcqarascrcsbpbqdpdqbrbsdrds]

98.2 μs
18.9 ms

[a🍕a👽b🍕b👽c🍕c👽a🐼a😸b🐼b😸c🐼c😸d🍕d👽e🍕e👽f🍕f👽d🐼d😸e🐼e😸f🐼f😸]

62.1 μs

[🍕0👽00🍕0👽🐼0😸00🐼0😸]

39.0 μs

[🍕👽00🐼😸0000🍕👽00🐼😸]

36.4 μs

[pr00qs0000pr00qs]

31.7 μs

[p0q00p0qr0s00r0s]

52.1 μs

It is very reasonable to express the Jacobian of the matrix square function as
I2X+XTI2

16.8 ms
51.0 ms

Key Kronecker identity

(A ⊗ B) * vec(C) = vec(BCAᵀ)

163 μs
true
113 μs
25×25 Matrix{Float64}:
 0.0012953    0.166326   0.0562614  0.0499152  …  0.184296   0.163508   0.589165
 0.143089     0.0619388  0.0722452  0.192885      0.236655   0.631837   0.727104
 0.0310261    0.214501   0.0969148  0.0296121     0.317465   0.0970006  0.520203
 0.0162279    0.136538   0.138209   0.0550643     0.452734   0.180375   0.0318315
 0.0896786    0.0640814  0.0263767  0.0670469     0.0864026  0.219626   0.577041
 0.000992756  0.127477   0.0431203  0.0382564  …  0.0449995  0.0399237  0.143856
 0.109667     0.0474716  0.0553708  0.147833      0.0577839  0.154276   0.177537
 ⋮                                             ⋱                        
 0.134359     0.0960087  0.0395185  0.100452      0.104482   0.265582   0.697785
 0.00143673   0.184485   0.0624041  0.0553651  …  0.200821   0.178169   0.641993
 0.158711     0.0687014  0.0801332  0.213945      0.257875   0.688491   0.792301
 0.0344136    0.237921   0.107496   0.0328452     0.345931   0.105698   0.566848
 0.0179997    0.151445   0.1533     0.0610763     0.493329   0.196548   0.0346857
 0.09947      0.0710779  0.0292566  0.0743673     0.09415    0.239319   0.628782
17.8 μs

Useful Krockecker identities

  • (AB)T=ATBT

  • (AB)1=A1B1

  • det(AB)=det(A)mdet(B)n

    , An,n,Bm,m

  • trace(AB)=trace(A)trace(B)

  • AB

    is orthogonal if A and B are orthogonal

  • (AB)(CD)=(AC)(BD)

  • If Au=λu, and Bv=μv, then if X=vuT, then BXAT=λμX, and also AXTBT=λμXT. Therefore AB and BA have the same eigenvalues, and transposed eigenvectors.

(See Wikipedia for more properties. )

491 μs

The Jacobian in Kronecker notation

140 μs

You see (I⊗X + X'⊗I) vec(dX) = vec(XdX + dX X) = vec( d(X²))
showing that d(X²) = (I⊗X + X'⊗I) dX.

(I feel it's okay to drop the "vec" and think of the kronecker notation as defining the linear operator from matrices to matrices)

Do look this over.

259 μs

Automatic Differentiation (is not finite differences nor symbolic)

It comes in forward and reverse modes. Let's try forward.

171 μs
76.4 ms

[2prq0qp+s0qr0p+sr0rq2s]

9.9 μs
4×4 Matrix{Int64}:
 2  2  3  0
 3  5  0  3
 2  0  5  2
 0  2  3  8
4.8 s

[2320250230530328]

116 ms

[2prq0qp+s0qr0p+sr0rq2s]

2.0 s

2) The matrix cube Function

121 μs

[p3+qrs+2pqrr2q+p2r+s2r+prsp2q+q2r+s2q+pqss3+pqr+2qrs]

3.9 s

Symbolic Jacobian

The Jacobian of the (flattened) matrix function X² symbolically

156 μs

[3p2+2qrrs+2prqs+2pqqrqs+2pqp2+ps+s2+2qrq2pq+2qsrs+2prr2p2+ps+s2+2qrpr+2rsqrpr+2rspq+2qs3s2+2qr]

920 ms

[3p2+2qrrs+2prqs+2pqqrqs+2pqp2+ps+s2+2qrq2pq+2qsrs+2prr2p2+ps+s2+2qrpr+2rsqrpr+2rspq+2qs3s2+2qr]

1.5 s

LinearTransformation Jacobian

121 μs

dX X² + X dX X + dX X²

127 μs

with numerical data:

126 μs
#7 (generic function with 1 method)
21.0 μs
2×2 Matrix{Float64}:
 0.0111011  0.0162016
 0.0243024  0.0354035
23.9 μs
2×2 Matrix{Float64}:
 0.0111  0.0162
 0.0243  0.0354
22.3 μs

check against the symbolic answer

123 μs

[1512186183392712433186182760]

287 ms
5.3 ms

The Jacobian in Kronecker Notation

146 μs

[3p2+2qrrs+2prqs+2pqqrqs+2pqp2+ps+s2+2qrq2pq+2qsrs+2prr2p2+ps+s2+2qrpr+2rsqrpr+2rspq+2qs3s2+2qr]

71.9 ms

3) The LU Decomposition

Recall the LU Decomposition factors a matrix into unit lower-trianguar and upper triangular:

378 μs
147 ms

[prqs]

520 ms

The four entries of X: p,q,r,s are transformed into these four entries in LU:

191 μs
15.0 μs

[qp21p0010000010qrp2rpqp1]

653 ms

Exercise: Relate this to d(LU) = dL U + L dU

126 μs

4) Traceless symmetric eigenproblem: an example with two parameters not four

123 μs
S

[pssp]

5.2 ms

We know that the eigenvalues add to 0 (from the trace) and the eigenvectors are orthogonal (from being symmetric), so we can represent the eigenvectors and eigenvalues:

135 μs
Q

[cos(12θ)sin(12θ)sin(12θ)cos(12θ)]

386 ms
Λ

[r00r]

97.4 μs

[cos(12θ)sin(12θ)sin(12θ)cos(12θ)]

9.3 μs

[r00r]

6.9 μs

[rcos(θ)rsin(θ)rsin(θ)rcos(θ)]

6.2 ms

The relationship between θ,r to p,s:

127 μs
15.8 ms

[cos(θ)rsin(θ)sin(θ)rcos(θ)]

2.4 s

Interesting mathematical observation: these are the formulas you may remember from other classes that relate cartesian coordinates to polar coordinates in the plane.

134 μs
jacobian_det

r

1.4 s

Mathematical aside det J=r , this is the change of variables from x,y to r,θ that you may have seen in 18.02. This eigenvalue problem is the same as the cartesian coordinates to polar representations of the plane. Often written dx dy = r dr dθ

139 μs

5) The full 2x2 symmetric eigenproblem

122 μs
43.0 ms

We think of

(pssr)=(cos(θ)sin(θ)sin(θ)cos(θ))(λ100λ2)(cos(θ)sin(θ)sin(θ)cos(θ))T
as the function from
λ1,λ2,θp,r,s

287 μs

S = QΛQ':

124 μs

[cos2(θ)sin2(θ)2λ1cos(θ)sin(θ)+2λ2cos(θ)sin(θ)sin2(θ)cos2(θ)2λ1cos(θ)sin(θ)2λ2cos(θ)sin(θ)cos(θ)sin(θ)cos(θ)sin(θ)cos2(θ)λ1+sin2(θ)λ2cos2(θ)λ2sin2(θ)λ1]

89.2 ms

The determinant of this transformation simplifies to λ1λ2 which some people interpret as a kind of repulsion between the two eigenvalues: that is there is a tendency for the two eigenvalues to not want to be too close together. (If both are equal, when n=2, the matrix is αI, one condition takes three parameters down to 1)

153 μs
Loading...
ii