Note: This page hasn't been updated for a long time, and is most likely outdated. If you need it to be updated, please submit a ticket to help@sharcnet.ca with the link to the page. |

## Contents

EIGENVALUES OF NON-SYMMETRIC MATRICES

**Nick Chepurniy, Ph.D.**

**e-mail: nickc@sharcnet.ca**

**HPC Consultant - SHARCNET**

**http://www.sharcnet.ca**

**Compute Canada**

**http://www.computecanada.org**

### Overview

Programs written in OCTAVE, LAPACK and ScaLAPACK are presented for obtaining the eigenvalues of non-symmetric matrices of a given size.

### Introduction

The objective of this online-tutorial is to present programs for obtaining the eigenvalues of non-symmetric matrices of a given size using OCTAVE, LAPACK and ScaLAPACK.

### Some Definitions

We shall denote the order of the matrix A by N. Three packages will be used (OCTAVE, LAPACK and ScaLAPACK) to obtain the eigenvalues of matrix A. We will denote the size of the matrices as small, medium and large according to the following criteria: OCTAVE will be able to handle matrices of size up to "small", LAPACK will be able to handle matrices of size up to "medium' and ScaLAPACK will be able to handle matrices of size up to "large".

### Preparing the Code

Preparing the code for these packages is very easy for OCTAVE, a bit more complicated for LAPACK and much harder for ScaLAPACK. To test the programs we pick the order of the matrix of size "small" which by definition can be run using all three packages. If all the elements of the matrix are real then we have a REAL non-symmetric matrix. If at least one elements is complex then the matrix is COMPLEX. The data type for a matrix will be denoted by the variable DATYPE: 'D' for real double precision matrices and 'Z' for complex double precision matrices.

For LAPACK and ScaLAPACK the programs: (1) obtain the upper Hessenberg form of matrix A, (2) use the Schur decomposition to obtain the eigenvalues. In OCTAVE the command: [U,S]=schur(A) computes the matrix S and the eigenvalues are obtained from the command: eigS = eig(S);

We shall define very simple non-symmetric matrices inside the programs and compare the eigenvalues obtained by the three packages.

The complete set of programs and scripts can be found in this tar file:

Eigenvalues and eigenvectors of Non-symmetric matrices (OCTAVE, LAPACK, ScaLAPACK)

See Eigenvalues and eigenvectors of Non-symmetric matrices (OCTAVE, LAPACK, ScaLAPACK)

You can copy above tar file into your /work/$USER directory on a cluster, say, narwhal, and untar the file by using the following commands:

ssh narwhal.sharcnet.ca cd /work/$USER/ wget http://nickc.sharcnet.ca/EIGEN.tar.gz tar -xvzf EIGEN.tar.gz wget http://nickc.sharcnet.ca/NAG_LAPACK.tar.gz tar -xvzf NAG_LAPACK.tar.gz

In subdirectory NAG_LAPACK you will find script and source files for installing NAG's "extras" library. See

https://www.sharcnet.ca/help/index.php/NAG_LAPACK_%22extras%22_library

for instructions to install the "extras" library.

The EIGEN subdirectory has two subdirecrories:

/work/$USER/EIGEN/OCTAVE /work/$USER/EIGEN/ZGEEV_EX

The OCTAVE programs are in /work/$USER/EIGEN/OCTAVE and the LAPACK programs in /work/$USER/EIGEN/ZGEEV_EX.

The ScaLAPACK programs are in subdirectory /work/$USER/EIGEN

#### OCTAVE

Let's start with the OCTAVE programs. Go to subdirecory OCTAVE and invoke octave and then execute "all" the OCTAVE commands in the file HIST. For example executing "gen08" (the first command in HIST) should produce the following output:

[nar316:/work/$USER/EIGEN/OCTAVE] octave GNU Octave, version 3.2.2 octave:1> gen0008 eps = 1.0000e-08 N = 8 All tests passed for eig_0008 ... generating file trace_A = 8 sum_eigval = 8.0000 eps = 1.0000e-12 Sum eigenvalues = trace(A) criteria satisfied! octave:2>

and the fourth command in the HIST file should produce:

octave:4> Zgen0064 eps = 1.0000e-08 N = 64 All tests passed for Zeig_0064 ... generating file trace_A = 0 + 64i sum_eigval = -1.0202e-13 + 6.4000e+01i eps = 1.0000e-12 Sum eigenvalues = trace(A) criteria satisfied! octave:5>

The quit command will exit from octave and listing your files you should see four new files:

OCTAVE_Deig_0064.dat OCTAVE_Zeig_0008.dat OCTAVE_Zeig_0064.dat OCTAVE_Deig_0008.dat

These are the files containing the eigenvalues for the four cases. Here the 'Z' and 'D' are the data type of the matrix A (described above) and 0064 and 0008 are the sizes of A.

Executing the script sort_eig_files by using command:

./sort_eig_files

will produce files with the eigenvalues sorted:

[nar316:/work/nickc/EIGEN/OCTAVE] ./sort_eig_files Sorting the eigenvalues files ... Sorted files: S_OCTAVE_Deig_0008.dat S_OCTAVE_Deig_0064.dat S_OCTAVE_Zeig_0008.dat S_OCTAVE_Zeig_0064.dat

We will compare these files with those generated by the other two programs once we generate similar files with LAPACK and ScaLAPACK.

#### LAPACK

To run the LAPACK jobs you require the "extras" libraries from NAG. This library should be placed in a subdirectory called lib under your home directory:

The LAPACK programs are in subdirectory:

/work/$USER/EIGEN/ZGEEV_EX

The commands we need to execute are in the HIST file:

make clean make ./run_4_cases ./sort_eig_files ./compare_LAPACK_2_OCTAVE

The "make clean" and "make" commands use the makefile "Makefile" to compile
the LAPACK program that calculates the eigenvalues for matrix A which is
defined to be identical to the A in the OCTAVE subdirectory.

The script "./run_4_cases" is used to submit the jobs to the test queue. We have executed the jobs interactively, since they are very short. These four jobs produce four files containing the eigenvalues:

LAPACK_Deig_0008.dat LAPACK_Zeig_0008.dat LAPACK_Deig_0064.dat LAPACK_Zeig_0064.dat

The next script "./sort_eig_files" sorts above files and places the corresponding sorted eigenvalues into:

S_LAPACK_Deig_0008.dat S_LAPACK_Deig_0064.dat S_LAPACK_Zeig_0008.dat S_LAPACK_Zeig_0064.dat

Then the final script in HIST, "./compare_LAPACK_2_OCTAVE", compares the sorted eigenvalues generated by OCTAVE and LAPACK for each of the four cases. You should see the following:

[nar316:/work/nickc/EIGEN/ZGEEV_EX] ./compare_LAPACK_2_OCTAVE Comparing Zeig_0008 No DIFFERENCES detected for Zeig_0008 Comparing Zeig_0064 No DIFFERENCES detected for Zeig_0064 Comparing Deig_0008 No DIFFERENCES detected for Deig_0008 Comparing Deig_0064 No DIFFERENCES detected for Deig_0064

#### ScaLAPACK

So far we have been using a set of matrices of size 8 or 64 with commands in HIST. A second set of matrices of sizes 800 and 880 can be found in HIST2. Of course you can copy HIST or HIST2 into HIST3 and make the appropriate changes in HIST3 and copy all other files referenced by HIST3 commands and do the required changes to run different cases. Note that there is no need to have four cases per HIST file, you can have any munber of cases.

For ScaLAPACK jobs we must use the scripts in the HIST file in subdirectory:

/work/$USER/EIGEN

Here is the HIST file for ScaLAPACK:

# SOURCE FILES vi pznepdriver.f vi pznepinfo.f # COMPILE rm -rf *.o make clean make ./sub_all_4_2 # Above script submits jobs into the regular queue sqjobs # Sort eigenvalues in the files ./sort_eig_files_2 # Compare results ./compare_ScaLAPACK_2_OCTAVE_2 NOTE: If the system is busy and the regular jobs (i.e. those not going though the test queue) are taking a long time your jobs might run faster if you use scripts that submit jobs to the test queue: ./tsubD_N0800 ./tsubZ_N0800 ./tsubD_N0880 ./tsubZ_N0880 However, since you can have only one job in the test queue you must wait until the job in the test queue finishes before you submit the next job to the test queue. # Sort eigenvalues in the files ./sort_eig_files_2 # Compare results ./compare_ScaLAPACK_2_OCTAVE_2

The final script in HIST, ./compare_ScaLAPACK_2_OCTAVE, compares the eigenvalues
generated by OCTAVE and ScaLAPACK for each of the four cases in HIST2:

[nar316:/work/nickc/EIGEN] ./compare_ScaLAPACK_2_OCTAVE_2 Comparing Zeig_0800 No DIFFERENCES detected for Zeig_0800 Comparing Zeig_0880 No DIFFERENCES detected for Zeig_0880 Comparing Deig_0800 No DIFFERENCES detected for Deig_0800 Comparing Deig_0880 No DIFFERENCES detected for Deig_0880

#### Running a new set of problems

To run a new set of problems calculating eigenvalues:

(1) Copy the HIST file into a NEW_HIST file, say HIST3 (2) In HIST3 append "_3" to the three scripts, i.e. ./sub_all_4 becomes ./sub_all_4_2 ./sort_eig_files becomes ./sort_eig_files_2 ./compare_ScaLAPACK_2_OCTAVE becomes ./compare_ScaLAPACK_2_OCTAVE_2 (3) Create the new scripts by copying them from those in HIST, i.e. cp ./sub_all_4 ./sub_all_4_2 cp ./sort_eig_files ./sort_eig_files_2 cp ./compare_ScaLAPACK_2_OCTAVE ./compare_ScaLAPACK_2_OCTAVE_2 (4) Edit the new scripts. (A) In ./sub_all_4_2 specify the new NEP.dat_Xnnnn file where X is either X of D and nnnn is the size of the matrix Then create the new NEP.dat_Xnnnn files by copying those in HIST into the new name, e.g. cp NEP.dat_D0008 NEP.dat_D0950 then edit NEP.dat_D0950 and enter the correct values. (5) Run the ./sub_all_4_3 script and read the OUTPUT statements. You will probably need to edit programs and recompile it using the "make" command see HIST3 file. (6) Edit ./sort_eig_files_3 and run this script (7) Edit ./compare_ScaLAPACK_2_OCTAVE_3 and run this script

### Eigenvalues and Eigenvectors

The program PZGEEV can be used to get the eigenvalues and eigenvectors of a non-symmetric matrix.

Last update: 12 Sept 2013