anderkve / FYS3150

https://anderkve.github.io/FYS3150
26 stars 14 forks source link

Tips for making tridiagonal matrix #47

Closed JohanCarlsen closed 1 year ago

JohanCarlsen commented 2 years ago

I found that you can make a tridiagonal matrix pretty easy with this:

arma::mat A = arma::mat(N,N); A.diag(-1).fill(a); A.diag(0).fill(d); A.diag(1).fill(e);

The method .diag(k) extracts the k'th diagonal, with k=0 being the main diagonal, k=-1 the sub diagonal and k=1 the super diagonal.