factorial of negative one (-1)
Robert Kern
robert.kern at gmail.com
Fri Oct 29 17:16:55 CEST 2010
On 10/29/10 12:02 AM, Chris Rebert wrote:
> On Thu, Oct 28, 2010 at 9:41 PM, Bj Raz<whitequill.bj at gmail.com> wrote:
>> I am working with differential equations of the higher roots of negative
>> one. (dividing enormous numbers into other enormous numbers to come out with
>> very reasonable numbers).
>> I am mixing this in to a script for Maya (the final output is graph-able as
>> a spiral.)
>> I have heard that Sage, would be a good program to do this in, but I'd like
>> to try and get this to work in native python if I can.
>> The script I am trying to port to Python is; http://pastebin.com/sc1jW1n4.
>
> Unless your code is really long, just include it in the message in the future.
> So, for the archive:
> indvar = 200;
> q = 0;
> lnanswer = 0;
> for m = 1:150
> lnanswer = (3 * m) * log(indvar) - log(factorial(3 * m)) ;
> q(m+1) = q(m)+ ((-1)^m) * exp(lnanswer);
> end
> lnanswer
> q
>
> Also, it helps to point out *what language non-Python code is in*. I'm
> guessing MATLAB in this case.
>
> Naive translation attempt (Disclaimer: I don't know much MATLAB):
>
> from math import log, factorial, exp
> indvar = 200
> q = [0]
> lnanswer = 0
> for m in range(1, 151):
> lnanswer = (3 * m) * log(indvar) - log(factorial(3 * m))
> q.append(q[-1] + (1 if m % 2 == 0 else -1) * exp(lnanswer))
> print(lnanswer)
> print(q)
I promised that I would reply when the OP posted here. Except you gave the
answer that I would have.
To the OP: In your code snippet, q(m+1) and q(m) are not function calls. They
are array indexing operations (with the special semantics that assigning beyond
the last element in the array appends a new element to the array). You are not
setting the result of a function to anything, just building up an array of
results. You don't need symbolic math libraries like SAGE for this.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list