def add(value, sample_args, context=None): """E.g., "Let f(x)=2x+1, g(x)=3x+2. What is 5*f(x) - 7*g(x)?".""" is_question = context is None if context is None: context = composition.Context() entropy, sample_args = sample_args.peel() if value is None: max_degree = 3 degree = random.randint(1, max_degree) entropy -= math.log10(max_degree) entropy_value = entropy / 2 entropy -= entropy_value value = polynomials.sample_coefficients( degree, entropy=entropy_value, min_non_zero=random.randint(1, 3)) value = composition.Polynomial(value) c1, c2, coeffs1, coeffs2 = polynomials.coefficients_linear_split( value.coefficients, entropy) coeffs1 = polynomials.trim(coeffs1) coeffs2 = polynomials.trim(coeffs2) c1, c2, fn1, fn2 = context.sample( sample_args, [c1, c2, composition.Polynomial(coeffs1), composition.Polynomial(coeffs2)] ) var = sympy.var(context.pop()) expression = ( c1.handle * fn1.handle.apply(var) + c2.handle * fn2.handle.apply(var)) if is_question: answer = polynomials.coefficients_to_polynomial(value.coefficients, var) answer = answer.sympy() template = random.choice(_TEMPLATES) return example.Problem( question=example.question(context, template, composed=expression), answer=answer) else: intermediate_symbol = context.pop() intermediate = sympy.Function(intermediate_symbol)(var) return composition.Entity( context=context, value=value, description='Let {intermediate} = {composed}.', handle=composition.FunctionHandle(intermediate_symbol), intermediate=intermediate, composed=expression)
def coefficient_named(value, sample_args, context=None): """E.g., "Express x^2 + 2x in the form h * x^2 + k * x + t and give h.".""" del value # not used if context is None: context = composition.Context() variable = sympy.Symbol(context.pop()) entropy, sample_args = sample_args.peel() degree = random.randint(1, 4) if random.choice([False, True]): coefficients = polynomials.sample_coefficients( degree, entropy / 2, min_non_zero=random.randint(degree - 1, degree)) expanded = polynomials.expand_coefficients(coefficients, entropy / 2) expression = polynomials.coefficients_to_polynomial(expanded, variable) else: expression = polynomials.sample_with_brackets(variable, degree, entropy) coefficients = list(reversed(sympy.Poly(expression).all_coeffs())) named_coeffs = [sympy.Symbol(context.pop()) for _ in range(degree + 1)] canonical = polynomials.coefficients_to_polynomial(named_coeffs, variable) if random.random() < 0.2: # only small probability of non-zero power power = random.randint(0, degree) else: non_zero_powers = [ i for i in range(degree + 1) if coefficients[i] != 0 ] power = random.choice(non_zero_powers) value = coefficients[power] named_coeff = named_coeffs[power] template = random.choice([ 'Ekspresikan {expression} sebagai {canonical} dan berikan {target}. ' 'Atur ulang {expression} menjadi {canonical} dan berikan {target}.', 'Ekspresikan {expression} dalam bentuk {canonical} dan berikan {target}.', 'Atur ulang {expression} ke bentuk {canonical} dan berikan {target}.', ]) return example.Problem(question=example.question(context, template, expression=expression, canonical=canonical, target=named_coeff), answer=value)
def _sample_integrand(coefficients, derivative_order, derivative_axis, entropy): """Integrates `coefficients` and adds sampled "constant" terms.""" coefficients = np.asarray(coefficients) # Integrate (with zero for constant terms). integrand = coefficients for _ in range(derivative_order): integrand = polynomials.integrate(integrand, derivative_axis) # Add on sampled constant terms. constant_degrees = np.array(integrand.shape) - 1 constant_degrees[derivative_axis] = derivative_order - 1 extra_coeffs = polynomials.sample_coefficients(constant_degrees, entropy) pad_amount = coefficients.shape[derivative_axis] pad = [(0, pad_amount if i == derivative_axis else 0) for i in range(coefficients.ndim)] extra_coeffs = np.pad(extra_coeffs, pad, 'constant', constant_values=0) return integrand + extra_coeffs