本文整理汇总了Python中numpy.copysign函数的典型用法代码示例。如果您正苦于以下问题:Python copysign函数的具体用法?Python copysign怎么用?Python copysign使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了copysign函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: symOrtho
def symOrtho(self, a, b):
""" Computes the radius, cosine, and sine for the
orthogonal transformation.
@param a x vector.
@param b y vector.
@return [c,s,r]. Cosine value, Sine value, and the radius.
"""
s = 0
c = 0
r = 0
if not b:
r = np.abs(a)
c = np.copysign(1,0,a)
elif not a:
r = np.abs(b)
s = np.copysign(1,0,b)
elif np.abs(b) > np.abs(a):
t = a / b
s = np.copysign(1.0,b) / np.sqrt(1.0 + t**2)
c = s * t
r = b / s
else:
t = b / a
c = np.copysign(1.0,a) / np.sqrt(1.0 + t**2)
s = c * t
r = a / c
return [c,s,r]
示例2: test_cross_div
def test_cross_div(dtypea, dtypeb, dtypec):
if dtypea == np.int8 and dtypeb == np.int8:
pytest.skip("Different behaviour in c++ and python for int8 / int8".format(dtypea, dtypeb))
def fkt(a, b, c):
c[:] = a / b
hfkt = hope.jit(fkt)
(ao, ah), (bo, bh), (co, ch) = random(dtypea, [10]), random(dtypeb, [10]), random(dtypec, [10])
ao, ah, bo, bh = ao.astype(np.float64), ah.astype(np.float64), bo.astype(np.float64), bh.astype(np.float64)
ao, ah = (
np.copysign(np.power(np.abs(ao), 1.0 / 4.0), ao).astype(dtypea),
np.copysign(np.power(np.abs(ah), 1.0 / 4.0), ah).astype(dtypea),
)
bo, bh = (
np.copysign(np.power(np.abs(bo), 1.0 / 4.0), bo).astype(dtypeb),
np.copysign(np.power(np.abs(bh), 1.0 / 4.0), bh).astype(dtypeb),
)
if np.count_nonzero(bo == 0) > 0:
bo[bo == 0] += 1
if np.count_nonzero(bh == 0) > 0:
bh[bh == 0] += 1
fkt(ao, bo, co), hfkt(ah, bh, ch)
assert check(co, ch)
fkt(ao, bo, co), hfkt(ah, bh, ch)
assert check(co, ch)
示例3: test_copysign
def test_copysign():
assert_(np.copysign(1, -1) == -1)
with np.errstate(divide="ignore"):
assert_(1 / np.copysign(0, -1) < 0)
assert_(1 / np.copysign(0, 1) > 0)
assert_(np.signbit(np.copysign(np.nan, -1)))
assert_(not np.signbit(np.copysign(np.nan, 1)))
示例4: test_signbit
def test_signbit(self):
from numpy import signbit, add, copysign, nan
assert signbit(add.identity) == False
assert (signbit([0, 0.0, 1, 1.0, float("inf")]) == [False, False, False, False, False]).all()
assert (signbit([-0, -0.0, -1, -1.0, float("-inf")]) == [False, True, True, True, True]).all()
assert (signbit([copysign(nan, 1), copysign(nan, -1)]) == [False, True]).all()
示例5: __step
def __step(self, t, y, dt, control):
u_v, u_s = control(t, y)
v_x, v_y, r, s_f, x, y, o = y
a_f = (v_y + self.L_f * r) / (v_x + 0.00001) - s_f
a_r = (v_y - self.L_r * r) / (v_x + 0.00001)
F_yf = -self.C_af * a_f
F_yr = -self.C_ar * a_r
n_v = self.noise_v(t) if self.noise_v else 0
n_s = self.noise_s(t) if self.noise_s else 0
u_s = u_s if abs(u_s) < self.max_u_s else np.copysign(self.max_u_s, u_s)
u_v = u_v if abs(u_v) < self.max_u_v else np.copysign(self.max_u_v, u_v)
if abs(s_f + u_s * dt) > self.max_s:
u_s = np.copysign(self.max_s - 0.05, s_f) - s_f
self._uv = u_v
self._us = u_s
F_res = self.F_res() * v_x
d_v_x = (-F_yf * np.sin(s_f) + u_v - F_res * np.cos(s_f) - F_res + n_v) / self.m + v_y * r
d_v_y = (F_yf * np.cos(s_f) + F_yr - F_res * np.sin(s_f)) / self.m - v_x * r
d_r = self.L_f * (F_yf * np.cos(s_f) - F_res * np.sin(s_f)) / self.I_z - self.L_r * F_yr / self.I_z + n_s
d_s = u_s
d_x = v_x * np.cos(o) - v_y * np.sin(o)
d_y = v_x * np.sin(o) + v_y * np.cos(o)
d_o = r
return [d_v_x, d_v_y, d_r, d_s, d_x, d_y, d_o]
示例6: test_copysign_array
def test_copysign_array(self):
assert np.all(np.copysign(np.array([1., 2., 3.]) * u.s, -1.) == -np.array([1., 2., 3.]) * u.s)
assert np.all(np.copysign(np.array([1., 2., 3.]) * u.s, -1. * u.m) == -np.array([1., 2., 3.]) * u.s)
assert np.all(np.copysign(np.array([1., 2., 3.]) * u.s, np.array([-2.,2.,-4.]) * u.m) == np.array([-1., 2., -3.]) * u.s)
q = np.copysign(np.array([1., 2., 3.]), -3 * u.m)
assert np.all(q == np.array([-1., -2., -3.]))
assert not isinstance(q, u.Quantity)
示例7: calibrate
def calibrate(self, steps): # calibration values in uW
rng = np.arange(0,steps)
x = np.zeros(steps,dtype = float)
y = np.zeros(steps,dtype = float)
self.set_power(0)
self._ins_pm.set_wavelength(self._wavelength)
time.sleep(2)
bg = self._ins_pm.get_power()
print 'background power: %.4f uW' % (bg*1e6)
time.sleep(.2)
V_max = self.get_V_max()
V_min = self.get_V_min()
if V_max + V_min < 0: rng=np.flipud(rng)
for a in rng:
x[a] = a*(V_max-V_min)/float(steps-1)+V_min
self.apply_voltage(x[a])
time.sleep(0.5)
y[a] = self._ins_pm.get_power() - bg
print 'measured power at %.2f V: %.4f uW' % \
(x[a], y[a]*1e6)
#x= x*(V_max-V_min)/float(steps-1)+V_min
a, xc, k = np.copysign(np.max(y), V_max + V_min), np.copysign(.1, V_max + V_min), np.copysign(5., V_max + V_min)
fitres = fit.fit1d(x,y, common.fit_AOM_powerdependence,
a, xc, k, do_print=True, ret=True)
fd = np.zeros(len(x))
if type(fitres) != type(False):
p1 = fitres['params_dict']
self.set_cal_a(p1['a'])
self.set_cal_xc(p1['xc'])
self.set_cal_k(p1['k'])
fd = fitres['fitfunc'](x)
else:
print 'could not fit calibration curve!'
dat = qt.Data(name= 'aom_calibration_'+self._name+'_'+\
self._cur_controller)
dat.add_coordinate('Voltage [V]')
dat.add_value('Power [W]')
dat.add_value('fit')
dat.create_file()
plt = qt.Plot2D(dat, 'rO', name='aom calibration', coorddim=0, valdim=1,
clear=True)
plt.add_data(dat, coorddim=0, valdim=2)
dat.add_data_point(x,y,fd)
dat.close_file()
plt.save_png(dat.get_filepath()+'png')
self.save_cfg()
print (self._name+' calibration finished')
示例8: test_binary_pow
def test_binary_pow(dtype, shape):
def fkt(a, c):
c[:] = a ** 2
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
ao, ah = np.copysign(np.sqrt(np.abs(ao)), ao).astype(dtype), np.copysign(np.sqrt(np.abs(ah)), ah).astype(dtype)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
示例9: test_copysign
def test_copysign():
assert_(np.copysign(1, -1) == -1)
old_err = np.seterr(divide="ignore")
try:
assert_(1 / np.copysign(0, -1) < 0)
assert_(1 / np.copysign(0, 1) > 0)
finally:
np.seterr(**old_err)
assert_(np.signbit(np.copysign(np.nan, -1)))
assert_(not np.signbit(np.copysign(np.nan, 1)))
示例10: test_augmented_mult
def test_augmented_mult(dtype, shape):
def fkt(a, c):
c[:] *= a
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
ao, ah = np.copysign(np.power(np.abs(ao), 1. / 4.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 4.), ah).astype(dtype)
co, ch = np.copysign(np.power(np.abs(co), 1. / 4.), co).astype(dtype), np.copysign(np.power(np.abs(ch), 1. / 4.), ch).astype(dtype)
ro, rh = fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
ro, rh = fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
示例11: adjustDxn
def adjustDxn(dx1,dx2,rcore,tol=1e-10):
r = np.sqrt(dx1**2 + dx2**2)
if (r**2 < rcore**2*tol):
rcorefac = rcore/np.sqrt(2.0)
dx1 = np.copysign(rcorefac,dx1)
dx2 = np.copysign(rcorefac,dx2)
elif (r**2 < rcore**2):
rfac = rcore/r
dx1 = dx1*rfac
dx2 = dx2*rfac
return dx1, dx2
示例12: test_opt_pow_array
def test_opt_pow_array(dtype, shape):
def fkt(a, c):
c[:] = a ** 2.0 + a ** 4 + a ** 7
hope.config.optimize = True
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
ao, ah = np.copysign(np.power(np.abs(ao), 1. / 8.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 8.), ah).astype(dtype)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
hope.config.optimize = False
示例13: dms2deg
def dms2deg(char):
"""
Function used to transform the Declination from the fits file given in degrees, minutes and seconds
to degrees.
Argument: The dec as a string of characters
output: A float number: the Dec written in degrees
"""
d,m,s = [float(x) for x in char.split()]
m,s = np.copysign(m,d),np.copysign(s,d) # The arcminutes and arcseconds adquire the same sign of degrees to be added later
out = (d + m/60.+ s/3600.)
return out
示例14: test_opt_pow_scalar
def test_opt_pow_scalar(dtype):
def fkt(a, c):
c[0] = a ** 2 + a ** 4 + a ** 7.0
hope.config.optimize = True
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, []), random(dtype, [1])
ao, ah = np.copysign(np.power(np.abs(ao), 1. / 8.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 8.), ah).astype(dtype)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
hope.config.optimize = False
示例15: test_return_arr_expr
def test_return_arr_expr(dtype, shape):
def fkt(a, c):
return a ** -2 + a ** -4.0 + a ** -7
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
ao, ah = np.copysign(np.power(np.abs(ao), 1. / 8.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 8.), ah).astype(dtype)
if np.count_nonzero(ao == 0) > 0: ao[ao == 0] += 1
if np.count_nonzero(ah == 0) > 0: ah[ah == 0] += 1
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)