On Fri, Sep 14, 2018 at 9:00 AM, Peter Luschny <peter.lusc...@gmail.com> wrote:
> How can I speed up this computation?
>
>     H.<i,j,k> = QuaternionAlgebra(SR, -1, -1)

Do NOT use SR.  Instead use QQ(x).  Then it is 100x faster than Mathematica...

R.<x> = QQ[]
K = R.fraction_field()
H.<i,j,k> = QuaternionAlgebra(K, -1, -1)
def Q(a, b, c, d): return H(a + b*i + c*j + d*k)
def P(n): return Q(x+1,1,1,1)*P(n-1) if n > 0 else Q(1,0,0,0)
def p(n): return P(n)[0].numerator().list()
for n in (0..20): print [n], p(n)

This takes 0.05s walltime.  See

https://share.cocalc.com/share/4a5f0542-5873-4eed-a85c-a18c706e8bcd/support/2018-09-14-091151-quat.sagews?viewer=share

Also, if you want to actually push things further, note that your
function P depends on all smaller values of
P, so you could make this massively faster by making P use
@cached_function, like this:

R.<x> = QQ[]
K = R.fraction_field()
H.<i,j,k> = QuaternionAlgebra(K, -1, -1)
def Q(a, b, c, d): return H(a + b*i + c*j + d*k)
@cached_function
def P(n):
    return Q(x+1,1,1,1)*P(n-1) if n > 0 else Q(1,0,0,0)
def p(n): return P(n)[0].numerator().list()
for n in (0..20): print [n], p(n)

This takes 0.01s walltime, and will work for 20 replaced by a much
larger number.
Read about this cached_function decorator:

http://doc.sagemath.org/html/en/reference/misc/sage/misc/cachefunc.html


>     def Q(a, b, c, d): return H(a + b*i + c*j + d*k)
>     def P(n): return Q(x+1,1,1,1)*P(n-1) if n > 0 else Q(1,0,0,0)
>     def p(n): return P(n)[0].list()
>     for n in (0..20): print [n], p(n)
>
>     [0] [1]
>     [1] [1, 1]
>     [2] [-2, 2, 1]
>     [3] [-8, -6, 3, 1]
>     [4] [-8, -32, -12, 4, 1]
>     [5] [16, -40, -80, -20, 5, 1]
>     [6] [64, 96, -120, -160, -30, 6, 1]
>     ...
>
> With Mathematica this takes 6 sec, with Sage it takes
> hours, (in fact I interrupted after n=15).
>
> Thanks, Peter
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support+unsubscr...@googlegroups.com.
> To post to this group, send email to sage-support@googlegroups.com.
> Visit this group at https://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.



-- 
William (http://wstein.org)

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to