def calcPhase(particle, detector, sim, waveLen): """Calculate the phase the particle has at the detector (current phase assumed to be 0)""" detCellIdx = getBinnedDetCellIdx(particle, detector, sim) #print("Target angles:", (detCellIdx - detector.size / 2) * detector.anglePerCell) dx = detector.distance + (sim.size[0] - particle.pos[0]) * sim.cellSize[0] dzdy = (detCellIdx - detector.size / 2) * detector.cellSize dydz = np.flipud(dzdy) + (sim.size[1:] / 2 - particle.pos[1:]) * sim.cellSize[1:] distance = bf.sqrt(dx**2 + dydz[0]**2 + dydz[1]**2) phase = 2 * bf.const_pi() / BigFloat(waveLen) * distance phase = bf.mod(phase, 2 * bf.const_pi()) return phase
def calcPhaseFarField(particle, sim, waveLen): """Calculate the phase the particle has at the detector in the far field""" # Only scatter in 1 direction assert (particle.directionAngles[0] == 0 or particle.directionAngles[1] == 0) # One of the sin is 0 -> Ok to use addition instead of a full projection pathDiff = particle.pos[1] * sim.cellSize[1] * bf.sin(particle.directionAngles[0]) + \ particle.pos[2] * sim.cellSize[2] * bf.sin(particle.directionAngles[1]) # Negate as increasing position decreases phase pathDiff = -pathDiff phase = 2 * bf.const_pi() / BigFloat(waveLen) * pathDiff phase = bf.mod(phase, 2 * bf.const_pi()) return phase
def bfangle_diff(a, b): diff = a - b twopi = 2.0 * bigfloat.const_pi(bfcontext) while diff < 0.0: diff += twopi while diff >= twopi: diff -= twopi return diff
def bfbuild_nyk_ext(thetas, dups): nt = len(thetas) nyk_idx = [0] * nt nyk = [0] * nt pi = bigfloat.const_pi(bfcontext) for i, theta in enumerate(thetas): if i > 0: nyk_idx[i] = nyk_idx[i - 1] - 1 nyk[i] = nyk[i - 1] - dups[i - 1] while angle_diff(thetas[(i + nyk_idx[i]) % nt], theta) < pi: nyk[i] += dups[(i + nyk_idx[i]) % nt] nyk_idx[i] += 1 while angle_diff(thetas[(i + nyk_idx[i]) % nt], theta) > pi: nyk_idx[i] -= 1 nyk[i] -= dups[(i + nyk_idx[i]) % nt] return nyk, nyk_idx
def bfbuild_nyk(thetas): nt = len(thetas) nyk = [0] * nt pi = bigfloat.const_pi(bfcontext) for i, theta in enumerate(thetas): if i > 0: nyk[i] = nyk[i - 1] - 1 while ( angle_diff(thetas[(i + nyk[i]) % nt], theta) < pi or thetas[(i + nyk[i] + 1) % nt] == thetas[(i + nyk[i]) % nt] ): nyk[i] += 1 while ( angle_diff(thetas[(i + nyk[i]) % nt], theta) >= pi or thetas[(i + nyk[i] - 1) % nt] == thetas[(i + nyk[i]) % nt] ): nyk[i] -= 1 return nyk
#!/usr/bin/python from bigfloat import const_pi, precision from string import translate, maketrans print translate(str(const_pi(precision(5482))), maketrans('0123456789', '.Hdlro lwe'))
import sys from bigfloat import precision, BigFloat, const_pi, sqrt, asin if sys.version_info > (3, ): long = int with precision(100): L_sect_area = (1 - BigFloat(const_pi()) / 4) def calc_ratio(n): l = BigFloat(0) h = BigFloat(1) m = (l + h) * 0.5 def calc_y1(x): return x / n def diff(x): return (calc_y1(x) - (1 - sqrt(1 - (1 - x)**2))) m_d = diff(m) DIFF = BigFloat('1e-20') while abs(m_d) > DIFF: if m_d < 0: l = m else: h = m m = (l + h) * 0.5 m_d = diff(m) x = x1 = m y1 = calc_y1(x)
#!/usr/bin/python from bigfloat import const_pi, precision import math import itertools def convert_base(x, base=3, precision=None): length_of_int = int(math.log(x, base)) iexps = range(length_of_int, -1, -1) if precision == None: fexps = itertools.count(-1, -1) else: fexps = range(-1, -int(precision + 1), -1) def cbgen(x, base, exponents): for e in exponents: d = int(x // (base ** e)) x -= d * (base ** e) yield d if x == 0 and e < 0: break return cbgen(int(x), base, iexps), cbgen(x - int(x), base, fexps) gi, gf = convert_base(float(const_pi(precision(1000))), base=10) print ''.join(map(str, gf))
#!/usr/bin/python from bigfloat import const_pi, precision from string import digits, maketrans, translate print translate(str(const_pi(precision(24746))), maketrans(digits, 'rd\neHwlo! ')) #print #print translate(str(const_pi(precision(5482))), maketrans(digits, '.Hdlro lwe'))
lower, upper = interval out_interval = [bf.atan(lower, context_down), bf.atan(upper, context_up)] derivs = [1 / (1 + lower**2), 0], [0, 1 / (1 + upper**2)] return out_interval, derivs # def interval_pow(interval, context_down, context_up): # lower, upper = interval # out_interval = [bf.log(lower, context_down), bf.log(upper, context_up)] # derivs = [1 / lower, 0], [0, 1 / upper] if __name__ == '__main__': context_down = bf.precision(100) + bf.RoundTowardNegative context_up = bf.precision(100) + bf.RoundTowardPositive pi_down = bf.const_pi(context_down) pi_up = bf.const_pi(context_up) # Testing sin (lower, upper), derivs = interval_sin([5 * pi_down / 6, 7 * pi_down / 4], context_down, context_up) assert lower == -1 and 0.4 < upper < 0.6 assert derivs[0][0] == derivs[1][0] == derivs[1][1] and derivs[0][1] < -0.5 (lower, upper), derivs = interval_sin([5 * pi_down / 4, 11 * pi_down / 6], context_down, context_up) assert lower == -1 and -0.6 < upper < -0.4 assert derivs[0][0] == derivs[1][0] == derivs[0][1] and 0.8 < derivs[1][ 1] < 0.9 (lower, upper), derivs = interval_sin([pi_down / 3, 5 * pi_down / 6],