def test_dict_numpy_complex(self): x = {'foo': np.complex128(1.0 + 1.0j), 'bar': np.complex128(2.0 + 2.0j)} x_rec = self.encode_decode(x) self.assertEqual(x, x_rec) for key in x: self.assertEqual(type(x[key]), type(x_rec[key]))
def ordere(self, ms, v1_in): """ Modulator response to O(ε) slow-time input linearized about an O(1) behaviour """ # Check for a prior O(1) solution assert self.O1_complete, "O(1) solve must be performed before calling O(ε) solve" # Fiber parameters fiber = self.fibers[0] alpha = np.complex128(fiber.alpha(self.wl)) gamma = np.complex128(fiber.gamma(self.wl)) betaw = fiber.beta(self.wl, ms.omega).astype(np.complex128) # Solver only deals with flat arrays .. how sad y_in = ms.fft(v1_in).flatten() # Setup solver # r = odesolve.ode_rk45(df_nls_ordere, atol=1e-10, rtol=1e-10) r = ode(df_nls_ordere) r.set_integrator("zvode", method="adams", nsteps=5000, atol=1e-8, rtol=1e-8) r.set_initial_value(y_in, 0) r.set_f_params(self.O1_y, self.O1_z, alpha, gamma, betaw, v1_in.shape[0], v1_in.shape[1]) # Solve to the end - no need for intermediate solutions v1_out = r.integrate(fiber.length) v1_out = ms.ifft(v1_out.reshape(v1_in.shape)) return ms, v1_out
def order1(self, ps, vm_in): """ Optical propagation of O(1) periodic input Here we take the input as an phasor in the optical domain and the output is returned as a phasor in the optical domain vm_out = [a₀, a₁, a₂ ... aᵥ, a₋ᵥ ... a₋₂, a₋₁, a₋₀] where the optical signal at optical carrier frequency wc is: E(t) = sum_{k=-v}^v a_k exp(j k w0 t) exp(j wc t) using the positive frequency convention (as in Rubiola) """ from scipy.integrate import ode fiber = self.fibers[0] alpha = np.complex128(fiber.alpha(self.wl)) gamma = np.complex128(fiber.gamma(self.wl)) betaw = fiber.beta(self.wl, ps.omega).astype(np.complex128) r = ode(df_nls_order1) r.set_integrator('zvode', nsteps=1000, atol=1e-10, rtol=1e-10) r.set_initial_value(vm_in, 0) r.set_f_params(alpha,gamma,betaw) #Solve r.integrate(fiber.length) return ps, r.y
def test_numpy(self): assert chash(np.bool_(True)) == chash(np.bool_(True)) assert chash(np.int8(1)) == chash(np.int8(1)) assert chash(np.int16(1)) assert chash(np.int32(1)) assert chash(np.int64(1)) assert chash(np.uint8(1)) assert chash(np.uint16(1)) assert chash(np.uint32(1)) assert chash(np.uint64(1)) assert chash(np.float32(1)) == chash(np.float32(1)) assert chash(np.float64(1)) == chash(np.float64(1)) assert chash(np.float128(1)) == chash(np.float128(1)) assert chash(np.complex64(1+1j)) == chash(np.complex64(1+1j)) assert chash(np.complex128(1+1j)) == chash(np.complex128(1+1j)) assert chash(np.complex256(1+1j)) == chash(np.complex256(1+1j)) assert chash(np.datetime64('2000-01-01')) == chash(np.datetime64('2000-01-01')) assert chash(np.timedelta64(1,'W')) == chash(np.timedelta64(1,'W')) self.assertRaises(ValueError, chash, np.object()) assert chash(np.array([[1, 2], [3, 4]])) == \ chash(np.array([[1, 2], [3, 4]])) assert chash(np.array([[1, 2], [3, 4]])) != \ chash(np.array([[1, 2], [3, 4]]).T) assert chash(np.array([1, 2, 3])) == chash(np.array([1, 2, 3])) assert chash(np.array([1, 2, 3], dtype=np.int32)) != \ chash(np.array([1, 2, 3], dtype=np.int64))
def eta_fil(self, x, V_app, apprx=(0, 0, 0, 0)): m_eff = self.m_r * const.electron_mass mpmath.mp.dps = 20 x0 = Symbol('x0') # eta_fil x1 = Symbol('x1') # eta_ac x2 = Symbol('x2') # eta_hop x3 = Symbol('x3') # V_tunnel f0 = const.Boltzmann * self.T / (1 - self.alpha) / const.elementary_charge / self.z * \ ln(self.A_fil/self.A_ac*(exp(- self.alpha * const.elementary_charge * self.z / const.Boltzmann / self.T * x0) - 1) + 1) - x1# eta_ac = f(eta_fil) x1 = f(x0) f1 = x*2*const.Boltzmann*self.T/self.a/self.z/const.elementary_charge*\ asinh(self.j_0et/self.j_0hop*(exp(- self.alpha * const.elementary_charge * self.z / const.Boltzmann / self.T * x0) - 1)) - x2# eta_hop = f(eta_fil) f2 = x1 - x0 + x2 - x3 f3 = -V_app + ((self.C * 3 * sqrt(2 * m_eff * ((4+x3/2)*const.elementary_charge)) / 2 / x * (const.elementary_charge / const.Planck)**2 * \ exp(- 4 * const.pi * x / const.Planck * sqrt(2 * m_eff * ((4+x3/2)*const.elementary_charge))) * self.A_fil*x3) + (self.j_0et*self.A_fil*(exp(-self.alpha*const.elementary_charge*self.z*x0/const.Boltzmann/self.T) - 1))) * (self.R_el + self.R_S + self.rho_fil*(self.L - x) / self.A_fil) \ + x3 eta_fil, eta_ac, eta_hop, V_tunnel = nsolve((f0, f1, f2, f3), [x0, x1, x2, x3], apprx) eta_fil = np.real(np.complex128(eta_fil)) eta_ac = np.real(np.complex128(eta_ac)) eta_hop = np.real(np.complex128(eta_hop)) V_tunnel = np.real(np.complex128(V_tunnel)) current = ((self.C * 3 * sqrt(2 * m_eff * ((4+V_tunnel)*const.elementary_charge)) / 2 / x * (const.elementary_charge / const.Planck)**2 * \ exp(- 4 * const.pi * x / const.Planck * sqrt(2 * m_eff * ((4+V_tunnel)*const.elementary_charge))) * self.A_fil*V_tunnel) + (self.j_0et*self.A_fil*(exp(-self.alpha*const.elementary_charge*self.z*eta_fil/const.Boltzmann/self.T) - 1))) print(eta_fil, eta_ac, eta_hop, V_tunnel) # print(eta_ac - eta_fil + eta_hop - V_tunnel) return eta_fil, eta_ac, eta_hop, V_tunnel, current
def test_cublasZgemmBatched(self): l, m, k, n = 11, 7, 5, 3 A = (np.random.rand(l, m, k)+1j*np.random.rand(l, m, k)).astype(np.complex128) B = (np.random.rand(l, k, n)+1j*np.random.rand(l, k, n)).astype(np.complex128) C_res = np.einsum('nij,njk->nik', A, B) a_gpu = gpuarray.to_gpu(A) b_gpu = gpuarray.to_gpu(B) c_gpu = gpuarray.empty((l, m, n), np.complex128) alpha = np.complex128(1.0) beta = np.complex128(0.0) a_arr = bptrs(a_gpu) b_arr = bptrs(b_gpu) c_arr = bptrs(c_gpu) cublas.cublasZgemmBatched(self.cublas_handle, 'n','n', n, m, k, alpha, b_arr.gpudata, n, a_arr.gpudata, k, beta, c_arr.gpudata, n, l) assert np.allclose(C_res, c_gpu.get())
def test_dict_numpy_complex(self): x = {"foo": np.complex128(1.0 + 1.0j), "bar": np.complex128(2.0 + 2.0j)} x_rec = self.encode_decode(x) self.assert_( all(map(lambda x, y: x == y, x.values(), x_rec.values())) and all(map(lambda x, y: type(x) == type(y), x.values(), x_rec.values())) )
def distance_matrix(K, N, dim, obs, sampl, method): dist = np.zeros((K, N), dtype=np.complex128) if method == "eucl": for i in range(0, N): for j in range(0, K): zero = np.complex128(0) for h in range(0, dim): zero += (obs[j, h] - sampl[i, h])**2 dist[j,i] = ma.sqrt(zero) if method == "city": for i in range(0, N): for j in range(0, K): zero = np.complex128(0) for h in range(0, dim): zero += abs(obs[j, h] - sampl[i, h]) dist[j,i] = ma.sqrt(zero) return dist
def order1_alt(self, ps, v0_in): fiber = self.fibers[0] alpha = np.complex128(fiber.alpha(self.wl)) gamma = np.complex128(fiber.gamma(self.wl)) betaw = fiber.beta(self.wl, ps.omega).astype(np.complex128) # r = odesolve.ode_rk45(df_nls_order1) r = integrate.ode(df_nls_order1) r.set_integrator("zvode", method="adams", nsteps=5000, atol=1e-12, rtol=1e-12) r.set_initial_value(v0_in, 0) r.set_f_params(alpha, gamma, betaw) # Solve Nz = 100 dz = fiber.length / (Nz - 1.0) self.O1_z = np.arange(0, Nz) * dz self.O1_y = np.zeros(v0_in.shape + (Nz,), np.complex_) # Solve to each step in the given range self.O1_y[..., 0] = v0_in for ii in xrange(1, Nz): y_out = r.integrate(self.O1_z[ii]) self.O1_y[..., ii] = y_out # Flag O(1) run as done self.O1_run_complete = True return ps, y_out
def test_dict_numpy_complex(self): x = {'foo': np.complex128(1.0 + 1.0j), 'bar': np.complex128(2.0 + 2.0j)} x_rec = self.encode_decode(x) tm.assert_dict_equal(x, x_rec) for key in x: tm.assert_class_equal(x[key], x_rec[key], obj="numpy complex128")
def PV(fns,n,p,cols,rows,bands): '''Return p-values for change indices R^ell_j''' j = np.float64(len(fns)) eps = sys.float_info.min k = 0.0; a = 0.0; rho = 0.0; xsi = 0.0; b = 0.0; zeta = 0.0 for fn in fns: result = getmat(fn,cols,rows,bands) if p==3: k1,a1,rho1,xsi1,b1,zeta1 = result k1 = n*np.float64(k1) a1 = n*np.complex128(a1) rho1 = n*np.complex128(rho1) xsi1 = n*np.float64(xsi1) b1 = n*np.complex128(b1) zeta1 = n*np.float64(zeta1) k += k1; a += a1; rho += rho1; xsi += xsi1; b += b1; zeta += zeta1 elif p==2: k1,a1,xsi1 = result k1 = n*np.float64(k1) a1 = n*np.complex128(a1) xsi1 = n*np.float64(xsi1) k += k1; a += a1; xsi += xsi1 elif p==1: k1 = n*np.float64(result[0]) k += k1 if p==3: detsumj = k*xsi*zeta + 2*np.real(a*b*np.conj(rho)) - xsi*(abs(rho)**2) - k*(abs(b)**2) - zeta*(abs(a)**2) k -= k1; a -= a1; rho -= rho1; xsi -= xsi1; b -= b1; zeta -= zeta1 detsumj1 = k*xsi*zeta + 2*np.real(a*b*np.conj(rho)) - xsi*(abs(rho)**2) - k*(abs(b)**2) - zeta*(abs(a)**2) detj = k1*xsi1*zeta1 + 2*np.real(a1*b1*np.conj(rho1)) - xsi1*(abs(rho1)**2) - k1*(abs(b1)**2) - zeta1*(abs(a1)**2) elif p==2: detsumj = k*xsi - abs(a)**2 k -= k1; a -= a1; xsi -= xsi1 detsumj1 = k*xsi - abs(a)**2 detj = k1*xsi1 - abs(a1)**2 elif p==1: detsumj = k k -= k1 detsumj1 = k detj = k1 detsumj = np.nan_to_num(detsumj) detsumj = np.where(detsumj <= eps,eps,detsumj) logdetsumj = np.log(detsumj) detsumj1 = np.nan_to_num(detsumj1) detsumj1 = np.where(detsumj1 <= eps,eps,detsumj1) logdetsumj1 = np.log(detsumj1) detj = np.nan_to_num(detj) detj = np.where(detj <= eps,eps,detj) logdetj = np.log(detj) # test statistic lnRj = n*( p*( j*np.log(j)-(j-1)*np.log(j-1.) ) + (j-1)*logdetsumj1 + logdetj - j*logdetsumj ) f =p**2 rhoj = 1 - (2.*p**2 - 1)*(1. + 1./(j*(j-1)))/(6.*p*n) omega2j = -(p*p/4.)*(1.-1./rhoj)**2 + (1./(24.*n*n))*p*p*(p*p-1)*(1+(2.*j-1)/(j*(j-1))**2)/rhoj**2 # return p-values Z = -2*rhoj*lnRj return 1.0 - ((1.-omega2j)*stats.chi2.cdf(Z,[f])+omega2j*stats.chi2.cdf(Z,[f+4]))
def add_vdot(M, v, out, beta=0.0, transM='N', handle=None): if handle is None: handle = scm._global_cublas_handle assert M.strides[1] <= M.strides[0], 'only C-order arrays supported' transM = transM.lower() if transM == 'n': trans = 't' m = M.shape[1] n = M.shape[0] alpha = 1.0 lda = M.strides[0] // M.dtype.itemsize if v.shape[0] != M.shape[1] or out.shape[0] != M.shape[0]: raise ValueError('dimension mismatch: %s %s %s' % (M.shape, v.shape, out.shape)) elif transM == 't': trans = 'n' m = M.shape[1] n = M.shape[0] alpha = 1.0 lda = M.strides[0] // M.dtype.itemsize if v.shape[0] != M.shape[0] or out.shape[0] != M.shape[1]: raise ValueError('dimension mismatch: %s %s %s' % (M.shape, v.shape, out.shape)) else: raise ValueError('transM must be n or t') if (M.dtype == np.complex64 and v.dtype == np.complex64): cublas_func = scikits.cuda.cublas.cublasCgemv alpha = np.complex64(alpha) beta = np.complex64(beta) elif (M.dtype == np.float32 and v.dtype == np.float32): cublas_func = scikits.cuda.cublas.cublasSgemv alpha = np.float32(alpha) beta = np.float32(beta) elif (M.dtype == np.complex128 and v.dtype == np.complex128): cublas_func = scikits.cuda.cublas.cublasZgemv alpha = np.complex128(alpha) beta = np.complex128(beta) elif (M.dtype == np.float64 and v.dtype == np.float64): cublas_func = scikits.cuda.cublas.cublasDgemv alpha = np.float64(alpha) beta = np.float64(beta) else: raise ValueError('unsupported combination of input types') incx = 1 incy = 1 cublas_func(handle, trans, m, n, alpha, M.gpudata, lda, v.gpudata, incx, beta, out.gpudata, incy)
def test_dict_numpy_complex(self): x = {b'foo': np.complex128(1.0+1.0j), b'bar': np.complex128(2.0+2.0j)} x_rec = self.encode_decode(x) assert_array_equal(sorted(x.values(), key=np.linalg.norm), sorted(x_rec.values(), key=np.linalg.norm)) assert_array_equal([type(e) for e in sorted(x.values(), key=np.linalg.norm)], [type(e) for e in sorted(x_rec.values(), key=np.linalg.norm)]) assert_array_equal(sorted(x.keys()), sorted(x_rec.keys())) assert_array_equal([type(e) for e in sorted(x.keys())], [type(e) for e in sorted(x_rec.keys())])
def test_legacy_mode_scalars(self): # in legacy mode, str of floats get truncated, and complex scalars # use * for non-finite imaginary part np.set_printoptions(legacy='1.13') assert_equal(str(np.float64(1.123456789123456789)), '1.12345678912') assert_equal(str(np.complex128(complex(1, np.nan))), '(1+nan*j)') np.set_printoptions(legacy=False) assert_equal(str(np.float64(1.123456789123456789)), '1.1234567891234568') assert_equal(str(np.complex128(complex(1, np.nan))), '(1+nanj)')
def test_cublasXtCgemm(self): a = (np.random.rand(4, 4)+1j*np.random.rand(4, 4)).astype(np.complex128) b = (np.random.rand(4, 4)+1j*np.random.rand(4, 4)).astype(np.complex128) c = np.zeros((4, 4), np.complex128) cublasxt.cublasXtDeviceSelect(handle, 2, np.array([0, 1], np.int32).ctypes.data) cublasxt.cublasXtCgemm(self.handle, cublasxt._CUBLAS_OP['N'], cublasxt._CUBLAS_OP['N'], 4, 4, 4, np.complex128(1.0), a.ctypes.data, 4, b.ctypes.data, 4, np.complex128(0.0), c.ctypes.data, 4) np.allclose(np.dot(b.T, a.T).T, c)
def test_prod_sum_fn (self): compiled_prod_sum_fn = self.jit(argtypes = [complex128, complex128, complex128], restype = complex128)(prod_sum_fn) rng = numpy.arange(-1., 1.1, 0.5) for ar, ai, xr, xi, br, bi in itertools.product(rng, rng, rng, rng, rng, rng): a = numpy.complex128(ar + ai * 1j) x = numpy.complex128(xr + xi * 1j) b = numpy.complex128(br + bi * 1j) self.assertEqual(prod_sum_fn(a, x, b), compiled_prod_sum_fn(a, x, b))
def test_cublasXtCgemm(self): a = (np.random.rand(4, 4)+1j*np.random.rand(4, 4)).astype(np.complex128) b = (np.random.rand(4, 4)+1j*np.random.rand(4, 4)).astype(np.complex128) c = np.zeros((4, 4), np.complex128) cublasxt.cublasXtCgemm(self.handle, 'N', 'N', 4, 4, 4, np.complex128(1.0), a.ctypes.data, 4, b.ctypes.data, 4, np.complex128(0.0), c.ctypes.data, 4) np.allclose(np.dot(b.T, a.T).T, c)
def test_prod_sum_fn (self): compiled_prod_sum_fn = numba_compile(arg_types = ['D', 'D', 'D'], ret_type = 'D')(prod_sum_fn) rng = numpy.arange(-1., 1.1, 0.5) for ar, ai, xr, xi, br, bi in itertools.product(rng, rng, rng, rng, rng, rng): a = numpy.complex128(ar + ai * 1j) x = numpy.complex128(xr + xi * 1j) b = numpy.complex128(br + bi * 1j) self.assertEqual(prod_sum_fn(a, x, b), compiled_prod_sum_fn(a, x, b))
def test_cublasZgemv(self): a = (np.random.rand(2, 3)+1j*np.random.rand(2, 3)).astype(np.complex128) x = (np.random.rand(3, 1)+1j*np.random.rand(3, 1)).astype(np.complex128) a_gpu = gpuarray.to_gpu(a.T.copy()) x_gpu = gpuarray.to_gpu(x) y_gpu = gpuarray.empty((2, 1), np.complex128) alpha = np.complex128(1.0) beta = np.complex128(0.0) cublas.cublasZgemv('n', 2, 3, alpha, a_gpu.gpudata, 2, x_gpu.gpudata, 1, beta, y_gpu.gpudata, 1) assert np.allclose(y_gpu.get(), np.dot(a, x))
def get_wavefunction(self, ibzk, n, check_focc=True, spin=0): if (self.calc.wfs.world.size == 1 or self.calc.wfs.gd.comm.size != 1 or self.calc.input_parameters['mode'] == 'lcao'): if not check_focc: return else: psit_G = self.calc.wfs.get_wave_function_array(n, ibzk, spin) if self.calc.wfs.world.size == 1: return np.complex128(psit_G) if self.calc.wfs.world.rank != 0: psit_G = self.calc.wfs.gd.empty(dtype=self.calc.wfs.dtype, global_array=True) self.calc.wfs.world.broadcast(psit_G, 0) return np.complex128(psit_G) else: # support ground state calculation with kpoint and band parallelization # but domain decomposition must = 1 kpt_rank, u = self.calc.wfs.kd.get_rank_and_index(0, ibzk) bzkpt_rank = self.kcomm.rank band_rank, myn = self.calc.wfs.bd.who_has(n) assert self.calc.wfs.gd.comm.size == 1 world_rank = (kpt_rank * self.calc.wfs.band_comm.size + band_rank) # in the following, kpt_rank is assigned to world_rank klist = np.array([world_rank, u, bzkpt_rank, myn]) klist_kcomm = np.zeros((self.kcomm.size, 4), dtype=int) self.kcomm.all_gather(klist, klist_kcomm) check_focc_global = np.zeros(self.kcomm.size, dtype=bool) self.kcomm.all_gather(np.array([check_focc]), check_focc_global) psit_G = self.calc.wfs.gd.empty(dtype=self.calc.wfs.dtype) for i in range(self.kcomm.size): if check_focc_global[i]: kpt_rank, u, bzkpt_rank, nlocal = klist_kcomm[i] if kpt_rank == bzkpt_rank: if rank == kpt_rank: psit_G = self.calc.wfs.kpt_u[u].psit_nG[nlocal] else: if rank == kpt_rank: world.send(self.calc.wfs.kpt_u[u].psit_nG[nlocal], bzkpt_rank, 1300+bzkpt_rank) if rank == bzkpt_rank: psit_G = self.calc.wfs.gd.empty(dtype=self.calc.wfs.dtype) world.receive(psit_G, kpt_rank, 1300+bzkpt_rank) self.wScomm.broadcast(psit_G, 0) return psit_G
def get_wavefunction(self, ibzk, n, check_focc=True, spin=0): if self.calc.wfs.kpt_comm.size != world.size or world.size == 1: if check_focc == False: return else: psit_G = self.calc.wfs.get_wave_function_array(n, ibzk, spin) if self.calc.wfs.world.size == 1: return np.complex128(psit_G) if not self.calc.wfs.world.rank == 0: psit_G = self.calc.wfs.gd.empty(dtype=self.calc.wfs.dtype, global_array=True) self.calc.wfs.world.broadcast(psit_G, 0) return np.complex128(psit_G) else: if self.nkpt % size != 0: raise ValueError('The number of kpoints should be divided by the number of cpus for no wfs dumping mode ! ') # support ground state calculation with only kpoint parallelization kpt_rank, u = self.calc.wfs.kd.get_rank_and_index(0, ibzk) bzkpt_rank = rank klist = np.array([kpt_rank, u, bzkpt_rank, n]) klist_kcomm = np.zeros((self.kcomm.size, 4), dtype=int) self.kcomm.all_gather(klist, klist_kcomm) check_focc_global = np.zeros(self.kcomm.size, dtype=bool) self.kcomm.all_gather(np.array([check_focc]), check_focc_global) psit_G = self.calc.wfs.gd.empty(dtype=self.calc.wfs.dtype) for i in range(self.kcomm.size): if check_focc_global[i] == True: kpt_rank, u, bzkpt_rank, nlocal = klist_kcomm[i] if kpt_rank == bzkpt_rank: if rank == kpt_rank: psit_G = self.calc.wfs.kpt_u[u].psit_nG[nlocal] else: if rank == kpt_rank: world.send(self.calc.wfs.kpt_u[u].psit_nG[nlocal], bzkpt_rank, 1300+bzkpt_rank) if rank == bzkpt_rank: psit_G = self.calc.wfs.gd.empty(dtype=self.calc.wfs.dtype) world.receive(psit_G, kpt_rank, 1300+bzkpt_rank) self.wScomm.broadcast(psit_G, 0) return psit_G
def test_cublasZgeam(self): a = (np.random.rand(2, 3)+1j*np.random.rand(2, 3)).astype(np.complex128) b = (np.random.rand(2, 3)+1j*np.random.rand(2, 3)).astype(np.complex128) a_gpu = gpuarray.to_gpu(a.copy()) b_gpu = gpuarray.to_gpu(b.copy()) c_gpu = gpuarray.zeros_like(a_gpu) alpha = np.complex128(np.random.rand()+1j*np.random.rand()) beta = np.complex128(np.random.rand()+1j*np.random.rand()) cublas.cublasZgeam(self.cublas_handle, 'n', 'n', 2, 3, alpha, a_gpu.gpudata, 2, beta, b_gpu.gpudata, 2, c_gpu.gpudata, 2) assert np.allclose(c_gpu.get(), alpha*a+beta*b)
def test_different_dtypes_fail(self): in_shape = self.input_shapes["2d"] out_shape = self.output_shapes["2d"] axes = (-1,) a, b = self.create_test_arrays(in_shape, out_shape) a_ = numpy.complex64(a) b_ = numpy.complex128(b) self.assertRaisesRegex(ValueError, "Invalid scheme", FFTW, *(a_, b_)) a_ = numpy.complex128(a) b_ = numpy.complex64(b) self.assertRaisesRegex(ValueError, "Invalid scheme", FFTW, *(a_, b_))
def test32_1 (): rng = numpy.arange(-1., 1.0000001, 0.5) for ar, ai, xr, xi, br, bi in itertools.product(rng, repeat = 6): # So the call to prod_sum_fn() will corrupt the stack on a # 32-bit system, causing the next expression to generate an # invalid multiply. I assume calling prod_sum_fn_wrap() on a # 64-bit system will also corrupt the stack?! ai * 1j a = numpy.complex128(ar + ai * 1j) x = numpy.complex128(xr + xi * 1j) b = numpy.complex128(br + bi * 1j) print "a = %r, x = %r, b = %r, a * x + b = %r, => %r" % ( a, x, b, a * x + b, prod_sum_fn_wrap(a, x, b)) print prod_sum_fn_wrap(a, x, b)
def test_prod_sum_fn (self): if self.skip: raise unittest.SkipTest( 'Complex return values not supported on 32-bit systems.') compiled_prod_sum_fn = self.jit(argtypes = [complex128, complex128, complex128], restype = complex128)(prod_sum_fn) rng = numpy.arange(-1., 1.1, 0.5) for ar, ai, xr, xi, br, bi in itertools.product(rng, rng, rng, rng, rng, rng): a = numpy.complex128(ar + ai * 1j) x = numpy.complex128(xr + xi * 1j) b = numpy.complex128(br + bi * 1j) self.assertEqual(prod_sum_fn(a, x, b), compiled_prod_sum_fn(a, x, b))
def tfsf_field(fw, w_dt, m, n): x = 2 * np.sin(w_dt / 2) #k_dx = 2 * np.arcsin((-1 <= x) * (x <= 1) * x) k_dx = 2 * np.arcsin( np.complex128(x) ) fxt = np.fft.irfft(fw[:] * np.exp(- 1j * k_dx[:] * m) * np.exp(- 1j * w_dt[:] * n)) return fxt
def test_cublasZscal(self): x = (np.random.rand(5)+1j*np.random.rand(5)).astype(np.complex128) x_gpu = gpuarray.to_gpu(x) alpha = np.complex128(np.random.rand()+1j*np.random.rand()) cublas.cublasZscal(self.cublas_handle, x_gpu.size, alpha, x_gpu.gpudata, 1) assert np.allclose(x_gpu.get(), alpha*x)
def logscale_spec(spec, sr=44100, factor=20.0): """ Scale frequency axis logarithmically. """ timebins, freqbins = _np.shape(spec) scale = _np.linspace(0, 1, freqbins) ** factor scale *= (freqbins - 1) / max(scale) scale = _np.unique(_np.round(scale)) # create spectrogram with new freq bins newspec = _np.complex128(_np.zeros([timebins, scale.size])) for i in range(0, scale.size): if i == scale.size - 1: newspec[:, i] = _np.sum(spec[:, scale[i] :], axis=1) else: newspec[:, i] = _np.sum(spec[:, scale[i] : scale[i + 1]], axis=1) # list center freq of bins allfreqs = _np.abs(_np.fft.fftfreq(freqbins * 2, 1.0 / sr)[: freqbins + 1]) freqs = [] for i in range(0, scale.size): if i == scale.size - 1: freqs += [_np.mean(allfreqs[scale[i] :])] else: freqs += [_np.mean(allfreqs[scale[i] : scale[i + 1]])] return newspec, freqs
def test_is_number(self): self.assertTrue(is_number(True)) self.assertTrue(is_number(1)) self.assertTrue(is_number(1.1)) self.assertTrue(is_number(1 + 3j)) self.assertTrue(is_number(np.bool(False))) self.assertTrue(is_number(np.int64(1))) self.assertTrue(is_number(np.float64(1.1))) self.assertTrue(is_number(np.complex128(1 + 3j))) self.assertTrue(is_number(np.nan)) self.assertFalse(is_number(None)) self.assertFalse(is_number('x')) self.assertFalse(is_number(datetime(2011, 1, 1))) self.assertFalse(is_number(np.datetime64('2011-01-01'))) self.assertFalse(is_number(Timestamp('2011-01-01'))) self.assertFalse(is_number(Timestamp('2011-01-01', tz='US/Eastern'))) self.assertFalse(is_number(timedelta(1000))) self.assertFalse(is_number(Timedelta('1 days'))) # questionable self.assertFalse(is_number(np.bool_(False))) self.assertTrue(is_number(np.timedelta64(1, 'D')))
def test_compile_helperlib(self): with self.check_cc_compiled(self._test_module.cc_helperlib) as lib: res = lib.power(2, 7) self.assertPreciseEqual(res, 128) for val in (-1, -1 + 0j, np.complex128(-1)): res = lib.sqrt(val) self.assertPreciseEqual(res, 1j) for val in (4, 4.0, np.float64(4)): res = lib.np_sqrt(val) self.assertPreciseEqual(res, 2.0) res = lib.spacing(1.0) self.assertPreciseEqual(res, 2**-52) # Implicit seeding at startup should guarantee a non-pathological # start state. self.assertNotEqual(lib.random(-1), lib.random(-1)) res = lib.random(42) expected = np.random.RandomState(42).random_sample() self.assertPreciseEqual(res, expected) res = lib.size(np.float64([0] * 3)) self.assertPreciseEqual(res, 3) code = """if 1: from numpy.testing import assert_equal, assert_allclose res = lib.power(2, 7) assert res == 128 res = lib.random(42) assert_allclose(res, %(expected)s) res = lib.spacing(1.0) assert_allclose(res, 2**-52) """ % {'expected': expected} self.check_cc_compiled_in_subprocess(lib, code)
def Xfft_SNR(X_fft, Y, crop, SAMPLES): snr_t = SNR_wrapper(X_fft, Y[:crop], 256, SAMPLES, np.complex128, 10) # filtering if abs value is bigger than ... filter = np.abs(snr_t) < 3 X_fft_filtered = X_fft X_fft_filtered[:, filter] = np.complex128(0)
def assemble_to(self, asm_obj, val, iels, mode='vector', diff_var=None): """ Assemble the results of term evaluation. For standard terms, assemble the values in `val` corresponding to elements/cells `iels` into a vector or a CSR sparse matrix `asm_obj`, depending on `mode`. For terms with a dynamic connectivity (e.g. contact terms), in `'matrix'` mode, return the extra COO sparse matrix instead. The extra matrix has to be added to the global matrix by the caller. By default, this is done in :func:`Equations.evaluate() <sfepy.discrete.equations.Equations.evaluate()>`. """ import sfepy.discrete.common.extmods.assemble as asm vvar = self.get_virtual_variable() dc_type = self.get_dof_conn_type() extra = None if mode == 'vector': if asm_obj.dtype == nm.float64: assemble = asm.assemble_vector else: assert_(asm_obj.dtype == nm.complex128) assemble = asm.assemble_vector_complex for ii in range(len(val)): if not (val[ii].dtype == nm.complex128): val[ii] = nm.complex128(val[ii]) if not isinstance(val, tuple): dc = vvar.get_dof_conn(dc_type) assert_(val.shape[2] == dc.shape[1]) assemble(asm_obj, val, iels, 1.0, dc) else: vals, rows, var = val if var.eq_map is not None: eq = var.eq_map.eq rows = eq[rows] active = (rows >= 0) vals, rows = vals[active], rows[active] # Assumes no repeated indices in rows! asm_obj[rows] += vals elif mode == 'matrix': if asm_obj.dtype == nm.float64: assemble = asm.assemble_matrix else: assert_(asm_obj.dtype == nm.complex128) assemble = asm.assemble_matrix_complex svar = diff_var tmd = (asm_obj.data, asm_obj.indptr, asm_obj.indices) if ((asm_obj.dtype == nm.complex128) and (val.dtype == nm.float64)): val = val.astype(nm.complex128) sign = 1.0 if self.arg_derivatives[svar.name]: if not self.is_quasistatic or (self.step > 0): sign *= 1.0 / self.dt else: sign = 0.0 if not isinstance(val, tuple): rdc = vvar.get_dof_conn(dc_type) is_trace = self.arg_traces[svar.name] trace_region = self.arg_trace_regions[svar.name] cdc = svar.get_dof_conn(dc_type, is_trace, trace_region) assert_(val.shape[2:] == (rdc.shape[1], cdc.shape[1])) assemble(tmd[0], tmd[1], tmd[2], val, iels, sign, rdc, cdc) else: from scipy.sparse import coo_matrix vals, rows, cols, rvar, cvar = val if rvar.eq_map is not None: req, ceq = rvar.eq_map.eq, cvar.eq_map.eq rows, cols = req[rows], ceq[cols] active = (rows >= 0) & (cols >= 0) vals, rows, cols = vals[active], rows[active], cols[active] extra = coo_matrix((sign * vals, (rows, cols)), shape=asm_obj.shape) else: raise ValueError('unknown assembling mode! (%s)' % mode) return extra
def kernel_matrix_sparse_opt(Nx, Nz, Npml, Cpml, omega, delta, rho, v): # propriedades indiretas b = 1. / (rho) K = rho * (v**2) imag = 1j csix = np.zeros((Nx, 1), dtype=np.complex128) for i in range(0, Nx): if (Npml > 1 and i < Npml): # dentro da primeira camada de absorcao vertical gammax = Cpml * np.cos(np.pi / 2.0 * (i - 1) / (Npml - 1)) elif (Npml > 1 and i > (Nx - Npml)): # dentro da segunda camada de absorcao vertical gammax = Cpml * np.cos(np.pi / 2.0 * (Nx - i) / (Npml - 1)) else: # dentro do modelo gammax = 0.0 csix[i] = 1.0 + imag * gammax / omega csiz = np.zeros((Nx, 1), dtype=np.complex128) for k in range(0, Nz): # so tem um else se so tiver a camada inferior if (Npml > 1 and k > (Nz - Npml)): # dentro da segunda camada de absorcao horizontal (leito) gammaz = Cpml * np.cos(np.pi / 2.0 * (Nz - k) / (Npml - 1)) elif (Npml > 1 and k < Npml): # dentro da primeira camada de absorcao vertical (topo) gammaz = Cpml * np.cos(np.pi / 2.0 * (k - 1) / (Npml - 1)) else: # dentro do modelo gammaz = 0.0 csiz[k] = 1.0 + imag * gammaz / omega from scipy.sparse import csr_matrix row = [] col = [] data = [] # no interno - nao tem c.c. aplicada for i in range(1, Nx - 1): for j in range(1, Nz - 1): linha = (i) * Nz + j #numero do noh ij # ordem das colunas, da menor p maior: col4, col2, col1, col3, col5 col1 = linha # corresponde ao noh i,k #numero do noh ij col2 = linha - 1 # corresponde ao noh i-1,k #numero do noh da esquerda col3 = linha + 1 # corresponde ao noh i+1,k #numero do noh da direita col4 = linha - Nz # corresponde ao noh i,k-1 #numero do noh de cima col5 = linha + Nz # corresponde ao noh i,k+1 #numero do noh de baixo # if(i != 0 and i != Nx-1 and j != 0 and j != Nz-1 ): # staggered grid five point stencil (hustedt et al., 2004 equacao 8) c2 = (1.0 / (csiz[j] * delta**2)) * (b[i, j] + b[i - 1, j]) / ( csiz[j] + csiz[j - 1]) c3 = (1.0 / (csiz[j] * delta**2)) * (b[i, j] + b[i + 1, j]) / ( csiz[j] + csiz[j + 1]) c4 = (1.0 / (csix[i] * delta**2)) * (b[i, j] + b[i, j - 1]) / ( csix[i] + csix[i - 1]) c5 = (1.0 / (csix[i] * delta**2)) * (b[i, j] + b[i, j + 1]) / ( csix[i] + csix[i + 1]) c1 = omega**2 / K[i, j] - c2 - c3 - c4 - c5 row.append(linha) col.append(col1) data.append(np.complex128(c1)) row.append(linha) col.append(col2) data.append(np.complex128(c2)) row.append(linha) col.append(col3) data.append(np.complex128(c3)) row.append(linha) col.append(col4) data.append(np.complex128(c4)) row.append(linha) col.append(col5) data.append(np.complex128(c5)) # for i in range(0,Nx): for j in range(0, Nz): i = 0 linha = (i) * Nz + j #numero do noh ij # ordem das colunas, da menor p maior: col4, col2, col1, col3, col5 col1 = linha # corresponde ao noh i,k #numero do noh ij # col2 = linha - 1 # corresponde ao noh i-1,k #numero do noh da esquerda # col3 = linha + 1 # corresponde ao noh i+1,k #numero do noh da direita # col4 = linha - Nz # corresponde ao noh i,k-1 #numero do noh de cima col5 = linha + Nz # corresponde ao noh i,k+1 #numero do noh de baixo # borda esquerda # if(i == 0): c1 = -(1.0 / delta + 1j * omega / np.sqrt(K[i, j] * b[i, j])) c5 = +(1.0 / delta) LB = c1 LBI = c5 row.append(linha) col.append(col1) data.append([np.complex128(LB)]) row.append(linha) col.append(col5) data.append([np.complex128(LBI)]) # for j in range(0,Nz): i = Nx - 1 linha = (i) * Nz + j #numero do noh ij # ordem das colunas, da menor p maior: col4, col2, col1, col3, col5 col1 = linha # corresponde ao noh i,k #numero do noh ij # col2 = linha - 1 # corresponde ao noh i-1,k #numero do noh da esquerda # col3 = linha + 1 # corresponde ao noh i+1,k #numero do noh da direita col4 = linha - Nz # corresponde ao noh i,k-1 #numero do noh de cima # col5 = linha + Nz # corresponde ao noh i,k+1 #numero do noh de baixo # borda direita # elif(i == Nx-1): c1 = +(1.0 / delta + 1j * omega / np.sqrt(K[i, j] * b[i, j])) c4 = -(1.0 / delta) RB = c1 RBI = c4 row.append(linha) col.append(col1) data.append([np.complex128(RB)]) row.append(linha) col.append(col4) data.append([np.complex128(RBI)]) for i in range(1, Nx - 1): j = 0 linha = (i) * Nz + j #numero do noh ij # ordem das colunas, da menor p maior: col4, col2, col1, col3, col5 col1 = linha # corresponde ao noh i,k #numero do noh ij # col2 = linha - 1 # corresponde ao noh i-1,k #numero do noh da esquerda col3 = linha + 1 # corresponde ao noh i+1,k #numero do noh da direita # col4 = linha - Nz # corresponde ao noh i,k-1 #numero do noh de cima # col5 = linha + Nz # corresponde ao noh i,k+1 #numero do noh de baixo # topo # elif(j == 0): c1 = -(1.0 / delta + 1j * omega / np.sqrt(K[i, j] * b[i, j])) c3 = +(1.0 / delta) FS = c1 FSI = c3 # FS = 1.0 # FSI = 1.0 row.append(linha) col.append(col1) data.append([np.complex128(FS)]) row.append(linha) col.append(col3) data.append([np.complex128(FSI)]) # for i in range(1,Nx-1): j = Nz - 1 linha = (i) * Nz + j #numero do noh ij # ordem das colunas, da menor p maior: col4, col2, col1, col3, col5 col1 = linha # corresponde ao noh i,k #numero do noh ij col2 = linha - 1 # corresponde ao noh i-1,k #numero do noh da esquerda # col3 = linha + 1 # corresponde ao noh i+1,k #numero do noh da direita # col4 = linha - Nz # corresponde ao noh i,k-1 #numero do noh de cima # col5 = linha + Nz # corresponde ao noh i,k+1 #numero do noh de baixo # fundo # elif(j == Nz-1): c1 = +(1.0 / delta + 1j * omega / np.sqrt(K[i, j] * b[i, j])) c2 = -(1.0 / delta) BB = c1 BBI = c2 row.append(linha) col.append(col1) data.append([np.complex128(BB)]) row.append(linha) col.append(col2) data.append([np.complex128(BBI)]) # for i in range(0,Nx): # # for j in range(0,Nz): # # linha = (i)*Nz + j #numero do noh ij # # # ordem das colunas, da menor p maior: col4, col2, col1, col3, col5 # col1 = linha # corresponde ao noh i,k #numero do noh ij # col2 = linha - 1 # corresponde ao noh i-1,k #numero do noh da esquerda # col3 = linha + 1 # corresponde ao noh i+1,k #numero do noh da direita # col4 = linha - Nz # corresponde ao noh i,k-1 #numero do noh de cima # col5 = linha + Nz # corresponde ao noh i,k+1 #numero do noh de baixo # # # borda esquerda # if(i == 0): # # c1 = -(1.0/delta + 1j * omega/np.sqrt(K[i,j]*b[i,j])) # c5 = +(1.0/delta) # # LB = c1 # LBI = c5 # # row.append(linha) # col.append(col1) # data.append([np.complex128(LB)]) # # row.append(linha) # col.append(col3) # data.append([np.complex128(LBI)]) # # # # borda direita # elif(i == Nx-1): # # c1 = +(1.0/delta + 1j * omega/np.sqrt(K[i,j]*b[i,j])) # c4 = -(1.0/delta) # # RB = c1 # RBI = c4 # # # row.append(linha) # col.append(col1) # data.append([np.complex128(RB)]) # # row.append(linha) # col.append(col2) # data.append([np.complex128(RBI)]) # # # # # topo # elif(j == 0): # # c1 = -(1.0/delta + 1j * omega/np.sqrt(K[i,j]*b[i,j])) # c3 = +(1.0/delta) # # FS = c1 # FSI = c3 # ## FS = 1.0 ## FSI = 1.0 # # row.append(linha) # col.append(col1) # data.append([np.complex128(FS)]) # # row.append(linha) # col.append(col5) # data.append([np.complex128(FSI)]) # # # # # fundo # elif(j == Nz-1): # # c1 = +(1.0/delta + 1j * omega/np.sqrt(K[i,j]*b[i,j])) # c2 = -(1.0/delta) # # BB = c1 # BBI = c2 # # # row.append(linha) # col.append(col1) # data.append([np.complex128(BB)]) # # row.append(linha) # col.append(col4) # data.append([np.complex128(BBI)]) # row = np.array(row) # print(row) col = np.array(col) # print(col) data = np.asarray(data) data = data.ravel() # print(data) Csp = csr_matrix((data, (row, col))) # Csp_ver = csr_matrix((data, (row, col)), shape=(Nx*Nz,Nx*Nz)).toarray() # print(Csp_ver) return Csp
def test_list_mixed(self): x = [1.0, np.float32(3.5), np.complex128(4.25), b'foo'] x_rec = self.encode_decode(x) assert_array_equal(x, x_rec) assert_array_equal([type(e) for e in x], [type(e) for e in x_rec])
def dot_batch_nocheck(a_arr_gpu, b_arr_gpu, c_arr_gpu, a_ptr_gpu, b_ptr_gpu, c_ptr_gpu, transa='N', transb='N', a=1, b=1, handle=None): """ Implementation of batched dot products using cuda. Parameters ---------- a_arr_gpu : list of pycuda.gpuarray.GPUArray Input array. b_arr_gpu : list of pycuda.gpuarray.GPUArray Input array. c_arr_gpu : list of pycuda.gpuarray.GPUArray Input/Output array. a_ptr_gpu : pycuda.gpuarray.GPUArray Array of pointers to arrays in a_arr_gpu b_ptr_gpu : pycuda.gpuarray.GPUArray Array of pointers to arrays in b_arr_gpu c_ptr_gpu : pycuda.gpuarray.GPUArray Array of pointers to arrays in c_arr_gpu transa : char If 'T', compute the product of the transpose of `a_arr_gpu[i]`. If 'C', compute the product of the Hermitian of `a_arr_gpu[i]`. transb : char If 'T', compute the product of the transpose of `b_arr_gpu[i]`. If 'C', compute the product of the Hermitian of `b_arr_gpu[i]`. handle : int CUBLAS context. If no context is specified, the default handle from `scikits.cuda.misc._global_cublas_handle` is used. Returns ------- None. Output is stored into the arrays in c_arr_gpu Notes ----- The input matrices must all contain elements of the same data type. All matrics in a list must have same size. Examples -------- >>> import pycuda.driver >>> import pycuda.autoinit >>> from pycuda import gpuarray >>> import numpy as np >>> from scikits.cuda import linalg >>> linalg.init() >>> a_arr = [np.asarray(np.random.rand(4, 2), np.float32) for i in range(10)] >>> b_arr = [np.asarray(np.random.rand(2, 2), np.float32) for i in range(10)] >>> c_arr = [np.asarray(np.random.rand(4, 2), np.float32) for i in range(10)] >>> a_arr_gpu = [gpuarray.to_gpu(a_gpu) for a_gpu in a_arr] >>> b_arr_gpu = [gpuarray.to_gpu(b_gpu) for b_gpu in b_arr] >>> c_arr_gpu = [gpuarray.to_gpu(c_gpu) for c_gpu in c_arr] >>> a_ptr_gpu = gpuarray.to_gpu(np.asarray([int(a_gpu.gpudata) for a_gpu in a_arr_gpu])) >>> b_ptr_gpu = gpuarray.to_gpu(np.asarray([int(b_gpu.gpudata) for b_gpu in b_arr_gpu])) >>> c_ptr_gpu = gpuarray.to_gpu(np.asarray([int(c_gpu.gpudata) for c_gpu in c_arr_gpu])) >>> linalg.dot_batch_nocheck(a_arr_gpu, b_arr_gpu, c_arr_gpu, a_ptr_gpu, b_ptr_gpu, c_ptr_gpu) >>> for i in range(10): ... print np.allclose(np.dot(a_arr[i], b_arr[i]) + c_arr[i], c_arr_gpu[i].get()) ... True True True True True True True True True True >>> """ if handle is None: handle = misc._global_cublas_handle N = len(a_arr_gpu) a_shape = a_arr_gpu[0].shape a_dtype = a_arr_gpu[0].dtype b_shape = b_arr_gpu[0].shape b_dtype = b_arr_gpu[0].dtype c_shape = c_arr_gpu[0].shape c_dtype = c_arr_gpu[0].dtype for i in range(N): assert a_arr_gpu[i].shape == a_shape assert a_arr_gpu[i].dtype == a_dtype assert b_arr_gpu[i].shape == b_shape assert b_arr_gpu[i].dtype == b_dtype assert c_arr_gpu[i].shape == c_shape assert c_arr_gpu[i].dtype == c_dtype assert a_arr_gpu[i].flags.c_contiguous assert b_arr_gpu[i].flags.c_contiguous assert c_arr_gpu[i].flags.c_contiguous if len(a_shape) == 1: a_shape = (1, a_shape[0]) if len(b_shape) == 1: b_shape = (1, b_shape[0]) if len(c_shape) == 1: c_shape = (1, c_shape[0]) transa = lower(transa) transb = lower(transb) if transb in ['t', 'c']: m, k = b_shape elif transb in ['n']: k, m = b_shape else: raise ValueError('invalid value for transb') if transa in ['t', 'c']: l, n = a_shape elif transa in ['n']: n, l = a_shape else: raise ValueError('invalid value for transa') i, j = c_shape if l != k: raise ValueError('objects are not aligned') if i != n: raise ValueError('objects are not aligned') if j != m: raise ValueError('objects are not aligned') if transb == 'n': lda = max(1, m) else: lda = max(1, k) if transa == 'n': ldb = max(1, k) else: ldb = max(1, n) ldc = max(1, m) if (a_dtype == np.complex64 and b_dtype == np.complex64 \ and c_dtype == np.complex64): cublas_func = cublas.cublasCgemmBatched alpha = np.complex64(a) beta = np.complex64(b) elif (a_dtype == np.float32 and b_dtype == np.float32\ and c_dtype == np.float32): cublas_func = cublas.cublasSgemmBatched alpha = np.float32(a) beta = np.float32(b) elif (a_dtype == np.complex128 and b_dtype == np.complex128\ and c_dtype == np.complex128): cublas_func = cublas.cublasZgemmBatched alpha = np.complex128(a) beta = np.complex128(b) elif (a_dtype == np.float64 and b_dtype == np.float64\ and c_dtype == np.float64): cublas_func = cublas.cublasDgemmBatched alpha = np.float64(a) beta = np.float64(b) else: raise ValueError('unsupported combination of input types') cublas_func(handle, transb, transa, m, n, k, alpha, b_ptr_gpu.gpudata, lda, a_ptr_gpu.gpudata, ldb, beta, c_ptr_gpu.gpudata, ldc, N)
def geam(a_gpu, b_gpu, c_gpu, transa='N', transb='N', alpha=1, beta=1, handle=None): if handle is None: handle = misc._global_cublas_handle if len(a_gpu.shape) == 1 and len(b_gpu.shape) == 1: raise RuntimeError('dot products not supported for geam') # Get the shapes of the arguments (accounting for the # possibility that one of them may only have one dimension): a_shape = a_gpu.shape b_shape = b_gpu.shape if len(a_shape) == 1: a_shape = (1, a_shape[0]) if len(b_shape) == 1: b_shape = (1, b_shape[0]) # Perform matrix multiplication for 2D arrays: if (a_gpu.dtype == np.complex64 and b_gpu.dtype == np.complex64): cublas_func = cublas.cublasCgeam alpha = np.complex64(alpha) beta = np.complex64(beta) elif (a_gpu.dtype == np.float32 and b_gpu.dtype == np.float32): cublas_func = cublas.cublasSgeam alpha = np.float32(alpha) beta = np.float32(beta) elif (a_gpu.dtype == np.complex128 and b_gpu.dtype == np.complex128): cublas_func = cublas.cublasZgeam alpha = np.complex128(alpha) beta = np.complex128(beta) elif (a_gpu.dtype == np.float64 and b_gpu.dtype == np.float64): cublas_func = cublas.cublasDgeam alpha = np.float64(alpha) beta = np.float64(beta) else: raise ValueError('unsupported combination of input types') transa = transa.lower() transb = transb.lower() if a_gpu.flags.c_contiguous != b_gpu.flags.c_contiguous: raise ValueError('unsupported combination of input order') if a_gpu.flags.f_contiguous: if transa in ['t', 'c']: k, m = a_shape elif transa in ['n']: m, k = a_shape else: raise ValueError('invalid value for transa') if transb in ['t', 'c']: n, l = b_shape elif transb in ['n']: l, n = b_shape else: raise ValueError('invalid value for transb') if m != l or k != n: raise ValueError('objects are not aligned') lda = max(1, a_shape[0]) ldb = max(1, b_shape[0]) ldc = max(1, m) if c_gpu.shape != (m, n) or c_gpu.dtype != a_gpu.dtype: raise ValueError('invalid value for c_gpu') if a_gpu.flags.f_contiguous != c_gpu.flags.f_contiguous: raise ValueError('invalid order for c_gpu') cublas_func(handle, transa, transb, m, n, alpha, a_gpu.gpudata, lda, beta, b_gpu.gpudata, ldb, c_gpu.gpudata, ldc) else: if transb in ['t', 'c']: m, k = b_shape elif transb in ['n']: k, m = b_shape else: raise ValueError('invalid value for transb') if transa in ['t', 'c']: l, n = a_shape elif transa in ['n']: n, l = a_shape else: raise ValueError('invalid value for transa') if m != l or k != n: raise ValueError('objects are not aligned') lda = max(1, b_shape[1]) ldb = max(1, a_shape[1]) ldc = max(1, m) # Note that the desired shape of the output matrix is the transpose # of what CUBLAS assumes: if c_gpu.shape != (n, ldc) or c_gpu.dtype != a_gpu.dtype: raise ValueError('invalid value for c_gpu') if a_gpu.flags.f_contiguous != c_gpu.flags.f_contiguous: raise ValueError('invalid order for c_gpu') cublas_func(handle, transb, transa, m, n, alpha, a_gpu.gpudata, lda, beta, b_gpu.gpudata, ldb, c_gpu.gpudata, ldc)
), ( DimensionlessParticle, { "charge": (1 + 2j) * u.dimensionless_unscaled }, InvalidParticleError, ), (CustomParticle, { "charge": 5 + 2j }, InvalidParticleError), (CustomParticle, { "mass": 5 + 2j }, InvalidParticleError), (CustomParticle, { "charge": np.complex128(5 + 2j) }, InvalidParticleError), (CustomParticle, { "mass": np.complex128(5 + 2j) }, InvalidParticleError), (CustomParticle, { "mass": -1e-36 * u.kg }, InvalidParticleError), (CustomParticle, { "mass": "not a mass" }, InvalidParticleError), (CustomParticle, { "mass": "5.0 km" }, InvalidParticleError), (CustomParticle, { "mass": np.array([1, 1]) * u.kg
def main(_): # CHECK-LABEL: TEST: bitwise_not bool[7] # CHECK: mhlo.not # CHECK-SAME: tensor<7xi1> print_ir(np.empty([7], np.bool_))(lax.bitwise_not) # CHECK-LABEL: TEST: neg int8[] # CHECK: mhlo.negate # CHECK-SAME: tensor<i8> print_ir(np.int8(0))(lax.neg) # CHECK-LABEL: TEST: neg int16[0] # CHECK: mhlo.negate # CHECK-SAME: tensor<0xi16> print_ir(np.empty([0], np.int16))(lax.neg) # CHECK-LABEL: TEST: neg int32[2,3] # CHECK: mhlo.negate # CHECK-SAME: tensor<2x3xi32> print_ir(np.empty([2, 3], np.int32))(lax.neg) # CHECK-LABEL: TEST: neg int64[2,3,4] # CHECK: mhlo.negate # CHECK-SAME: tensor<2x3x4xi64> print_ir(np.empty([2, 3, 4], np.int64))(lax.neg) # CHECK-LABEL: TEST: add uint8[4,0,1] uint8[4,0,1] # CHECK: mhlo.add # CHECK-SAME: tensor<4x0x1xui8> print_ir(np.empty([4, 0, 1], np.uint8), np.empty([4, 0, 1], np.uint8))(lax.add) # CHECK-LABEL: TEST: add uint16[] uint16[] # CHECK: mhlo.add # CHECK-SAME: tensor<ui16> print_ir(np.uint16(0), np.uint16(0))(lax.add) # CHECK-LABEL: TEST: add uint32[] uint32[] # CHECK: mhlo.add # CHECK-SAME: tensor<ui32> print_ir(np.uint32(0), np.uint32(0))(lax.add) # CHECK-LABEL: TEST: add uint64[] uint64[] # CHECK: mhlo.add # CHECK-SAME: tensor<ui64> print_ir(np.uint64(0), np.uint64(0))(lax.add) # CHECK-LABEL: TEST: sin float16[] # CHECK: mhlo.sine # CHECK-SAME: tensor<f16> print_ir(np.float16(0))(lax.sin) # CHECK-LABEL: TEST: sin bfloat16[] # CHECK: mhlo.sine # CHECK-SAME: tensor<bf16> print_ir(jnp.bfloat16(0))(lax.sin) # CHECK-LABEL: TEST: sin float32[] # CHECK: mhlo.sine # CHECK-SAME: tensor<f32> print_ir(np.float32(0))(lax.sin) # CHECK-LABEL: TEST: sin float64[] # CHECK: mhlo.sine # CHECK-SAME: tensor<f64> print_ir(np.float64(0))(lax.sin) # CHECK-LABEL: TEST: cos complex64[] # CHECK: mhlo.cosine # CHECK-SAME: tensor<complex<f32>> print_ir(np.complex64(0))(lax.cos) # CHECK-LABEL: TEST: cos complex128[] # CHECK: mhlo.cosine # CHECK-SAME: tensor<complex<f64>> print_ir(np.complex128(0))(lax.cos)
def test_list_numpy_float_complex(self): x = [np.float32(np.random.rand()) for i in range(5)] + \ [np.complex128(np.random.rand() + 1j * np.random.rand()) for i in range(5)] x_rec = self.encode_decode(x) assert all(map(lambda x, y: x == y, x, x_rec)) and all( map(lambda x, y: type(x) == type(y), x, x_rec))
def spod( x, window="hamming", weight=None, noverlap=None, dt=1, mean=None, isreal=None, nt=None, conflvl=None, normvar=False, debug=0, lowmem=False, savefile=None, nmodes=None, savefreqs=None, ): """" Spectral proper orthogonal decomposition Parameters ---------- x : array or function object Data array whose first dimension is time, or function that retrieves one snapshot at a time like x(i). x(i), like x, can have any dimension. If x is a function, it is recommended to specify the total number of snaphots in nt (see below). If not specified, nt defaults to 10000. isreal should be specified if a two-sided spectrum is desired even though the data is real-valued, or if the data is initially real-valued, but complex-valued-valued for later snaphots. window : vector, int, or string, optional A temporal window. If WINDOW is a vector, X is divided into segments of the same length as WINDOW. Each segment is then weighted (pointwise multiplied) by WINDOW. If WINDOW is a scalar, a Hamming window of length WINDOW is used. If WINDOW is none or 'hamming', a Hamming window is used. weight : array, optional A spatial inner product weight. SPOD modes are optimally ranked and orthogonal at each frequency. WEIGHT must have the same spatial dimensions as x. noverlap : int, optional Number of snaptions to overlap consecutive blocks. noverlap defaults to 50% of the length of WINDOW if not specified. dt : float, optional Time step between consecutive snapshots to determine a physical frequency F. dt defaults to 1 if not specified. mean : array or string, optional A mean that is subtracted from each snapshot. If 'blockwise', the mean of each block is subtracted from itself. If x is a function the mean provided is a temporal mean. isreal : bool, optional Describes if x data is real. nt : int, optional Number of snapshots. If x is an array, this is determined from x dimensions. If x is a function, this defaults to 10000 if not specified. conflvl : bool or float, optional Calculate and return confidence interval levels of L (Lc). If True, the lower and upper 95% confidence levels of the j-th most energetic SPOD mode at the i-th frequency are returned in Lc(i,j,1) and Lc(i,j,2), respectively. If a float between 0 and 1, the conflvl*100% confidence interval of L is returned. A chi-squared distribution is used, i.e. we assume a standard normal distribution of the SPOD eigenvalues. Defaults to None/False. normvar : bool, optional Normalize each block by pointwise variance. Defaults to false. debug : {0, 1, 2}, optional Verbosity of output. 0 hides output. 1 shows some output, 2 shows all output. Defaults to 0. lowmem : bool, optional Specifies whether to use low-memory mode. If True, this stores the FFT blocks in a temporary file on disk, and also stores all returned quantities on disk, returning a file handle. Default is False, keeping everything in memory. If lowmem is True, savefile must be specified for the returned data. This mode requires the h5py package. savefile : string, optional Filename to which to save the results in HDF5 format. If lowmem is True, a handle for this file is returned. If False or None, the in-memory results are returned in a dictionary. If file exists, it is overwritten. Defaults to None/False. nmodes : int, optional Number of most energetic SPOD modes to be saved. Defaults to all modes. savefreqs: list of ints, optional List of frequency indices to calculate modes (P) and spectral energies (L). Meant to reduce size of data if not all frequences are needed. Defaults to all frequences. Returns ------- output A dictionary containing SPOD modes (P), modal energy spectra (L), and frequency vector (f). If conflvl is specified, confidence interval is returned (Lc). If lowmem is True, a file object is returned which points to the on-disk HDF5 dataset. """ # verbosity of output global verbosity verbosity = debug # In low memory mode, a save file must be specified if lowmem: assert savefile is not None, "savefile must be provided in lowmem mode" # Warn about overwriting file if savefile and os.path.exists(savefile): warn("%s will be overwritten" % savefile) # Get problem dimensions if isinstance(x, FunctionType) or callable(x): xfun = True if nt is None: warn( 'Please specify number of snapshots in "opts.nt". Trying to use default value of 10000 snapshots.' ) nt = 10000 x0 = x(0) sizex = np.shape(x0) xtype = x0.dtype nx = np.prod(sizex) dim = [nt] + list(sizex) else: xfun = False dim = np.shape(x) nt = dim[0] nx = np.prod(dim[1:]).astype(int) # Determine if data is real or complex if isreal is not None: isrealx = isreal elif not xfun: isrealx = np.isrealobj(x) else: isrealx = np.isrealobj(x0) del x0 # Fix datatype if not xfun: x = np.float64(x) if isrealx else np.complex128(x) # Parse parameters window, weight, noverlap, nDFT, nBlks, conflvl, nmodes = spod_parser( nt, nx, isrealx, window, weight, noverlap, conflvl, nmodes ) # determine correction for FFT window gain winWeight = 1.0 / np.mean(window) # Use data mean if not provided through opts['mean'] if type(mean) == str and mean == "blockwise": blk_mean = True else: blk_mean = False # Calculate mean if xfun: if (mean is not None) and (not blk_mean): x_mean = mean.flatten() mean_name = "user specified" elif blk_mean: mean_name = "blockwise mean" else: x_mean = 0 warn( "No mean subtracted. Consider providing long-time mean through opts['mean'] for better accuracy at low frequencies." ) mean_name = "0" else: if blk_mean: mean_name = "blockwise mean" else: x_mean = np.mean(x, axis=0).flatten() mean_name = "long-time (true) mean" printer("Mean : %s" % mean_name, 1) # obtain frequency axis if isrealx: f = np.arange(np.ceil(nDFT / 2) + 1) / dt / nDFT else: f = np.arange(nDFT) / dt / nDFT if np.mod(nDFT, 2) == 0: f[nDFT / 2 :] -= 1.0 / dt else: f[(nDFT + 1) / 2 :] -= 1.0 / dt nFreq = np.size(f) if savefreqs is None: savefreqs = np.arange(nFreq) # In low memory mode, open temporary file for Q_hat. Else, hold in memory. if lowmem: tempf = tempfile.TemporaryFile() tempfh5 = h5py.File(tempf, "a") Q_hat = tempfh5.create_dataset( "Q_hat", (nFreq, nx, nBlks), dtype=np.cdouble, compression="gzip" ) else: Q_hat = np.zeros((nFreq, nx, nBlks), dtype=np.cdouble) # loop over number of blocks and generate Fourier realizations printer("\nCalculating temporal DFT\n------------------------------------", 1) for iBlk in range(nBlks): offset = int(min(iBlk * (nDFT - noverlap) + nDFT, nt) - nDFT) timeIdx = np.arange(nDFT) + offset printer( "block %d / %d (%d:%d)" % (iBlk + 1, nBlks, timeIdx[0] + 1, timeIdx[-1] + 1), 2, ) # build present block if blk_mean: x_mean = 0 if xfun: Q_blk = np.zeros((nDFT, nx), dtype=xtype) for ti in timeIdx: Q_blk[ti - offset, :] = x(ti).flatten() - x_mean else: Q_blk = np.subtract(np.reshape(x[timeIdx, :], (nDFT, -1)), np.expand_dims(x_mean, 0)) # if block mean is to be subtracted, do it now that all data is collected if blk_mean: Q_blk = np.subtract(Q_blk, np.mean(Q_blk, axis=0, keepdims=True)) # normalize by pointwise variance if normvar: Q_var = np.sum( np.power(np.subtract(Q_blk, np.mean(Q_blk, axis=0, keepdims=True)), 2), axis=0, ) / (nDFT - 1) # address division-by-0 problem with NaNs eps = np.finfo(Q_var.dtype).eps Q_var[Q_var < 4 * eps] = 1 Q_blk = np.divide(Q_blk, Q_var) # window and Fourier transform block Q_blk = np.multiply(Q_blk, np.expand_dims(window, axis=1)) Q_blk_hat = (winWeight / nDFT) * scipy.fft.fft(Q_blk, axis=0, workers=-1) Q_blk_hat = Q_blk_hat[:nFreq, :] # correct Fourier coefficients for one-sided spectrum if isrealx: Q_blk_hat[1:-1, :] *= 2.0 # keep FFT blocks in memory Q_hat[:, :, iBlk] = Q_blk_hat # Dimensions of P pDim = [nFreq] + list(dim[1:]) + [nmodes] # In low memory mode, save L and P to files. Else, hold in memory. if lowmem: output = h5py.File(savefile, "a") if "L" in output: del output["L"] L = output.create_dataset("L", (nFreq, nBlks), dtype=np.double, compression="gzip") if nmodes > 0: if "P" in output: del output["P"] P = output.create_dataset("P", pDim, dtype=np.cdouble, compression="gzip") else: L = np.zeros((nFreq, nBlks), dtype=np.double) if nmodes > 0: P = np.zeros(pDim, dtype=np.cdouble) # loop over all frequencies and calculate SPOD printer("\nCalculating SPOD\n------------------------------------", 1) for iFreq in savefreqs: printer("frequency %d / %d (f=%g)" % (iFreq + 1, nFreq, f[iFreq]), 2) Q_hat_f = Q_hat[iFreq, :, :] M = ( np.matmul( np.conj(np.transpose(Q_hat_f)), np.multiply(Q_hat_f, np.expand_dims(weight, axis=1)), ) / nBlks ) if nmodes > 0: Lambda, Theta = scipy.linalg.eig(M) else: Lambda = scipy.linalg.eigvals(M) Lambda = np.abs(Lambda) idx = np.argsort(Lambda)[::-1] Lambda = Lambda[idx] if nmodes > 0: Theta = Theta[:, idx[:nmodes]] Psi = np.matmul( np.matmul(Q_hat_f, Theta), np.diag(np.reciprocal(np.lib.scimath.sqrt(Lambda[:nmodes])) / np.sqrt(nBlks)), ) P[iFreq, :] = Psi.reshape(pDim[1:]) # mode L[iFreq, :] = Lambda # energy distribution # Calculate confidence interval if conflvl: if lowmem: Lc = output.create_dataset( "Lc", shape=(nFreq, nBlks, 2), dtype=L.dtype, compression="gzip" ) else: Lc = np.zeros((nFreq, nBlks, 2), dtype=L.dtype) Lc[:, :, 0] = L * nBlks / gammaincinv(nBlks, conflvl) Lc[:, :, 1] = L * nBlks / gammaincinv(nBlks, 1.0 - conflvl) # If held in memory, create output dictionary if not lowmem: output = {"L": L, "f": f} if nmodes > 0: output["P"] = P if conflvl: output["Lc"] = Lc # Create output save file if savefile: if lowmem: tempfh5.close() tempf.close() if "f" in output: del output["f"] output.create_dataset("f", data=f, compression="gzip") else: with h5py.File(savefile, "a") as outputfile: for key, value in output.items(): if key in outputfile: del outputfile[key] outputfile.create_dataset(key, data=value, compression="gzip") # Return either in-memory or on-disk dictionary return output
def test_type_match_complex128(self): assert (self.converter.type_match(numpy.complex128(5. + 1j)))
def random_complex_nonzero(shape, eps=1e-4, rng=None): return np.complex128(random_nonzero(shape, eps, rng=rng))
def test_inline(self): a = numpy.complex128(1 + 1j) result = inline_tools.inline("return_val=1.0/a;", ['a']) assert (result == .5 - .5j)
converter = converter.vararray_type(field, converter, arraysize, config) return converter numpy_dtype_to_field_mapping = { np.float64().dtype.num: 'double', np.float32().dtype.num: 'float', np.bool_().dtype.num: 'bit', np.uint8().dtype.num: 'unsignedByte', np.int16().dtype.num: 'short', np.int32().dtype.num: 'int', np.int64().dtype.num: 'long', np.complex64().dtype.num: 'floatComplex', np.complex128().dtype.num: 'doubleComplex', np.unicode_().dtype.num: 'unicodeChar' } numpy_dtype_to_field_mapping[np.bytes_().dtype.num] = 'char' def _all_matching_dtype(column): first_dtype = False first_shape = () for x in column: if not isinstance(x, np.ndarray) or len(x) == 0: continue if first_dtype is False: first_dtype = x.dtype
def test_list_numpy_float_complex(self): x = [np.float32(np.random.rand()) for i in range(5)] + \ [np.complex128(np.random.rand()+1j*np.random.rand()) for i in range(5)] x_rec = self.encode_decode(x) assert_array_equal(x, x_rec) assert_array_equal([type(e) for e in x], [type(e) for e in x_rec])
def test_complex64(self): s = readsav(path.join(DATA_PATH, 'scalar_complex64.sav'), verbose=False) assert_identical(s.c64, np.complex128(1.1987253647623157e+112-5.1987258887729157e+307j))
def zpaxz(self, z, a, x): arr_size = x.size WGS, WGS_tot = self.get_wgs(arr_size) self._zpaxz_c2c_knl(self.queue, (WGS_tot, ), (WGS, ), np.complex128(a), x.data, z.data, np.uint32(arr_size)).wait()
def test_pointers(self): s = readsav(path.join(DATA_PATH, 'scalar_heap_pointer.sav'), verbose=False) assert_identical(s.c64_pointer1, np.complex128(1.1987253647623157e+112-5.1987258887729157e+307j)) assert_identical(s.c64_pointer2, np.complex128(1.1987253647623157e+112-5.1987258887729157e+307j)) assert_true(s.c64_pointer1 is s.c64_pointer2)
def __init__( self, filename, input_units="uK_CMB", input_reference_frequency_GHz=None, nside=None, target_shape=None, target_wcs=None, precompute_output_map=True, has_polarization=True, pixel_indices=None, ): """Generic component based on Precomputed Alms Load a set of Alms from a FITS file and generate maps at the requested resolution and frequency assuming the CMB black body spectrum. A single set of Alms is used for all frequencies requested by PySM, consider that PySM expects the output of components to be in uK_RJ. See more details at https://so-pysm-models.readthedocs.io/en/latest/so_pysm_models/models.html Parameters ---------- filename : string Path to the input Alms in FITS format input_units : string Input unit strings as defined by pysm.convert_units, e.g. K_CMB, uK_RJ, MJysr input_reference_frequency_GHz : float If input units are K_RJ or Jysr, the reference frequency nside : int HEALPix NSIDE of the output maps precompute_output_map : bool If True (default), Alms are transformed into a map in the constructor, if False, the object only stores the Alms and generate the map at each call of the signal method, this is useful to generate maps convolved with different beams has_polarization : bool whether or not to simulate also polarization maps Default: True pixel_indices : ndarray of ints Output a partial maps given HEALPix pixel indices in RING ordering """ self.nside = nside self.shape = target_shape self.wcs = target_wcs self.filename = filename self.input_units = input_units if not input_units.endswith( "CMB") and input_reference_frequency_GHz is None: raise Exception( "If the input maps are in not in K_CMB, you need to specify `input_reference_frequency_GHz`" ) self.input_reference_frequency_GHz = input_reference_frequency_GHz self.pixel_indices = pixel_indices self.has_polarization = has_polarization alm = np.complex128( hp.read_alm(self.filename, hdu=(1, 2, 3) if self.has_polarization else 1)) if precompute_output_map: self.output_map = self.compute_output_map(alm) else: self.alm = alm
def testJSONSerialize(self): provider = JsonSerializeProvider() node2 = Node2(a=[['ss'], ['dd']], data=[3, 7, 212]) node1 = Node1(a='test1', b1=2, b2=2000, b3=5000, b4=500000, c1=2, c2=2000, c3=5000, c4=500000, d1=2.5, d2=7.37, d3=5.976321, cl1=1 + 2j, cl2=2.5 + 3.1j, e=False, f=node2, g=Node2(a=[['1', '2'], ['3', '4']]), h=[[2, 3], node2, True, { 1: node2 }, np.datetime64('1066-10-13'), np.timedelta64(1, 'D'), np.complex64(1 + 2j), np.complex128(2 + 3j)], i=[Node8(b1=111), Node8(b1=222)]) node3 = Node3(value=node1) serials = serializes(provider, [node2, node3]) serials = [ json.loads(json.dumps(s), object_hook=OrderedDict) for s in serials ] d_node2, d_node3 = deserializes(provider, [Node2, Node3], serials) self.assertIsNot(node2, d_node2) self.assertEqual(node2.a, d_node2.a) self.assertEqual(node2.data, d_node2.data) self.assertIsNot(node3, d_node3) self.assertIsInstance(d_node3.value, Node8) self.assertIsNot(node3.value, d_node3.value) self.assertEqual(node3.value.a, d_node3.value.a) self.assertEqual(node3.value.b1, d_node3.value.b1) self.assertEqual(node3.value.b2, d_node3.value.b2) self.assertEqual(node3.value.b3, d_node3.value.b3) self.assertEqual(node3.value.b4, d_node3.value.b4) self.assertEqual(node3.value.c1, d_node3.value.c1) self.assertEqual(node3.value.c2, d_node3.value.c2) self.assertEqual(node3.value.c3, d_node3.value.c3) self.assertEqual(node3.value.c4, d_node3.value.c4) self.assertAlmostEqual(node3.value.d1, d_node3.value.d1, places=2) self.assertAlmostEqual(node3.value.d2, d_node3.value.d2, places=4) self.assertAlmostEqual(node3.value.d3, d_node3.value.d3) self.assertAlmostEqual(node3.value.cl1, d_node3.value.cl1) self.assertAlmostEqual(node3.value.cl2, d_node3.value.cl2) self.assertEqual(node3.value.e, d_node3.value.e) self.assertIsNot(node3.value.f, d_node3.value.f) self.assertEqual(node3.value.f.a, d_node3.value.f.a) self.assertIsNot(node3.value.g, d_node3.value.g) self.assertEqual(node3.value.g.a, d_node3.value.g.a) self.assertEqual(node3.value.h[0], d_node3.value.h[0]) self.assertNotIsInstance(d_node3.value.h[1], six.string_types) self.assertIs(d_node3.value.h[1], d_node3.value.f) self.assertEqual(node3.value.h[2], True) self.assertAlmostEqual(node3.value.h[6], d_node3.value.h[6]) self.assertAlmostEqual(node3.value.h[7], d_node3.value.h[7]) self.assertEqual([n.b1 for n in node3.value.i], [n.b1 for n in d_node3.value.i]) self.assertIsInstance(d_node3.value.i[0], Node8) with self.assertRaises(ValueError): serializes(provider, [Node3(value='sth else')])
I = np.zeros((M, M)) print "SpatialOrbitals: ", M w = 1.0 a = np.sqrt(w) r = sp.Symbol('r') for j in range(0, M): nj, mj = indexMapSpinFree[j] R_njmj = R_nm(int(nj), int(mj), r) N_njmj = N(int(nj), int(mj)) for k in range(j, M): nk, mk = indexMapSpinFree[k] R_nkmk = R_nm(int(nk), int(mk), r) N_nkmk = N(int(nk), int(mk)) theta_int = a**2 * np.complex128(theta_integral2(int(mj), int(mk))) if (abs(mk - mj) != 1): R_integral = sp.integrate( R_njmj * R_nkmk * r**2, (r, 0, sp.oo)) #One r from r dr dt, and one from r |cost| Integral = N_nkmk * N_njmj * float(R_integral) * theta_int.real I[j, k] = Integral I[k, j] = Integral #Assemble H = [H_{jk}] H = np.zeros((M, M)) R = 2.0 for j in range(0, M): nj, mj = indexMapSpinFree[j] eps_j = 2.0 * nj + abs(mj) + 1.0 H[j, j] = eps_j + (1.0 / 8.0) * w**2 * R**2
import numpy as np c16 = np.complex128() f8 = np.float64() i8 = np.int64() u8 = np.uint64() c8 = np.complex64() f4 = np.float32() i4 = np.int32() u4 = np.uint32() dt = np.datetime64(0, "D") td = np.timedelta64(0, "D") b_ = np.bool_() b = bool() c = complex() f = float() i = int() AR = np.array([0], dtype=np.int64) AR.setflags(write=False) SEQ = (0, 1, 2, 3, 4) # Time structures dt > dt
def testJSONSerialize(self): provider = JsonSerializeProvider() node2 = Node2(a=[['ss'], ['dd']], data=[3, 7, 212]) node1 = Node1(a='test1', b1=2, b2=2000, b3=5000, b4=500000, c1=2, c2=2000, c3=5000, c4=500000, d1=2.5, d2=7.37, d3=5.976321, cl1=1+2j, cl2=2.5+3.1j, e=False, f1=Node2Entity(node2), f2=Node2Entity(node2), g=Node2(a=[['1', '2'], ['3', '4']]), h=[[2, 3], node2, True, {1: node2}, np.datetime64('1066-10-13'), np.timedelta64(1, 'D'), np.complex64(1+2j), np.complex128(2+3j), lambda x: x + 2, pytz.timezone('Asia/Shanghai'), pd.arrays.IntervalArray([pd.Interval(0, 1), pd.Interval(1, 5)])], i=[Node8(b1=111), Node8(b1=222)], j=Node2(a=[['u'], ['v']]), k=[Node5(a='uvw'), Node8(b1=222, j=Node5(a='xyz')), None], l=lambda x: x + 1, m=pytz.timezone('Asia/Shanghai'), n=pd.arrays.IntervalArray([pd.Interval(0, 1), pd.Interval(1, 5)])) node3 = Node3(value=node1) serials = serializes(provider, [node2, node3]) serials = [json.loads(json.dumps(s), object_hook=OrderedDict) for s in serials] d_node2, d_node3 = deserializes(provider, [Node2, Node3], serials) self.assertIsNot(node2, d_node2) self.assertEqual(node2.a, d_node2.a) self.assertEqual(node2.data, d_node2.data) self.assertIsNot(node3, d_node3) self.assertIsInstance(d_node3.value, Node8) self.assertIsNot(node3.value, d_node3.value) self.assertEqual(node3.value.a, d_node3.value.a) self.assertEqual(node3.value.b1, d_node3.value.b1) self.assertEqual(node3.value.b2, d_node3.value.b2) self.assertEqual(node3.value.b3, d_node3.value.b3) self.assertEqual(node3.value.b4, d_node3.value.b4) self.assertEqual(node3.value.c1, d_node3.value.c1) self.assertEqual(node3.value.c2, d_node3.value.c2) self.assertEqual(node3.value.c3, d_node3.value.c3) self.assertEqual(node3.value.c4, d_node3.value.c4) self.assertAlmostEqual(node3.value.d1, d_node3.value.d1, places=2) self.assertAlmostEqual(node3.value.d2, d_node3.value.d2, places=4) self.assertAlmostEqual(node3.value.d3, d_node3.value.d3) self.assertAlmostEqual(node3.value.cl1, d_node3.value.cl1) self.assertAlmostEqual(node3.value.cl2, d_node3.value.cl2) self.assertEqual(node3.value.e, d_node3.value.e) self.assertIsNot(node3.value.f1, d_node3.value.f1) self.assertEqual(node3.value.f1.a, d_node3.value.f1.a) self.assertIsNot(node3.value.f2, d_node3.value.f2) self.assertEqual(node3.value.f2.a, d_node3.value.f2.a) self.assertIsNot(node3.value.g, d_node3.value.g) self.assertEqual(node3.value.g.a, d_node3.value.g.a) self.assertEqual(node3.value.h[0], d_node3.value.h[0]) self.assertNotIsInstance(d_node3.value.h[1], str) self.assertIs(d_node3.value.h[1], d_node3.value.f1) self.assertEqual(node3.value.h[2], True) self.assertAlmostEqual(node3.value.h[6], d_node3.value.h[6]) self.assertAlmostEqual(node3.value.h[7], d_node3.value.h[7]) self.assertEqual(node3.value.h[8](2), 4) self.assertEqual(node3.value.h[9], d_node3.value.h[9]) np.testing.assert_array_equal(node3.value.h[10], d_node3.value.h[10]) self.assertEqual([n.b1 for n in node3.value.i], [n.b1 for n in d_node3.value.i]) self.assertIsInstance(d_node3.value.i[0], Node8) self.assertIsInstance(d_node3.value.j, Node2) self.assertEqual(node3.value.j.a, d_node3.value.j.a) self.assertIsInstance(d_node3.value.k[0], Node5) self.assertEqual(node3.value.k[0].a, d_node3.value.k[0].a) self.assertIsInstance(d_node3.value.k[1], Node8) self.assertEqual(node3.value.k[1].b1, d_node3.value.k[1].b1) self.assertIsInstance(d_node3.value.k[1].j, Node5) self.assertEqual(node3.value.k[1].j.a, d_node3.value.k[1].j.a) self.assertIsNone(node3.value.k[2]) self.assertEqual(d_node3.value.l(1), 2) self.assertEqual(d_node3.value.m, node3.value.m) np.testing.assert_array_equal(d_node3.value.n, node3.value.n) with self.assertRaises(ValueError): serializes(provider, [Node3(value='sth else')])
def dot(x_gpu, y_gpu, transa='N', transb='N', handle=None, target=None): """ Dot product of two arrays. For 1D arrays, this function computes the inner product. For 2D arrays of shapes `(m, k)` and `(k, n)`, it computes the matrix product; the result has shape `(m, n)`. Parameters ---------- x_gpu : pycuda.gpuarray.GPUArray Input array. y_gpu : pycuda.gpuarray.GPUArray Input array. transa : char If 'T', compute the product of the transpose of `x_gpu`. If 'C', compute the product of the Hermitian of `x_gpu`. transb : char If 'T', compute the product of the transpose of `y_gpu`. If 'C', compute the product of the Hermitian of `y_gpu`. handle : int CUBLAS context. If no context is specified, the default handle from `scikits.cuda.misc._global_cublas_handle` is used. Returns ------- c_gpu : pycuda.gpuarray.GPUArray, float{32,64}, or complex{64,128} Inner product of `x_gpu` and `y_gpu`. When the inputs are 1D arrays, the result will be returned as a scalar. Notes ----- The input matrices must all contain elements of the same data type. Examples -------- >>> import pycuda.gpuarray as gpuarray >>> import pycuda.autoinit >>> import numpy as np >>> import linalg >>> import misc >>> linalg.init() >>> a = np.asarray(np.random.rand(4, 2), np.float32) >>> b = np.asarray(np.random.rand(2, 2), np.float32) >>> a_gpu = gpuarray.to_gpu(a) >>> b_gpu = gpuarray.to_gpu(b) >>> c_gpu = linalg.dot(a_gpu, b_gpu) >>> np.allclose(np.dot(a, b), c_gpu.get()) True >>> d = np.asarray(np.random.rand(5), np.float32) >>> e = np.asarray(np.random.rand(5), np.float32) >>> d_gpu = gpuarray.to_gpu(d) >>> e_gpu = gpuarray.to_gpu(e) >>> f = linalg.dot(d_gpu, e_gpu) >>> np.allclose(np.dot(d, e), f) True """ if handle is None: handle = _global_cublas_handle if len(x_gpu.shape) == 1 and len(y_gpu.shape) == 1: if x_gpu.size != y_gpu.size: raise ValueError('arrays must be of same length') # Compute inner product for 1D arrays: if (x_gpu.dtype == np.complex64 and y_gpu.dtype == np.complex64): cublas_func = cublas.cublasCdotu elif (x_gpu.dtype == np.float32 and y_gpu.dtype == np.float32): cublas_func = cublas.cublasSdot elif (x_gpu.dtype == np.complex128 and y_gpu.dtype == np.complex128): cublas_func = cublas.cublasZdotu elif (x_gpu.dtype == np.float64 and y_gpu.dtype == np.float64): cublas_func = cublas.cublasDdot else: raise ValueError('unsupported combination of input types') return cublas_func(handle, x_gpu.size, x_gpu.gpudata, 1, y_gpu.gpudata, 1) else: # Get the shapes of the arguments (accounting for the # possibility that one of them may only have one dimension): x_shape = x_gpu.shape y_shape = y_gpu.shape if len(x_shape) == 1: x_shape = (1, x_shape[0]) if len(y_shape) == 1: y_shape = (1, y_shape[0]) # Perform matrix multiplication for 2D arrays: if (x_gpu.dtype == np.complex64 and y_gpu.dtype == np.complex64): cublas_func = cublas.cublasCgemm alpha = np.complex64(1.0) beta = np.complex64(0.0) elif (x_gpu.dtype == np.float32 and y_gpu.dtype == np.float32): cublas_func = cublas.cublasSgemm alpha = np.float32(1.0) beta = np.float32(0.0) elif (x_gpu.dtype == np.complex128 and y_gpu.dtype == np.complex128): cublas_func = cublas.cublasZgemm alpha = np.complex128(1.0) beta = np.complex128(0.0) elif (x_gpu.dtype == np.float64 and y_gpu.dtype == np.float64): cublas_func = cublas.cublasDgemm alpha = np.float64(1.0) beta = np.float64(0.0) else: raise ValueError('unsupported combination of input types') transa = lower(transa) transb = lower(transb) if transb in ['t', 'c']: m, k = y_shape elif transb in ['n']: k, m = y_shape else: raise ValueError('invalid value for transb') if transa in ['t', 'c']: l, n = x_shape elif transa in ['n']: n, l = x_shape else: raise ValueError('invalid value for transa') if l != k: raise ValueError('objects are not aligned') if transb == 'n': lda = max(1, m) else: lda = max(1, k) if transa == 'n': ldb = max(1, k) else: ldb = max(1, n) ldc = max(1, m) # Note that the desired shape of the output matrix is the transpose # of what CUBLAS assumes: if target is None: target = gpuarray.empty((n, ldc), x_gpu.dtype) cublas_func(handle, transb, transa, m, n, k, alpha, y_gpu.gpudata, lda, x_gpu.gpudata, ldb, beta, target.gpudata, ldc) return target
def click_figure(depth, x_0 = x_0, x_1 = x_1, y_0 = y_0, y_1 = y_1, _ix = _ix, _iy = _iy, \ init_r = init_r, init_c = init_c, base = base, p = p, my_cmap = my_cmap): global ix global iy global images ix = _ix iy = _iy x_range = x_1 - x_0 y_range = y_1 - y_0 f = plt.figure() ax = f.add_subplot(1,1,1) f.set_tight_layout(True) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.imshow(mandel(0, f_str, seed = np.complex128(init_r+init_c*1j), res = (res_x,res_y), xrng = (x_0,x_1), yrng = (y_0,y_1), iter_max = base+int(math.log10(((float(y_range)/x_range)))**p), julia = jules, esc_radius = esc_radius), cmap=my_cmap) def on_click(event): global ix global iy ix, iy = pixToCoor((event.xdata, event.ydata), res_x, res_y, x_range, y_range, x_0, y_0) print("Zooming on (real: " + str(ix) + ", complex: " + str(iy) + ")") plt.close(f) cid = f.canvas.mpl_connect('button_press_event', on_click) plt.show(f) bar = progressbar.ProgressBar(maxval=depth, \ widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage(), ' ', progressbar.ETA()]) bar.start() center = (float(ix), float(iy)) images = [] for i in range(1, depth+1): x_range = x_range / float(scale) y_range = x_range / float(scale) x_0, x_1 = center[0] - (x_range / 2.0), center[0] + (x_range / 2.0) y_0, y_1 = center[1] - (y_range / 2.0), center[1] + (y_range / 2.0) # TODO: every once in a while, find brightest set of pixels in image (max pooling) and zoom in there data = mandel(i, f_str, seed = np.complex128(init_r+init_c*1j), res = (res_x,res_y), xrng = (x_0,x_1), yrng = (y_0,y_1), iter_max = base,#+int(math.log10(((4.0/float(y_range))))**p), julia = jules, esc_radius = esc_radius) images.append(data) bar.update(i) bar.finish() input("Calculated zoom, hit any key to render.") print("Rendering...") fig = plt.figure() ax = fig.add_subplot(1,1,1) fig.set_tight_layout(True) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) im = ax.imshow(images[0], cmap=my_cmap) def update_image(i): """Returns updated ax""" im.set_data(images[i]) fig.canvas.draw() return ax ani = animation.FuncAnimation(fig, lambda x: update_image(x), \ frames=np.arange(0, depth), interval=10, repeat=False) plt.show(fig)
def test_impl(): return np.complex128(1.)
reveal_type(x.itemsize) # E: int reveal_type(x.shape) # E: Tuple[] reveal_type(x.strides) # E: Tuple[] reveal_type(x.ndim) # E: Literal[0] reveal_type(x.size) # E: Literal[1] reveal_type(x.squeeze()) # E: {complex64} reveal_type(x.byteswap()) # E: {complex64} reveal_type(x.transpose()) # E: {complex64} reveal_type(x.dtype) # E: numpy.dtype[{complex64}] reveal_type(np.complex64().real) # E: {float32} reveal_type(np.complex128().imag) # E: {float64} reveal_type(np.unicode_('foo')) # E: numpy.str_ reveal_type(np.str0('foo')) # E: numpy.str_ # Aliases reveal_type(np.unicode_()) # E: numpy.str_ reveal_type(np.str0()) # E: numpy.str_ reveal_type(np.byte()) # E: {byte} reveal_type(np.short()) # E: {short} reveal_type(np.intc()) # E: {intc} reveal_type(np.intp()) # E: {intp} reveal_type(np.int0()) # E: {intp} reveal_type(np.int_()) # E: {int_} reveal_type(np.longlong()) # E: {longlong}
def test_impl(): return np.ones((10, 10)) + np.complex128(1.)
def test_complex(self): s = compute_fingerprint(1j) self.assertEqual(s, compute_fingerprint(1+0j)) s = compute_fingerprint(np.complex64()) self.assertEqual(compute_fingerprint(np.complex64(2.0)), s) self.assertNotEqual(compute_fingerprint(np.complex128()), s)
def test_list_mixed(self): x = [1.0, np.float32(3.5), np.complex128(4.25), 'foo'] x_rec = self.encode_decode(x) assert all(map(lambda x, y: x == y, x, x_rec)) and all( map(lambda x, y: type(x) == type(y), x, x_rec))