Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_math_module():
"Operations with the math module"
x = uncertainties.ufloat((-1.5, 0.1))
# The exponent must not be differentiated, when calculating the
# following (the partial derivative with respect to the exponent
# is not defined):
assert (x**2).nominal_value == 2.25
# Regular operations are chosen to be unchanged:
assert isinstance(umath.sin(3), float)
# Python >=2.6 functions:
if sys.version_info >= (2, 6):
# factorial() must not be "damaged" by the umath module, so as
# to help make it a drop-in replacement for math (even though
# factorial() does not work on numbers with uncertainties
# because it is restricted to integers, as for
# math.factorial()):
assert umath.factorial(4) == 24
# Boolean functions:
assert not umath.isinf(x)
# Comparison, possibly between an AffineScalarFunc object and a
Both the nominal values and the covariances are compared between
the direct calculation performed in this module and a Monte-Carlo
simulation.
"""
try:
import numpy
import numpy.random
except ImportError:
import warnings
warnings.warn("Test not performed because NumPy is not available")
return
# Works on numpy.arrays of Variable objects (whereas umath.sin()
# does not):
sin_uarray_uncert = numpy.vectorize(umath.sin, otypes=[object])
# Example expression (with correlations, and multiple variables combined
# in a non-linear way):
def function(x, y):
"""
Function that takes two NumPy arrays of the same size.
"""
# The uncertainty due to x is about equal to the uncertainty
# due to y:
return 10 * x**2 - x * sin_uarray_uncert(y**3)
x = uncertainties.ufloat((0.2, 0.01))
y = uncertainties.ufloat((10, 0.001))
function_result_this_module = function(x, y)
nominal_value_this_module = function_result_this_module.nominal_value
def test_compound_expression():
"""
Test equality between different formulas.
"""
x = uncertainties.ufloat((3, 0.1))
# Prone to numerical errors (but not much more than floats):
assert umath.tan(x) == umath.sin(x)/umath.cos(x)
def test_numerical_example():
"Test specific numerical examples"
x = uncertainties.ufloat((3.14, 0.01))
result = umath.sin(x)
# In order to prevent big errors such as a wrong, constant value
# for all analytical and numerical derivatives, which would make
# test_fixed_derivatives_math_funcs() succeed despite incorrect
# calculations:
assert ("%.6f +/- %.6f" % (result.nominal_value, result.std_dev())
== "0.001593 +/- 0.010000")
# Regular calculations should still work:
assert("%.11f" % umath.sin(3) == "0.14112000806")
par['rR'] = mp['dR'] / 2./ pi / mp['nR']
# calculate the frame/fork fundamental geometry
if 'w' in mp.keys(): # if there is a wheelbase
# steer axis tilt in radians
par['lam'] = pi / 180. * (90. - mp['gamma'])
# wheelbase
par['w'] = mp['w']
# fork offset
forkOffset = mp['f']
else:
h = (mp['h1'], mp['h2'], mp['h3'], mp['h4'], mp['h5'])
d = (mp['d1'], mp['d2'], mp['d3'], mp['d4'], mp['d'])
a, b, c = calculate_abc_geometry(h, d)
par['lam'] = lambda_from_abc(par['rF'], par['rR'], a, b, c)
par['w'] = (a + b) * umath.cos(par['lam']) + c * umath.sin(par['lam'])
forkOffset = b
# trail
par['c'] = trail(par['rF'], par['lam'], forkOffset)[0]
return par
lam : float or ufloat
Steer axis tilt in radians.
point : narray, shape(3,)
A point that lies in the symmetry plane of the bicycle.
Returns
-------
d : float or ufloat
The minimal distance from the given point to the steer axis.
"""
pointOnAxis1 = np.array([w + c,
0.,
0.])
pointOnAxis2 = pointOnAxis1 +\
np.array([-umath.sin(lam),
0.,
-umath.cos(lam)])
pointsOnLine = np.array([pointOnAxis1, pointOnAxis2]).T
# this is the distance from the assembly com to the steer axis
return point_to_line_distance(point, pointsOnLine)
Parameters
----------
lam : float
Steer axis tilt.
l1, l2 : float
The distance from the front wheel center to the handlebar refernce
center perpendicular to and along the steer axis.
Returns
-------
u1, u2 : float
'''
u1 = l2 * umath.sin(lam) - l1 * umath.cos(lam)
u2 = u1 / umath.tan(lam) + l1 / umath.sin(lam)
return u1, u2
The steer axis tilt (pi/2 - headtube angle). The angle between the
headtube and a vertical line.
fo : float
The fork offset
Returns
-------
c: float
Trail
cm: float
Mechanical Trail
'''
# trail
c = (rF * umath.sin(lam) - fo) / umath.cos(lam)
# mechanical trail
cm = c * umath.cos(lam)
return c, cm
SR = p['IRyy'] / p['rR']
SF = p['IFyy'] / p['rF']
ST = SR + SF
SA = mA * uA + mu * mT * xT
Mpp = ITxx
Mpd = IAlx + mu * ITxz
Mdp = Mpd
Mdd = IAll + 2 * mu * IAlz + mu**2 * ITzz
M = np.array([[Mpp, Mpd], [Mdp, Mdd]])
K0pp = mT * zT # this value only reports to 13 digit precision it seems?
K0pd = -SA
K0dp = K0pd
K0dd = -SA * umath.sin(p['lam'])
K0 = np.array([[K0pp, K0pd], [K0dp, K0dd]])
K2pp = 0.
K2pd = (ST - mT * zT) / p['w'] * umath.cos(p['lam'])
K2dp = 0.
K2dd = (SA + SF * umath.sin(p['lam'])) / p['w'] * umath.cos(p['lam'])
K2 = np.array([[K2pp, K2pd], [K2dp, K2dd]])
C1pp = 0.
C1pd = (mu*ST + SF*umath.cos(p['lam']) + ITxz / p['w'] *
umath.cos(p['lam']) - mu*mT*zT)
C1dp = -(mu * ST + SF * umath.cos(p['lam']))
C1dd = (IAlz / p['w'] * umath.cos(p['lam']) + mu * (SA +
ITzz / p['w'] * umath.cos(p['lam'])))
C1 = np.array([[C1pp, C1pd], [C1dp, C1dd]])