@SAJW Yes, that's ok. But you shouldn't use sum as an identifier because it's the name of a built-in function, which sums lists & other iterators. Eg, sum(range(5)). Note that you don't need to supply a start arg for range when the range starts at zero.
As a matter of style, we usually put a space either side of operators. Here's the PEP-008 style guide, which has become the semi-official style guide for the Python community. You don't have to obey PEP-8, but it makes it easier for others to read your code if you do (mostly) follow its recommendations.
@SAJW Your pi series is the famous Leibniz series, which is one of the slowest ways known to compute pi. You need to do a million iterations just to get 6 decimal places. Fortunately, it's easy to make a much faster pi calculator.
cbg, @python_user
The Leibniz series basically calculates $\pi/4 = \arctan(1)$, using the Taylor series expansion: $\arctan(x) = x - x^3/3 + x^5/5 - x^7/7 + \ldots$. If we use that series with smaller x, the convergence is much faster.
It's easy to show that arctan(1) = arctan(1/2) + arctan(1/3). Here's a visual proof:
So we just need to compute those two arctans and sum them. There are faster series than the Taylor series, but it's fast enough for computing pi to the limit of precision of Python floats (~1.0E-15)
Here's a pi program, using a mysterious algorithm by Dutch mathematician / computer scientist, Dik T. Winter. It's not very fast, but it only uses integer arithmetic.
@python_user Basically. It's a computer algebra system built on top of Python, so the core syntax is identical, apart from a couple of things. The main syntactic difference is that you can use ^ for exponentiation, and you have to use ^^ if you want the xor operator.
@python_user It's not removed, you can still use **, if you want. The preprocessor just does a simple regex string replace to convert ^ to **. Ssge is built for mathematicians, and mathematicians use ^ all the time in LaTeX / MathJax.
Sage tries to keep its calculations symbolic, as much as possible. Which is great when you need it, but it can be annoying when you don't, until you get used to it.
would assume that would make debugging need more work than normal, for someone like me who has to print the intermediate values to know if I am on the right track
If you need its numeric value, you can use the n function or method, to get the value with as much precision as you want. Eg, pi.n(prec=150) will give you pi, with 150 bits of precision (compared to 53 bits of normal floats).
I was doing a simple program yesterday that was doing square roots in a loop, and plotting the results. It worked ok for small loops, but got really slow for larger loops, and it took me a few minutes to realise what the problem was.
I just needed to use n() inside the loop. Without it, Sage was chugging along symbolically computing expressions with hundreds of nested square roots, like sqrt(a1 + sqrt(a2 + sqrt(a3 + ...
Sage has no idea of the numeric value of such expressions, until you call the n function. But it knows the rules of algebra for manipulating stuff like (a+b)*(c+d), the rules for multiplying square roots, etc.
One cute thing that Sage has is symbolic variables. You can do stuff like x = var('x') and a, b = var('a,b') which creates symbolic variables with those names. And you can use those variables to do algebra & calculus
So if you then do print((a + b)^2) you get a^2 + 2*a*b + b^2
Most of the time when I'm using SageMathCell, I'm doing plain Python, and not using any Sage features, except maybe its plotting facilities, which are mostly from matplotlib.
Of course, it's not really plain Python, because I'm using Sage integers & floats, but most of the time, that makes no difference.
Most of the time, it's not an issue. The Sage Integer supplies its numeric value. But sometime the Python lib function tries to operate on the whole object. Eg, you can't pass a Sage Integer to random.seed
That's because random.seed used to accept any hashable object, but that's been restricted in recent versions.
Sage uses Numpy for bulk number crunching, so there's usually no problem calling Numpy in Sage and passing stuff back & forth between them. Eg, You can do m = matrix(a) to convert a Numpy array to a Sage matrix, or a = np.array(m), to do the reverse process.
I guess it doesn't really matter at the moment because the room isn't busy, but I try to not flood the room with Sage stuff. It's kind of on topic, but not of general interest.
but I guess one has to know that 9223372036854775807 is the max limit (as mentioned by PM), to arrive at this question, to me at first glance that is just a random number
There's no need for the index to go that high. A machine with that much RAM would be huge. ;) But it's comvenient to use a machine integer for array indices. And in Python, it needs to be a signed integer, because we permit negative indices.
On my computer x = list(range(100000000)) can barely even run...
On my computer the limit is:
>>> x = list(range(10000000000))
Traceback (most recent call last):
File "<pyshell#102>", line 1, in <module>
x = list(range(10000000000))
MemoryError
>>>
it's my new computer, still got some extra RAM not added in yet
Don't forget that a Python integer is an object, not a simple machine integer. So on 64 bit Python, a small integer object (i.e., one that fits in 64 bits) occupies 28 bytes.
Also, at the low level, a list isn't an array of objects. It's an array of pointers to objects. So when you do range(1000, 2000), you need 56 bytes for the list object itself, 1000*8 bytes for the pointers, and 1000*28 bytes for the integers from 1000 to 2000
OTOH, for list(range(100)), Python doesn't need to create the integer objects, since small integer objects from -5 to 256 are built into the interpreter.
@PM2Ring Yeah, the one that gives the MemoryError is the conversion to list, just doing range any number would work, because it only creates a reference object
If you do need to work with a huge sequence, you're better off using a Numpy array, which doesn't have all those overheads. And it's much faster to initialise a big array than to grow a big list.
@PM2Ring so did I understand it: arctan(1/2) + arctan(1/3) is faster to approximate than arctan(1)? and could I improve it further if I find out what arctan(1/2), arctan(1/3) are in terms of smaller x?
@SAJW Yes! So this leads to a whole infinite family of pi algorithms. By a nice "coincidence", arctan(1/2) = arctan(1/3) + arctan(1/7). So you can combine that with the previous equation to get arctan(1) = 2*arctan(1/3) + arctan(1/7)
Now tan(theta) = 1 / cot(theta), so arctan(1/n) = arccot(n). It's traditional to use numbers of this form because then we're just summing pure reciprocal in the Taylor series. That comes in useful when you're trying to calculate large numbers of digits... especially in the days before computers. ;) A few people gained a permanent place in maths history by doing these calculations to hundreds of digits.
@SAJW This stuff is probably not that interesting to the general room population, so we should probably continue this conversation in the Math room.
@zabop Well, it's either matplotlib that adds the decimals, or you did. We can't answer it for the yticks are a mess until you post the code. Why not wait until you had the example ready?
@roganjosh I hope the post makes it clear (it is titled "What is being plotted when I pass on a str column of a Pandas dataframe to matplotlib?", asked like a minute ago.)
Something about the plotted data looks sensible as it cascade from left-to-right, but I can't fathom it. Before I look at your question, I question what you're trying to display with the wonky y axis
I'm so lost. Your question doesn't seem to approximate the question you've posted on main. I would have expected things to blow up if you were comparing strings to ints, but... Yeah, I don't know where to start
I think you're right anyway. I guess the question is genuine in "what am I seeing?" but I don't have a robust answer about the lexicographical ordering. What you're seeing is nonsense...
@AlexandreMarcq True. :) But I wanted to use a fair bit of MathJax in my explanation, and we don't usually use that in this room. Here's the link if you're curious: chat.stackexchange.com/transcript/message/58947315#58947315 Chat doesn't have builtin MathJax support, but there's a link in the Math room info section to a page with several bookmarklets that enable MathJax.
I want to append to a csv file, but if the file doesn't yet exist (or is empty), then I want to write a header row. How terrible of an idea is it to use if file.tell() == 0: instead of something like if path.stat().st_size == 0:?
okay, so, previously, you've helped me out with checking for float and int (thank you for that), but on this version, it has a loop to iterate over every number and I decided to use the f-string integer type suggested to me by Kevin. But that function converts the result into int for division in particular.
Link: https://dpaste.com/63SXVZCWD
I tried it in doctest, it returns a testing failure where it expected 3.0 or .0 value in division.
but if that doesn't work, may I know how do I do the same using this similar function because is_integer won't work on list...
def check_int_float(number: float) -> float:
if number.is_integer(): number = int(number)
return number
This doesn't really help me understand your goal. There is no doctest in the code you posted, and you've done nothing to clarify how the number should be formatted
I'm trying to make the program returns division with decimal but the f-string will convert it to integer instead. For example, when user enters 6 and 3 and chooses to do a division on the numbers, it should return 2.0 and not 2
And if the input is 1.5 * 3.8? Then it should be printed as an int because that's not a division?
Anyway, it sounds like you want to use different format specifiers depending on some condition. Python actually allows placeholders inside the format specifier, so you can do stuff like this:
All completed without issue. I then installed the compiled package into a separate venv using my Pipfile. I did the following: pyarrow = { path = '/tmp/repos/arrow/python' } and I now see the package installed in my venv directory. However the same issue persists, and I'm stuck now on what my options are. Any advice on how to proceed?