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.Sat, 14 Mar 2015 18:05:50 +0100pipe into stdin vs attach -- differences?https://ask.sagemath.org/question/26182/pipe-into-stdin-vs-attach-differences/ Here's some code, called `slopes.sage`:
def classical_slopes(N,p,r,k,i):
assert p>2
assert r>1 ## wild
G=DirichletGroup(p^r) ## cyclic
alpha=G[1]
alpha_tame=alpha^(p^(r-1))
alpha_wild=alpha^(p-1)
K=alpha.base_ring() ## cyclotomic
chi=alpha_tame^i*alpha_wild
C=CuspForms(chi.extend(N*p^r),k)
f=C.hecke_polynomial(p).base_extend(K)
R=K.maximal_order()
primroot=primitive_root(p) ## p>2
X=R.ideal(p,alpha_tame(primroot)-primroot).factor()
assert len(X)==1 ## ideal should be a prime power
Qt = PolynomialRing(QQ, 't')
P,e=X[0]
c2=[2^(a.valuation(P)) if a!=0 else 0 for a in f.coefficients()]
return (e,Qt(c2).newton_slopes(2))
# off we go. The import sys bit is to flush stdout.
import sys
N=3
p=5
for k in range(2,10):
i=4-(k%4)
print N,p,3,k,i,classical_slopes(N,p,3,k,i)
sys.stdout.flush()
If I pipe it into sage like this:
sage < slopes.sage
I get the following output:
$ sage < slopes.sage
┌────────────────────────────────────────────────────────────────────┐
│ Sage Version 6.5, Release Date: 2015-02-17 │
│ Type "notebook()" for the browser-based notebook interface. │
│ Type "help()" for help. │
└────────────────────────────────────────────────────────────────────┘
sage: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: sage: sage: sage: sage: sage: ....: ....: ....: ....:
Exiting Sage (CPU time 0m0.03s, Wall time 0m0.20s).
But if I attach the file, within a sage console session, it runs as I expect it to run. Is this something to do with the pre-parser? If I've made a slip then that's great, but if it's harder than I think to pipe sage script into sage then I'd appreciate some tips as I would like to start a large sage job on a remote machine via ssh.Sat, 14 Mar 2015 02:38:59 +0100https://ask.sagemath.org/question/26182/pipe-into-stdin-vs-attach-differences/Answer by nbruin for <p>Here's some code, called <code>slopes.sage</code>:</p>
<pre><code>def classical_slopes(N,p,r,k,i):
assert p>2
assert r>1 ## wild
G=DirichletGroup(p^r) ## cyclic
alpha=G[1]
alpha_tame=alpha^(p^(r-1))
alpha_wild=alpha^(p-1)
K=alpha.base_ring() ## cyclotomic
chi=alpha_tame^i*alpha_wild
C=CuspForms(chi.extend(N*p^r),k)
f=C.hecke_polynomial(p).base_extend(K)
R=K.maximal_order()
primroot=primitive_root(p) ## p>2
X=R.ideal(p,alpha_tame(primroot)-primroot).factor()
assert len(X)==1 ## ideal should be a prime power
Qt = PolynomialRing(QQ, 't')
P,e=X[0]
c2=[2^(a.valuation(P)) if a!=0 else 0 for a in f.coefficients()]
return (e,Qt(c2).newton_slopes(2))
# off we go. The import sys bit is to flush stdout.
import sys
N=3
p=5
for k in range(2,10):
i=4-(k%4)
print N,p,3,k,i,classical_slopes(N,p,3,k,i)
sys.stdout.flush()
</code></pre>
<p>If I pipe it into sage like this:</p>
<pre><code>sage < slopes.sage
</code></pre>
<p>I get the following output:</p>
<pre><code>$ sage < slopes.sage
┌────────────────────────────────────────────────────────────────────┐
│ Sage Version 6.5, Release Date: 2015-02-17 │
│ Type "notebook()" for the browser-based notebook interface. │
│ Type "help()" for help. │
└────────────────────────────────────────────────────────────────────┘
sage: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: ....: sage: sage: sage: sage: sage: ....: ....: ....: ....:
Exiting Sage (CPU time 0m0.03s, Wall time 0m0.20s).
</code></pre>
<p>But if I attach the file, within a sage console session, it runs as I expect it to run. Is this something to do with the pre-parser? If I've made a slip then that's great, but if it's harder than I think to pipe sage script into sage then I'd appreciate some tips as I would like to start a large sage job on a remote machine via ssh.</p>
https://ask.sagemath.org/question/26182/pipe-into-stdin-vs-attach-differences/?answer=26183#post-id-26183You can do
sage slopes.sage
to run the file from the command line. The bad interaction is probably between the ipython readline interface and a non-tty stdin, not with the preparser. Note that IPython does some undesirable things for file input. For one thing, it uses auto-indent, which makes it hard or impossible to input some more complicated loops (unless you use %cpaste).
Sage or Ipython could check if stdin is a tty and revert to "file processing" (i.e., not use readline etc.) if it's not, but apparently (judging from your example) it doesn't.
In any case, the preparser does apply a few more efficient tricks (such as factoring out constants) when you use `sage <file>`, so it's better to use that if you can, rather than redirect the input. It has the side-effect of writing `slopes.sage.py` into the current directory, though.Sat, 14 Mar 2015 07:14:48 +0100https://ask.sagemath.org/question/26182/pipe-into-stdin-vs-attach-differences/?answer=26183#post-id-26183Comment by Kevin Buzzard for <p>You can do</p>
<pre><code>sage slopes.sage
</code></pre>
<p>to run the file from the command line. The bad interaction is probably between the ipython readline interface and a non-tty stdin, not with the preparser. Note that IPython does some undesirable things for file input. For one thing, it uses auto-indent, which makes it hard or impossible to input some more complicated loops (unless you use %cpaste).</p>
<p>Sage or Ipython could check if stdin is a tty and revert to "file processing" (i.e., not use readline etc.) if it's not, but apparently (judging from your example) it doesn't.</p>
<p>In any case, the preparser does apply a few more efficient tricks (such as factoring out constants) when you use <code>sage <file></code>, so it's better to use that if you can, rather than redirect the input. It has the side-effect of writing <code>slopes.sage.py</code> into the current directory, though.</p>
https://ask.sagemath.org/question/26182/pipe-into-stdin-vs-attach-differences/?comment=26189#post-id-26189For what its' worth: this did not work for me initially, but then I realised why -- I had mis-installed sage in a silly way (and slopes.sage was not actually being passed to sage at all). Thanks as ever Nils.Sat, 14 Mar 2015 18:05:50 +0100https://ask.sagemath.org/question/26182/pipe-into-stdin-vs-attach-differences/?comment=26189#post-id-26189