예제 #1
0
def checkConstantInit(val, st, signed, wl, fpf):
	"""Check the constant init, by comparing the three methods"""
	methods = ('', 'log','Benoit','threshold')
	c = [Constant(val, wl=wl, signed=signed, method=method, fpf=fpf, name=st) for method in methods]

	# check the mantissa range
	for cst in c:
		if signed:
			assert((-2 ** (wl - 1) <= cst.mantissa < -2 ** (wl - 2)) or (2 ** (wl-2) <= cst.mantissa < 2 ** (wl-1)))
		else:
			assert(2 ** (wl-1) <= cst.mantissa < 2 ** wl)
		# check str and value method
		str(c)
		repr(c)
		assert(cst.value == val)

	# compare the three constants (compare their FPF, mantissa and approx)
	cst = c[0]
	for i in range(1,len(c)):
		assert(cst.FPF == c[i].FPF)
		assert(cst.mantissa == c[i].mantissa)
		assert(cst.approx == c[i].approx)

	# check if |c - c_FxP| < 2 ^(l-1)
	for cst in c:
		if cst.mantissa == -2**(cst.FPF.wl-1):
			assert(almosteq(cst.approx, val, abs_eps=ldexp(1, cst.FPF.lsb)))
		else:
			assert (almosteq(cst.approx, val, abs_eps=ldexp(1, cst.FPF.lsb - 1)))

	# check relative and absolute error
	cst = c[0]
def system02_minimizer(p, l, i, kdpl, kdpi):
    kdpl = mpf(kdpl)
    kdpi = mpf(kdpi)
    if almosteq(kdpl, mpf_zero, mpf_tol):
        kdpl += mpf_tol
    if almosteq(kdpi, mpf_zero, mpf_tol):
        kdpi += mpf_tol
    p = mpf(p)
    l = mpf(l)
    i = mpf(i)

    def f(p_f, l_f, i_f):
        pl = p_f * l_f / kdpl
        pi = p_f * i_f / kdpi
        return p - (p_f + pl + pi), l - (l_f + pl), i - (i_f + pi)

    p_f, l_f, i_f = findroot(f, [mpf_zero, mpf_zero, mpf_zero],
                             tol=mpf_tol,
                             maxsteps=1e6)
    return {
        "pf": p_f,
        "lf": l_f,
        "if": i_f,
        "pl": (p_f * l_f) / kdpl,
        "pi": (p_f * i_f) / kdpi,
    }
def test_radial_sc_equatorial_medium_ecc():
    aa = 0
    slr = 6
    ecc = 0.5
    x = 1
    r1_ch, r2_ch, r3_ch, r4_ch = radial_roots(aa, slr, ecc, x, digits)
    r1 = mpf(
        "12.0000000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000000000000"
        + "000000000"
    )
    r2 = mpf(
        "4.00000000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000000000000"
        "000000000"
    )
    r3 = mpf(
        "6.00000000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000000000000"
        + "0000000"
    )
    r4 = mpf("0")
    assert almosteq(r1_ch, r1, eps)
    assert almosteq(r2_ch, r2, eps)
    assert almosteq(r3_ch, r3, eps)
    assert almosteq(r4_ch, r4, eps)
def test_radial_kerr_polar_high_ecc():
    aa = 0.9
    slr = 6
    ecc = 0.9999
    x = 0
    r1_ch, r2_ch, r3_ch, r4_ch = radial_roots(aa, slr, ecc, x, digits)
    r1 = mpf(
        "60000.0000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000000000000"
        + "00000"
    )
    r2 = mpf(
        "3.00015000750037501875093754687734386719335966798339916995849"
        + "7924896244812240612030601530076503825191259562978148907445372"
        + "268613431"
    )
    r3 = mpf(
        "4.23160150405948518323888791308221551987498093592515288030115"
        + "945354089115861330121257783337210134827499637485386611824933"
    )
    r4 = mpf(
        "0.49286586348019811079088367661175558062941254382055084863641"
        + "0998598718916996297039941129876200032081561790988663968206728"
    )
    assert almosteq(r1_ch, r1, eps)
    assert almosteq(r2_ch, r2, eps)
    assert almosteq(r3_ch, r3, eps)
    assert almosteq(r4_ch, r4, eps)
예제 #5
0
	def mini(r):
		nonlocal txi, tv
		xi = [r]
		y = f(r)
		v = r*f(r) + mpmath.quad(f, [r, mpmath.inf])
		if verbose:
			print('Trying r={0} (v={1})'.format(r, v), file=sys.stderr)
		for i in itertools.count():
			xm1 = xi[i]
			h = v / xm1
			y += h
			if y >= maximum or mpmath.almosteq(y, maximum, abs_eps=mpmath.mp.eps * 2**10):
				break
			# We solve for x via secant method instead of using f's inverse.
			x = mpmath.findroot(lambda x: f(x) - y, xm1)
			xi.append(x)
		xi.append(mpmath.mpf())
		if len(xi) == nseg:
			if mpmath.almosteq(y, maximum, abs_eps=mpmath.mp.eps * 2**10):
				txi, tv = xi[::-1], v
				return 0
			# If y > maximum, then v is too large, which means r is too far
			# left, so we want to return a negative value. The opposite holds
			# true when y < maximum.
			return maximum - y
		return len(xi) - nseg + h*mpmath.sign(len(xi) - nseg)
def test_radial_kerr_high_ecc():
    aa = 0.9
    slr = 6
    ecc = 0.9999
    x = 0.5
    r1_ch, r2_ch, r3_ch, r4_ch = radial_roots(aa, slr, ecc, x, digits)
    r1 = mpf(
        "60000.0000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000000000000"
        + "00000"
    )
    r2 = mpf(
        "3.00015000750037501875093754687734386719335966798339916995849"
        + "7924896244812240612030601530076503825191259562978148907445372"
        + "268613431"
    )
    r3 = mpf(
        "2.09909631071719289113848288619813896273182343075993333860146"
        + "83275256205764755741564757727720651989485563951626108414"
    )
    r4 = mpf(
        "0.54443257307207486035051683847000616594344254063022881916146"
        + "92087614587124232498346668603991435731832877296511569358"
    )
    assert almosteq(r1_ch, r1, eps)
    assert almosteq(r2_ch, r2, eps)
    assert almosteq(r3_ch, r3, eps)
    assert almosteq(r4_ch, r4, eps)
def test_radial_sc_equatorial_low_ecc():
    aa = 0
    slr = 6
    ecc = 0.1
    x = 1
    r1_ch, r2_ch, r3_ch, r4_ch = radial_roots(aa, slr, ecc, x, digits)
    r1 = mpf(
        "6.66666666666666666666666666666666666666666666666666666666666"
        + "6666666666666666666666666666666666666666666666666666666666666"
        + "666666667"
    )
    r2 = mpf(
        "5.45454545454545454545454545454545454545454545454545454545454"
        + "5454545454545454545454545454545454545454545454545454545454545"
        + "454545455"
    )
    r3 = mpf(
        "6.00000000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000000000000"
        + "0000000"
    )
    r4 = mpf("0")
    assert almosteq(r1_ch, r1, eps)
    assert almosteq(r2_ch, r2, eps)
    assert almosteq(r3_ch, r3, eps)
    assert almosteq(r4_ch, r4, eps)
def test_radial_kerr_even_higher_ecc():
    aa = 0.9
    slr = 6
    ecc = 0.999999
    x = 0.5
    r1_ch, r2_ch, r3_ch, r4_ch = radial_roots(aa, slr, ecc, x, digits)
    r1 = mpf(
        "6000000.00000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000000000000"
        + "00000"
    )
    r2 = mpf(
        "3.00000150000075000037500018750009375004687502343751171875585"
        + "9377929688964844482422241211120605560302780151390075695037847"
        + "518923759"
    )
    r3 = mpf(
        "2.09909500849781985664788052724951917390754315839076840288258"
        + "30079641104679477801203514429959540281638544173538"
    )
    r4 = mpf(
        "0.54443186482151970276714708796651839555550722672128921192288"
        + "90618833999920935428791168827444874586729766166531"
    )
    assert almosteq(r1_ch, r1, eps)
    assert almosteq(r2_ch, r2, eps)
    assert almosteq(r3_ch, r3, eps)
    assert almosteq(r4_ch, r4, eps)
def test_radial_sc_equatorial_high_ecc():
    aa = 0
    slr = 6
    ecc = 0.9999
    x = 1
    r1_ch, r2_ch, r3_ch, r4_ch = radial_roots(aa, slr, ecc, x, digits)
    r1 = mpf(
        "60000.0000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000000000000"
        + "00000"
    )
    r2 = mpf(
        "3.00015000750037501875093754687734386719335966798339916995849"
        + "7924896244812240612030601530076503825191259562978148907445372"
        + "268613431"
    )
    r3 = mpf(
        "6.00000000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000000000000"
        + "0000000"
    )
    r4 = mpf("0")
    assert almosteq(r1_ch, r1, eps)
    assert almosteq(r2_ch, r2, eps)
    assert almosteq(r3_ch, r3, eps)
    assert almosteq(r4_ch, r4, eps)
예제 #10
0
def test_radial_kerr_polar_circular():
    aa = 0.9
    slr = 6
    ecc = 0
    x = 0
    r1_ch, r2_ch, r3_ch, r4_ch = radial_roots(aa, slr, ecc, x, digits)
    r1 = mpf(
        "6.000000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000"
    )
    r2 = mpf(
        "6.000000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000"
    )
    r3 = mpf(
        "4.561339049348941915090005888955490497757108282190278250268"
        + "4625285077077054441036020702100329684574966701226"
    )
    r4 = mpf(
        "0.483045392174790095490549865184069452309707597442151923012"
        + "88456761367172626346831436319680699788709729280148"
    )
    assert almosteq(r1_ch, r1, eps)
    assert almosteq(r2_ch, r2, eps)
    assert almosteq(r3_ch, r3, eps)
    assert almosteq(r4_ch, r4, eps)
예제 #11
0
def my_assert_allclose_mp(A, AA, abs_tol):

	if AA.cols != A.cols or A.rows != AA.rows:
		raise ValueError('Trying to assert_close two matrices of different size! ')

	#if matrices are complex, then we need to check real and imaginary parts separately
	#we hope that if A[0,0] is complex, then all the elements are complex
	#this is to avoid the chek inside for for loops

	if abs_tol:
		if isinstance(A[0, 0], mpmath.mpc):
			for i in range(0, A.rows):
				for j in range(0, A.cols):
					if not mpmath.almosteq(A[i,j].real, AA[i,j].real, abs_eps=abs_tol) or not mpmath.almosteq(A[i,j].imag, AA[i,j.imag], abs_eps=abs_tol):
						assert False
		else:
			for i in range(0, A.rows):
				for j in range(0, A.cols):
					if not mpmath.almosteq(A[i, j], AA[i, j], abs_eps=abs_tol) :
						assert False

	else:
		assert False

	assert True
예제 #12
0
def test_basic():
    with mpmath.workdps(50):
        ntotal = 20
        ngood = 14
        nsample = 5
        # Precomputed results using Wolfram
        #   PMF{HypergeometricDistribution[5, 14, 20], k}
        #   CDF{HypergeometricDistribution[5, 14, 20], k}
        expected_pmf = [
            mpmath.mpf('1/2584'),
            mpmath.mpf('35/2584'),
            mpmath.mpf('455/3876'),
            mpmath.mpf('455/1292'),
            mpmath.mpf('1001/2584'),
            mpmath.mpf('1001/7752'),
        ]
        expected_cdf = [
            mpmath.mpf('1/2584'),
            mpmath.mpf('9/646'),
            mpmath.mpf('509/3876'),
            mpmath.mpf('937/1938'),
            mpmath.mpf('6751/7752'),
            mpmath.mpf('1'),
        ]
        for k in range(len(expected_pmf)):
            p = hypergeometric.pmf(k, ntotal, ngood, nsample)
            assert mpmath.almosteq(p, expected_pmf[k])
            c = hypergeometric.cdf(k, ntotal, ngood, nsample)
            assert mpmath.almosteq(c, expected_cdf[k])
            s = hypergeometric.sf(k, ntotal, ngood, nsample)
            assert mpmath.almosteq(s, 1 - expected_cdf[k])
def system12_minimizer(p, l, kdpl1, kdpl2):
    p = mpf(p)
    l = mpf(l)
    kdpl1 = mpf(kdpl1)
    kdpl2 = mpf(kdpl2)
    if almosteq(kdpl1, mpf_zero, mpf_tol):
        kdpl1 += mpf_tol
    if almosteq(kdpl2, mpf_zero, mpf_tol):
        kdpl2 += mpf_tol

    def f(p_f, l_f):
        pl1 = p_f * l_f / kdpl1
        pl2 = p_f * l_f / kdpl2
        pl12 = (pl1 * l_f + pl2 * l_f) / (kdpl1 + kdpl2)
        return p - (p_f + pl1 + pl2 + pl12), l - (l_f + pl1 + pl2 + 2 * pl12)

    p_f, l_f = findroot(f, [mpf_zero, mpf_zero], tol=mpf_tol, maxsteps=1e6)
    pl1 = (p_f * l_f) / kdpl1
    pl2 = (p_f * l_f) / kdpl2
    return {
        "pf": p_f,
        "lf": l_f,
        "pl1": pl1,
        "pl2": pl2,
        "pl12": (pl1 * l_f + pl2 * l_f) / (kdpl1 + kdpl2),
    }
예제 #14
0
def test_radial_kerr_circular():
    aa = 0.9
    slr = 6
    ecc = 0
    x = 0.5
    r1_ch, r2_ch, r3_ch, r4_ch = radial_roots(aa, slr, ecc, x, digits)
    r1 = mpf(
        "6.000000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000"
    )
    r2 = mpf(
        "6.000000000000000000000000000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000"
    )
    r3 = mpf(
        "2.107201636766182477809036698351808785794321226845138552595"
        + "5383217548393926383743377377252142128804042090704"
    )
    r4 = mpf(
        "0.547821639683837105742296185097970225527479832053292059782"
        + "11748376227722039540247078504892434983412451743782"
    )
    assert almosteq(r1_ch, r1, eps)
    assert almosteq(r2_ch, r2, eps)
    assert almosteq(r3_ch, r3, eps)
    assert almosteq(r4_ch, r4, eps)
예제 #15
0
 def __eq__(self, another):
     if (isinstance(another, Opinion)):
         return (mpmath.almosteq(self.getBelief(), another.getBelief(),epsilon) and \
                 mpmath.almosteq(self.getDisbelief(), another.getDisbelief(),epsilon) and \
                 mpmath.almosteq(self.getUncertainty(), another.getUncertainty(),epsilon) and\
                 mpmath.almosteq(self.getBase(), another.getBase(),epsilon)
                 )
     return NotImplemented
예제 #16
0
 def _sf_sympy(self, t, output='float'):
     if output == 'float':
         return np.float64(self._sf_sympy(t, output='mpf'))
     if t < min(self.a) or mp.almosteq(t, min(self.a), 1e-8):
         return mp.mpf(1)
     elif t > max(self.a) or mp.almosteq(t, max(self.a), 1e-8):
         return mp.mpf(0)
     else:
         return self._symbolic_sf.subs(self._t, t)
예제 #17
0
def test_specific_values_from_wolfram():
    # The function is MarcumQ[m, a, b] in Wolfram.
    with mpmath.workdps(50):
        values = [
            [(1, 1, 2),
             mpmath.mpf(
                 '0.269012060035909996678516959220271087421337500744873384155')
             ],
            [(4, 2, 2),
             mpmath.mpf(
                 '0.961002639616864002974959310625974743642314730388958932865')
             ],
            [(10, 2, 7),
             mpmath.mpf(
                 '0.003419880268711142942584170929968429599523954396941827739')
             ],
            [(10, 2, 20),
             mpmath.mpf(
                 '1.1463968305097683198312254572377449552982286967700350e-63')
             ],
            [(25, 3, 1),
             mpmath.mpf(
                 '0.999999999999999999999999999999999985610186498600062604510')
             ],
            [(100, 5, 25),
             mpmath.mpf(
                 '6.3182094741234192918553754494227940871375208133861094e-36')
             ],
        ]
        for (m, a, b), expected in values:
            q = marcumq(m, a, b)
            assert mpmath.almosteq(q, expected)

        values = [
            [(0.5, 1, 2),
             mpmath.mpf(
                 '0.839994848036912854058580730864442945100083598568223741623')
             ],
            [(0.5, 5, 13),
             mpmath.mpf(
                 '0.99999999999999937790394257282158764840048274118115775113')
             ],
            [(0.5, 5, mpmath.mpf('1/13')),
             mpmath.mpf(
                 '2.3417168332795098320214996797055274037932802604709459e-7')],
            [(0.75, 5, mpmath.mpf('0.001')),
             mpmath.mpf(
                 '7.6243509472783061143887290663443105556790864676878435e-11')
             ],
            [(51 / 2, 5, mpmath.mpf('0.1')),
             mpmath.mpf(
                 '9.9527768099307883918427141035705197017192239618506806e-91')]
        ]
        for (m, a, b), expected in values:
            c = cmarcumq(m, a, b)
            assert mpmath.almosteq(c, expected)
예제 #18
0
def test_polar_kerr_polar_circular():
    aa = 0.9
    slr = 6
    ecc = 0
    x = 0
    zp_ch, zm_ch = polar_roots(aa, slr, ecc, x, digits)
    zm = mpf("1")
    zp = mpf("0")
    assert almosteq(zm_ch, zm, eps)
    assert almosteq(zp_ch, zp, eps)
예제 #19
0
def test_polar_sc_polar_high_ecc():
    aa = 0
    slr = 6
    ecc = 0.9999
    x = 0
    zp_ch, zm_ch = polar_roots(aa, slr, ecc, x, digits)
    zm = mpf("1")
    zp = mpf("0")
    assert almosteq(zm_ch, zm, eps)
    assert almosteq(zp_ch, zp, eps)
예제 #20
0
def test_sf_invsf():
    with mpmath.workdps(50):
        x = mpmath.mpf('1.5')
        p = benktander1.sf(x, 2, 3)
        # Expected value computed with Wolfram Alpha:
        #    SurvivalFunction[BenktanderGibratDistribution[2, 3], 3/2]
        valstr = '0.40103000157608789634710325190011195010750064239976147223'
        expected = mpmath.mpf(valstr)
        assert mpmath.almosteq(p, expected)
        x1 = benktander1.invsf(expected, 2, 3)
        assert mpmath.almosteq(x1, x)
예제 #21
0
def compareValues( result1, result2 ):
    if isinstance( result1, RPNMeasurement ):
        if isinstance( result2, RPNMeasurement ):
            return result1.__eq__( result2 )
        else:
            return almosteq( result1.getValue( ), result2 )
    else:
        if isinstance( result2, RPNMeasurement ):
            return almosteq( result1, result2.getValue( ) )
        else:
            return almosteq( result1, result2 )
예제 #22
0
def test_cdf_invcdf():
    with mpmath.workdps(50):
        x = mpmath.mpf('1.5')
        p = benktander1.cdf(x, 2, 3)
        # Expected value computed with Wolfram Alpha:
        #    CDF[BenktanderGibratDistribution[2, 3], 3/2]
        valstr = '0.59896999842391210365289674809988804989249935760023852777'
        expected = mpmath.mpf(valstr)
        assert mpmath.almosteq(p, expected)
        x1 = benktander1.invcdf(expected, 2, 3)
        assert mpmath.almosteq(x1, x)
예제 #23
0
 def _pdf_mpmath(self, t, output='float'):
     if output == 'float':
         return np.float64(self._pdf_mpmath(t, output='mpf'))
     if t<min(self.a) or t>max(self.a) or\
        mp.almosteq(t, min(self.a), 1e-8) or\
        mp.almosteq(t, max(self.a), 1e-8):
         return mp.mpf(0)
     y = [(self.N - 1) * mp.power(x, self.N - 2)
          for x in np.maximum(self.a - t, 0)]
     y = [y1 / y2 for y1, y2 in zip(y, self._proda_jni_mpmath)]
     return mp.fsum(y)
예제 #24
0
 def _sf_mpmath(self, t, output='float'):
     if output == 'float':
         return np.float64(self._sf_mpmath(t, output='mpf'))
     if t < min(self.a) or mp.almosteq(t, min(self.a), 1e-8):
         return mp.mpf(1)
     elif t > max(self.a) or mp.almosteq(t, max(self.a), 1e-8):
         return mp.mpf(0)
     else:
         y = [mp.power(max(yy, 0), self.N - 1) for yy in self.a - t]
         y = [y1 / y2 for y1, y2 in zip(y, self._proda_jni_mpmath)]
         return mp.fsum(y)
예제 #25
0
def test_polar_sc_equatorial_medium_ecc():
    aa = 0
    slr = 6
    ecc = 0.5
    x = 1
    zp_ch, zm_ch = polar_roots(aa, slr, ecc, x, digits)
    zm = mpf("0")
    zp = mpf(
        "3.61813613493316347176174480364074910973864204973384028770038052303616\
506466441146913692007177631707371674097219948449810066411613")
    assert almosteq(zm_ch, zm, eps)
    assert almosteq(zp_ch, zp, eps)
예제 #26
0
def test_polar_sc_equatorial_high_ecc():
    aa = 0
    slr = 6
    ecc = 0.9999
    x = 1
    zp_ch, zm_ch = polar_roots(aa, slr, ecc, x, digits)
    zm = mpf("0")
    zp = mpf(
        "4.24242858159851701578554748323491974415361125635078536064052378598976\
876818690433867661492273770095319175758399527157325757304661")
    assert almosteq(zm_ch, zm, eps)
    assert almosteq(zp_ch, zp, eps)
예제 #27
0
def test_polar_sc_equatorial_low_ecc():
    aa = 0
    slr = 6
    ecc = 0.1
    x = 1
    zp_ch, zm_ch = polar_roots(aa, slr, ecc, x, digits)
    zm = mpf("0")
    zp = mpf(
        "3.46988959179744133351400773640924026761245476148250978303554387894263\
507063490349890758982948765400825359194934322782659963821907")
    assert almosteq(zm_ch, zm, eps)
    assert almosteq(zp_ch, zp, eps)
예제 #28
0
def test_polar_kerr_circular_equatorial():
    aa = 0.9
    slr = 6
    ecc = 0
    x = 1
    zp_ch, zm_ch = polar_roots(aa, slr, ecc, x, digits)
    zm = mpf("0")
    zp = mpf(
        "2.81576412442877462061915538563996125884423339096478120511885059807601\
627376072301848960531274422940925849602470358318845423602414")
    assert almosteq(zm_ch, zm, eps)
    assert almosteq(zp_ch, zp, eps)
예제 #29
0
def test_Q1_identities():
    with mpmath.workdps(50):
        for a in [mpmath.mp.mpf(1), mpmath.mp.mpf(13)]:
            q1 = marcumq(1, a, a)
            expected = 0.5 * (1 + mpmath.exp(-a**2) * mpmath.besseli(0, a**2))
            assert mpmath.almosteq(q1, expected)

        for a, b in [(1, 2), (10, 3.5)]:
            total = marcumq(1, a, b) + marcumq(1, b, a)
            expected = 1 + mpmath.exp(-(a**2 + b**2) / 2) * mpmath.besseli(
                0, a * b)
            assert mpmath.almosteq(total, expected)
예제 #30
0
def test_polar_sc_circular_equatorial():
    aa = 0
    slr = 6
    ecc = 0
    x = 1
    zp_ch, zm_ch = polar_roots(aa, slr, ecc, x, digits)
    zm = mpf("0")
    zp = mpf(
        "3.46410161513775458705489268301174473388561050762076125611161395890386\
603381760007416229237351449715135125228283081340605993989019")
    assert almosteq(zm_ch, zm, eps)
    assert almosteq(zp_ch, zp, eps)
예제 #31
0
def test_constants_sc_polar_circular():
    aa = 0
    slr = 6
    ecc = 0
    x = 0
    En_ch, Lz_ch, Q_ch = calc_constants(aa, slr, ecc, x, digits)
    En = mpf("0.94280904158206336586779248280646538571311458358463204878" +
             "4453158660488318974738025900258356218427715156676")
    Lz = mpf("0")
    Q = mpf("12")
    assert almosteq(En_ch, En, eps)
    assert almosteq(Lz_ch, Lz, eps)
    assert almosteq(Q_ch, Q, eps)
예제 #32
0
def test_basic_pdf():
    with mpmath.workdps(50):

        # Precomputed results using Wolfram
        #   PDF[NoncentralChiSquareDistribution[k, lam], x}

        expected = mpmath.mpf(
            '0.12876542477554595251711369063259389422017597538321')
        assert mpmath.almosteq(ncx2.pdf(1, 2, 3), expected)

        expected = mpmath.mpf(
            '6.91617141220678829079562729724319046638618761057660e-17')
        assert mpmath.almosteq(ncx2.pdf(100, 2, 3), expected)
예제 #33
0
def mpIntDigits(num):
    if not mpmath.almosteq(num,0):
        a = mpmath.log(abs(num), b=10)
        b = mpmath.nint(a)
        if mpmath.almosteq(a,b):
            return int(b)+1
        else:
            c = mpmath.ceil(a)
            try:
                return int(c)
            except:
                pass
    else:
        return 0
예제 #34
0
 def _findLeaving(self, enteringPos):
   increase = mp.fsub(mp.inf,'1')
   idx = mp.inf
   pos = -1
   
   # find the variable with the smaller upper bound to the increase in the entering variable
   # if there are multiple choices, set the one with the smaller index on the list of basic indexes
   for i in range(self.m):
     upperBound = self._getUpperBound(self.b[i], self.A[i, enteringPos])
     
     # if this variable impose more constraint on the increase of the entering variable
     # than the current leaving variable, then this is the new leaving variable
     if upperBound <= mp.fsub(increase, self.tolerance):
       idx = self.basicIdx[i]
       pos = i
       increase = upperBound
     # if this variable impose the same constraint on the increase of the entering variable
     # than the current leaving variable  (considering the tolerance) but has a lower index, 
     # then this is the new leaving variable
     elif mp.almosteq(upperBound, increase, self.tolerance) and self.basicIdx[i] < idx:
       idx = self.basicIdx[i]
       pos = i
   
   if pos >= 0:
     return idx, pos
   else:
     return Dictionary.UNBOUNDEDCODE, Dictionary.UNBOUNDED
예제 #35
0
def testAllConversions( unitTypeTable, unitConversionMatrix ):
    print( 'Testing all conversions for consistency...' )
    print( )

    validated = 0

    for unitType, unitList in unitTypeTable.items( ):
        for unit1, unit2, unit3 in itertools.permutations( unitList, 3 ):
            try:
                factor1 = unitConversionMatrix[ unit1, unit2 ]
                factor2 = unitConversionMatrix[ unit2, unit3 ]
                factor3 = unitConversionMatrix[ unit1, unit3 ]
            except:
                continue

            epsilon = power( 10, fneg( validationPrecision ) )

            if not almosteq( fmul( factor1, factor2 ), factor3, rel_eps=epsilon ):
                print( 'conversion inconsistency found for ' + unit1 + ', ' + unit2 + ', and', unit3, file=sys.stderr )
                print( unit1 + ' --> ' + unit2, factor1, file=sys.stderr )
                print( unit2 + ' --> ' + unit3, factor2, file=sys.stderr )
                print( unit3 + ' --> ' + unit1, factor3, file=sys.stderr )
                print( )

            validated += 1

            if validated % 1000 == 0:
                print( '\r' + '{:,} conversion permutations validated...'.format( validated ), end='' )

    print( '\r' + '{:,} conversion permutations were validated to {:,} digits precision.'. \
                                                                        format( validated, validationPrecision ) )
    print( 'No consistency problems detected.' )
예제 #36
0
파일: p91.py 프로젝트: domspad/euler
def are_perp(m1,m2):
	"""
	slopes are perpendicular
	
	m1,m2 in (-inf,inf)
	"""
	if m2 == 0:
		return m1 == float('inf') or m1 == -float('inf')
	else:
		return mp.almosteq(m1,-1./m2)  # THE CHANGE TO 
예제 #37
0
def compareValues( result1, result2 ):
    if isinstance( result1, RPNMeasurement ) != isinstance( result2, RPNMeasurement ):
        return False

    if isinstance( result1, RPNMeasurement ):
        return result1.__eq__( result2 )
    else:
        if isinf( result1 ):
            if isinf( result2 ):
                return True
            else:
                raise ValueError( 'unit test failed' )
                print( '**** error in results comparison' )
                print( type( result1 ), type( result2 ) )
                print( result1, result2, 'are not equal' )

        return almosteq( result1, result2 )
예제 #38
0
파일: libm.py 프로젝트: zholos/qml
 def emit(name, iname, cdf, args, no_small=False):
     V = []
     for arg in sorted(args):
         y = cdf(*arg)
         if isinstance(y, mpf):
             e = sp.nsimplify(y, rational=True)
             if e.is_Rational and e.q <= 1000 and \
                     mp.almosteq(mp.mpf(e), y, 1e-25):
                 y = e
         else:
             y = N(y)
         V.append(arg + (y,))
     for v in V:
         if name:
             test(name, *v)
     for v in V:
         if iname and (not no_small or 1/1000 <= v[-1] <= 999/1000):
             test(iname, *(v[:-2] + v[:-3:-1]))
예제 #39
0
 def _isInteger(num, tolerance = mp.power(10,-6)):
   return mp.almosteq(num, mp.nint(num), tolerance)
예제 #40
0
db3 scaling coefficients
Wavelet Analysis: The Scalable Structure of Information (2002)
Howard L. Resnikoff, Raymond O.Jr. Wells
Table 10.3 page 263
'''
db3 = [( _1 +    sqrt10 +    mp.sqrt(_5 + _2*sqrt10))/_16,
       ( _5 +    sqrt10 + _3*mp.sqrt(_5 + _2*sqrt10))/_16,
       (_10 - _2*sqrt10 + _2*mp.sqrt(_5 + _2*sqrt10))/_16,
       (_10 - _2*sqrt10 - _2*mp.sqrt(_5 + _2*sqrt10))/_16,
       ( _5 +    sqrt10 - _3*mp.sqrt(_5 + _2*sqrt10))/_16,
       ( _1 +    sqrt10 -    mp.sqrt(_5 + _2*sqrt10))/_16]
db3 = np.array(db3)
D6 = db3

for a in [D2, D4, D6]:
    norm = mpmath.norm(a)
    assert mpmath.almosteq(norm, mp.sqrt(_2))

def wavelet_coefficients(a):
    # TODO: is this just true for Daubechies wavelets?
    N = len(a)
    return np.array([(-1)**k*a[N-1-k] for k in range(N)])

# maps from genus (i.e. vanishing moments) to the scaling coefficients 
daubechies_wavelets = {
    1: D2,
    2: D4,
    3: D6,
}