# Jacobi Transformation of a Symmetric Matrix

*Based on Numerical Recipes in C++, Sec 11.1*

So you want to diagonalize a matrix, do you? Well, if you have a tiny symmetric matrix, you REALLY want to write up the algorithm by hand, and don’t want to spend much time trying to understand the algorithm, then you have come to the right place.

Otherwise, use LAPACK/BLAS to call a highly optimized routine that can work extremely quickly on large matrices. Julia has those libraries built in already. Even if you do call those matrices, you can make them work better by understanding what’s going on underneath the hood, which is why we are going through this now.

Start with a base *rotation matrix* of the Form

From our starting arbitrary symmetric ,

we will run a series of transformations,

where each iteration brings closer to diagonal form. Thus in our implementing our algorithm, we need to determine two things

- The values of and
- The pattern of sweeping and

And in the end we will need to finally determine if this actually converges, and if has any sort of efficiency.

So lets expand one transformation, and we if we can solve for $c$ and $s$.

## Determining $s$ and $c$

Given we specifically want $a^{\prime}_{pq}$ to be zero, we re-arrange the last equation,

At first glance, this equation might not look easier to solve for $s$ or $c$. At second glance either. We define a new parameter $t = s/c$, which now makes the equation,

now quite easily solvable by our friendly quadratic formula. Though the book does recommend using a form that pulls out smaller roots through

Then reverse solve back to

Though we could use the expressions above, if we simplify them with our new expressions for $c$ and $s$ analytically, we reduce computational load and round off error. These new expressions are

with the new variable

## Convergence

The sum of the squares of the off diagonal elements, choosen in either upper or lower triangles arbitrarily,

## Eigenvectors

By forming a product of every rotation matrix, we also come to approximate the matrix $V$ where

and $D$ is the diagonal form of $A$. $V$ is computed through iterative computation

### Enough with the talking! LETS COMPUTE STUFF

This makes a matrix easier to look at than when its filled with 1.043848974e-12 everywhere

Now on to the rotations!

We don’t always want to compute the eigenvectors, so those are in the optional entries slot.
Both tell the function to compute the vectors with `computeV=true`

and input the `V=V`

after the semicolon.

This function performs one sweep

Just creating some size of matrix

This output is after several sweeps

```
(
5x5 Array{Float64,2}:
-1.59942 0.0 0.0 0.0 0.0
0.0 1.03678 0.0 0.0 0.0
0.0 0.0 -0.823094 0.0 0.0
0.0 0.0 0.0 3.09433 0.0
0.0 0.0 0.0 0.0 1.3409,
5x5 Array{Float64,2}:
-1.59942 5.1314e-30 2.32594e-36 -9.54088e-49 -1.22782e-53
5.1314e-30 1.03678 2.65014e-38 9.13791e-56 6.64996e-67
2.32594e-36 2.65014e-38 -0.823094 -9.56652e-61 2.08002e-92
-9.54088e-49 9.13791e-56 -9.56652e-61 3.09433 0.0
-1.22782e-53 6.64996e-67 2.08002e-92 0.0 1.3409 ,
5x5 Array{Float64,2}:
0.0537334 0.0599494 -0.735228 0.139 0.658511
0.310018 0.612957 -0.14049 0.611348 -0.367001
0.759653 -0.475834 0.264118 0.282571 0.216575
-0.480405 -0.546544 -0.132383 0.644217 -0.194831
-0.305189 0.30913 0.593648 0.33477 0.588905,
2.6331310238375346e-59)
```

Compare the Optimized LAPLACK routine to your results

```
([-1.599424470672961,-0.8230937166650976,1.0367806031602211,
1.3408963512476402,3.0943321944116593],
5x5 Array{Float64,2}:
-0.0537334 0.735228 0.0599494 -0.658511 -0.139
-0.310018 0.14049 0.612957 0.367001 -0.611348
-0.759653 -0.264118 -0.475834 -0.216575 -0.282571
0.480405 0.132383 -0.546544 0.194831 -0.644217
0.305189 -0.593648 0.30913 -0.588905 -0.33477 )
```

```
5x5 Array{Float64,2}:
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
```

How long does it take to make a Sweep? How much memory will the computation take? This is dependent on how large the matrix is, and determines whether or not we want to use this algorithm.

```
0.000028 seconds (320 allocations: 30.469 KB)
0.000099 seconds (1.33 k allocations: 187.266 KB)
0.007413 seconds (34.66 k allocations: 17.448 MB, 14.20% gc time)
```

In addition to time per sweep, we need to know how many sweeps we need to run. So again we run it on a 10x10, 20x20, and 100x100. The efficiency of the algorithm would get a lot worse if we have to sweep the 100x100 a bunch of times.

```
7x4 Array{Float64,2}:
1.0 1.10944 2.43759 14.6644
2.0 0.105628 0.312076 2.87182
3.0 0.000265288 0.017073 0.498082
4.0 6.64324e-9 0.000119472 0.0390564
5.0 4.05463e-18 3.56679e-11 0.00133833
6.0 3.17274e-42 1.96318e-23 6.07661e-7
7.0 6.76289e-110 4.07871e-49 3.98102e-13
```

Well, so we’ve seen how to do one form of exact diagonalization that works, but doesn’t scale very well up to 100x100 matrices. So stay tuned for the Householder method, hopefully coming up soon.

Until then, happy computing :)