Ejemplo n.º 1
0
    def generate(self):
        LQ, eqn = None, None
        x = symbols('x')
        a, b, c, d, e, f = symbols('a b c d e f')
        # generate algebraic fractions and their latex representation
        AF1, AFL1 = algebraicFraction(x, randIntExcept(2, 5),
                                      randIntExcept(2, 5), randIntExcept(2, 5))
        AF2, AFL2 = algebraicFraction(x, randIntExcept(2, 5),
                                      randIntExcept(2, 5), randIntExcept(2, 5))
        AF3, AFL3 = algebraicFraction(x, randIntExcept(2, 5),
                                      randIntExcept(2, 5), randIntExcept(2, 5))
        if self.difficulty == 1:
            a = randIntExcept(2, 10)
            b = randIntExcept(2, 10)
            c = randIntExcept(-7, 7)
            d = randIntExcept(-7, 7)
            eqn, LQ = random.choice([
                (Eq(AF1, a), '{} = {}'.format(AFL1, a)),
                (Eq(AF1, a * x), '{} = {}x'.format(AFL1, a)),
                # this one causes exception since eqn=boolean for some reason
                # (Eq((a*x + b)/(c*x), d), r'\frac{{ {a}x+{b} }}{{ {c}x }} = {d}'.format(a=a, b=b, c=c, d=d)),
            ])
        elif self.difficulty == 2:
            # I want a 0 to appear more often than 1/20 times, i.e. 30% instead of 5%
            if random.random() < 0.7:
                a = randIntExcept(-10, 10)
            else:
                a = 0
            eqn, LQ = random.choice([
                (Eq(AF1, AF2), '{} = {}'.format(AFL1, AFL2)),
                (Eq(AF1 + AF2, a), '{} + {} = {}'.format(AFL1, AFL2, a)),
                (Eq(AF1 - AF2, a), '{} - {} = {}'.format(AFL1, AFL2, a)),
                # (Eq(AF1 * AF2, a), '{} \times {} = {}'.format(AFL1, AFL2, a)),
                # (Eq(AF1 / AF2, a), '{} \div {} = {}'.format(AFL1, AFL2, a)),
            ])
        elif self.difficulty == 3:
            eqn, LQ = random.choice([
                (Eq(AF1 + AF2, AF3), '{} + {} = {}'.format(AFL1, AFL2, AFL3)),
                (Eq(AF1 - AF2, AF3), '{} - {} = {}'.format(AFL1, AFL2, AFL3)),
            ])
        try:
            soln = Eq(x, solve(eqn, x)[0])
        except IndexError:
            # no solution
            soln = 'No solution'
        except TypeError as e:
            # print('TypeError', e)
            # print('diff', self.difficulty)
            # print('eqn', eqn)
            # print('LQ', LQ)

            # no solution either, something like -4x + 4x leaving no x's and no solution
            soln = 'No solution'
        Q = getPretty(eqn)
        A = getPretty(soln)
        LQ = '$\displaystyle {}$'.format(LQ)
        LA = '$\displaystyle {}$'.format(latex(soln))
        return Q, A, LQ, LA
Ejemplo n.º 2
0
 def generate(self):
     x, y, z = symbols('x y z')
     a, b, c, d, e, f = symbols('a b c d e f')
     substitutions, eqn, LQ = [], None, None
     if self.difficulty == 1:
         eqn, LQ = random.choice([
             (a * (b * x + c), r'{a}({b}x+{c})'),
             (a / d * (b * x + c), r'\frac{{ {a} }}{{ {d} }} ({b}x+{c})'),
             (d + a * (b * x - c), r'{d}+{a}({b}x-{c})'),
             (d + a * (b * x - c / e),
              r'{d}+{a}({b}x-\frac{{ {c} }}{{ {e} }})'),
             (-a * (b * x + c * y), r'-{a}({b}x+{c}y)'),
             (d - a * (-b * x + c * y), r'{d}-{a}(-{b}x+{c}y)'),
             (a * x * (b * y + c * z), r'{a}x({b}y+{c}z)'),
         ])
         substitutions = [
             (a, random.randint(2, 10)),
             (b, random.randint(2, 7)),
             (c, random.randint(2, 10)),
             (d, random.randint(2, 7)),
             (e, random.randint(2, 10)),
         ]
     elif self.difficulty == 2:
         eqn, LQ = random.choice([
             ((a * x + b) * (c * x + d), r'({a}x+{b})({c}x+{d})'),
             ((a / e * x + b) * (c * x + d),
              r'(\frac{{ {a}x }}{{ {e} }}+{b})({c}x+{d})'),
             ((-a * x + b) * (c * x - d),
              random.choice(
                  [r'(-{a}x+{b})({c}x-{d})', r'({c}x-{d})(-{a}x+{b})'])),
             ((a * y + b * x) * (c * y + d + e * z),
              r'({a}y+{b}x)({c}y+{d}+{e}z)'),
         ])
         substitutions = [
             (a, random.randint(2, 7)),
             (b, random.randint(2, 10)),
             (c, random.randint(2, 7)),
             (d, random.randint(2, 10)),
             (e, random.randint(2, 5)),
         ]
     for old, new in substitutions:
         with evaluate(False):
             eqn = eqn.replace(old, new)
     soln = expand(eqn)
     a = substitutions[0][1]
     b = substitutions[1][1]
     c = substitutions[2][1]
     d = substitutions[3][1]
     e = substitutions[4][1]
     Q = getPretty(eqn)
     A = getPretty(soln)
     LQ = '$\displaystyle {}$'.format(LQ.format(a=a, b=b, c=c, d=d, e=e))
     LA = '$\displaystyle {}$'.format(latex(soln))
     return Q, A, LQ, LA
Ejemplo n.º 3
0
    def generate(self):
        iters = 1
        if self.difficulty == 1:
            iters = 3
        elif self.difficulty == 2:
            iters = 5

        x = symbols('x')
        # self.pronumerals = symbols('a b c d e f k m n p r t w y z', real=True)
        self.pronumerals = symbols('a b c w y z', real=True)
        self.used = []
        substitutions, eqn, LQ = [], None, None

        # left and right hand expressions
        lh = x
        rh = 0

        for i in range(iters):
            # chance to make RHS a pronumeral
            if i == 0 and random.random() < 0.5:
                rh = self.getVar()

            # ensure something is done to LHS
            if i < 2:
                lh = self.extend(lh)
            # otherwise 50-50 it's LHS or RHS
            elif random.random() < 0.5:
                lh = self.extend(lh)
            else:
                rh = self.extend(rh)

        eqn = Eq(lh, rh)
        try:
            soln = solve(eqn, x)[0]
            soln = Eq(x, soln)
        except IndexError as e:
            print(e)
            soln = 'No solution'

        Q = getPretty(eqn)
        A = getPretty(soln)
        LQ = '$\displaystyle {}$'.format(latex(eqn))
        LA = '$\displaystyle {}$'.format(latex(soln))
        return Q, A, LQ, LA
Ejemplo n.º 4
0
 def generate(self):
     x = symbols('x')
     a, b, c, d, e, f = symbols('a b c d e f')
     substitutions, eqn, LQ = [], None, None
     # HCF
     if self.difficulty == 1:
         expr = expand(random.choice([
             x * (x + a),
             x * (x - a),
         ]))
         if random.random() < 0.3:
             expr *= b
         if random.random() < 0.3:
             expr *= -1
         eqn = Eq(expr, 0)
         substitutions = [
             (a, random.randint(2, 10)),
             (b, random.randint(2, 10)),
             (c, random.randint(2, 10)),
             (d, random.randint(1, 10)),
         ]
     # Cross
     elif self.difficulty == 2:
         expr = expand(
             random.choice([
                 (x + a) * (x + b),
                 (x - a) * (x + b),
                 (x + a) * (x - b),
                 (x - a) * (x - b),
             ]))
         if random.random() < 0.5:
             expr *= c
         eqn = Eq(expr, 0)
         substitutions = [
             (a, random.randint(2, 10)),
             (b, random.randint(1, 6)),
             (c, random.randint(2, 5)),
             (d, random.randint(1, 10)),
         ]
     # DOPS
     elif self.difficulty == 3:
         expr = expand(
             random.choice([
                 (x + a) * (x - a),
                 (a * x + b) * (a * x - b),
             ]))
         if random.random() < 0.3:
             expr *= c
         eqn = Eq(expr, 0)
         substitutions = [
             (a, random.randint(2, 10)),
             (b, random.randint(1, 10)),
             (c, random.randint(2, 5)),
             (d, random.randint(1, 10)),
         ]
     # Non-monic
     elif self.difficulty == 4:
         expr = expand(
             random.choice([
                 (a * x + b) * (x + d),
                 (a * x + b) * (c * x + d),
             ]))
         if random.random() < 0.3:
             expr *= a
         eqn = Eq(expr, 0)
         substitutions = [
             (a, random.randint(2, 4)),
             (b, random.randint(2, 10)),
             (c, random.randint(2, 5)),
             (d, random.randint(2, 5)),
         ]
     for old, new in substitutions:
         with evaluate(True):
             eqn = eqn.replace(old, new)
     try:
         soln = solve(eqn, x)[0], solve(eqn, x)[1]
     except IndexError:
         # no solution
         soln = 'No solution'
     a = substitutions[0][1]
     b = substitutions[1][1]
     c = substitutions[2][1]
     d = substitutions[3][1]
     Q = getPretty(eqn)
     A = getPretty(soln)
     LQ = '$\displaystyle {}$'.format(latex(eqn))
     LA = '$\displaystyle x={}, \ {}$'.format(soln[0], soln[1])
     return Q, A, LQ, LA
Ejemplo n.º 5
0
 def generate(self):
     x = symbols('x')
     a, b, c, d, e, f = symbols('a b c d e f')
     substitutions, eqn, LQ = [], None, None
     if self.difficulty == 1:
         eqn, LQ = random.choice([
             (Eq(x * (x + a), 0), r'x(x+{a}) = 0'),
             (Eq(x * (x + a / b), 0), r'x(x+\frac{{{a}}}{{{b}}}) = 0'),
             (Eq(x * (x - a), 0), r'x(x-{a}) = 0'),
             (Eq(a * x * (x + b), 0), r'{a}x(x+{b}) = 0'),
             (Eq(a * x * (x - b), 0), r'{a}x(x-{b}) = 0'),
             (Eq(a * x * (x - b / c),
                 0), r'{a}x(x-\frac{{{b}}}{{{c}}}) = 0'),
         ])
         substitutions = [
             (a, random.randint(2, 10)),
             (b, random.randint(1, 10)),
             (c, random.randint(1, 10)),
             (d, random.randint(1, 10)),
         ]
     elif self.difficulty == 2:
         eqn, LQ = random.choice([
             (Eq((x + a) * (x + b), 0), r'(x+{a})(x+{b}) = 0'),
             (Eq((x + a) * (x - b), 0),
              random.choice([r'(x+{a})(x-{b}) = 0',
                             r'(x-{b})(x+{a}) = 0'])),
         ])
         substitutions = [
             (a, random.randint(1, 10)),
             (b, random.randint(1, 10)),
             (c, random.randint(1, 10)),
             (d, random.randint(1, 10)),
         ]
     elif self.difficulty == 3:
         eqn, LQ = random.choice([
             (Eq((a * x + b) * (x + b), 0),
              random.choice(
                  [r'({a}x+{b})(x+{b}) = 0', r'(x+{b})({a}x+{b}) = 0'])),
             (Eq((a * x - b) * (x - b), 0),
              random.choice(
                  [r'({a}x-{b})(x-{b}) = 0', r'(x-{b})({a}x-{b}) = 0'])),
             (Eq((a * x + b) * (c * x - d), 0),
              random.choice([
                  r'({a}x+{b})({c}x-{d}) = 0', r'({c}x-{d})({a}x+{b}) = 0'
              ])),
         ])
         substitutions = [
             (a, random.randint(2, 10)),
             (b, random.randint(1, 10)),
             (c, random.randint(2, 10)),
             (d, random.randint(1, 10)),
         ]
     for old, new in substitutions:
         with evaluate(False):
             eqn = eqn.replace(old, new)
     try:
         soln = solve(eqn, x)[0], solve(eqn, x)[1]
     except IndexError:
         # no solution
         soln = 'No solution'
     a = substitutions[0][1]
     b = substitutions[1][1]
     c = substitutions[2][1]
     d = substitutions[3][1]
     Q = getPretty(eqn)
     A = getPretty(soln)
     LQ = '$\displaystyle {}$'.format(LQ.format(a=a, b=b, c=c, d=d))
     LA = '$\displaystyle x={}, \ {}$'.format(soln[0], soln[1])
     return Q, A, LQ, LA
Ejemplo n.º 6
0
 def generate(self):
     x = symbols('x')
     a, b, c, d, e, f = symbols('a b c d e f')
     if self.difficulty == 1:
         eqn = random.choice([
             Eq(x + a, b),
             Eq(x - a, b),
             Eq(a * x, b),
             Eq(x / a, b),
         ])
         eqn = eqn.subs([
             (a, random.randint(2, 10)),
             (b, random.randint(0, 10)),
         ])
         soln = Eq(x, solve(eqn, x)[0])
         Q = getPretty(eqn)
         A = getPretty(soln)
         LQ = '$\displaystyle {}$'.format(latex(eqn))
         LA = '$\displaystyle {}$'.format(latex(soln))
         return Q, A, LQ, LA
     elif self.difficulty == 2:
         eqn = random.choice([
             Eq(a * x + b, c),
             Eq(a * x + b / d, c),
             Eq(a * x - b, c),
             Eq(a * x - b / d, c),
             Eq(x / a + b, c),
             Eq(x / a - b, c),
         ])
         # todo flip sides of equations randomly
         eqn = eqn.subs([
             (a, random.randint(2, 10)),
             (b, random.randint(1, 10)),
             (c, random.randint(0, 10)),
             (d, random.randint(2, 7)),
         ])
         # todo my own function that uses try/except to find no solutions
         soln = Eq(x, solve(eqn, x)[0])
         # todo Q, A = getPretty(eqn, soln)
         # todo LQ, LA = getLatex(eqn, soln)
         Q = getPretty(eqn)
         A = getPretty(soln)
         LQ = '$\displaystyle {}$'.format(latex(eqn))
         LA = '$\displaystyle {}$'.format(latex(soln))
         return Q, A, LQ, LA
     elif self.difficulty == 3:
         eqn = random.choice([
             Eq(a * x + b + c * x, d),
             Eq(a * x + b, d + c * x),
         ])
         substitutions = [
             (a, randIntExcept(-10, 10)),
             (b, randIntExcept(-10, 10)),
             (c, randIntExcept(-10, 10)),
             (d, randIntExcept(-10, 10)),
         ]
         for old, new in substitutions:
             with evaluate(False):
                 eqn = eqn.replace(old, new)
         try:
             soln = Eq(x, solve(eqn, x)[0])
         except IndexError:
             # no solution - lines are parallel
             soln = 'No solution'
         Q = getPretty(eqn)
         A = getPretty(soln)
         LQ = '$\displaystyle {}$'.format(latex(eqn))
         LA = '$\displaystyle {}$'.format(latex(soln))
         return Q, A, LQ, LA
     elif self.difficulty == 4:
         eqn = random.choice([
             Eq(a * x + b + c * x, d + e * x),
         ])
         substitutions = [
             (a, randIntExcept(-10, 10)),
             (b, randIntExcept(-10, 10)),
             (c, randIntExcept(-10, 10)),
             (d, randIntExcept(-10, 10)),
             (e, randIntExcept(-10, 10)),
         ]
         for old, new in substitutions:
             with evaluate(False):
                 eqn = eqn.replace(old, new)
         try:
             soln = Eq(x, solve(eqn, x)[0])
         except IndexError:
             # no solution - lines are parallel
             soln = 'No solution'
         Q = getPretty(eqn)
         A = getPretty(soln)
         LQ = '$\displaystyle {}$'.format(latex(eqn))
         LA = '$\displaystyle {}$'.format(latex(soln))
         return Q, A, LQ, LA
Ejemplo n.º 7
0
	def generate(self):
		a, b, c, d, e, f = symbols('a b c d e f')
		if self.difficulty == 1:
			expr = random.choice([
				a/b + c/d,
			])
			substitutions = [
				(a, random.randint(1, 7)),
				(b, random.randint(2, 7)),
				(c, random.randint(1, 7)),
				(d, random.randint(2, 7)),
			]
			for old, new in substitutions:
				with evaluate(False):
					expr = expr.replace(old, new)
			soln = simplify(expr)
			Q = getPretty(expr)
			A = getPretty(soln)
			LQ = '$\displaystyle {}$'.format(latex(expr))
			LA = '$\displaystyle {}$'.format(latex(soln))
			return Q, A, LQ, LA
		elif self.difficulty == 2:
			expr = random.choice([
				a/b + c/d,
				a/b + c,
				a + c/d,
			])
			substitutions = [
				(a, random.randint(1, 10)),
				(b, random.randint(2, 7)),
				(c, random.randint(1, 10)),
				(d, random.randint(2, 7)),
			]
			for old, new in substitutions:
				with evaluate(False):
					expr = expr.replace(old, new)
			soln = simplify(expr)
			Q = getPretty(expr)
			A = getPretty(soln)
			LQ = '$\displaystyle {}$'.format(latex(expr))
			LA = '$\displaystyle {}$'.format(latex(soln))
			return Q, A, LQ, LA
		elif self.difficulty == 3:
			expr = random.choice([
				a/b - c/d,
				-a/b + c/d,
			])
			substitutions = [
				(a, random.randint(1, 7)),
				(b, random.randint(2, 7)),
				(c, random.randint(1, 7)),
				(d, random.randint(2, 7)),
			]
			for old, new in substitutions:
				with evaluate(False):
					expr = expr.replace(old, new)
			soln = simplify(expr)
			Q = getPretty(expr)
			A = getPretty(soln)
			LQ = '$\displaystyle {}$'.format(latex(expr))
			LA = '$\displaystyle {}$'.format(latex(soln))
			return Q, A, LQ, LA
		elif self.difficulty == 4:
			expr = random.choice([
				a/b - c/d,
				a/b - c,
				a - c/d,
			])
			substitutions = [
				(a, random.randint(1, 10)),
				(b, random.randint(2, 7)),
				(c, random.randint(1, 10)),
				(d, random.randint(2, 7)),
			]
			for old, new in substitutions:
				with evaluate(False):
					expr = expr.replace(old, new)
			soln = simplify(expr)
			Q = getPretty(expr)
			A = getPretty(soln)
			LQ = '$\displaystyle {}$'.format(latex(expr))
			LA = '$\displaystyle {}$'.format(latex(soln))
			return Q, A, LQ, LA
		elif self.difficulty == 5:
			expr = random.choice([
				a/b + c/d,
				a/b + c,
				a + c/d,
				a/b - c/d,
				a/b - c,
				a - c/d,
			])
			substitutions = [
				(a, random.randint(1, 10)),
				(b, random.randint(2, 10)),
				(c, random.randint(1, 10)),
				(d, random.randint(2, 10)),
			]
			for old, new in substitutions:
				with evaluate(False):
					expr = expr.replace(old, new)
			soln = simplify(expr)
			Q = getPretty(expr)
			A = getPretty(soln)
			LQ = '$\displaystyle {}$'.format(latex(expr))
			LA = '$\displaystyle {}$'.format(latex(soln))
			return Q, A, LQ, LA
Ejemplo n.º 8
0
	def generate(self):
		a, b, c, d, e, f = symbols('a b c d e f')
		substitutions, expr, LQ = [], None, None
		if self.difficulty == 1:
			expr, LQ = random.choice([
				(a/b * c/d, r'\frac{{{a}}}{{{b}}} \times \frac{{{c}}}{{{d}}}')
			])
			substitutions = [
				(a, random.randint(1, 7)),
				(b, random.randint(2, 7)),
				(c, random.randint(1, 7)),
				(d, random.randint(2, 7)),
			]
		elif self.difficulty == 2:
			expr, LQ = random.choice([
				(a/b * c/d, r'\frac{{{a}}}{{{b}}} \times \frac{{{c}}}{{{d}}}'),
				(a/b * c, r'\frac{{{a}}}{{{b}}} \times {c}'),
				(a * c/d, r'{a} \times \frac{{{c}}}{{{d}}}'),
			])
			substitutions = [
				(a, random.randint(1, 10)),
				(b, random.randint(2, 7)),
				(c, random.randint(1, 10)),
				(d, random.randint(2, 7)),
			]
		elif self.difficulty == 3:
			expr, LQ = random.choice([
				((a/b) / (c/d), r'\frac{{{a}}}{{{b}}} \div \frac{{{c}}}{{{d}}}'),
				(-(a/b) / (c/d), r'-\frac{{{a}}}{{{b}}} \div \frac{{{c}}}{{{d}}}'),
			])
			substitutions = [
				(a, random.randint(1, 7)),
				(b, random.randint(2, 7)),
				(c, random.randint(1, 7)),
				(d, random.randint(2, 7)),
			]
		elif self.difficulty == 4:
			expr, LQ = random.choice([
				((a/b) / (c/d), r'\frac{{{a}}}{{{b}}} \div \frac{{{c}}}{{{d}}}'),
				((a/b) / c, r'\frac{{{a}}}{{{b}}} \div {c}'),
				(a / (c/d), r'{a} \div \frac{{{c}}}{{{d}}}'),
			])
			substitutions = [
				(a, random.randint(1, 10)),
				(b, random.randint(2, 7)),
				(c, random.randint(1, 10)),
				(d, random.randint(2, 7)),
			]
		elif self.difficulty == 5:
			expr, LQ = random.choice([
				(a/b * c/d, r'\frac{{{a}}}{{{b}}} \times \frac{{{c}}}{{{d}}}'),
				(a/b * c, r'\frac{{{a}}}{{{b}}} \times {c}'),
				(a * c/d, r'{a} \times \frac{{{c}}}{{{d}}}'),
				((a/b) / (c/d), r'\frac{{{a}}}{{{b}}} \div \frac{{{c}}}{{{d}}}'),
				((a/b) / c, r'\frac{{{a}}}{{{b}}} \div {c}'),
				(a / (c/d), r'{a} \div \frac{{{c}}}{{{d}}}'),
			])
			substitutions = [
				(a, random.randint(1, 10)),
				(b, random.randint(2, 10)),
				(c, random.randint(1, 10)),
				(d, random.randint(2, 10)),
			]
		assert len(substitutions) > 0
		for old, new in substitutions:
			with evaluate(False):
				expr = expr.replace(old, new)
		a = substitutions[0][1]
		b = substitutions[1][1]
		c = substitutions[2][1]
		d = substitutions[3][1]
		soln = simplify(expr)
		Q = getPretty(expr)
		A = getPretty(soln)
		LQ = '$\displaystyle {}$'.format(LQ.format(a=a, b=b, c=c, d=d))
		LA = '$\displaystyle {}$'.format(latex(soln))
		return Q, A, LQ, LA