On 3 feb, 00:45, Simon King <[email protected]> wrote:
> Hi Jason,
>
> On 3 Feb., 05:41, Jason Grout <[email protected]> wrote:
>
> > ...
> > I would use fast_callable (which for me is 5x faster than subs), ...
>
> Note that the OP stated that he did try fast_callable, and it was
> *slower* than z.subs(K1=k1,K2=k2). Perhaps that indicates a bug?

Well, I may be wrong on that one, at this point I don't remember what
the results were
> Cheers,
> Simon

Thank you all for your suggestions, which seem very good :). I was
away from the internet for most of today, so I couldn't read your
replies until now. I made a function called fast_complex which takes a
symbolic expression, generates the corresponding fortran code, and
compiles it using f2py. It seems fast enough for what I need, though I
wonder whether some of the suggestions here might be better. I'll post
the code:


def fast_complex(expr,name,vars,domain=CC):
    varlist=''
    for i in vars:
        varlist+=str(i)+','
    varlist=varlist[:-1]

    s='!f90\n'
    #s='program programa_x\n'
    #s+='    implicit none\n'
    #s+='end program\n\n'


    s+='subroutine '+name+'('+varlist+',output_var)\n'
    s+='    implicit none\n'
    s+='    real, intent(in)  ::'+varlist
    s+='\n'
    if domain==RR:
        s+='    real, intent(out) :: output_var\n'
    else:
        s+='    complex, intent(out) :: output_var\n'
    s+='    complex :: I,e,z\n'
    s+='    e=exp(1.0)\n'
    s+='    I=(0,1)\n'

    fun='    z='
    fun+= str(expr).replace('^','**')
    nums=['0','1','2','3','4','5','6','7','8','9','*','(',')']
    while True:
        maxi=70
        while True:
            c1=fun[maxi-1:maxi]; c2=fun[maxi:maxi+1]
            if (c1 in nums) or (c2 in nums):
                maxi-=1
            else:
                break
        s+=fun[:maxi]+'&\n'
        if len(fun)<maxi:
            break
        fun=fun[maxi:]
    s=s[:-2]
    s+='\n\n'
    if domain==RR:
        s+='    output_var=real(z)\n'
    else:
        s+='    output_var=z\n'
    s+='end subroutine\n'


    f=file('/home/oscar/Escritorio/tesis/calculos/ejem.f90','w')
    f.write(s)
    f.close()

    return fortran(s)

I'll try to check this on the weekend, and maybe make a ticket and
patch :)

thank you all!

Oscar

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to