def test_sum_equation(self): e = Equation() for i in range(5): x = e.element(-1) e.create_sum(-1, x / 2) s = str(e) self.assertEqual(s, '1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/32')
def __init__(self, equation): Equation.__init__(self, equation) self.form = self.get_form() self.x_coefficient = self.get_x_coefficient() self.y_coefficient = self.get_y_coefficient() self.slope = self.get_slope() self.y_intercept = num(self.solve_for("x", 0))
def test_negative(self): e = Equation(-1) self.assertEqual(str(e), '-1') e = Equation() e.create_negation(-1) self.assertEqual(str(e), '-(-1)') e = Equation(-1) e.create_negation(-1) self.assertEqual(str(e), '-(-(-1))')
def test_negation(self): e = Equation() x = e.element(-1) e.create_sub(-1, x / 2) e.create_negation(-1) self.assertEqual(str(e), '1 1/2 + (-1/2)') e.create_negation(-1) self.assertEqual(str(e), '1 1/2 - (-(-1/2))')
def run_balance(): """ Runs the chemical equation balance algorithm """ print('=================================================') print( 'Insert chemical equation with element followed by the number of atoms:' ) print('Example: H2 + O2 => H2O') user_input = input('>>> ') try: equation = Equation(user_input) print('Balanced equation: ' + equation.equation_balancer()) # sleep(3) # run_balance() except IndexError: print('Invalid input...')
def solve(self): """Returns the solution to this system of equation.""" if self.is_compatible() is False: return None if self.get_number_of_solutions() == 'Infinitely many solutions': raise InfinitelySolutionsError x_coefficient = str( num(self.linear_2.x_coefficient) - num(self.linear_1.x_coefficient)) x_coefficient = '' if x_coefficient == '1' else x_coefficient equation = x_coefficient + 'x' + '=' + str( self.linear_1.y_intercept) + '-' + str(self.linear_2.y_intercept) x_value = Equation(equation).solve() return self.linear_1.get_point(x_value)
def processEquation(self, equationJSON): equation = Equation(equationJSON) independentClause = equation.independentClause() dependentClause = equation.dependentClause() independentClause.mandates(dependentClause)
from fractions import Element from equations import Equation from random import randint print(r'\documentclass[8pt]{article}') print(r'\usepackage[utf8]{inputenc}') print(r'\usepackage{polski}') print(r'\usepackage{amsmath}') print(r'\begin{document}') N = 15 for _ in range(100): print(r'\begin{align}') for i in range(N): e = Equation() # print('[') for j in range(5): m, n, x, y = randint(0, len(e) - 1), randint(0, 4), 1, randint(2, 10) e.complicate(m, n, x, y) if i < N - 1: print(e.tex(), r'\\') else: print(e.tex()) print(r'\end{align}') print(r'\end{document}')
def test_sub_equation(self): e = Equation() x = e.element(-1) e.create_sub(-1, x / 2) self.assertEqual(str(e), '1 1/2 - 1/2') x = e.element(-1) e.create_sub(-1, x / 2) self.assertEqual(str(e), '1 1/2 - (3/4 - 1/4)') x = e.element(-1) e.create_sub(-1, x / 2) self.assertEqual(str(e), '1 1/2 - (3/4 - (3/8 - 1/8))')
def test_complication(self): e = Equation(1) for m, n, x, y in [[0, 4, 1, 5], [1, 3, 1, 5], [1, 2, 1, 4]]: e.complicate(m, n, x, y) self.assertEqual(str(e), '1/5 : ((-1) * 1/5)') e = Equation(1) tab = [[0, 3, 1, 5], [1, 0, 1, 9], [0, 4, 1, 6], [1, 0, 1, 2], [0, 1, 1, 9], [5, 0, 1, 5], [3, 3, 1, 9], [1, 0, 1, 3]] for m, n, x, y in tab: e.complicate(m, n, x, y) e.tab = e.tab[0][0] e.tab[1] = Element(1) self.assertEqual(str(e), '17/18 - (-2/9 + 1/3) : 1') self.assertEqual(str(e), '17/18 - (-2/9 + 1/3)') e.tab = e.tab[0] print(e)
def test_division(self): e = Equation() e.create_division(-1, Element('1/2')) self.assertEqual(str(e), '1/2 : 1/2') e = Equation(Element('2/3')) e.create_division(-1, Element('1/2')) self.assertEqual(str(e), '1/3 : 1/2') e = Equation(Element('5/3')) e.create_division(-1, Element(2)) self.assertEqual(str(e), '3 1/3 : 2')
def test_multiplication(self): e = Equation() e.create_multiplication(-1, Element(2)) self.assertEqual(str(e), '1/2 * 2') e = Equation() e.create_multiplication(-1, Element(-2)) self.assertEqual(str(e), '(-1/2) * (-2)') e = Equation(1) e.create_multiplication(-1, Element(3, 2)) self.assertEqual(str(e), '2/3 * 1 1/2') e = Equation(1) e.create_multiplication(-1, Element('-3/2')) self.assertEqual(str(e), '(-2/3) * (-1 1/2)')
def eval_term( state, term_desc, conf, domain, variables, materials, ts, funmod=None, chunk_size=1000, term_prefixes=None, caches=None, ret_caches=False, override=True, new_geometries=True, dw_mode="vector", tangent_matrix=None, **kwargs ): """Evaluate a term. May not succeed!""" if term_prefixes is None: term_prefixes = {} if caches is None: caches = DataCaches() equation = Equation.from_desc("tmp", term_desc, term_prefixes) equation.setup_terms(domain.regions, variables, materials, caches, kwargs) for cache in caches.itervalues(): cache.set_mode(override=override) if new_geometries: i_names = equation.get_term_integral_names() integrals = Integrals.from_conf(conf.integrals, i_names) integrals.set_quadratures(quadratures) geometries = {} equation.describe_geometry(geometries, variables, integrals) variables.data_from_state(state) if "call_mode" in kwargs: itype = kwargs["call_mode"].split("_")[0] else: # itype according to the first term in term_desc! itype = equation.terms[0].itype if itype == "dw": variables.setup_dof_conns() if not variables.has_eq_map: variables.equation_mapping(conf.ebcs, conf.epbcs, domain.regions, ts, funmod) variables.setup_lcbc_operators(conf.lcbcs, domain.regions) variables.setup_a_dof_conns() if dw_mode == "vector": residual = variables.create_stripped_state_vector() assemble_vector(residual, equation, variables, materials, chunk_size, group_can_fail=False, **kwargs) if variables.has_lcbc: op_lcbc = variables.op_lcbc residual = op_lcbc.T * residual ret_val = residual elif dw_mode == "matrix": if tangent_matrix is None: tangent_matrix = variables.create_matrix_graph() tangent_matrix.data[:] = 0.0 assemble_matrix(tangent_matrix, equation, variables, materials, chunk_size, group_can_fail=False, **kwargs) if variables.has_lcbc: op_lcbc = variables.op_lcbc tangent_matrix = op_lcbc.T * tangent_matrix * op_lcbc tangent_matrix = tangent_matrix.tocsr() tangent_matrix.sort_indices() ret_val = tangent_matrix else: print dw_mode raise ValueError elif itype == "d": kwargs.setdefault("call_mode", "d_eval") val = 0.0 for term in equation.terms: args = build_args(term, variables, materials, **kwargs) for ig in term.iter_groups(): for aux, iels, status in term(chunk_size=chunk_size, **args): val += term.sign * aux ret_val = val elif itype == "di": val = None for term in equation.terms: args = build_args(term, variables, materials, **kwargs) for ig in term.iter_groups(): for aux, iels, status in term(chunk_size=chunk_size, **args): if val is None: val = term.sign * aux else: val += term.sign * aux ret_val = val elif (itype == "de") or (itype == "dq"): val = None for term in equation.terms: args = build_args(term, variables, materials, **kwargs) for ig in term.iter_groups(): for aux, iels, status in term(chunk_size=chunk_size, **args): if val is None: val = term.sign * aux else: val = nm.concatenate((val, term.sign * aux), axis=0) ret_val = val else: raise NotImplementedError, "unknown term integration type: %s" % itype if ret_caches: return ret_val, caches else: return ret_val