On 8/17/11 7:59 AM, Chris Godsil wrote:
Dima
Thanks for the comments. First I was aware of the eigenspaces()
command. I am also aware that things are
improving all the time. For my actual example I needed an orthonormal
basis for each eigenspace and the
graphs are pretty random, so the eigenvalues are more or less
arbitrary reals.
I might mention several other things. In fact, I was going to mention
this yesterday to show how using them was just as easy as MATLAB, but
after constructing the last example below, I realized you were right and
matlab was way easier! Anyways:
1. You can use the -pylab option to load a matlab-like environment
(which means you don't have to figure out a lot of the imports). The
-pylab option is actually an option to ipython (i.e., not
sage-specific), but works just as well with Sage:
sage: g=graphs.OctahedralGraph()
sage: a=g.am().numpy()
sage: eig(a) # matlab-like command from numpy/scipy through pylab
(array([ 4.00000000e+00, 1.40614246e-16, -2.00000000e+00,
-2.00000000e+00, -7.71031068e-17, -2.11137087e-16]), array([[
-4.08248290e-01, -7.07106781e-01, -5.77350269e-01,
-9.58724220e-02, -8.33596830e-18, -2.93912896e-17],
[ -4.08248290e-01, -8.75811329e-17, 2.88675135e-01,
-4.45121959e-01, 2.13205450e-01, 5.03280679e-01],
[ -4.08248290e-01, -5.98255573e-17, 2.88675135e-01,
5.40994381e-01, -6.74198366e-01, -4.96697653e-01],
[ -4.08248290e-01, -3.20699816e-17, 2.88675135e-01,
5.40994381e-01, 6.74198366e-01, 4.96697653e-01],
[ -4.08248290e-01, -3.20699816e-17, 2.88675135e-01,
-4.45121959e-01, -2.13205450e-01, -5.03280679e-01],
[ -4.08248290e-01, 7.07106781e-01, -5.77350269e-01,
-9.58724220e-02, 2.96978775e-17, 1.90446830e-17]]))
2. or you could use Sage's eigenmatrix_right command:
sage: g.am().eigenmatrix_right()
(
[ 4 0 0 0 0 0] [ 1 1 0 1 0 0]
[ 0 -2 0 0 0 0] [ 1 0 1 0 1 0]
[ 0 0 -2 0 0 0] [ 1 -1 -1 0 0 1]
[ 0 0 0 0 0 0] [ 1 -1 -1 0 0 -1]
[ 0 0 0 0 0 0] [ 1 0 1 0 -1 0]
[ 0 0 0 0 0 0], [ 1 1 0 -1 0 0]
)
which gives a much nicer orthogonal basis (columns of the right matrix)!
3. Or you could explicitly use the eigenvectors command, which gives the
eigenvalues and for each eigenvalue, a list of eigenvectors and the
algebraic multiplicity.
sage: g.eigenvectors()
[(4, [
(1, 1, 1, 1, 1, 1)
], 1), (-2, [
(1, 0, -1, -1, 0, 1),
(0, 1, -1, -1, 1, 0)
], 2), (0, [
(1, 0, 0, 0, 0, -1),
(0, 1, 0, 0, -1, 0),
(0, 0, 1, -1, 0, 0)
], 3)]
4. The reason I decided you were right about matlab was when I tried to
do the commands exactly as it would be in matlab, it looked a lot harder
than my example matlab session. Note again this is with sage -pylab
sage: a=array([[1,2,3],[4,5,6],[7,8,9]])
sage: b=a.dot(a.T) # b=a*a'
sage: eig(b)
(array([ 2.83858587e+02, 1.14141342e+00, -1.90551232e-15]),
array([[-0.21483724, -0.88723069, 0.40824829],
[-0.52058739, -0.24964395, -0.81649658],
[-0.82633754, 0.38794278, 0.40824829]]))
The "dot" above is really bothersome, especially since *. (i.e., star
*dot*) in matlab is the same as "*" for numpy arrays. Numpy folks have
been trying to get python to change for years and allow multiple
multiplication characters, and it probably isn't going to change anytime
soon. The "array" command above is also much harder than the matlab
a=[1 2 3; 4 5 6; 7 8 9].
You can make it a little easier in numpy if you use the numpy "matrix"
objects, but they are not extremely well-supported by numpy, so I avoid
them as much as possible. Note that "matrix" below is the *scipy*
matrix command, since -pylab overwrote Sage's matrix command. However,
matrix multiplication works naturally below, and the matrix-creating
command uses matlab-like syntax.
sage: a=matrix("1 2 3; 4 5 6; 7 8 9")
sage: a
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
sage: b=a*a.T
sage: eig(b)
(array([ 2.83858587e+02, 1.14141342e+00, -1.90551232e-15]),
matrix([[-0.21483724, -0.88723069, 0.40824829],
[-0.52058739, -0.24964395, -0.81649658],
[-0.82633754, 0.38794278, 0.40824829]]))
One conclusion: we should make Sage's matrix command accept a string
like the above example and parse it as matlab would.
Thanks,
Jason
--
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org