def make_linear_eq(x="", rhs=None, var_coeffs=True): """ Generates linear equation in one variable, and its solution. x : charector for the variable to be solved for. defaults to random selection from the global list `alpha`. OR a list of possible charectors. A random selection will be made from them. rhs : value to set for the right-hand side. If not given, the right-hand side will be a randomly generated linear expression var_coeffs : sets whether we want variables as coefficients in the problem. defaults to True. Set to False if you want a problem with strictly numerical coefficients. """ if not x: x = random.choice(alpha) elif isinstance(x, list): x = random.choice(x) exclude = [x.upper(), x.lower()] x = sympy.Symbol(x) c1, c2, c3, c4 = get_coefficients(4, var_coeffs=var_coeffs, reduce=False, exclude=exclude) lhs = c1 * x + c2 rhs = c3 * x + c4 e = sympy.Eq(lhs, rhs) sols = [render(ex, x) for ex in sympy.solve(e, x)] return "Solve for $%s$ : %s" % (x, render(e)), sols
def make_linear_eq(x="", rhs = None, var_coeffs=True): """ Generates linear equation in one variable, and its solution. x : charector for the variable to be solved for. defaults to random selection from the global list `alpha`. OR a list of possible charectors. A random selection will be made from them. rhs : value to set for the right-hand side. If not given, the right-hand side will be a randomly generated linear expression var_coeffs : sets whether we want variables as coefficients in the problem. defaults to True. Set to False if you want a problem with strictly numerical coefficients. """ if not x: x = random.choice(alpha) elif isinstance(x, list): x = random.choice(x) exclude = [x.upper(), x.lower()] x = sympy.Symbol(x) c1, c2, c3, c4 = get_coefficients(4, var_coeffs=var_coeffs, reduce=False, exclude = exclude) lhs = c1*x + c2 rhs = c3*x + c4 e = sympy.Eq(lhs, rhs) sols = [render(ex, x) for ex in sympy.solve(e, x)] return "Solve for $%s$ : %s" % (x, render(e)), sols
def make_quadratic_eq(target, var="x", rhs=None, integer=[0, 1]): """ Generates quadratic equation problem expression and set of solutions x : charector for the variable to be solved for. defaults to "x". OR a list of possible charectors. A random selection will be made from them. rhs : value to set for the right-hand side. If not given, the right-hand side will be a randomly generated polynomial expression of degree <= 2, in the same variable. integer : determines whether generated problem will have integer roots or not. Default is a random selection. """ if isinstance(var, str): var = sympy.Symbol(var) elif isinstance(var, list): var = sympy.Symbol(random.choice(var)) if isinstance(integer, list): integer = random.choice(integer) if integer: r1 = random.choice(digits_nozero) r2 = target - r1 #r2 = random.choice(digits_nozero) lhs = (var - r1) * (var - r2) lhs = lhs.expand() rhs = 0 else: c1, c2, c3 = get_coefficients(3) lhs = c1 * var**2 + c2 * var + c3 if rhs == None: c4, c5, c6 = get_coefficients(3, first_nonzero=False) rhs = c4 * var**2 + c5 * var + c6 e = sympy.Eq(lhs, rhs) pvar = str(var) sols = ', '.join( [pvar + " = " + sympy.latex(ex) for ex in sympy.solve(e, var)]) sols = "$$" + sols + "$$" if len(sols) == 0: return make_quadratic_eq() return "\\overline{" + render(e) + "}", sols
def make_quadratic_eq(var="x", rhs = None, integer=[0, 1]): """ Generates quadratic equation problem expression and set of solutions x : charector for the variable to be solved for. defaults to "x". OR a list of possible charectors. A random selection will be made from them. rhs : value to set for the right-hand side. If not given, the right-hand side will be a randomly generated polynomial expression of degree <= 2, in the same variable. integer : determines whether generated problem will have integer roots or not. Default is a random selection. """ if isinstance(var, str): var = sympy.Symbol(var) elif isinstance(var, list): var = sympy.Symbol(random.choice(var)) if isinstance(integer, list): integer = random.choice(integer) if integer: r1 = random.choice(digits_nozero) r2 = random.choice(digits_nozero) lhs = (var - r1) * (var - r2) lhs = lhs.expand() rhs = 0 else: c1, c2, c3 = get_coefficients(3) lhs = c1*var**2 + c2*var + c3 if rhs == None: c4, c5, c6 = get_coefficients(3, first_nonzero=False) rhs = c4*var**2 + c5*var + c6 e = sympy.Eq(lhs, rhs) pvar = str(var) sols = ', '.join([pvar+" = " + sympy.latex(ex) for ex in sympy.solve(e, var)]) sols = "$$" + sols + "$$" if len(sols) == 0: return make_quadratic_eq() return render(e), sols
def poly1(x): vals = sum([k * x**i for i, k in enumerate(reversed(get_coefficients(2)))]) return vals