load("eigen")$

matrix1:matrix([m1,0,0,0,m5],[0,m2,0,0,m5],[0,0,m3,0,m5],[0,0,0,m4,m5],[0,0,0,0,0]);

matrix2:matrix([1,2,3,4],[0,3,4,5],[0,0,5,6],[0,0,0,9]);

/* the next function takes a matrix as its argument and returns the
	eigenvalues and the unit eigen vectors of that matrix... */

uniteigenvectors(matrix2);

/* if you already know the eigenvectors you can set the flag 
	knowneigvects to true and the global variable listeigvects to the
	list of the eigen vectors... 
	the next function takes a matrix as its argument and returns the eigen
	values and the unit eigen vectors of that matrix.  in addition if
	the flag nondiagonalizable is false,two global matrices leftmatrix and
	rightmatrix will be generated.  these matrices have the property that
	leftmatrix.(matrix).rightmatrix is a diagonal matrix with the eigen 
	values of the (matrix) on the diagonal...  */

similaritytransform(matrix1)$
nondiagonalizable;
ratsimp(leftmatrix.matrix1.rightmatrix);

/* now that you know how to use the eigen package, here are some
	examples about how not to use it.
	consider the following matrix :   */

matrix3:matrix([1,0],[0,1]);

/* as you've undoubtedly noticed, this is the 2*2 identity matrix.
	let's find the eigen values and the eigen vectors of this matrix...
*/

eigenvectors(matrix3);

/* "nothing special happened", you say.  everyone knows what the eigen
	values and the eigen vectors of the identity matrix are, right?
	right.  now consider the following matrix :  */

matrix4:matrix([1,e],[e,1]);

/* let e>0, but as small as you can imagine.  say 10^(-100).
	let's find the eigen values and the eigen vectors of this matrix :
*/

eigenvectors(matrix4);

/* since e~10^(-100), the eigen values of matrix4 are equal to the
	eigen values of matrix3 to a very good accuracy.  but, look
	at the eigen vectors!!!  eigen vectors of matrix4 are nowhere
	near the eigen vectors of matrix3.  there is angle of %pi/4
	between the corresponding eigen vectors.  so, one learns
	another fact of life :

	matrices which have approximately the same eigen values do not
	have approximately the same eigen vectors in general.

	this example may seem artificial to you, but it is not if you think
	a little bit more about it.  so, please be careful when you
	approximate the entries of whatever matrix you have.  you may
	get good approximations to its eigen values, however the eigen
	vectors you get may be entirely spurious( or some may be correct,
	but some others may be totally wrong ).

	now, here is another sad story :
	let's take a look at the following matrix :  */

matrix5:matrix([5/2,50-25*%i],[50+25*%i,2505/2]);

/* nice looking matrix, isn't it?  as usual, we will find the eigen
	values and the eigen vectors of it :  */

eigenvectors(matrix5);

/* well, here they are.  suppose that this was not what you wanted.
	instead of those sqrt(70)'s, you want the numerical values of
	everything.  one way of doing this is to set the flag "numer"
	to true and use the eigenvectors command again :  */

numer:true;
eigenvectors(matrix5);

/* ooops!!!  what happened??  we got the eigen values, but there are
	no eigenvectors.  nonsense, there must be a bug in eigen, right?
	wrong.  there is no bug in eigen.  we have done something which
	we should not have done.  let me explain : 
	when one is solving for the eigen vectors, one has to find the
	solution to homogeneous equations like :  */

equation1:a*x+b*y=0;
equation2:c*x+d*y=0;

/* in order for this set of equations to have a solution other than
	the trivial solution ( the one in which x=0 and y=0 ), the 
	determinant of the coefficients ( in this case a*d-b*c ) should
	vanish.  exactly.   if the determinant does not vanish the only
	solution will be the trivial solution and we will get no eigen
	vectors.  during this demo, i did not set a,b,c,d to any
	particular values.  let's see what happens when we try to solve
	the set above :  */

algsys([equation1,equation2],[x,y]);

/* you see?  the infamous trivial solution.  now let me set a,b,c,d
	to some numerical values :  */

a:4;
b:6;
c:2;
d:3;
a*d-b*c;
equation1:ev(equation1);
equation2:ev(equation2);
algsys([equation1,equation2],[x,y]);

/* now we have a nontrivial solution with one arbitrary constant.
	( %r(something) ).  what happened in the previous case is that
	the numerical errors caused the determinant not to vanish, hence
	algsys gave the trivial solution and we got no eigen vectors.
	if you want a numerical answer, first calculate it exactly,
	then set "numer" to true and evaluate the answer.  */

numer:false;
notnumerical:eigenvectors(matrix5);
numer:true;
ev(notnumerical);

/* you see, it works now.  actually, if you have a matrix with
	numerical entries and you can live with reasonably accurate
	answers, there are much better (faster) programs.  ask somebody
	about the imsl routines on the share directory... 
	this is all...  if you think that the names of the functions are too 
	long, there are shorter names for them and they are given in the file
	eigen usage dsk:share;.  good luck!!!!!!!!!!!!!...... yekta  */


"end of demo"$
