示例#1
0
文件: scheme.py 项目: Nasrollah/pylbm
    def _create_moments_matrices(self):
        """
        Create the moments matrices M and M^{-1} used to transform the repartition functions into the moments

        Three versions of these matrices are computed:

          - a sympy version M and invM for each scheme
          - a numerical version Mnum and invMnum for each scheme
          - a global numerical version MnumGlob and invMnumGlob for all the schemes
        """
        M_, invM_, Mu_, Tu_ = [], [], [], []
        u_tild = sp.Matrix([rel_ux, rel_uy, rel_uz])

        if self.symb_la is not None:
            LA = self.symb_la
        else:
            LA = self.la

        compt = 0
        for iv, v in enumerate(self.stencil.v):
            p = self.P[self.stencil.nv_ptr[iv]:self.stencil.nv_ptr[iv + 1]]
            compt += 1
            lv = len(v)
            M_.append(sp.zeros(lv, lv))
            Mu_.append(sp.zeros(lv, lv))
            for i in range(lv):
                for j in range(lv):
                    sublist = [(str(self.symb_coord[d]),
                                sp.Integer(v[j].v[d]) * LA)
                               for d in range(self.dim)]
                    M_[-1][i, j] = p[i].subs(sublist)

                    if self.rel_vel is not None:
                        sublist = [(str(self.symb_coord[d]),
                                    v[j].v[d] * LA - u_tild[d])
                                   for d in range(self.dim)]
                        Mu_[-1][i, j] = p[i].subs(sublist)

            invM_.append(M_[-1].inv())
            Tu_.append(Mu_[-1] * invM_[-1])

        gshape = (self.stencil.nv_ptr[-1], self.stencil.nv_ptr[-1])
        Tu = sp.eye(gshape[0])
        M = sp.zeros(*gshape)
        invM = sp.zeros(*gshape)

        try:
            for k in range(self.nschemes):
                nvk = self.stencil.nv[k]
                for i in range(nvk):
                    for j in range(nvk):
                        index = self.stencil.nv_ptr[
                            k] + i, self.stencil.nv_ptr[k] + j
                        M[index] = M_[k][i, j]
                        invM[index] = invM_[k][i, j]

                        if self.rel_vel is not None:
                            Tu[index] = Tu_[k][i, j]
        except TypeError:
            log.error(
                "Unable to convert to float the expression %s or %s.\nCheck the 'parameters' entry.",
                M[k][i, j], invM[k][i, j])  #pylint: disable=undefined-loop-variable
            sys.exit()

        alltogether(Tu, nsimplify=True)
        alltogether(M, nsimplify=True)
        alltogether(invM, nsimplify=True)
        Tmu = Tu.subs(list(zip(u_tild, -u_tild)))
        return M, invM, Tu, Tmu
示例#2
0
 def evaluate(self, evaluation):
     return Complex(sympy.Integer(0), sympy.Integer(1))
示例#3
0
   3
   |\
   | \    positive normal in CCW
   |  \   ----------------------
   |___\
   1    2

"""
DOF = 2
num_nodes = 3

sympy.var('h, A', positive=True, real=True)
sympy.var('x1, y1, x2, y2, x3, y3, x, y', real=True, positive=True)
sympy.var('rho, E, nu', real=True, positive=True)

ONE = sympy.Integer(1)

R = CoordSys3D('R')
r1 = x1 * R.i + y1 * R.j
r2 = x2 * R.i + y2 * R.j
r3 = x3 * R.i + y3 * R.j
r = x * R.i + y * R.j

Aexpr = cross(r1 - r3, r2 - r3).components[R.k] / 2
print('A =', Aexpr)

AN1 = cross(r2 - r, r3 - r).components[R.k] / 2
AN2 = cross(r3 - r, r1 - r).components[R.k] / 2
N1 = simplify(AN1 / A)
N2 = simplify(AN2 / A)
N3 = simplify((A - AN1 - AN2) / A)
示例#4
0
import sympy as sp

x, y = sp.var("x y")

linear_2d = [sp.Integer(1), x, y]
quadratic_2d = [sp.Integer(1), x, x**2, y, y**2, x * y]
cubic_2d = [
    sp.Integer(1), x, x**2, y, y**2, x * y, x**3, y**3, y * x**2, x * y**2
]
示例#5
0
    def apply(self, items, evaluation):
        'Times[items___]'

        #TODO: Clean this up and optimise it

        items = items.numerify(evaluation).get_sequence()
        number = (sympy.Integer(1), sympy.Integer(0))
        leaves = []

        prec = min_prec(*items)
        is_real = all([not isinstance(i, Complex) for i in items])

        for item in items:
            if isinstance(item, Number):
                if isinstance(item, Complex):
                    sym_real, sym_imag = item.real.to_sympy(
                    ), item.imag.to_sympy()
                else:
                    sym_real, sym_imag = item.to_sympy(), sympy.Integer(0)

                if prec is not None:
                    sym_real = sym_real.n(dps(prec))
                    sym_imag = sym_imag.n(dps(prec))

                if sym_real.is_zero and sym_imag.is_zero and prec is None:
                    return Integer('0')
                number = (number[0] * sym_real - number[1] * sym_imag,
                          number[0] * sym_imag + number[1] * sym_real)
            elif leaves and item == leaves[-1]:
                leaves[-1] = Expression('Power', leaves[-1], Integer(2))
            elif leaves and item.has_form('Power', 2) and leaves[-1].has_form(
                    'Power', 2) and item.leaves[0].same(leaves[-1].leaves[0]):
                leaves[-1].leaves[1] = Expression('Plus', item.leaves[1],
                                                  leaves[-1].leaves[1])
            elif leaves and item.has_form('Power', 2) and item.leaves[0].same(
                    leaves[-1]):
                leaves[-1] = Expression(
                    'Power', leaves[-1],
                    Expression('Plus', item.leaves[1], Integer(1)))
            elif leaves and leaves[-1].has_form(
                    'Power', 2) and leaves[-1].leaves[0].same(item):
                leaves[-1] = Expression(
                    'Power', item,
                    Expression('Plus', Integer(1), leaves[-1].leaves[1]))
            else:
                leaves.append(item)
        if number == (1, 0):
            number = None
        elif number == (-1, 0) and leaves and leaves[0].has_form('Plus', None):
            leaves[0].leaves = [
                Expression('Times', Integer(-1), leaf)
                for leaf in leaves[0].leaves
            ]
            number = None

        if number is not None:
            if number[1].is_zero and is_real:
                leaves.insert(0, Number.from_mp(number[0], prec))
            elif number[1].is_zero and number[1].is_Integer and prec is None:
                leaves.insert(0, Number.from_mp(number[0], prec))
            else:
                leaves.insert(
                    0,
                    Complex(from_sympy(number[0]), from_sympy(number[1]),
                            prec))

        if not leaves:
            return Integer(1)
        elif len(leaves) == 1:
            return leaves[0]
        else:
            return Expression('Times', *leaves)
示例#6
0
 def test_ratio_to_cents(self):
     self.assertEqual(0, ratio_to_cents(1))
     self.assertEqual(0, ratio_to_cents(sp.Integer(1)))
     self.assertEqual(1200, ratio_to_cents(2))
     self.assertEqual(1200, ratio_to_cents(sp.Integer(2)))
     self.assertAlmostEqual(701.95500086, ratio_to_cents(sp.Rational(3, 2)))
示例#7
0
quad_num = quad.Quad.gauss_hermite(nquad,
                                   dirs=[1, 2],
                                   mean=[0, 0],
                                   cov=[[σy, 0], [0, σz]])

# Discretization in Hermite space
fyz = sym.Function('fyz')(y, z)

op = quad_num.discretize_op(L0.subs(f, fyz),
                            degree,
                            sparse=False,
                            index_set="triangle")
# }}}
# Expansion of the solution {{{
nterms = 7
zeros = [sym.Integer(0)] * nterms
u, centered, unk = zeros, zeros.copy(), zeros.copy()


def iL0(term):
    t_rhs = quad_num.transform(term, degree)
    solution = la.solve(op.matrix[1:, 1:], t_rhs.coeffs[1:])
    solution = np.array([0, *solution])
    sol_series = series.Series(solution, t_rhs.position, significant=14)
    symbolic = sol_series.to_function().as_xyz()
    symbolic = func.Function.sanitize(symbolic, max_denom=1e8)

    print("--> Solving cell problem with rhs: " + str(term))
    print("----> Solution: " + str(symbolic))
    diff = (L0.subs(f, symbolic).doit() - term)
    if len(diff.expand().free_symbols) > 0:
 def _visit_int(self, tree: IntegerLiteralCalchasExpression,
                *args) -> sympy.Expr:
     return sympy.Integer(tree.value)
示例#9
0
 def create_mgf_expr(self, coal_rates, migration_rates, initial_config):
     """ Generates the mgf, only should be called by __init__
     Arguments:
     coal_rates      -- a list giving the coalescent rate in each deme
                        ex: [1, 1]
     migration_rates -- a matrix giving the migration rates from each deme to each other deme
                        ex: sympy.Matrix([[0, 1], [1, 0]])
     initial_config  -- a list giving the starting configuration of the lineages
                        ex: [ [[1], [2]], [[3]] ]
     """
     Eqns = []
     num_demes = len(coal_rates)
     # Get all partitions of starting lineages aka all coalescent states
     indv_partitions = list(partition(self.lineages))
     indv_partitions.remove([[
         individual for lineage in self.lineages for individual in lineage
     ]])
     deme_partitions = [
         deme_part for indv_partition in indv_partitions
         for deme_part in deme_partition(indv_partition, num_demes)
     ]
     print("There are " + str(len(deme_partitions)) + " possible states.")
     print("Setting up a system of " + str(len(deme_partitions)) +
           " equations...")
     deme_part_symbols = []
     all_symbols = []
     for deme_part in deme_partitions:
         print("Working with partition: " + str(deme_part))
         deme_part_symbol = deme_part_to_symbol(deme_part)
         deme_part_symbols.append(deme_part_symbol)
         all_symbols.append(deme_part_symbol)
         # Make the factor for this partition
         print("Creating the factor for this partition")
         deme_part_factor = sympy.Integer(0)
         for deme_idx, deme in enumerate(deme_part):
             n_deme_lineages = len(deme)
             deme_part_factor += sympy.binomial(n_deme_lineages,
                                                2) * coal_rates[deme_idx]
             for other_deme_idx in range(num_demes):
                 if deme_idx != other_deme_idx:
                     deme_part_factor += n_deme_lineages * migration_rates[
                         deme_idx, other_deme_idx]
             for lineage in deme:
                 deme_part_factor -= sympy.symbols(
                     's_' + '.'.join([str(tip) for tip in sorted(lineage)]))
         # Add the left side in (sign is negative, see eq 8 Lohse et al.)
         deme_part_eqn = -deme_part_factor * deme_part_symbol
         # sympy.pprint(deme_part_eqn)
         # Add the transfer rates to coalescent states
         print("Adding rates to other coal states")
         for deme_idx, deme in enumerate(deme_part):
             for pair in itertools.combinations(deme, 2):
                 # If only two lineages left, the next coal event reduces us to one
                 if sum([len(deme_lins) for deme_lins in deme_part]) == 2:
                     symbol_after_coal = sympy.Integer(1)
                 else:
                     symbol_after_coal = deme_part_symbol_after_coal(
                         deme_part, pair)
                     all_symbols.append(symbol_after_coal)
                 deme_part_eqn += coal_rates[deme_idx] * symbol_after_coal
         # Add the transfer rates for migration events
         print("Adding rates to other migration states")
         for deme_idx, deme in enumerate(deme_part):
             # For each lineage in the deme add a term for the probability of that lineage
             # immigrating to each other deme
             for lin in deme:
                 for deme_to_idx in range(num_demes):
                     deme_part_eqn += (
                         migration_rates[deme_idx, deme_to_idx] *
                         deme_part_symbol_after_migr(
                             deme_part, lin, deme_idx, deme_to_idx))
                     all_symbols.append(
                         deme_part_symbol_after_migr(
                             deme_part, lin, deme_idx, deme_to_idx))
         print("Done! Adding this to the list of equations")
         Eqns.append(deme_part_eqn)
     print("The terms being solved for are:")
     for deme_part_symbol in deme_part_symbols:
         sympy.pprint(deme_part_symbol)
     all_symbols = set(all_symbols)
     print("There are " + str(len(all_symbols)) +
           " terms in the system of equation")
     for symbol in all_symbols:
         sympy.pprint(symbol)
     starting_part = deme_part_to_symbol(initial_config)
     starting_idx = deme_part_symbols.index(starting_part)
     print('Solving system of equations...')
     mgf_solution = sympy.linsolve(Eqns, *deme_part_symbols)
     return list(mgf_solution)[0][starting_idx]
示例#10
0
    def apply(self, items, evaluation):
        "Plus[items___]"

        items = items.numerify(evaluation).get_sequence()
        leaves = []
        last_item = last_count = None

        prec = min_prec(*items)
        is_machine_precision = any(item.is_machine_precision()
                                   for item in items)
        numbers = []

        def append_last():
            if last_item is not None:
                if last_count == 1:
                    leaves.append(last_item)
                else:
                    if last_item.has_form("Times", None):
                        leaves.append(
                            Expression("Times", from_sympy(last_count),
                                       *last_item.leaves))
                    else:
                        leaves.append(
                            Expression("Times", from_sympy(last_count),
                                       last_item))

        for item in items:
            if isinstance(item, Number):
                numbers.append(item)
            else:
                count = rest = None
                if item.has_form("Times", None):
                    for leaf in item.leaves:
                        if isinstance(leaf, Number):
                            count = leaf.to_sympy()
                            rest = item.get_mutable_leaves()
                            rest.remove(leaf)
                            if len(rest) == 1:
                                rest = rest[0]
                            else:
                                rest.sort()
                                rest = Expression("Times", *rest)
                            break
                if count is None:
                    count = sympy.Integer(1)
                    rest = item
                if last_item is not None and last_item == rest:
                    last_count = last_count + count
                else:
                    append_last()
                    last_item = rest
                    last_count = count
        append_last()

        if numbers:
            if prec is not None:
                if is_machine_precision:
                    numbers = [item.to_mpmath() for item in numbers]
                    number = mpmath.fsum(numbers)
                    number = from_mpmath(number)
                else:
                    with mpmath.workprec(prec):
                        numbers = [item.to_mpmath() for item in numbers]
                        number = mpmath.fsum(numbers)
                        number = from_mpmath(number, dps(prec))
            else:
                number = from_sympy(sum(item.to_sympy() for item in numbers))
        else:
            number = Integer0

        if not number.sameQ(Integer0):
            leaves.insert(0, number)

        if not leaves:
            return Integer0
        elif len(leaves) == 1:
            return leaves[0]
        else:
            leaves.sort()
            return Expression("Plus", *leaves)
def construct_objective_function(window_length, step_length, n_classes):
    p = [sympy.Symbol("P_" + str(i)) for i in range(n_classes)]
    c = [sympy.Symbol("C_" + str(i)) for i in range(n_classes)]

    m = sympy.Symbol("P(M)")

    p_given_m = [
        sympy.Symbol("P(P_" + str(i) + "|M)") for i in range(n_classes)
    ]
    p_given_c = [[
        sympy.Symbol("P(P_" + str(i) + "|C_" + str(j) + ")")
        for j in range(n_classes)
    ] for i in range(n_classes)]
    c_given_m = [
        sympy.Symbol("P(C_" + str(j) + "|M)") for j in range(n_classes)
    ]
    m_given_c = [
        sympy.Symbol("P(M|C_" + str(j) + ")") for j in range(n_classes)
    ]
    p_given_cm = [[
        sympy.Symbol("P(P_" + str(i) + "|C_" + str(j) + ",M)")
        for j in range(n_classes)
    ] for i in range(n_classes)]

    f_given_c = [[
        sympy.Function("F_" + str(i) + "C_" + str(j) + "")
        for j in range(n_classes)
    ] for i in range(n_classes)]

    t = [sympy.Symbol("t_" + str(i)) for i in range(n_classes)]

    itr = sympy.Add(*[
        p_given_cm[i][j] * c_given_m[j] * sympy.Piecewise(
            (0, sympy.Le(p_given_m[i], 0)), (0, sympy.Le(p_given_cm[i][j], 0)),
            (sympy.log(p_given_cm[i][j] / p_given_m[i], 2), True))
        for i in range(n_classes) for j in range(n_classes)
    ])
    # itr = sympy.Add(*[p_given_cm[i][j]*c_given_m[j]*sympy.log(p_given_cm[i][j]/p_given_m[i], 2) for i in range(n_classes) for j in range(n_classes)])

    mdt = window_length + (1 / m - 1) * step_length

    unfolded_itr = itr * 60 / mdt

    unfolded_itr = unfolded_itr.subs({
        p_given_cm[i][j]: p_given_c[i][j] / m_given_c[j]
        for i in range(n_classes) for j in range(n_classes)
    })
    unfolded_itr = unfolded_itr.subs({
        m_given_c[j]: sympy.Add(*[p_given_c[i][j] for i in range(n_classes)])
        for j in range(n_classes)
    })
    unfolded_itr = unfolded_itr.subs({
        c_given_m[j]:
        sympy.Add(*[p_given_c[i][j] * c[j] / m for i in range(n_classes)])
        for j in range(n_classes)
    })
    unfolded_itr = unfolded_itr.subs(
        {p_given_m[i]: p[i] / m
         for i in range(n_classes)})
    unfolded_itr = unfolded_itr.subs(
        {m: sympy.Add(*[p[i] for i in range(n_classes)])})
    unfolded_itr = unfolded_itr.subs({
        p[i]: sympy.Add(*[p_given_c[i][j] * c[j] for j in range(n_classes)])
        for i in range(n_classes)
    })
    unfolded_itr = unfolded_itr.subs(
        {c[j]: sympy.Integer(1) / n_classes
         for j in range(n_classes)})
    unfolded_itr = unfolded_itr.subs({
        p_given_c[i][k]: (1 - f_given_c[i][k](t[i])) *
        sympy.Mul(*[f_given_c[j][k](t[j]) for j in range(n_classes) if j != i])
        for i in range(n_classes) for k in range(n_classes)
    })

    itr_gradient = [unfolded_itr.diff(t[i]) for i in range(n_classes)]

    itr_function = sympy.lambdify(
        [e(t[i]) for i, l in enumerate(f_given_c) for e in l], unfolded_itr,
        "numpy")

    gradient_arguments = [
        sympy.Derivative(f_given_c[i][j](t[i]), t[i]) for i in range(n_classes)
        for j in range(n_classes)
    ]
    gradient_arguments += [
        f_given_c[i][j](t[i]) for i in range(n_classes)
        for j in range(n_classes)
    ]
    gradient_function = sympy.lambdify(gradient_arguments, itr_gradient,
                                       "numpy")
    return itr_function, gradient_function
示例#12
0
 def test_max_shear(self):
     # Test does not pass due to numerical error if a simple comparison (==) is made.
     # Must compute the error (difference) and "chop" off very small values.
     difference = compute_difference(sp.Float(self.strain_state.max_shear),
                                     1.732579428363763e-05)
     self.assertEqual(difference, sp.Integer(0))
示例#13
0
 def test_octahedral_shear(self):
     # Test does not pass due to numerical error if a simple comparison (==) is made.
     # Must compute the error (difference) and "chop" off very small values.
     difference = compute_difference(self.strain_state.octahedral_shear,
                                     1.45907124188262e-5)
     self.assertEqual(difference, sp.Integer(0))
def main():
    sympy.var('xi, eta, lex, ley, rho')
    sympy.var('R')
    sympy.var('A11, A12, A16, A22, A26, A66')
    sympy.var('B11, B12, B16, B22, B26, B66')
    sympy.var('D11, D12, D16, D22, D26, D66')

    ONE = sympy.Integer(1)

    # shape functions
    # - from Reference:
    #     OCHOA, O. O.; REDDY, J. N. Finite Element Analysis of Composite Laminates. Dordrecht: Springer, 1992.
    # bi-linear
    Li = lambda xii, etai: ONE / 4. * (1 + xi * xii) * (1 + eta * etai)
    # cubic
    Hwi = lambda xii, etai: ONE / 16. * (xi + xii)**2 * (xi * xii - 2) * (
        eta + etai)**2 * (eta * etai - 2)
    Hwxi = lambda xii, etai: -lex / 32. * xii * (xi + xii)**2 * (
        xi * xii - 1) * (eta + etai)**2 * (eta * etai - 2)
    Hwyi = lambda xii, etai: -ley / 32. * (xi + xii)**2 * (
        xi * xii - 2) * etai * (eta + etai)**2 * (eta * etai - 1)
    Hwxyi = lambda xii, etai: lex * ley / 64. * xii * (xi + xii)**2 * (
        xi * xii - 1) * etai * (eta + etai)**2 * (eta * etai - 1)

    # node 1 (-1, -1)
    # node 2 (+1, -1)
    # node 3 (+1, +1)
    # node 4 (-1, +1)

    Nu = sympy.Matrix([[
        #u, v, w,     , phix  , phiy,  d2w/(dxdy)
        Li(-1, -1),
        0,
        0,
        0,
        0,
        0,
        Li(+1, -1),
        0,
        0,
        0,
        0,
        0,
        Li(+1, +1),
        0,
        0,
        0,
        0,
        0,
        Li(-1, +1),
        0,
        0,
        0,
        0,
        0,
    ]])
    Nv = sympy.Matrix([[
        #u, v, w,     , phix  , phiy,  d2w/(dxdy)
        0,
        Li(-1, -1),
        0,
        0,
        0,
        0,
        0,
        Li(+1, -1),
        0,
        0,
        0,
        0,
        0,
        Li(+1, +1),
        0,
        0,
        0,
        0,
        0,
        Li(-1, +1),
        0,
        0,
        0,
        0,
    ]])
    Nw = sympy.Matrix([[
        #u, v, w,     , phix  , phiy,  d2w/(dxdy)
        0,
        0,
        Hwi(-1, -1),
        Hwxi(-1, -1),
        Hwyi(-1, -1),
        Hwxyi(-1, -1),
        0,
        0,
        Hwi(+1, -1),
        Hwxi(+1, -1),
        Hwyi(+1, -1),
        Hwxyi(+1, -1),
        0,
        0,
        Hwi(+1, +1),
        Hwxi(+1, +1),
        Hwyi(+1, +1),
        Hwxyi(+1, +1),
        0,
        0,
        Hwi(-1, +1),
        Hwxi(-1, +1),
        Hwyi(-1, +1),
        Hwxyi(-1, +1),
    ]])
    A = sympy.Matrix([[A11, A12, A16], [A12, A22, A26], [A16, A26, A66]])
    B = sympy.Matrix([[B11, B12, B16], [B12, B22, B26], [B16, B26, B66]])
    D = sympy.Matrix([[D11, D12, D16], [D12, D22, D26], [D16, D26, D66]])

    # membrane
    Nu_x = (2 / lex) * Nu.diff(xi)
    Nu_y = (2 / ley) * Nu.diff(eta)
    Nv_x = (2 / lex) * Nv.diff(xi)
    Nv_y = (2 / ley) * Nv.diff(eta)
    Bm = sympy.Matrix([Nu_x, Nv_y + 1 / R * Nw, Nu_y + Nv_x])

    # bending
    Nphix = -(2 / lex) * Nw.diff(xi)
    Nphiy = -(2 / ley) * Nw.diff(eta)
    Nphix_x = (2 / lex) * Nphix.diff(xi)
    Nphix_y = (2 / ley) * Nphix.diff(eta)
    Nphiy_x = (2 / lex) * Nphiy.diff(xi)
    Nphiy_y = (2 / ley) * Nphiy.diff(eta)
    Bb = sympy.Matrix([Nphix_x, Nphiy_y, Nphix_y + Nphiy_x])

    # Constitutive linear stiffness matrix
    Ke = sympy.zeros(4 * DOF, 4 * DOF)
    Ke[:, :] = (lex * ley) / 4. * (Bm.T * A * Bm + Bm.T * B * Bb +
                                   Bb.T * B * Bm + Bb.T * D * Bb)
    #TODO nonlinear terms

    # integrating matrices in natural coordinates
    #TODO integrate only upper or lower triangle

    print('Integrating Ke')
    integrands = []
    for ind, integrand in np.ndenumerate(Ke):
        integrands.append(integrand)
    p = Pool(cpu_count)
    a = list(p.map(integrate_simplify, integrands))
    for i, (ind, integrand) in enumerate(np.ndenumerate(Ke)):
        Ke[ind] = a[i]

    # K represents the global stiffness matrix
    # in case we want to apply coordinate transformations
    K = Ke

    def name_ind(i):
        if i >= 0 and i < DOF:
            return 'c1'
        elif i >= DOF and i < 2 * DOF:
            return 'c2'
        elif i >= 2 * DOF and i < 3 * DOF:
            return 'c3'
        elif i >= 3 * DOF and i < 4 * DOF:
            return 'c4'
        else:
            raise

    print('printing for code')
    for ind, val in np.ndenumerate(K):
        i, j = ind
        si = name_ind(i)
        sj = name_ind(j)
        print('        K[%d+%s, %d+%s]' % (i % DOF, si, j % DOF, sj), '+=',
              K[ind])
示例#15
0
"""

from __future__ import division, print_function

import unittest, sys

import sympy as sp

from pytuning.utilities import normalize_interval, distinct_intervals, get_mode_masks, mask_scale, \
    mask_to_steps, ratio_to_cents, cents_to_ratio, note_number_to_freq, \
    compare_two_scales, ratio_to_name

from pytuning.scales import create_edo_scale, create_pythagorean_scale

pythag_scale = [
    sp.Integer(1),
    sp.Rational(256, 243),
    sp.Rational(9, 8),
    sp.Rational(32, 27),
    sp.Rational(81, 64),
    sp.Rational(4, 3),
    sp.Rational(1024, 729),
    sp.Rational(3, 2),
    sp.Rational(128, 81),
    sp.Rational(27, 16),
    sp.Rational(16, 9),
    sp.Rational(243, 128),
    sp.Integer(2),
]

masked_pythag_scale = [
示例#16
0
文件: test_stuff.py 项目: vdods/vorpy
def mu(qp):
    x, y, z = qp[0, :]

    beta = sp.Integer(16)

    return r_squared(qp)**2 + beta * z**2
示例#17
0
 def test_ratio_to_name(self):
     self.assertEqual("Perfect Fifth", ratio_to_name(sp.Rational(3, 2)))
     self.assertTrue(ratio_to_name(sp.Integer(12)) is None)
示例#18
0
    def calculate_cache_access(self):
        # FIXME handle multiple datatypes
        element_size = self.kernel.datatypes_size[self.kernel.datatype]
        
        results = {'dimensions': {}}
        
        def sympy_compare(a,b):
            c = 0
            for i in range(min(len(a), len(b))):
                s = a[i] - b[i]
                if sympy.simplify(s > 0):
                    c = -1
                elif sympy.simplify(s == 0):
                    c = 0
                else:
                    c = 1
                if c != 0: break
            return c
        
        accesses = defaultdict(list)
        sympy_accesses = defaultdict(list)
        for var_name in self.kernel.variables:
            for r in self.kernel._sources.get(var_name, []):
                if r is None: continue
                accesses[var_name].append(r)
                sympy_accesses[var_name].append(self.kernel.access_to_sympy(var_name, r))
            for w in self.kernel._destinations.get(var_name, []):
                if w is None: continue
                accesses[var_name].append(w)
                sympy_accesses[var_name].append(self.kernel.access_to_sympy(var_name, w))
            # order accesses by increasing order
            accesses[var_name].sort(key=cmp_to_key(sympy_compare))#cmp=sympy_compare)
        
        results['accesses'] = accesses
        results['sympy_accesses'] = sympy_accesses
        
        # For each dimension (1D, 2D, 3D ... nD)
        for dimension in range(1, len(list(self.kernel.get_loop_stack()))+1):
            results['dimensions'][dimension] = {}
            
            slices = defaultdict(list)
            slices_accesses = defaultdict(list)
            for var_name in accesses:
                for a in accesses[var_name]:
                    # slices are identified by the tuple of indices of higher dimensions
                    slice_id = tuple([var_name, tuple(a[:-dimension])])
                    slices[slice_id].append(a)
                    slices_accesses[slice_id].append(self.kernel.access_to_sympy(var_name, a))
            results['dimensions'][dimension]['slices'] = slices
            results['dimensions'][dimension]['slices_accesses'] = slices_accesses
            
            slices_distances = defaultdict(list)
            for k,v in slices_accesses.items():
                for i in range(1, len(v)):
                    slices_distances[k].append((v[i-1] - v[i]).simplify())
            results['dimensions'][dimension]['slices_distances'] = slices_distances
            
            # Check that distances contain only free_symbols based on constants
            for dist in chain(*slices_distances.values()):
                if any([s not in self.kernel.constants.keys() for s in dist.free_symbols]):
                    raise ValueError("Some distances are not based on non-constants: "+str(dist))
            
            # Sum of lengths between relative distances
            slices_sum = sum([sum(dists) for dists in slices_distances.values()])
            results['dimensions'][dimension]['slices_sum'] = slices_sum
            
            # Max of lengths between relative distances
            # Work-around, the arguments with the most symbols get to stay
            # FIXME, may not be correct in all cases. e.g., N+M vs. N*M
            def FuckedUpMax(*args):
                if len(args) == 1:
                    return args[0]
                # expand all expressions:
                args = [a.expand() for a in args]
                # Filter expressions with less than the maximum number of symbols
                max_symbols = max([len(a.free_symbols) for a in args])
                args = list(filter(lambda a: len(a.free_symbols) == max_symbols, args))
                if max_symbols == 0:
                    return sympy.Max(*args)
                # Filter symbols with lower exponent
                max_coeffs = 0
                for a in args:
                    for s in a.free_symbols:
                        max_coeffs = max(max_coeffs, len(sympy.Poly(a, s).all_coeffs()))
                def coeff_filter(a):
                    return max(
                        0, 0,
                        *[len(sympy.Poly(a, s).all_coeffs()) for s in a.free_symbols]) == max_coeffs
                args = list(filter(coeff_filter, args))

                m = sympy.Max(*args)
                #if m.is_Function:
                #    raise ValueError("Could not resolve {} to maximum.".format(m))
                return m

            slices_max = FuckedUpMax(sympy.Integer(0),
                                     *[FuckedUpMax(*dists) for dists in slices_distances.values()])
            results['dimensions'][dimension]['slices_sum'] = slices_sum
            
            # Nmber of slices
            slices_count = len(slices_accesses)
            results['dimensions'][dimension]['slices_count'] = slices_count
            
            # Cache requirement expression
            cache_requirement_bytes = (slices_sum + slices_max*slices_count)*element_size
            results['dimensions'][dimension]['cache_requirement_bytes'] = cache_requirement_bytes
            
            # Apply to all cache sizes
            csim = self.machine.get_cachesim()
            results['dimensions'][dimension]['caches'] = {}
            for cl in  csim.levels(with_mem=False):
                cache_equation = sympy.Eq(cache_requirement_bytes, cl.size())
                if len(self.kernel.constants.keys()) <= 1:
                    inequality = sympy.solve(sympy.LessThan(cache_requirement_bytes, cl.size()),
                                             *self.kernel.constants.keys())
                else:
                    # Sympy does not solve for multiple constants
                    inequality = sympy.LessThan(cache_requirement_bytes, cl.size())
                results['dimensions'][dimension]['caches'][cl.name] = {
                    'cache_size': cl.size(),
                    'equation': cache_equation,
                    'lt': inequality,
                    'eq': sympy.solve(cache_equation, *self.kernel.constants.keys(), dict=True)
                }
        
        return results
示例#19
0
def generate_spherical_coeff_symb(l, m, lx, ly, lz, unnorm=False):
    j = (lx + ly - abs(m))
    if j % 2 == 0:
        j = int(j / 2)
    else:
        return sympy.Integer(0)

    j_symb = sympy.Integer(j)
    l_symb = sympy.Integer(l)
    m_symb = sympy.Integer(abs(m))
    lx_symb = sympy.Integer(lx)
    ly_symb = sympy.Integer(ly)
    lz_symb = sympy.Integer(lz)

    prefactor = symb_fact(2 * lx_symb) * symb_fact(2 * ly_symb) * symb_fact(
        2 * lz_symb) * symb_fact(l_symb)
    prefactor = prefactor * symb_fact(l_symb - m_symb)
    prefactor = prefactor / (symb_fact(2 * l_symb) * symb_fact(lx_symb) *
                             symb_fact(ly_symb) * symb_fact(lz_symb))
    prefactor = prefactor / symb_fact(l_symb + m_symb)

    # Ed's stupid normalization convention...
    if unnorm:
        prefactor = prefactor * symb_fact2(2 * l - 1) / symb_fact2(
            2 * lx - 1) / symb_fact2(2 * ly - 1) / symb_fact2(2 * lz - 1)

    prefactor = sympy.sqrt(prefactor)

    term1 = sympy.Integer(0)
    for i in range(int((l - abs(m)) / 2) + 1):
        term1 = term1 + sympy.Integer(binomial(l,i)) * sympy.Integer(binomial(i,j)) * \
                        sympy.Integer(math.pow(-1,i)) * symb_fact( 2*l_symb - sympy.Integer(2*i) ) / \
                        symb_fact( l_symb - m_symb - sympy.Integer(2*i) )

    term1 = term1 / (2**l_symb) / symb_fact(l)

    m_fact_symb = sympy.Integer(1)
    if m < 0:
        m_fact_symb = -m_fact_symb

    term2 = sympy.Integer(0)
    for k in range(j + 1):
        z = sympy.exp(m_fact_symb * sympy.pi / 2 *
                      (m_symb - lx_symb + sympy.Integer(2 * k)) * symb_I)
        term2 = term2 + sympy.Integer(binomial(j, k)) * sympy.Integer(
            binomial(abs(m), lx - 2 * k)) * z

    return prefactor * term1 * term2
示例#20
0
def test_Rationals():
    assert theq(theano_code(sympy.Integer(2) / 3), tt.true_div(2, 3))
    assert theq(theano_code(S.Half), tt.true_div(1, 2))
示例#21
0
 def wrap(self, num):
     return PySymInt(sympy.Integer(num), self.shape_env, constant=num)
示例#22
0
def test_Integers():
    assert theano_code(sympy.Integer(3)) == 3
示例#23
0
    def apply(self, items, evaluation):
        'Plus[items___]'

        items = items.numerify(evaluation).get_sequence()
        leaves = []
        last_item = last_count = None

        prec = min_prec(*items)
        is_real = all([not isinstance(i, Complex) for i in items])

        if prec is None:
            number = (sympy.Integer(0), sympy.Integer(0))
        else:
            number = (sympy.Float('0.0',
                                  dps(prec)), sympy.Float('0.0', dps(prec)))

        def append_last():
            if last_item is not None:
                if last_count == 1:
                    leaves.append(last_item)
                else:
                    if last_item.has_form('Times', None):
                        last_item.leaves.insert(0, Number.from_mp(last_count))
                        leaves.append(last_item)
                    else:
                        leaves.append(
                            Expression('Times', Number.from_mp(last_count),
                                       last_item))

        for item in items:
            if isinstance(item, Number):
                #TODO: Optimise this for the case of adding many real numbers
                if isinstance(item, Complex):
                    sym_real, sym_imag = item.real.to_sympy(
                    ), item.imag.to_sympy()
                else:
                    sym_real, sym_imag = item.to_sympy(), sympy.Integer(0)

                if prec is not None:
                    sym_real = sym_real.n(dps(prec))
                    sym_imag = sym_imag.n(dps(prec))

                number = (number[0] + sym_real, number[1] + sym_imag)
            else:
                count = rest = None
                if item.has_form('Times', None):
                    for leaf in item.leaves:
                        if isinstance(leaf, Number):
                            count = leaf.to_sympy()
                            rest = item.leaves[:]
                            rest.remove(leaf)
                            if len(rest) == 1:
                                rest = rest[0]
                            else:
                                rest.sort()
                                rest = Expression('Times', *rest)
                            break
                if count is None:
                    count = sympy.Integer(1)
                    rest = item
                if last_item is not None and last_item == rest:
                    last_count = add(last_count, count)
                else:
                    append_last()
                    last_item = rest
                    last_count = count
        append_last()
        if prec is not None or number != (0, 0):
            if number[1].is_zero and is_real:
                leaves.insert(0, Number.from_mp(number[0], prec))
            elif number[1].is_zero and number[1].is_Integer and prec is None:
                leaves.insert(0, Number.from_mp(number[0], prec))
            else:
                leaves.insert(0, Complex(number[0], number[1], prec))
        if not leaves:
            return Integer(0)
        elif len(leaves) == 1:
            return leaves[0]
        else:
            leaves.sort()
            return Expression('Plus', *leaves)
示例#24
0
def solve_equation(matrix, symbol_header="X"):
    """Solve linear equations.

    :type matrix: _mat.Matrix
    :type symbol_header: str
    :param matrix: The matrix that contains the linear equations.
    :param symbol_header: The symbol header of created symbols.
    :rtype : SolvedEquation
    :return: The solutions (presents with SolvedEquation class).
    :raise RuntimeError: When a bug appears.
    """

    #  Get matrix property.
    cur_unknown = 0
    row_c = matrix.get_row_count()
    col_c = matrix.get_column_count()

    #  Initialize
    ans = [None] * (col_c - 1)
    process_stack = _base_stack.Stack()

    #  Push initial matrix onto the stack.
    process_stack.push(_EqSolverStackItem(0, 0, 0))

    while len(process_stack) != 0:
        #  Pop off the process from process stack.
        cur_process = process_stack.pop()

        #  Get the row and column offset.
        #  In comments below, the 'first' column means the column whose offset is |offset_col| and the
        #  first row means the row whose offset is |offset_row|. 'Current matrix' means the sub-matrix
        #  of |matrix| range from [|offset_row|][|offset_column|] to [|row_c|][|col_c|].
        offset_row = cur_process.get_offset_row()
        offset_col = cur_process.get_offset_column()

        #  Get the process status(break point).
        break_point = cur_process.get_break_point()

        if break_point == 0:
            found_nz_row = False

            #  Simplify current column.
            for row_id in range(offset_row, row_c):
                tmp_offset = matrix.get_item_offset(row_id, offset_col)
                tmp_value = matrix.get_item_by_offset(tmp_offset)
                if len(tmp_value.free_symbols) != 0:
                    tmp_value = tmp_value.simplify()
                    matrix.write_item_by_offset(tmp_offset, tmp_value)

            #  Determine the row(in current matrix) whose first item is non-zero and exchange
            #  the row with the first row. If there's no such row, keep variable |found_nz_row|
            #  unchanged.
            for row_id in range(offset_row, row_c):
                if not matrix.get_item_by_position(row_id, offset_col).is_zero:
                    #  Exchange the row with the first row only if it's not the first row.
                    if row_id != offset_row:
                        matrix.exchange_row(row_id, offset_row)

                    #  Mark that we have found such row.
                    found_nz_row = True

                    break

            #  If all items in the first column are zero, set the value of unknown corresponding to
            #  this column to a new created unknown.
            if not found_nz_row:
                #  Set the value of unknown corresponding to the first column.
                ans[offset_col] = _sympy.Symbol(unknown_id_to_symbol(cur_unknown, symbol_header))
                cur_unknown += 1

                #  If there are still some unknown, solve them.
                if offset_col + 2 != col_c:
                    process_stack.push(_EqSolverStackItem(offset_row, offset_col + 1, 0))

                continue

            #  If there's only one unknown in current matrix, get the value of the unknown.
            if offset_col + 2 == col_c:
                tmp_offset = matrix.get_item_offset(offset_row, offset_col)
                ans[offset_col] = matrix.get_item_by_offset(tmp_offset + 1) / matrix.get_item_by_offset(tmp_offset)

                continue

            #  Get the offset of the first row.
            first_row_ofx = matrix.get_row_offset(offset_row)

            if offset_row + 1 == row_c:
                #  Initialize the value of the unknown corresponding to the first column.
                tmp_ans = matrix.get_item_by_offset(first_row_ofx + col_c - 1)

                #  Initialize the offset of coefficients.
                coeff_ofx = first_row_ofx + offset_col + 1

                #  Create new unknowns for the columns other than the first column and
                #  use these unknowns to represent the value of the unknown corresponding to
                #  the first column.
                for col_id in range(offset_col + 1, col_c - 1):
                    #  Set the value of unknown corresponding to this column.
                    new_sym = _sympy.Symbol(unknown_id_to_symbol(cur_unknown, symbol_header))
                    ans[col_id] = new_sym
                    cur_unknown += 1

                    #  Get the coefficient.
                    coeff = matrix.get_item_by_offset(coeff_ofx)
                    coeff_ofx += 1

                    #  Calculate the value.
                    tmp_ans -= coeff * new_sym

                #  Save the calculated value.
                ans[offset_col] = tmp_ans / matrix.get_item_by_offset(first_row_ofx + offset_col)

                continue

            #  Save the value of the first item of the first row and set its value to 1.
            tmp_offset = matrix.get_item_offset(offset_row, offset_col)
            first_value = matrix.get_item_by_offset(tmp_offset)
            matrix.write_item_by_offset(tmp_offset, _sympy.Integer(1))

            #  Move to next item.
            tmp_offset += 1

            #  Let all other items of the first row divide by the first item of the first row.
            for col_id in range(offset_col + 1, col_c):
                #  Get the value at specific position.
                tmp_value = matrix.get_item_by_offset(tmp_offset)

                #  Do division and change the value if it's not zero. (Just an optimization).
                if not tmp_value.is_zero:
                    tmp_value /= first_value
                    matrix.write_item_by_offset(tmp_offset, tmp_value)

                #  Move to next item.
                tmp_offset += 1

            #  Do elimination for rows other than the first row.
            for row_id in range(offset_row + 1, row_c):
                #  Get the value of the first item of the row whose offset is |row_id|.
                tmp_offset = matrix.get_item_offset(row_id, offset_col)
                first_value = matrix.get_item_by_offset(tmp_offset)

                #  Ignore this row if the first value of it is zero.
                if first_value.is_zero:
                    continue

                #  Set the value of the first value to zero.
                matrix.write_item_by_offset(tmp_offset, _sympy.Integer(0))

                #  Move to next item.
                tmp_offset += 1

                #  Do elimination with the first row.
                for col_id in range(offset_col + 1, col_c):
                    #  Get the value at specific position.
                    tmp_value = matrix.get_item_by_offset(tmp_offset)

                    #  Do elimination
                    tmp_value = tmp_value / first_value - matrix.get_item_by_offset(first_row_ofx + col_id)

                    #  Write the value back.
                    matrix.write_item_by_offset(tmp_offset, tmp_value)

                    #  Move to next item.
                    tmp_offset += 1

            #  Save current process. We will do back substitution next time.
            process_stack.push(_EqSolverStackItem(offset_row, offset_col, 1))

            #  Solve the order-reduced matrix.
            process_stack.push(_EqSolverStackItem(offset_row + 1, offset_col + 1, 0))

            continue

        if break_point == 1:
            #  Get the offset of the first row.
            first_row_ofx = matrix.get_row_offset(offset_row)

            #  Initialize the value of the unknown corresponding to the first column.
            tmp_ans = matrix.get_item_by_offset(first_row_ofx + col_c - 1)

            #  Initialize the offset of coefficients.
            coeff_ofx = first_row_ofx + offset_col + 1

            #  Use the calculated values to get the value of the unknown corresponding to the first column.
            for col_id in range(offset_col + 1, col_c - 1):
                #  Get the coefficient.
                coeff = matrix.get_item_by_offset(coeff_ofx)
                coeff_ofx += 1

                #  Calculate the value.
                tmp_ans -= coeff * ans[col_id]

            #  Save the calculated value.
            ans[offset_col] = tmp_ans

            continue

        raise RuntimeError("Invalid break point.")

    return SolvedEquation(ans, cur_unknown)
示例#25
0
    def apply(self, items, evaluation):
        'Power[items__]'

        items_sequence = items.get_sequence()

        if len(items_sequence) == 2:
            x, y = items_sequence
        else:
            return Expression('Power', *items_sequence)

        if y.get_int_value() == 1:
            return x
        elif x.get_int_value() == 1:
            return x
        elif y.get_int_value() == 0:
            if x.get_int_value() == 0:
                evaluation.message('Power', 'indet', Expression('Power', x, y))
                return Symbol('Indeterminate')
            else:
                return Integer(1)

        elif x.has_form('Power', 2) and isinstance(y, Integer):
            return Expression('Power', x.leaves[0],
                              Expression('Times', x.leaves[1], y))
        elif x.has_form('Times', None) and isinstance(y, Integer):
            return Expression(
                'Times', *[Expression('Power', leaf, y) for leaf in x.leaves])

        elif isinstance(x, Number) and isinstance(
                y, Number) and not (x.is_inexact() or y.is_inexact()):
            sym_x, sym_y = x.to_sympy(), y.to_sympy()
            try:
                if sym_y >= 0:
                    result = sym_x**sym_y
                else:
                    if sym_x == 0:
                        evaluation.message('Power', 'infy')
                        return Symbol('ComplexInfinity')
                    result = sympy.Integer(1) / (sym_x**(-sym_y))
                if isinstance(result, sympy.Pow):
                    result = result.simplify()
                    args = [from_sympy(expr) for expr in result.as_base_exp()]
                    result = Expression('Power', *args)
                    result = result.evaluate_leaves(evaluation)
                    return result

                return from_sympy(result)
            except ValueError:
                return Expression('Power', x, y)
            except ZeroDivisionError:
                evaluation.message('Power', 'infy')
                return Symbol('ComplexInfinity')

        elif isinstance(x, Number) and isinstance(
                y, Number) and (x.is_inexact() or y.is_inexact()):
            try:
                prec = min(max(x.get_precision(), 64),
                           max(y.get_precision(), 64))
                with mpmath.workprec(prec):
                    mp_x = sympy2mpmath(x.to_sympy())
                    mp_y = sympy2mpmath(y.to_sympy())
                    result = mp_x**mp_y
                    if isinstance(result, mpmath.mpf):
                        return Real(str(result), prec)
                    elif isinstance(result, mpmath.mpc):
                        return Complex(str(result.real), str(result.imag),
                                       prec)
            except ZeroDivisionError:
                evaluation.message('Power', 'infy')
                return Symbol('ComplexInfinity')
        else:
            numerified_items = items.numerify(evaluation)
            return Expression('Power', *numerified_items.get_sequence())
示例#26
0
# -*- coding: utf-8 -*-
"""
Created on Tue May 26 11:15:40 2015

@author: mark
"""
from __future__ import print_function, division
import sympy as sp

all = [ "cent", "perfect_fifth", "perfect_fourth", "octave", "perfect_major_third", \
       "quarter_comma", "five_limit_constructors", "edo12_constructors", \
       "lucy_construtors", "all_constructors", "interval_catalog"]

# Ratio Constants

cent = sp.Integer(2)**sp.Rational(1, 1200)
perfect_fifth = sp.Integer(3) / sp.Integer(2)
perfect_fourth = sp.Integer(4) / sp.Integer(3)
octave = sp.Integer(2)
perfect_major_third = sp.Integer(5) / sp.Integer(4)
syntonic_comma = sp.Rational(81, 80)
quarter_comma = sp.sqrt(sp.sqrt(syntonic_comma))

# Scale definition constants

five_limit_constructors = [
    (sp.Rational(16, 15), "s"),
    (sp.Rational(10, 9), "t"),
    (sp.Rational(9, 8), "T"),
]
示例#27
0
文件: ecm.py 项目: zeta1999/kerncraft
    def calculate_cycles(self):
        """
        Calculate performance model cycles from cache stats.

        calculate_cache_access() needs to have been execute before.
        """
        element_size = self.kernel.datatypes_size[self.kernel.datatype]
        elements_per_cacheline = float(
            self.machine['cacheline size']) // element_size
        iterations_per_cacheline = (
            sympy.Integer(self.machine['cacheline size']) /
            sympy.Integer(self.kernel.bytes_per_iteration))
        self.results['iterations per cacheline'] = iterations_per_cacheline
        cacheline_size = float(self.machine['cacheline size'])

        loads, stores = (self.predictor.get_loads(),
                         self.predictor.get_stores())

        for cache_level, cache_info in list(
                enumerate(self.machine['memory hierarchy']))[1:]:
            throughput, duplexness = cache_info['upstream throughput']

            if type(throughput
                    ) is str and throughput == 'full socket memory bandwidth':
                # Memory transfer
                # we use bandwidth to calculate cycles and then add panalty cycles (if given)

                # choose bw according to cache level and problem
                # first, compile stream counts at current cache level
                # write-allocate is allready resolved in cache predictor
                read_streams = loads[cache_level]
                write_streams = stores[cache_level]
                # second, try to find best fitting kernel (closest to stream seen stream counts):
                threads_per_core = 1
                bw, measurement_kernel = self.machine.get_bandwidth(
                    cache_level, read_streams, write_streams, threads_per_core)

                # calculate cycles
                if duplexness == 'half-duplex':
                    cycles = float(loads[cache_level] + stores[cache_level]) * \
                             float(elements_per_cacheline) * float(element_size) * \
                             float(self.machine['clock']) / float(bw)
                else:  # full-duplex
                    raise NotImplementedError(
                        "full-duplex mode is not (yet) supported for memory transfers."
                    )
                # add penalty cycles for each read stream
                if 'penalty cycles per read stream' in cache_info:
                    cycles += stores[cache_level] * \
                              cache_info['penalty cycles per read stream']

                self.results.update({
                    'memory bandwidth kernel': measurement_kernel,
                    'memory bandwidth': bw
                })
            else:
                # since throughput is given in B/cy, and we need CL/cy:
                throughput = float(throughput) / cacheline_size
                # only cache cycles count
                if duplexness == 'half-duplex':
                    cycles = (loads[cache_level] +
                              stores[cache_level]) / float(throughput)
                elif duplexness == 'full-duplex':
                    cycles = max(loads[cache_level] / float(throughput),
                                 stores[cache_level] / float(throughput))
                else:
                    raise ValueError(
                        "Duplexness of cache throughput may only be 'half-duplex'"
                        "or 'full-duplex', found {} in {}.".format(
                            duplexness, cache_info['name']))

            self.results['cycles'].append((cache_info['level'], cycles))

            self.results[cache_info['level']] = cycles

        return self.results
import numpy as np
import sympy

from typing import Union, Callable, Collection, List, Iterable

from beluga.numeric.compilation import LocalCompiler, jit_compile_func
from beluga.symbolic.special_functions import custom_functions, tables

# default_tol = 1e-4
sym_zero = sympy.Integer(0)
sym_one = sympy.Integer(1)


class GenericStruct:
    def __init__(self, local_compiler: LocalCompiler = None):
        if not (hasattr(self, 'local_compiler') and local_compiler is None):
            self.local_compiler = local_compiler

    @staticmethod
    def _ensure_list(item):
        if isinstance(item, Iterable):
            return list(item)
        else:
            return [item]

    def _sympify_internal(
        self, expr: Union[str, Iterable,
                          sympy.Basic]) -> Union[Iterable, sympy.Basic]:
        if self.local_compiler is not None:
            return self.local_compiler.sympify(expr)
        else:
示例#29
0
def test_conv5():
    x = Integer(5)
    y = Integer(6)
    assert x._sympy_() == sympy.Integer(5)
    assert (x / y)._sympy_() == sympy.Integer(5) / sympy.Integer(6)
示例#30
0
#!/usr/bin/env python
#
#  Copyright 2014 - 2016 The BCE Authors. All rights reserved.
#  Use of this source code is governed by a BSD-style license that can be
#  found in the license.txt file.
#

import sympy as _sympy

ZERO = _sympy.Integer(0)
ONE = _sympy.Integer(1)