mirror of
https://github.com/DeaDvey/mathgenerator.git
synced 2025-11-28 14:35:23 +01:00
Merge pull request #72 from RobertWetzler/system_of_equations
System of equations
This commit is contained in:
@@ -276,26 +276,51 @@ def factoringFunc(range_x1 = 10, range_x2 = 10):
|
|||||||
solution = f"(x{x1})(x{x2})"
|
solution = f"(x{x1})(x{x2})"
|
||||||
return problem, solution
|
return problem, solution
|
||||||
|
|
||||||
def simplifyRadicalFunc(range_x = 100):
|
def systemOfEquationsFunc(range_x = 10, range_y = 10, coeff_mult_range=10):
|
||||||
x = random.randint(0, range_x)
|
# Generate solution point first
|
||||||
if x == 1 or x == 0:
|
x = random.randint(-range_x, range_x)
|
||||||
return f"sqrt({x})", str(x)
|
y = random.randint(-range_y, range_y)
|
||||||
inside = x
|
# Start from reduced echelon form (coeffs 1)
|
||||||
outside = 1
|
c1 = [1, 0, x]
|
||||||
factor = 2
|
c2 = [0, 1, y]
|
||||||
while factor * factor <= inside:
|
|
||||||
if inside % (factor * factor) == 0:
|
def randNonZero():
|
||||||
# move factor^2 from inside to outside
|
return random.choice([i for i in range(-coeff_mult_range, coeff_mult_range)
|
||||||
inside //= (factor * factor)
|
if i != 0])
|
||||||
outside *= factor
|
# Add random (non-zero) multiple of equations (rows) to each other
|
||||||
else:
|
c1_mult = randNonZero()
|
||||||
factor += 1
|
c2_mult = randNonZero()
|
||||||
problem = f"sqrt({x})"
|
new_c1 = [c1[i] + c1_mult * c2[i] for i in range(len(c1))]
|
||||||
# exclude redundant multiplications by 1
|
new_c2 = [c2[i] + c2_mult * c1[i] for i in range(len(c2))]
|
||||||
solution = str(outside if outside != 1 else '') + \
|
|
||||||
(f"sqrt({inside})" if inside != 1 else "")
|
# For extra randomness, now add random (non-zero) multiples of original rows
|
||||||
|
# to themselves
|
||||||
|
c1_mult = randNonZero()
|
||||||
|
c2_mult = randNonZero()
|
||||||
|
new_c1 = [new_c1[i] + c1_mult * c1[i] for i in range(len(c1))]
|
||||||
|
new_c2 = [new_c2[i] + c2_mult * c2[i] for i in range(len(c2))]
|
||||||
|
|
||||||
|
def coeffToFuncString(coeffs):
|
||||||
|
# lots of edge cases for perfect formatting!
|
||||||
|
x_sign = '-' if coeffs[0] < 0 else ''
|
||||||
|
# No redundant 1s
|
||||||
|
x_coeff = str(abs(coeffs[0])) if abs(coeffs[0]) != 1 else ''
|
||||||
|
# If x coeff is 0, dont include x
|
||||||
|
x_str = f'{x_sign}{x_coeff}x' if coeffs[0] != 0 else ''
|
||||||
|
# if x isn't included and y is positive, dont include operator
|
||||||
|
op = ' - ' if coeffs[1] < 0 else (' + ' if x_str != '' else '')
|
||||||
|
# No redundant 1s
|
||||||
|
y_coeff = abs(coeffs[1]) if abs(coeffs[1]) != 1 else ''
|
||||||
|
# Don't include if 0, unless x is also 0 (probably never happens)
|
||||||
|
y_str = f'{y_coeff}y' if coeffs[1] != 0 else ('' if x_str != '' else '0')
|
||||||
|
return f'{x_str}{op}{y_str} = {coeffs[2]}'
|
||||||
|
|
||||||
|
problem = f"{coeffToFuncString(new_c1)}, {coeffToFuncString(new_c2)}"
|
||||||
|
solution = f"x = {x}, y = {y}"
|
||||||
return problem, solution
|
return problem, solution
|
||||||
|
|
||||||
|
# Add random (non-zero) multiple of equations to each other
|
||||||
|
|
||||||
# || Class Instances
|
# || Class Instances
|
||||||
|
|
||||||
#Format is:
|
#Format is:
|
||||||
@@ -322,4 +347,5 @@ areaOfTriangle = Generator("Area of Triangle", 18, "Area of Triangle with side l
|
|||||||
doesTriangleExist = Generator("Triangle exists check", 19, "Does triangle with sides a, b and c exist?","Yes/No", isTriangleValidFunc)
|
doesTriangleExist = Generator("Triangle exists check", 19, "Does triangle with sides a, b and c exist?","Yes/No", isTriangleValidFunc)
|
||||||
midPointOfTwoPoint=Generator("Midpoint of the two point", 20,"((X1,Y1),(X2,Y2))=","((X1+X2)/2,(Y1+Y2)/2)",MidPointOfTwoPointFunc)
|
midPointOfTwoPoint=Generator("Midpoint of the two point", 20,"((X1,Y1),(X2,Y2))=","((X1+X2)/2,(Y1+Y2)/2)",MidPointOfTwoPointFunc)
|
||||||
factoring = Generator("Subtraction", 21, "x^2+(x1+x2)+x1*x2", "(x-x1)(x-x2)", factoringFunc)
|
factoring = Generator("Subtraction", 21, "x^2+(x1+x2)+x1*x2", "(x-x1)(x-x2)", factoringFunc)
|
||||||
simplifyRadical = Generator("Simplify a Radical", 22, "sqrt(72)", "6sqrt(2)", simplifyRadicalFunc)
|
systemOfEquations = Generator("Solve a System of Equations in R^2", 23, "2x + 5y = 13, -3x - 3y = -6", "x = -1, y = 3",
|
||||||
|
systemOfEquationsFunc)
|
||||||
Reference in New Issue
Block a user