Pattern Matching

Patterns weren’t documented in the old version. Some of the members were listed (as empty entries) but they were never documented. Currently its only mentioned in the advanced > Interpreting Math section.
https://teacher.desmos.com/computation-layer/documentation#interpreting-math

That was it: I remembered all the members listed as blank entries! After viewing your videos today, I thought I would pull up that list and see what else I could remix and figure out how to use. Thanks for pointing me to the examples that are in the current documentation.

Any chance there’s a workaround for division?

I tried using p.contain but could only pull apart the first term.
image

My particular use-case is to see if either fraction 1 or fraction 2 is a unit fraction in the division expression.

No workaround yet, but this is a great example for us to see for the development of this feature. Thanks!

If there’s an update I’ll post here.

1 Like

I’m loving this! Just curious, is there a pattern match for subscripts (something similar to exponents). I need some code that uses the subscript number and I’d rather do that than make a huge “when” list. :laughing:

What’s your purpose? You might be able to use lists in a graph to achieve your intent.

We’re doing a lesson on scheduling for a math 1030 course and I would like the student to enter a task, T_n, and be able to tell which task they have entered. Right now I have some code like this:
number(n) = when this.input=T_1 1
when this.input=T_2 2 …
and so on.
It would be nice to be able to just look at the subscript.

There’s nothing to support that. Maybe this type of thing would help? You’d need to define i_{nput} in your graph CL.

Is there a way to parse compound inequalities with pattern matching? I know parse inequality doesn’t recognize them, but parsing something like 0<x<8 for piecewise functions would be useful for validation.

1 Like

Not through pattern matching, but you might be able to handle it without.

Can you show how to use radical pattern matching? Thanks for any help!

Sure!

The radical pattern takes one or two arguments. the required argument is the radicand and the optional argument is the degree, i.e.
p.radical(radicand)
p.radical(radicand,degree)

Currently, the pattern checks strictly for radicals in each form, meaning that p.radical(p.expression,2) won’t match \sqrt{5} because a degree of 2 is not specified. Likewise, \sqrt[2]{5} won’t match p.radical(p.expression) because a degree is not asked for in the pattern. This may change as improvements are made to the pattern library.

here are a few working examples: Radical Pattern • Activity Builder by Desmos

1 Like

Thank you so much! I can’t wait to try this out.

Sandy Daly

Love your tutorials. Question, I am attempting to use the patterns.mixedNumber. Is there anywhere I can find any documentation on what is required with this pattern?

Using what you shared in the fractions tutorial, I am able to make it work for patterns.fraction however when I replace fraction with mixedNumber (which it recognizes) I am unable to get the rest of that section of code to work. Any help or direction would be appreciated.

Love that you’re getting into pattern writing!

The mixed number pattern actually doesn’t take any arguments like the fraction pattern does. Thats because mixed numbers are very strictly defined in the calculator - mostly to make a clear distinction between mixed numbers (addition) and something else (multiplication).

Here are all the branches of the pattern you’ll need:

Thank you so much. This is exactly what I needed.

Also, thank you again for your well-organized tutorials. I appreciate the time you put into explaining the bigger picture instead of just telling your audience to “type this” then “type that”. Have a great weekend.

@Jay
Hi, I am trying to write a pattern for \left(x-1\right)+2x\left(x+1\right)=\left(x-1\right)\left(x+2\right)

I have this so far…and it’s not working…

X = p.literal(x)

p=patterns

matchlhs = p.sum( p.difference(X,p.integer), p.product(p.integer, X, p.sum(X,p.integer)))
matchrhs = p.product( p.difference(X,p.integer), p.sum(X,p.integer) )

lhs = parseEquation(this.cellContent(4,1)).lhs
rhs = parseEquation(this.cellContent(4,1)).rhs

ismatch = when matchlhs.matches(lhs) and matchrhs.matches(rhs) “Structure :heavy_check_mark:” otherwise “Structure :x:

To make it work I have to place parenthesis around the first x factor. How can I make this work so the unnecessary parentheses are not needed?
Example that works: \left(x-1\right)+2\left(x\right)\left(x+1\right)=\left(x-1\right)\left(x+2\right)

Thank you. Love patterns.
Also looking for a log pattern😊

@Jay, is this a bug? This pattern actually works as long as you place a cdot after the 2x.

Shayne, what’s the purpose of the format? You could use some formula evaluation to verify equivalent expressions, and include some countNumberUsage since the pattern matching isn’t working.

checkNums= countNumberUsage(lhs,2)=1 and countNumberUsage(lhs,1)=2

Its behaving as designed. The calculator is interpreting x() as function notation. More support for this is in the works.

Hi Jay,
Thank you so much for posting the tutorial videos. I learn so much from your videos.
I am teaching a unit on Partial Fraction Decomposition, and I want to be able to check the correctness of students’ answer.
I was able to use the following code to extract the denominator of students’ response for difference.
frac= patterns.fraction(patterns.number, patterns.expression)
diff=patterns.difference(frac,frac)
den=diff.parse(this.cellContent(1,1)).subtrahend.denominator
But I am not able to do it for sum as these is no “addend” when parsing a sum. Is there a workaround for this?
Thank you very much.