Niles Bernoulli
Niles Bernoulli

Reputation: 127

Scalar Multiplication in Sage?

I have a file named test1.sage:

  test1.sage:    
      M = Matrix([[1,2,3],[4,5,6]])
      R = 1/2*M
      print R
      print M

%run test1.sage
[1 2 3]
[4 5 6]

[0 0 0]
[0 0 0]

When I run the .sage file its giving me this random zero matrix. But, line-by-line, in the sage shell, it works perfectly fine, any ideas?

Upvotes: 2

Views: 294

Answers (1)

kcrisman
kcrisman

Reputation: 4402

Your problem is that in Python (2.x) 1/2 gives zero. This is (was) a long standard, and certainly isn't the only language that does this.

If you run the file with Sage you should be fine.

$ sage test1.sage 
[1 2 3]
[4 5 6]
[1/2   1 3/2]
[  2 5/2   3]

The comments indicate that %run must be an IPython "magic" function. My guess is that it isn't changed in Sage to make it a Sage magic function, and the following confirms it:

Make a file containing just print 1/2. Do sage test1.sage to get the test1.py file in your directory. Then run Sage and do this:

sage: %run test1.sage
0
sage: %run test1.py
1/2

I've opened Trac 15891 for this, thanks!

Upvotes: 3

Related Questions