Ask Your Question
0

How to save output calculation in 2 columns in simple txt file?

asked 2014-09-20 15:27:32 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Dear All,

I am trying to do a simple calculation with sage:

var('x')
for a in [0..10,step=2]:
     func=numerical_integral(cos(a*x),0,1)[0]
     print a, func

As you can see, the output in sage is written in 2 column. Firs column the number A and the second is the result of calculation:

0  1.0
2  0.454648713413
4  -0.189200623827
6  -0.0465692496998
8  0.123669780828
10 -0.0544021110889

How is it possible to save tow-column result in a simple txt file as it is shown in above? Just number and no any array, no any comma! Just number. I have came across huge documentation in ask.sagemath.org, Sage manual with such offering such as open("example.txt","w") and even with python commands! No result I could get. For example:

var('x')
f=open("/tmp/my2.dat", "w")
for a in [0..10,step=2]:
     func=numerical_integral(cos(a*x),0,1)[0]
     print a, func
f.close()

The question is I don't know how to put PRINT OUTPUT in my2.dat file as two column in above code? I have read somewhere that it is possible by the following python command:

var('x')
with open("/tmp/my2.dat", "w") as out:
      for a in [0..10,step=2]:
            func=numerical_integral(cos(a*x),0,1)[0]
      print a, func >> out, line
f.close (or implicit_close)

No reslut and with error. Please If some one has a practical suggestion to my first code, let me know about that. I don't want use the commercial MATHEMATICA and so on! I just want to use only SAGE and SAGE!

Thank you.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-09-21 00:47:43 +0100

ndomes gravatar image

Look at Python documentation: https://docs.python.org/2/tutorial/in...

With Sage Notebbook you can do (download the file via Data menu):

f=open(DATA+"mydat.txt", "w")
for a in [0..10,step=2]:
     func=numerical_integral(cos(a*x),0,1)[0]
     s = " %4i  %8.4f \n"%(a,func)
     f.write(s)
f.close()
edit flag offensive delete link more

Comments

Thank you for your answer. I have read about that. But your solution is when I use the graphical interface such as notebook. I would like to do that in command shell of sage to save file in a directory such /tmp/ in linux. I don't know how to do that! Do you know?

Kazhal gravatar imageKazhal ( 2014-09-22 12:30:36 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-09-20 15:27:32 +0100

Seen: 1,373 times

Last updated: Sep 21 '14