Help with pattern matching for an algebraic fraction

13 views
Skip to first unread message

Ben Brawn

unread,
Nov 29, 2021, 5:45:48 PM11/29/21
to Numbas Users
I'm making a question where a student needs to multiply two algebraic fractions to form a single algebraic fraction. Without pattern matching the student can simply reenter the question as the answer and get it correct. 

I am very weak with pattern matching (and regular expressions). Can someone explain how to ensure the answer doesn't use two slashes for division, or ensure it is just something divided by something? (I don't actually care if the student expands or cancels, I really just want to see them multiply, so the numerator/denominator could be a sum or a product).

I had assumed ?/? would do it, or `!((?/?)*(?/?)) but ? doesn't seem to do what I think it does.


Cheers
Ben


Christian Lawson-Perfect

unread,
Nov 30, 2021, 3:24:51 AM11/30/21
to numbas...@googlegroups.com
A way of thinking about this that I've used in the past is that you want the expression to consist solely of the division of two things that don't have any divisions in them.

A pattern matching "something that doesn't have any divisions in it" is
   `! m_anywhere( ?/? )
("look for a division anywhere in the expression. If you find one, this pattern doesn't match")

Then you want the division of two things like that:
   `! m_anywhere( ?/? ) / `! m_anywhere( ?/? )

But a problem you encounter is that by default, the pattern-matcher treats division as multiplication by the reciprocal. You need to turn on "strict inverse" mode to avoid that:
   m_strictinverse(
     `! m_anywhere(?/?)
     /
     `! m_anywhere(?/?)`?
   )

I've chucked a `? on the end of the denominator, so it's optional - the student might think that the fraction cancels.

--
You received this message because you are subscribed to the Google Groups "Numbas Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numbas-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/numbas-users/69ed8a3f-64cb-4768-a5b2-9948d67eba26n%40googlegroups.com.

Christian Lawson-Perfect

unread,
Nov 30, 2021, 4:05:09 AM11/30/21
to numbas...@googlegroups.com
Actually, having thought about it, m_anywhere(?/?) should be fine without "strict inverse". The reason it wasn't matching on its own was that "allow other terms" isn't turned on, and when there's more than one factor in the numerator, just ?/? on its own didn't match.
I've changed m_anywhere to turn on "allow other terms" for the pattern it's given, so the restriction is now a bit simpler:
     `! m_anywhere(?/?)
     /
     `! m_anywhere(?/?)`?

Christian Lawson-Perfect

unread,
Nov 30, 2021, 4:18:13 AM11/30/21
to numbas...@googlegroups.com
... Nope, should have tested before sending that email! You do need strict inverse after all. Sorry for all the emails, everyone!

Ben Brawn

unread,
Nov 30, 2021, 6:08:52 PM11/30/21
to Numbas Users
Thanks, Christian. That helps a lot.
Reply all
Reply to author
Forward
0 new messages