Last active
December 19, 2018 00:12
-
-
Save mjkaufer/75665a52499f09fe8aae900124caa8ad to your computer and use it in GitHub Desktop.
Count the number of students in the example in my blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from CoefficientCalculator import Term, MultiTerm, Expression, Scalar | |
# Some brief terminology (no pun intended) | |
# A term is something like x, x^2, x^3, etc. | |
# 1 can also be a term | |
# A multiterm is the product of several terms. Something like x * y or x * z^2 is fine | |
# Multiterms also have constants, so 5 * x * y^3 is also a valid multiterm | |
# Expressions are several multiterms added together. Something like (1 + x) is an expression | |
# create classes (as in school classes, not object oriented classes) x, y, z | |
x = Term("x") | |
y = Term("y") | |
z = Term("z") | |
# w is the "global" class | |
w = Term("w") | |
students = [] | |
# student a | |
students.append(MultiTerm([x, z, w])) | |
# student b | |
students.append(MultiTerm([x, w])) | |
# student c | |
students.append(MultiTerm([x, y, z, w])) | |
# student d | |
students.append(MultiTerm([x, y, w])) | |
# student e | |
students.append(MultiTerm([y, w])) | |
# student f | |
students.append(MultiTerm([y, w])) | |
# student g | |
students.append(MultiTerm([z, w])) | |
# student h | |
students.append(MultiTerm([z, w])) | |
# add a "1 + " to the start of each student's multiterm, making it an expression | |
studentExpressions = [Expression([Scalar(), s]) for s in students] | |
# base expression of "1" | |
e = Expression([Scalar()]) | |
# take the product of all the expressions | |
for s in studentExpressions: | |
e *= s | |
# get the coefficient for the Thanos problem | |
e.getCoefficient(MultiTerm([Term("x", 2), Term("y", 2), Term("z", 2), Term("w", 4)])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment