示例#1
0
 def test_get_f(self):
     alpha = math.pi / 4
     beta = math.pi / 4
     a = BigFloat.exact(0.2)
     b = BigFloat.exact(0.6)
     wavelength = BigFloat.exact(0.1)
     f = processor.get_f(alpha, beta, a, b, wavelength)
     self.assertAlmostEqual(f, 1.002 * 10 ** (-5))
示例#2
0
    def generate_random_gaussian(cls, n_dimensions, one_vector):
        mean = np.array([BigFloat.exact("0", precision=110)] * n_dimensions)
        sigma = np.array([BigFloat.exact("0", precision=110)] * n_dimensions)

        for i in range(n_dimensions):
            # TODO: Multivariate normal should accept 0 values, not require just a small value.
            mean[i] = one_vector.component(i) + random.randint(-100, 100) / 50
            # sigma[i][i] = random.randint(100, 1000) / 0.5
            sigma[i] = 3e1

        return Gaussian(mean, sigma)
示例#3
0
    def mk_bfc(self, x, y):

        from bigfloat import BigFloat, precision
        from mbrat.util import Arguments

        with precision(self.prec):
            bf_x = BigFloat.exact(x)
            bf_y = BigFloat.exact(y)

        return Arguments(
            {'real': bf_x, 'imag': bf_y},
            string="bfc(real='{0}', imag='{1}')".format(bf_x, bf_y) )
def compute_boltzmann(energies, debug):
    # create Boltzmann distribution
    # https://en.wikipedia.org/wiki/Boltzmann_distribution
    # p_i = exp( -E_i / kT ) / ∑ exp( -E_j / kT )
    # where:
    #    p_i is the probability of state i occuring
    #    E_i is the energy of state i
    #    E_j is the energy of state j, where the ∑ in the denominator
    #    iterates over all states j
    # first we calculate exp( -E_i / kT ) for all states
    if len(energies) == 0:
        return []
    if debug:
        print("Boltzmann Distribution")
    divisor = BigFloat.exact(0.0)
    for energy in energies:
        ep = bigfloat.exp(-energy)
        if debug:
            print("energy, ep = %g, %g" % (energy, ep))
        # divisor = ∑ exp( -E_j / kT )
        divisor += ep
    probabilities = []
    for energy in energies:
        # p_i = exp( -E_i / kT ) / divisor
        numerator = bigfloat.exp(-energy)
        probability = numerator / divisor
        # save probability to dictionary
        probabilities.append(float(probability))
        if debug:
            print("%s / %s = %s" % (numerator, divisor, probability))

    return probabilities
示例#5
0
 def __json_list_to_numpy_list(self, jsonList):
     newList = []
     for v in iter(jsonList):
         newList.append(
             BigFloat.exact(v[Gaussian.__VALUE],
                            precision=v[Gaussian.__PRECISION]))
     return np.array(newList)
示例#6
0
def __super_vectors_mean(svectors, weights):
    dimensions = svectors[0].dimensions()
    mean = np.array([BigFloat.exact("0", precision=110)] * dimensions)
    for i in range(dimensions):
        for svec_id in range(len(svectors)):
            mean[i] += svectors[svec_id].component(i) * weights[svec_id]
        mean[i] /= sum(weights)
    return mean
示例#7
0
def __probabilities_to_weights(probabilities):
    weights = []
    sums = np.array([BigFloat.exact("0", precision=110)] *
                    len(probabilities[0]))
    for p in iter(probabilities):
        weights.append(np.array(p))
        sums += weights[len(weights) - 1]

    for i in range(len(weights)):
        weights[i] /= sums

    return weights
示例#8
0
def __super_vectors_sigma(svectors, mean, weights):
    dimensions = svectors[0].dimensions()
    values_from_dimensions = __supervectors_to_array_of_values_for_each_dimension(
        svectors)
    sigma = np.array([BigFloat.exact("0", precision=110)] * dimensions)
    for i in range(dimensions):
        sigma[i] = __weighted_variance(mean[i], values_from_dimensions[i],
                                       weights)
        # if math.isclose(sigma[i], 0, abs_tol=3.0e-20):
        # print(sigma[i][i])
        # raise NoVarianceException
        # sigma[i] = 3.0e-20
    return sigma
示例#9
0
from decimal import *
import sys
import numpy as np
from bigfloat import BigFloat
import math

finput = open(sys.argv[1], 'r')
foutput = open(sys.argv[2], 'w')

n = int(finput.readline())
while n > 0:
    line = finput.readline().split(" ")
    a = BigFloat.exact(line[0], precision=1000)  # glucose
    b = BigFloat.exact(line[1], precision=1000)  # oxygen
    x = BigFloat.exact(line[2], precision=1000)  # bank
    print "\n$%.2f in bank, $%.2f for glucose, $%.2f for oxygen" % (x, a, b)
    o = 12 * a * x / (457 * a + 6 * b)
    g = (x - b * o) / a
    print "Buy %f oxygen and %f glucose" % (o, g)
    print "%f moles of oxygen at $%f/mole costs %f" % (o, b, o * b)
    print "%f moles of glucose at $%f/mole costs %f" % (g, a, g * a)
    r = 38 * (o / 6)
    print "%f moles of ATP by respiration" % r
    ferment = g - (o / 6)
    print "%f moles of ATP by fermentation" % ferment
    ATP = r + ferment
    print "%f moles of ATP total" % ATP
    # if 38*a < (a + 6*b) :
    # 	ATP = x / a
    # else:
    # 	ATP = (38 * x) / (6 * b + a)
示例#10
0
 def __multiply_sigma(self):
     result = BigFloat.exact('1', precision=110)
     for v in iter(self.__sigma):
         result *= v
     return result
示例#11
0
 def __double_pi_powered_by(self, value):
     doubled = 2 * math.pi
     result = BigFloat.exact('1', precision=110)
     for _ in range(value):
         result *= doubled
     return result
示例#12
0
 def __calculate_fraction(self):
     powered_pi = self.__double_pi_powered_by(self.__d)
     return BigFloat.exact('1', precision=110) / bigfloat.sqrt(
         (powered_pi * self.__multiply_sigma()))