def test_trunc(self):
self._testMath(torch.trunc, lambda x: x - math.fmod(x, 1))
python类fmod()的实例源码
def test_fmod(self):
m1 = torch.Tensor(10, 10).uniform_(-10., 10.)
res1 = m1.clone()
q = 2.1
res1[:, 3].fmod_(q)
res2 = m1.clone()
for i in range(m1.size(1)):
res2[i, 3] = math.fmod(res2[i, 3], q)
self.assertEqual(res1, res2)
def test_cfmod(self):
self._test_cop(torch.fmod, math.fmod)
def feature_inview_2d(f, x, theta, rmax, thmax):
"""Checks to see wheter features is in view of robot
Parameters
----------
f : np.array of size 2
Feature position
x : np.array of size 2
Robot position
theta : float
Robot's current heading in radians
rmax : float
Max sensor range
thmax : float
Max sensor bearing
Returns
-------
Boolean to denote whether feature is in view of robot
"""
# Distance in x and y
dx = f[0] - x[0]
dy = f[1] - x[1]
# Calculate range and bearing
r = sqrt(dx**2 + dy**2)
th = fmod(atan2(dy, dx) - theta, 2 * pi)
if th > pi:
th = th - 2 * pi
# Check to see if range and bearing is within view
if ((r < rmax) and (abs(th) < thmax)):
return True
else:
return False
def wrap180(euler_angle):
""" Wrap angle to 180
Args:
euler_angle (float): Euler angle
Returns:
Wrapped angle
"""
return fmod((euler_angle + 180.0), 360.0) - 180.0
def gmst(lod, jul_day):
'''Computes smoothed Greenwich siderial time:
http://articles.adsabs.harvard.edu//full/1982A%26A...105..359A/0000360.000.html'''
tut1 = (jul_day - 2451545.0) / 36525.0
seconds = -6.2e-6 * tut1 * tut1 * tut1 + 0.093104 * tut1 * tut1 + \
(876600.0 * 3600.0 + 8640184.812866) * tut1 + 67310.54841
_theta = 2 * pi * fmod(seconds / 86400.0, 1.0)
theta = _theta if _theta > 0 else _theta + 2*pi
omega = 7.29211514670698e-5 * (1.0 - lod / 86400.0)
return theta, omega
def wrapTo2PI(stradiance):
result = 0.0
if stradiance == pi2:
return pi2;
else:
result = math.fmod(stradiance, pi2);
if result < 0:
return (result + pi2);
return result
def test_frac(self):
self._testMath(torch.frac, lambda x: math.fmod(x, 1))
def test_trunc(self):
self._testMath(torch.trunc, lambda x: x - math.fmod(x, 1))
def test_fmod(self):
m1 = torch.Tensor(10, 10).uniform_(-10., 10.)
res1 = m1.clone()
q = 2.1
res1[:, 3].fmod_(q)
res2 = m1.clone()
for i in range(m1.size(1)):
res2[i, 3] = math.fmod(res2[i, 3], q)
self.assertEqual(res1, res2)
def test_cfmod(self):
self._test_cop(torch.fmod, math.fmod)
def divfmod(x, y):
fmod = math.fmod(x, y)
div = (x-fmod)//y
return div, fmod
def normalize_angle_positive(angle):
"""
map an angle from 0 to 2pi
"""
return fmod(fmod(angle, 2.0*pi) + 2.0*pi, 2.0*pi)
# map to -pi to +pi
def addHandles(self):
self.removeHandles()
self.cp_cidmap = {}
print(len(self.cc.road))
for r in self.cc.road:
print(len(r))
cids = self.addMoveHandle(r)
for cid in cids:
self.r_cidmap[cid] = r
self.imap[r] = cids
self.canvas.tag_lower("rail","segment")
sys.stdout.flush()
minslotlen = 50
#tex = m.fmod(self.cc.length(),minslotlen)
#numslots = int((self.cc.length() - tex)/minslotlen + 2)
numslots = int(self.cc.length()/minslotlen + 2)
ex = (self.cc.length() - numslots*minslotlen)/numslots
slotlen = minslotlen + ex
try:
self.canvas.delete("slots")
except tk.TclError:
pass
for i in range(numslots):
s = i*slotlen
op, ot = self.cc.pointAndTangentAt(s)
on = la.perp2ccw(ot)
p1 = op + 30*la.unit(on)
p2 = op - 30*la.unit(on)
self.canvas.create_polygon([(p1[0],p1[1]),(p2[0],p2[1])],outline="black",tags="slots")
def drawContours(self, path):
# print("load contours",path)
try:
self.canvas.delete("contour")
except tk.TclError:
pass
import re
self.contours = np.load(path)
ex = self.scene["ex"]
lf = len(self.contours.files)
for a in self.contours.files:
cs = self.contours[a]
h = int(re.findall('\d+', a)[0])
h /= lf
# print("file",a,h)
# print("contours",len(cs))
col = colorsys.rgb_to_hsv(0.7, 0.9, 0.85)
hue = col[0] - h / 2
hue = m.fmod(hue, 1)
col = (hue, max(0, min(col[1], 1)), max(0, min(col[2], 1)))
col = colorsys.hsv_to_rgb(*col)
hexcol = rgb2hex(col)
for c in cs:
if len(c):
cc = [((x[1] - 512) / 1024 * ex * 2, (x[0] - 512) / 1024 * ex * 2) for x in c]
if la.norm(c[-1] - c[0]) < 0.01:
self.canvas.create_polygon(cc, fill="", outline=hexcol, width=7, tag="contour")
else:
self.canvas.create_line(cc, fill=hexcol, width=7, tag="contour")
try:
self.canvas.tag_lower("contour")
except tk.TclError:
pass
sys.stdout.flush()
def halton2(dim, nsims):
"""
la fonction ne crash plus.
Version 2 de la suite d'halton sans la librairie Python existante.
"""
h = np.empty(nsims * dim)
h.fill(np.nan)
p = np.empty(nsims)
p.fill(np.nan)
Base = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
lognsims = log(nsims + 1)
for i in range(dim):
base = Base[i]
n = int(ceil(lognsims / log(base)))
for t in range(n):
p[t] = pow(base, -(t + 1) )
for j in range(nsims):
d = j + 1
somme = fmod(d, base) * p[0]
for t in range(1, n):
d = floor(d / base)
somme += fmod(d, base) * p[t]
h[j*dim + i] = somme
return norm.ppf(h.reshape(dim, nsims))
def SanitizeAngleValue(kernelCenter, angle):
numDistinctLines = kernelCenter * 4
angle = math.fmod(angle, 180.0)
validLineAngles = np.linspace(0,180, numDistinctLines, endpoint = False)
angle = nearestValue(angle, validLineAngles)
return angle
def cycle_day (path):
""" provides the day in cycle given the path number
"""
cycle_day_path1 = 5
cycle_day_increment = 7
nb_days_after_day1 = cycle_day_path1 + cycle_day_increment*(path-1)
cycle_day_path = math.fmod(nb_days_after_day1,16)
if path >= 98: #change date line
cycle_day_path += 1
return cycle_day_path
def test_frac(self):
self._testMath(torch.frac, lambda x: math.fmod(x, 1))
def test_trunc(self):
self._testMath(torch.trunc, lambda x: x - math.fmod(x, 1))