Pattern Match Absolute Value Equation

I am struggling with building a pattern match for equations of the type |x-5|=6. I have watched, and worked through most, of Jay’s videos (TY) for pattern matching but …

Does pattern matching the LHS to a difference work (even if you can’t specify absolute value)? If so, evaluating should help with any ambiguity, right?

Yea, I can parse the equation and check the different sides. Can pattern match with the difference (Jay’s videos were great) but I am stuck with verifying the absolute value. Probably can work around that-- verifying numerically-- but wondering…

I don’t see an absolute value pattern in the library, so you might have to explore another option. If you are looking for something to use as an errorMessage in case they forgot the symbols, you could check to see if one solution evaluates correctly and if the other doesn’t, then they probably forgot the symbols.

Hi,

I know I am super late to this party, but I am just now trying to solve a similar problem, and I found a work-around. I know you’re probably no longer thinking about this, but hopefully when people search for and find your question, they’ll find this work around.

I am working on a problem that prompts students to use absolute value symbols to find the distance between two integers. So if the integers are 7 and 4, I expect them to write |7-4|

The workaround relies on the fact that pattern matching can detect and reject parenthesis, but also that absolute value can act like parenthesis.

I use not(p.difference(p.number,p.number).allowParens.matches(this.latex)) to make sure they aren’t just writing (7-4)

Then I use func = simpleFunction(x\cdot${this.latex},“x”).evaluateAt(-3) to artificially multiply by -3 in front of their input, and if they don’t have absolute value symbols, the order of ops will only multiply the 7 by negative 3.

Finally I check that func = -9

This makes it so that 7-4 and (7-4) are marked incorrect, but |7-4| is correct.
I hope this helps somebody

p = patterns

ans =
  when 
    this.numericValue = 3 and
    not(p.difference(p.number,p.number).allowParens.matches(this.latex)) and
    func = -9 and
    p.contains(p.difference(p.integer,p.integer)).matches(this.latex) 1
  otherwise 0
  
correct: ans = 1
showSubmitButton: false

func = simpleFunction(`x\cdot${this.latex}`,"x").evaluateAt(-3)
1 Like