def testDescendants(self): constants = [ops.Constant(i) for i in range(6)] # (1 + 2*3**4) / 5 - 6 expression = ops.Sub( ops.Div( ops.Add( constants[0], ops.Mul( constants[1], ops.Pow( constants[2], constants[3]))), constants[4]), constants[5]) descendants = expression.descendants() descendants = ops._flatten(descendants) for constant in constants: self.assertIn(constant, descendants) self.assertEqual(descendants.count(constant), 1) # Also test top-level. self.assertEqual(constants[0].descendants(), [constants[0]]) # Also general structure. constant = ops.Constant(3) expression = ops.Neg(constant) self.assertEqual(set(expression.descendants()), set([constant, expression]))
def _arithmetic(value, sample_args, add_sub, mul_div): """Internal arithmetic thingy....""" assert sample_args.count >= 0 if sample_args.count == 0: assert sample_args.entropy == 0 return ops.Constant(value) allowed = [] if add_sub and _add_sub_filter(value, sample_args): allowed.append(_add_op) allowed.append(_sub_op) if mul_div and _mul_filter(value, sample_args): allowed.append(_mul_op) if mul_div and _div_filter(value, sample_args): allowed.append(_div_op) if not allowed: raise ValueError( 'No valid ops found, add_sub={} mul_div={} value={} sample_args={}' .format(add_sub, mul_div, value, sample_args)) choice = random.choice(allowed) op, args, sample_args = choice(value, sample_args, rationals_allowed=mul_div) sample_args = sample_args.split(args) child_expressions = [_arithmetic(arg, child_sample_arg, add_sub, mul_div) for arg, child_sample_arg in zip(args, sample_args)] return op(*child_expressions)
def collect(value, sample_args, context=None): """Collect terms in an unsimplified polynomial.""" is_question = context is None if context is None: context = composition.Context() entropy, sample_args = sample_args.peel() if value is None: entropy_value, entropy = entropy * np.random.dirichlet([2, 3]) degrees = [random.randint(1, 3)] value = composition.Polynomial( polynomials.sample_coefficients(degrees, entropy_value)) assert isinstance(value, composition.Polynomial) coefficients = value.coefficients all_coefficients_are_integer = True for coeff in coefficients.flat: if not number.is_integer(coeff): all_coefficients_are_integer = False break if all_coefficients_are_integer: coefficients = polynomials.expand_coefficients(coefficients, entropy) else: # put back the unused entropy sample_args = composition.SampleArgs(sample_args.num_modules, sample_args.entropy + entropy) num_variables = coefficients.ndim variables = [sympy.Symbol(context.pop()) for _ in range(num_variables)] unsimplified = polynomials.coefficients_to_polynomial( coefficients, variables) simplified = unsimplified.sympy().expand() # Bit of a hack: handle the very rare case where no number constants appearing if not ops.number_constants(unsimplified): unsimplified = ops.Add(unsimplified, ops.Constant(0)) context.sample_by_replacing_constants(sample_args, unsimplified) if is_question: template = 'Упростите {unsimplified}.' return example.Problem(question=example.question( context, template, unsimplified=unsimplified), answer=simplified) else: function_symbol = context.pop() function = sympy.Function(function_symbol)(*variables) return composition.Entity( context=context, value=value, handle=composition.FunctionHandle(function_symbol), expression=unsimplified, polynomial_variables=variables, description='Пусть {function} = {unsimplified}.', function=function, unsimplified=unsimplified)
def testMul(self): mul = ops.Mul() self.assertEqual(str(mul), '1') self.assertEqual(mul.sympy(), 1) mul = ops.Mul(2, 3) self.assertEqual(str(mul), '2*3') self.assertEqual(mul.sympy(), 6) mul = ops.Mul(ops.Identity(ops.Constant(-2)), 3) self.assertEqual(str(mul), '-2*3') self.assertEqual(mul.sympy(), -6) mul = ops.Mul(ops.Add(1, 2), 3) self.assertEqual(str(mul), '(1 + 2)*3') self.assertEqual(mul.sympy(), 9) mul = ops.Mul(ops.Mul(2, 3), 5) self.assertEqual(str(mul), '2*3*5') self.assertEqual(mul.sympy(), 30)
def testNumberConstants(self): constant = ops.Constant(3) expression = ops.Neg(constant) constants = ops.number_constants([expression]) self.assertEqual(constants, [constant])