本文整理匯總了Python中math.fabs方法的典型用法代碼示例。如果您正苦於以下問題:Python math.fabs方法的具體用法?Python math.fabs怎麽用?Python math.fabs使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類math
的用法示例。
在下文中一共展示了math.fabs方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: contains
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def contains(self, other):
if isinstance(other, self.__class__):
if self.is_inverted():
if other.is_inverted():
return other.lo() >= self.lo() and other.hi() <= self.hi()
return (other.lo() >= self.lo() or other.hi() <= self.hi()) \
and not self.is_empty()
else:
if other.is_inverted():
return self.is_full() or other.is_empty()
return other.lo() >= self.lo() and other.hi() <= self.hi()
else:
assert math.fabs(other) <= math.pi
if other == -math.pi:
other = math.pi
return self.fast_contains(other)
示例2: test_speed_limiting
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def test_speed_limiting(self):
obj = pySmartDL.SmartDL(self.res_testfile_1gb, dest=self.dl_dir, progress_bar=False, connect_default_logger=self.enable_logging)
obj.limit_speed(1024**2) # 1MB per sec
obj.start(blocking=False)
while not obj.get_dl_size():
time.sleep(0.1)
time.sleep(30)
expected_dl_size = 30 * 1024**2
allowed_delta = 0.6 # because we took only 30sec, the delta needs to be quite big, it we were to test 60sec the delta would probably be much smaller
diff = math.fabs(expected_dl_size - obj.get_dl_size()) / expected_dl_size
obj.stop()
obj.wait()
self.assertLessEqual(diff, allowed_delta)
示例3: check_success
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def check_success(self, position, close_goal):
rospy.logwarn("gripping force: " + str(self._gripper.get_force()))
rospy.logwarn("gripper position: " + str(self._gripper.get_position()))
rospy.logwarn("gripper position deadzone: " + str(self._gripper.get_dead_zone()))
if not self._gripper.is_moving():
success = True
else:
success = False
# success = fabs(self._gripper.get_position() - position) < self._gripper.get_dead_zone()
rospy.logwarn("gripping success: " + str(success))
return success
示例4: _adjust
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def _adjust(self, xa, ya, x, y, xb, yb):
""" fix the sides of the map """
if self.grid[x][y] == 0:
d = math.fabs(xa - xb) + math.fabs(ya - yb)
ROUGHNESS = self.params.get('roughness')
v = (self.grid[xa][ya] + self.grid[xb][yb]) / 2.0 \
+ (random.random() - 0.5) * d * ROUGHNESS
c = int(math.fabs(v) % 257)
if y == 0:
self.grid[x][self.size - 1] = c
if x == 0 or x == self.size - 1:
if y < self.size - 1:
self.grid[x][self.size - 1 - y] = c
range_low, range_high = self.params.get('height_range')
if c < range_low:
c = range_low
elif c > range_high:
c = range_high
self.grid[x][y] = c
示例5: function
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def function(self):
r"""Return benchmark evaluation function.
Returns:
Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
"""
def f(D, x):
r"""Fitness function.
Args:
D (int): Dimensionality of the problem
sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
Returns:
float: Fitness value for the solution.
"""
val1, val2 = 0.0, 0.0
for i in range(D): val1 += x[i] ** 2
for i in range(D): val2 += x[i]
return fabs(val1 ** 2 - val2 ** 2) ** (1 / 2) + (0.5 * val1 + val2) / D + 0.5
return f
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
示例6: function
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def function(self):
r"""Return benchmark evaluation function.
Returns:
Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
"""
def f(D, x):
r"""Fitness function.
Args:
D (int): Dimensionality of the problem
sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
Returns:
float: Fitness value for the solution.
"""
val = 1.0
for i in range(D):
valt = 1.0
for j in range(1, 33): valt += fabs(2 ** j * x[i] - round(2 ** j * x[i])) / 2 ** j
val *= (1 + (i + 1) * valt) ** (10 / D ** 1.2) - (10 / D ** 2)
return 10 / D ** 2 * val
return f
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
示例7: function
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def function(self):
r"""Return benchmark evaluation function.
Returns:
Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
"""
def g(z, D):
if z > 500: return (500 - fmod(z, 500)) * sin(sqrt(fabs(500 - fmod(z, 500)))) - (z - 500) ** 2 / (10000 * D)
elif z < -500: return (fmod(z, 500) - 500) * sin(sqrt(fabs(fmod(z, 500) - 500))) + (z - 500) ** 2 / (10000 * D)
return z * sin(fabs(z) ** (1 / 2))
def h(x, D): return g(x + 420.9687462275036, D)
def f(D, sol):
r"""Fitness function.
Args:
D (int): Dimensionality of the problem
sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
Returns:
float: Fitness value for the solution.
"""
val = 0.0
for i in range(D): val += h(sol[i], D)
return 418.9829 * D - val
return f
示例8: get_version_from_list
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def get_version_from_list(v, vlist):
"""See if we can match v (string) in vlist (list of strings)
Linux has to match in a fuzzy way."""
if is_windows:
# Simple case, just find it in the list
if v in vlist: return v
else: return None
else:
# Fuzzy match: normalize version number first, but still return
# original non-normalized form.
fuzz = 0.001
for vi in vlist:
if math.fabs(linux_ver_normalize(vi) - linux_ver_normalize(v)) < fuzz:
return vi
# Not found
return None
示例9: assertNear
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def assertNear(self, f1, f2, err, msg=None):
"""Asserts that two floats are near each other.
Checks that |f1 - f2| < err and asserts a test failure
if not.
Args:
f1: A float value.
f2: A float value.
err: A float value.
msg: An optional string message to append to the failure message.
"""
self.assertTrue(
math.fabs(f1 - f2) <= err,
"%f != %f +/- %f%s" % (f1, f2, err, " (%s)" % msg
if msg is not None else ""))
示例10: testAUCMeter
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def testAUCMeter(self):
mtr = meter.AUCMeter()
test_size = 1000
mtr.add(torch.rand(test_size), torch.zeros(test_size))
mtr.add(torch.rand(test_size), torch.Tensor(test_size).fill_(1))
val, tpr, fpr = mtr.value()
self.assertTrue(math.fabs(val - 0.5) < 0.1, msg="AUC Meter fails")
mtr.reset()
mtr.add(torch.Tensor(test_size).fill_(0), torch.zeros(test_size))
mtr.add(torch.Tensor(test_size).fill_(0.1), torch.zeros(test_size))
mtr.add(torch.Tensor(test_size).fill_(0.2), torch.zeros(test_size))
mtr.add(torch.Tensor(test_size).fill_(0.3), torch.zeros(test_size))
mtr.add(torch.Tensor(test_size).fill_(0.4), torch.zeros(test_size))
mtr.add(torch.Tensor(test_size).fill_(1),
torch.Tensor(test_size).fill_(1))
val, tpr, fpr = mtr.value()
self.assertEqual(val, 1.0, msg="AUC Meter fails")
示例11: nextX
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def nextX(self):
if self.c[1] != self.a[1] and self.c[1] != self.b[1]:
# inverse quadratic interpolation
s = self.a[0] * self.b[1] * self.c[1] / ((self.a[1] - self.b[1]) * (self.a[1] - self.c[1])) \
+ self.b[0] * self.a[1] * self.c[1] / ((self.b[1] - self.a[1]) * (self.b[1] - self.c[1])) \
+ self.c[0] * self.a[1] * self.b[1] / ((self.c[1] - self.a[1]) * (self.c[1] - self.b[1]))
else:
s = (self.a[0] * self.b[1] - self.b[0] * self.a[1]) / (self.b[1] - self.a[1])
c_dist = fabs(self.c[0] - self.b[0] if self.bisect else self.d)
self.bisect = (s - self.b[0]) * (s - 0.75 * self.a[0] - 0.25 * self.b[0]) >= 0. \
or fabs(s - self.b[0]) > 0.5 * c_dist \
or c_dist < self.tol
if self.bisect:
s = 0.5 * (self.a[0] + self.b[0])
self.d = s
return s
示例12: is_fallen
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def is_fallen(self):
"""Decide whether the minitaur has fallen.
Returns:
Boolean value that indicates whether the minitaur has fallen.
"""
roll, pitch, _ = self.minitaur.GetTrueBaseRollPitchYaw()
is_fallen = math.fabs(roll) > 0.3 or math.fabs(pitch) > 0.3
return is_fallen
示例13: is_fallen
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def is_fallen(self):
"""Decides whether the minitaur is in a fallen state.
If the roll or the pitch of the base is greater than 0.3 radians, the
minitaur is considered fallen.
Returns:
Boolean value that indicates whether the minitaur has fallen.
"""
roll, pitch, _ = self.minitaur.GetTrueBaseRollPitchYaw()
return math.fabs(roll) > 0.3 or math.fabs(pitch) > 0.3
示例14: _reward
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def _reward(self):
roll, pitch, _ = self.minitaur.GetBaseRollPitchYaw()
return 1.0 / (0.001 + math.fabs(roll) + math.fabs(pitch))
示例15: _sift_sentiment_scores
# 需要導入模塊: import math [as 別名]
# 或者: from math import fabs [as 別名]
def _sift_sentiment_scores(self, sentiments):
# want separate positive versus negative sentiment scores
pos_sum = 0.0
neg_sum = 0.0
neu_count = 0
for sentiment_score in sentiments:
if sentiment_score > 0:
pos_sum += (float(sentiment_score) +1) # compensates for neutral words that are counted as 1
if sentiment_score < 0:
neg_sum += (float(sentiment_score) -1) # when used with math.fabs(), compensates for neutrals
if sentiment_score == 0:
neu_count += 1
return pos_sum, neg_sum, neu_count