ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 12 Aug 2012 18:52:32 -0500reading, computing, writinghttp://ask.sagemath.org/question/9228/reading-computing-writing/OK, I just installed Sage for the first time and now I would like to figure out how to read and write a file as well as run 'functions' like primepi (count of prime numbers).
Axxxxxx is a sequence in the OEIS at oeis.org.
The data file has two columns "n, A104272(n)" with n > 3*10^6:
1 2
2 11
3 17
4 29
5 41
... ...
After reading the data I need it take the difference of the A182873(n) = A104272(n+1) - A104272(n), find primepi(A104272(n+1)) and primepi(A104272(n)) and the difference of those two A190874(n) = primepi(A104272(n+1)) - primepi(A104272(n)). After that I would like to write the data back to a file.
I have been looking at the tutorial, but I have not found the section on reading data or anything like it.reddwarf2956Sun, 12 Aug 2012 18:52:32 -0500http://ask.sagemath.org/question/9228/Convert from int to double and back in Cythonhttp://ask.sagemath.org/question/8088/convert-from-int-to-double-and-back-in-cython/This is a really basic question, I'm sure, for cython types.
In the notebook, I have the following cell to compute a famous but totally inefficient $\pi(x)$ (prime counting function). It already is about 100 times faster than the equivalent in native Python.
Question: The `result += (fact - j*int(floor(t)))` line still is bright yellow. I don't know if there is any easy way to mix these types. The problem is I need for `t` to take two ints and make them something I can apply floor to, which appears to be a double. Then I need to make the floor an int again to multiply it by `j`. Any ideas? Again, I'm sure this is simple, but I haven't earned my Cython stripes yet.
%cython
cdef extern from "math.h":
double floor(double x)
cdef int primeish(int n):
cdef int fact
cdef int result
cdef int j
cdef double t
if n==1:
return 0
elif n==2:
return 1
elif n==3:
return 2
else:
result = -1
fact = 1
for j in range(3,n+1):
fact = fact*(j-2)
t = fact/j
result += (fact - j*int(floor(t)))
return resultkcrismanWed, 20 Apr 2011 11:06:34 -0500http://ask.sagemath.org/question/8088/plotting prime_pihttp://ask.sagemath.org/question/7673/plotting-prime_pi/Maybe I am missing something really simple, but shouldn't one be able to plot prime_pi?
<pre><code>
----------------------------------------------------------------------
| Sage Version 4.5.1, Release Date: 2010-07-19 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: plot(prime_pi(x),3,10)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/dept/finotti/<ipython console> in <module>()
/scratch/local/sage-4.5.1/local/lib/python2.6/site-packages/sage/functions/prime_pi.so in sage.functions.prime_pi.PrimePi.__call__ (sage/functions/prime_pi.c:1064)()
/scratch/local/sage-4.5.1/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.__int__ (sage/symbolic/expression.cpp:4171)()
/scratch/local/sage-4.5.1/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.n (sage/symbolic/expression.cpp:17042)()
TypeError: cannot evaluate symbolic expression numerically
</code></pre>
It seems wot work fine with real input
<pre><code>
sage: prime_pi(pi)
_3 = 2
sage: prime_pi(1020.312788)
_4 = 171
</code></pre>
Thanks,
LuisfinottiTue, 07 Sep 2010 04:32:48 -0500http://ask.sagemath.org/question/7673/