ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 11 Dec 2015 17:04:15 +0100Output result in several columnshttps://ask.sagemath.org/question/31464/output-result-in-several-columns/Dear Sage users,
I have a simple function **f(k,x)=Sin(kx**).
In Sage notebook, I would like to get an output of **f(k,x)** for x in [0..2*pi,step=0.01] and then
for k in [1..10,step=4] (a nested loop). I prefer to get output in an txt-file in several columns as in the following (just numbers):
**x** **f(k<sub>1</sub>,x)** **f(k<sub>2</sub>,x)** **f(k<sub>3</sub>,x)**
<p> 0 0 0 0
<p> 0.01 0.01 0.05 0.0899<p>
... ... ... ...
<p>
I have tried several codes but without success. I think that the problem of my code is not the Sage but my poor knowledge of Python.<p>
Could somebody tell me how to get output in more correct form?<p>
Firs line of my output is normal, But the next three columns are separated in a not preferred manner:
x=var('x')
html('<!--notruncate-->')
f=file("sage_sinkx.txt","w")
for x in [0..2*pi,step=0.01]:
rows=n(x,digits=4),[n(sin(k*x), digits=3) for k in [1..10,step=4]]
s=table(rows)
f.write(str(s)+ "\n")
print s
f.close()
Let me know please if anybody could help.<p>
Thank to all.Thu, 10 Dec 2015 17:42:04 +0100https://ask.sagemath.org/question/31464/output-result-in-several-columns/Answer by calc314 for <p>Dear Sage users,
I have a simple function <strong>f(k,x)=Sin(kx</strong>).
In Sage notebook, I would like to get an output of <strong>f(k,x)</strong> for x in [0..2*pi,step=0.01] and then
for k in [1..10,step=4] (a nested loop). I prefer to get output in an txt-file in several columns as in the following (just numbers):</p>
<p> <strong>x</strong> <strong>f(k<sub>1</sub>,x)</strong> <strong>f(k<sub>2</sub>,x)</strong> <strong>f(k<sub>3</sub>,x)</strong>
</p><p> 0 0 0 0
</p><p> 0.01 0.01 0.05 0.0899</p><p>
... ... ... ...
</p><p>
I have tried several codes but without success. I think that the problem of my code is not the Sage but my poor knowledge of Python.</p><p>
Could somebody tell me how to get output in more correct form?</p><p>
Firs line of my output is normal, But the next three columns are separated in a not preferred manner:</p>
<pre><code>x=var('x')
html('<!--notruncate-->')
f=file("sage_sinkx.txt","w")
for x in [0..2*pi,step=0.01]:
rows=n(x,digits=4),[n(sin(k*x), digits=3) for k in [1..10,step=4]]
s=table(rows)
f.write(str(s)+ "\n")
print s
f.close()
</code></pre>
<p>Let me know please if anybody could help.</p><p>
Thank to all.</p>
https://ask.sagemath.org/question/31464/output-result-in-several-columns/?answer=31466#post-id-31466Here is an option using the `excel-tab` dialect in python.
with open('fun.csv', 'w') as f1:
writefile = csv.writer(f1, dialect='excel-tab')
for x in [0..2*pi,step=0.01]:
result=[n(x,digits=4)]+[n(sin(k*x), digits=3) for k in [1..10,step=4]]
writefile.writerow(result)
Thu, 10 Dec 2015 20:28:28 +0100https://ask.sagemath.org/question/31464/output-result-in-several-columns/?answer=31466#post-id-31466Comment by Chuvac for <p>Here is an option using the <code>excel-tab</code> dialect in python.</p>
<pre><code>with open('fun.csv', 'w') as f1:
writefile = csv.writer(f1, dialect='excel-tab')
for x in [0..2*pi,step=0.01]:
result=[n(x,digits=4)]+[n(sin(k*x), digits=3) for k in [1..10,step=4]]
writefile.writerow(result)
</code></pre>
https://ask.sagemath.org/question/31464/output-result-in-several-columns/?comment=31467#post-id-31467call314: Thank you for your rapid response.Your suggestion has solved my problem.Thu, 10 Dec 2015 21:34:51 +0100https://ask.sagemath.org/question/31464/output-result-in-several-columns/?comment=31467#post-id-31467Comment by kcrisman for <p>Here is an option using the <code>excel-tab</code> dialect in python.</p>
<pre><code>with open('fun.csv', 'w') as f1:
writefile = csv.writer(f1, dialect='excel-tab')
for x in [0..2*pi,step=0.01]:
result=[n(x,digits=4)]+[n(sin(k*x), digits=3) for k in [1..10,step=4]]
writefile.writerow(result)
</code></pre>
https://ask.sagemath.org/question/31464/output-result-in-several-columns/?comment=31469#post-id-31469In that case, be sure to mark this answer as accepted by clicking the check mark icon, so that other users will know if they search for this question - thanks!Fri, 11 Dec 2015 03:53:58 +0100https://ask.sagemath.org/question/31464/output-result-in-several-columns/?comment=31469#post-id-31469Comment by Chuvac for <p>Here is an option using the <code>excel-tab</code> dialect in python.</p>
<pre><code>with open('fun.csv', 'w') as f1:
writefile = csv.writer(f1, dialect='excel-tab')
for x in [0..2*pi,step=0.01]:
result=[n(x,digits=4)]+[n(sin(k*x), digits=3) for k in [1..10,step=4]]
writefile.writerow(result)
</code></pre>
https://ask.sagemath.org/question/31464/output-result-in-several-columns/?comment=31470#post-id-31470Ok. The final point in my question could be the following: In order to import output data to such software as Grace, it would be better to use other dialect **delimiter=' '** instead of **excel-tab**, and also use use **fun.dat** instead of **fun.csv**. It doesn't make any difference and output file is the same. Then grace can read DAT file.Fri, 11 Dec 2015 08:10:16 +0100https://ask.sagemath.org/question/31464/output-result-in-several-columns/?comment=31470#post-id-31470Comment by kcrisman for <p>Here is an option using the <code>excel-tab</code> dialect in python.</p>
<pre><code>with open('fun.csv', 'w') as f1:
writefile = csv.writer(f1, dialect='excel-tab')
for x in [0..2*pi,step=0.01]:
result=[n(x,digits=4)]+[n(sin(k*x), digits=3) for k in [1..10,step=4]]
writefile.writerow(result)
</code></pre>
https://ask.sagemath.org/question/31464/output-result-in-several-columns/?comment=31486#post-id-31486Thanks for that point; I do not know Grace but that could be helpful to others who do use it.Fri, 11 Dec 2015 17:04:15 +0100https://ask.sagemath.org/question/31464/output-result-in-several-columns/?comment=31486#post-id-31486