0

I am trying to execute this code:

import numpy as np
import numexpr as ne

def julia(h, w, maxit=20, a=0.0):
    x = np.linspace(-1.8, 1.8, w, dtype=np.float32)
    y = np.linspace(-1.8, 1.8, h, dtype=np.float32)
    xv, yv = np.meshgrid(x, y)
    c = xv+yv*1j
    z = c
    divtime = maxit + np.zeros(z.shape, dtype=np.uint8)
    q = np.complex64(0.7885*np.exp(1j*a))

    for i in range(maxit):
        ne.evaluate('z*z + q', out=z) # line with error
        diverge = z*np.conj(z) > 4
        div_now = diverge & (divtime == maxit)
        divtime[div_now] = i
        z[diverge] = 2
    return divtime

julia(100, 100)

Variables z and q are both complex64 type, but numexpr returns complex128. Is there a way to make it operate with complex64 values to increase performance?

  • How about `z = ne.evaluate('z*z + q')`? – Divakar Nov 04 '19 at 06:30
  • @Divakar it works, but when i run it with 'out=z' it gives error 'TypeError: Iterator requested dtype could not be cast from dtype('complex128') to dtype('complex64'), the operand 0 dtype, according to the rule 'safe'' I suppose that it internally converts complex64 to complex128 and I don't want that – Timofey Chernikov Nov 05 '19 at 06:38
  • Because `z` has a dtype of complex64, while the result of `'z*z + q'` would be `complex128`, so you can't write back into `z`? – Divakar Nov 05 '19 at 06:41

0 Answers0