Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions source/linear-algebra/exercises/outcomes/AT/AT5/generator.sage
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,61 @@ class Generator(BaseGenerator):
v2 = vector([x2,y2])
v3 = vector([x3,y3])
v = vector([x,y])
cs, ds = 2,3
v1s = vector([1,2])
v2s = vector([2,3])
v3s = vector([3,4])
vs = vector([1,2])
vectorsimplify = lambda v : vector([simplify(expand(x)) for x in v])

true_property_options = ["add_assoc","add_comm","mul_assoc","dist_v","dist_s","mul_id"]
false_only_property_options = ["add_id","add_inv"]

def verify(plus,times,hardfalseproperties=[]):
def verify(plus,times):
trueproperties={}
falseproperties=hardfalseproperties
falseproperties={}
for prop in true_property_options:
if prop == "add_assoc":
LHS = plus(v1,plus(v2,v3))
RHS = plus(plus(v1,v2),v3)
LHS_sample = plus(v1s,plus(v2s,v3s))
RHS_sample = plus(plus(v1s,v2s),v3s)
elif prop == "add_comm":
LHS = plus(v1,v2)
RHS = plus(v2,v1)
LHS_sample = plus(v1s,v2s)
RHS_sample = plus(v2s,v1s)
elif prop == "mul_assoc":
LHS = times(c*d,v)
RHS = times(c,times(d,v))
LHS_sample = times(cs*ds,vs)
RHS_sample = times(cs,times(ds,vs))
elif prop == "mul_id":
LHS = times(1,v)
RHS = v
LHS_sample = times(1,vs)
RHS_sample = vs
elif prop == "dist_v":
LHS = times(c,plus(v1,v2))
RHS = plus(times(c,v1),times(c,v2))
LHS_sample = times(cs,plus(v1s,v2s))
RHS_sample = plus(times(cs,v1s),times(cs,v2s))
elif prop == "dist_s":
LHS = times(c+d,v)
RHS = plus(times(c,v),times(d,v))
LHS_sample = times(cs+ds,vs)
RHS_sample = plus(times(cs,vs),times(ds,vs))
LHS = vectorsimplify(LHS)
RHS = vectorsimplify(RHS)
if LHS == RHS:
trueproperties[prop]=vectorsimplify(LHS)
trueproperties[prop]=LHS
else:
falseproperties.append(prop)
falseproperties[prop]={
"LHS": LHS,
"RHS": RHS,
"LHS_sample": LHS_sample,
"RHS_sample": RHS_sample
}
for prop in false_only_property_options:
if "dist_s" in trueproperties and "mul_id" in trueproperties:
if prop == "add_id":
Expand All @@ -54,9 +76,6 @@ class Generator(BaseGenerator):
if vectorsimplify(LHS) != vectorsimplify(RHS):
falseproperties.append("add_inv")
return (trueproperties, falseproperties)

#Use this to code in false properties that cannot be checked automatically ("add_id" and "add_inv")
hardfalseproperties=[]

#Use this to list a property that is true, but you don't want students to check
#because it is too easy (usually "add_comm") or too hard
Expand All @@ -73,8 +92,6 @@ class Generator(BaseGenerator):
theta = lambda v : vector([v[0]+a,v[1]+b])
untheta = lambda v : vector([v[0]-a,v[1]-b])

hardfalseproperties += ["add_id","add_inv"]

elif n==1:
plus = lambda v1,v2 : vector([v1[0]+v2[0], v1[1]+v2[1]])
r1 = randrange(1,9)
Expand All @@ -87,9 +104,7 @@ class Generator(BaseGenerator):

elif n==2:
plus = lambda v1,v2 : vector([v1[0]+v2[0], v1[1]+v2[1]])
r2 = randrange(2,4)
#times= lambda c,v : vector([c*v[0],c^(r2)*v[1]])
times= lambda c,v : vector([c^(r2)*v[0],c*v[1]])
times= lambda c,v : vector([c^2*v[0],c*v[1]])
a=randrange(1,8)
b=randrange(2,8)
theta = lambda v: vector([v[0]+b*v[1],v[1]+a])
Expand Down Expand Up @@ -131,12 +146,12 @@ class Generator(BaseGenerator):
oplus = lambda v1,v2 : theta(plus(untheta(v1),untheta(v2)))
otimes = lambda c,v : theta(times(c,untheta(v)))

trueproperties, falseproperties = verify(oplus,otimes,hardfalseproperties)
trueproperties, falseproperties = verify(oplus,otimes)

for prop in true_no_check_properties:
if prop in trueproperties.keys():
trueproperties.pop(prop)
else:
print("WARNING: Property "+prop + " was false.")
assert prop in trueproperties.keys()
trueproperties.pop(prop)

trueproperty, verification = choice(list(trueproperties.items()))

return {
Expand All @@ -145,5 +160,5 @@ class Generator(BaseGenerator):
"trueproperty": {
trueproperty: verification
},
"falseproperties": {f: True for f in falseproperties},
"falseproperties": falseproperties,
}
56 changes: 26 additions & 30 deletions source/linear-algebra/exercises/outcomes/AT/AT5/template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,6 @@ Show both sides simplify to <m>{{add_comm}}</m>.
</outtro>
</knowl>
<!-- {{/add_comm}} -->
<!-- {{#add_id}} -->
<!-- <knowl>
<content>
<p>
Show that there exists an additive identity element, that is:
<me>
\text{There exists }(w,z)\in V
\text{ such that }(x,y)\oplus(w,z)=(x,y).
</me>
</p>
</content>
<outtro><p>TODO</p></outtro>
</knowl> -->
<!-- {{/add_id}} -->
<!-- {{#add_inv}} -->
<!-- <knowl>
<content>
<p>
Show that additive inverses exist.
</p>
</content>
<outtro><p>TODO</p></outtro>
</knowl> -->
<!-- {{/add_inv}} -->
<!-- {{#mul_assoc}} -->
<knowl>
<content>
Expand Down Expand Up @@ -160,14 +136,20 @@ any one of the following properties does not hold:
<!-- {{#add_assoc}} -->
<item>
<p>
Vector addition is not associative.
Vector addition is not associative: <me>(x_1,y_1)\oplus((x_2,y_2)\oplus(x_3,y_3))={{LHS}}</me>
<me>((x_1,y_1)\oplus(x_2,y_2))\oplus(x_3,y_3)={{RHS}}</me>
For example: <me>(1,2)\oplus((2,3)\oplus(3,4))={{LHS_sample}}</me>
<me>((1,2)\oplus(2,3))\oplus(3,4)={{RHS_sample}}</me>
</p>
</item>
<!-- {{/add_assoc}} -->
<!-- {{#add_comm}} -->
<item>
<p>
Vector addition is not commutative.
Vector addition is not commutative: <me>(x_1,y_1)\oplus(x_2,y_2)={{LHS}}</me>
<me>(x_2,y_2)\oplus(x_1,y_1)={{RHS}}</me>
For example: <me>(1,2)\oplus(2,3)={{LHS_sample}}</me>
<me>(2,3)\oplus(1,2)={{RHS_sample}}</me>
</p>
</item>
<!-- {{/add_comm}} -->
Expand All @@ -188,28 +170,42 @@ Additive inverses do not always exist.
<!-- {{#mul_assoc}} -->
<item>
<p>
Scalar multiplication is not associative.
Scalar multiplication is not associative: <me>c\odot(d\odot(x,y))={{LHS}}</me>
<me>(cd)\odot(x,y)={{RHS}}</me>
For example: <me>2\odot(3\odot(1,2))={{LHS_sample}}</me>
<me>(2\cdot 3)\odot(1,2)={{RHS_sample}}</me>
</p>
</item>
<!-- {{/mul_assoc}} -->
<!-- {{#mul_id}} -->
<item>
<p>
<m>1</m> is not a scalar multiplication identity.
<m>1</m> is not a scalar multiplication identity: <me>1\odot(x,y)={{LHS}} \neq (x,y)</me>
For example: <me>1\odot(1,2)={{LHS_sample}} \neq (1,2)</me>
</p>
</item>
<!-- {{/mul_id}} -->
<!-- {{#dist_v}} -->
<item>
<p>
Scalar multiplication does not distribute over vector addition.
Scalar multiplication does not distribute over vector addition:
<me>c\odot((x_1,y_1)\oplus(x_2,y_2))={{LHS}}</me>
<me>(c\odot(x_1,y_1))\oplus(c\odot(x_2,y_2))={{RHS}}</me>
For example:
<me>2\odot((1,2)\oplus(2,3))={{LHS_sample}}</me>
<me>(2\odot(1,2))\oplus(2\odot(2,3))={{RHS_sample}}</me>
</p>
</item>
<!-- {{/dist_v}} -->
<!-- {{#dist_s}} -->
<item>
<p>
Scalar multiplication does not distribute over scalar addition.
Scalar multiplication does not distribute over scalar addition:
<me>(c+d)\odot(x,y)={{LHS}}</me>
<me>(c\odot(x,y))\oplus(d\odot(x,y))={{RHS}}</me>
For example:
<me>(2+3)\odot(1,2)={{LHS_sample}}</me>
<me>(2\odot(1,2))\oplus(3\odot(1,2))={{RHS_sample}}</me>
</p>
</item>
<!-- {{/dist_s}} -->
Expand Down
Loading