Patterns#

The official documentation on patterns can be found here.

WARNING: Patterns are currently a beta feature, so be careful while using them. For advanced pattern usage, take a look at the Desmos CL forums here.

Brief Introduction#

The different patterns in Desmos CL are tools which can be used to require student answers in certain formats or to parse complex student inputs. Some of these patterns can themselves contain other patterns, like the fraction pattern which requires a pattern for the numerator and denominator.

Generating Patterns#

I personally think that examples are the best way to learn the peculiar format for defining patterns. Let's say that we want to make a pattern that matches a basic fraction in the format \(\frac{a}{b}\). We could define that as:

frac_pattern = patterns.fraction(patterns.number, patterns.number)

Then, let's say that students are putting their answer (as a fraction) into a math response component named math_resp. Then we could get the numerator of their response with:

frac_pattern.parse(math_resp.latex).numerator().latex

The parse method parses the student response and returns an object which we can get the numerator, denominator, or latex from. Note that each pattern object will have different things you can parse out of it (fractions have numerators and denominators, exponents have bases and exponents, etc.).

For a more complex pattern example, let's say that we gave our students the challenge of converting \(\left(\frac{a}{b}\right)^5\) into a fraction, with an expected answer of \(\frac{a^5}{b^5}\). This would correspond to the following pattern. Note that we can assign patterns to a shorthand variable to reduce the size of our pattern definition.

p = patterns
ans_pattern = p.fraction(p.exponent(p.literal("a"), p.integer), p.exponent(p.literal("b"), p.integer))

Now that we have the answer, we can first check that the student answer is in this format. To do this, we would use matches like so:

ans_matches = ans_pattern.matches(math_resp.latex)

Once we've checked that, we can confirm that the exponents are correct by using multiple applications of parse like so:

a_exp = ans_pattern.parse(math_resp.latex).numerator().exponent().numericValue
b_exp = ans_pattern.parse(math_resp.latex).denominator().exponent().numericValue

is_correct = (a_exp = 5) and (b_exp = 5)

Pattern Playground#

If you want to play around with the different patterns that are possible, below you can find a kind of "pattern playground" which will render the latex that corresponds to the kind of pattern you are constructing. Note that "patterns." or "p." both work as prefixes, though only "patterns." will work as a prefix in the CL editor (unless you've included the line p = patterns). Additionally, this playground only includes a set of the more common patterns (the ones with explanations currently in this documentation).

Input your pattern here:

Pattern Preview

Pattern Warnings

Kinds of Patterns#

pattern.number#

Pattern Attributes#

numberPattern

Attribute Type Description
satisfies(ltx::latex) numberPattern ltx should be some expression containing the variable x which evaluates to either true or false. For example, the following will match only to negative integers:

neg_int = patterns.integer.satisfies(`x < 0`)
matches(ltx::latex) boolean Description needed
parse(ltx::latex) numberPatternMatch Description needed
rejectParens numberPattern Description needed
allowParens numberPattern Description needed

Match Attributes#

numberPatternMatch

Attribute Type Description
numericValue number Description needed
latex latex Description needed

pattern.integer#

Pattern Attributes#

numberPattern

Attribute Type Description
satisfies(ltx::latex) numberPattern ltx should be some expression containing the variable x which evaluates to either true or false. For example, the following will match only to negative integers:

neg_int = patterns.integer.satisfies(`x < 0`)
matches(ltx::latex) boolean Description needed
parse(ltx::latex) numberPatternMatch Description needed
rejectParens numberPattern Description needed
allowParens numberPattern Description needed

Match Attributes#

numberPatternMatch

Attribute Type Description
numericValue number Description needed
latex latex Description needed

pattern.mixedNumber#

Pattern Attributes#

mixedNumberPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) mixedNumberPatternMatch Description needed

Match Attributes#

mixedNumberPatternMatch

Attribute Type Description
numericValue number Description needed
latex latex Description needed
whole number Description needed
numerator number Description needed
denominator number Description needed

pattern.scientific#

Pattern Attributes#

scientificPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) scientificPatternMatch Description needed

Match Attributes#

scientificPatternMatch

Attribute Type Description
numericValue number Description needed
latex latex Description needed
coefficient number Description needed
exponent number Description needed

pattern.literal#

patterns.literal(ltx::latex)::literalPattern

Pattern Attributes#

literalPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) literalPatternMatch Description needed
rejectParens literalPattern Description needed
allowParens literalPattern Description needed

Match Attributes#

literalPatternMatch

Attribute Type Description
latex latex Description needed

pattern.expression#

Pattern Attributes#

expressionPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) expressionPatternMatch Description needed
rejectParens expressionPattern Description needed
allowParens expressionPattern Description needed

Match Attributes#

expressionPatternMatch

Attribute Type Description
latex latex Description needed

pattern.anyOf#

patterns.anyOf(term1::pattern, term2::pattern, ...)::orPattern

anyOf accepts any number of terms, and will match any of them.

Pattern Attributes#

orPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) orPatternMatch Description needed

Match Attributes#

orPatternMatch

Attribute Type Description
latex latex Description needed

pattern.sum#

patterns.sum(term1::pattern, term2::pattern, ...)::sumPattern

sum accepts any number of terms, and will match their addition.

Pattern Attributes#

sumPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) sumPatternMatch Description needed
strictOrder sumPattern Description needed
rejectParens sumPattern Description needed
allowParens sumPattern Description needed
rejectNegativeTerms sumPattern Description needed
allowNegativeTerms sumPattern Description needed

Match Attributes#

sumPatternMatch

Attribute Type Description
latex latex Description needed
termX patternMatch where X is the index of a term in the patterns.sum call. This returns the patternMatch of the type of the indexed term. For example, to get the match for the first term of the sum, one would call:

patterns.sum(patterns.integer, patterns.integer).parse(`1+2`).term1

Note: Since strict order is by default not enforced, X is the index of the matching pattern, not necessarily of the value in the expression.

pattern.contains#

Pattern Attributes#

containsPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) containsPatternMatch Description needed

Match Attributes#

containsPatternMatch

Attribute Type Description
latex latex Description needed
term1 patternMatch Description needed

pattern.product#

patterns.product(term1::pattern, term2::pattern, ...)::productPattern

product accepts any number of terms, and will match their product.

Pattern Attributes#

productPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) productPatternMatch Description needed
strictOrder productPattern Description needed
rejectParens productPattern Description needed
allowParens productPattern Description needed

Match Attributes#

productPatternMatch

Attribute Type Description
latex latex Description needed
termX patternMatch where X is the index of a term in the patterns.product call. This returns the patternMatch of the type of the indexed term. For example, to get the match for the first term of the product, one would call:

patterns.product(patterns.integer, patterns.integer).parse(`1*2`).term1

Note: Since strict order is by default not enforced, X is the index of the matching pattern, not necessarily of the value in the expression.

pattern.juxt#

patterns.juxt(term1::pattern, term2::pattern, ...)::juxtPattern

juxt accepts any number of terms, and will match their juxtaposition.

WARNING: juxt will not recognize the juxtaposition of two numbers (as these are often parsed as a single number) and in general is not completely intuitive. Make sure to test thoroughly when using this feature.

Pattern Attributes#

juxtPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) juxtPatternMatch Description needed
strictOrder juxtPattern Description needed
rejectParens juxtPattern Description needed
allowParens juxtPattern Description needed

Match Attributes#

juxtPatternMatch

Attribute Type Description
latex latex Description needed
termX patternMatch where X is the index of a term in the patterns.juxt call. This returns the patternMatch of the type of the indexed term. For example, to get the match for the first term of the juxt, one would call:

patterns.juxt(patterns.integer, patterns.literal(`x`)).parse(`1x`).term1

Note: Since strict order is by default not enforced, X is the index of the matching pattern, not necessarily of the value in the expression.

pattern.fraction#

patterns.fraction(numerator::pattern, denominator::pattern)::fractionPattern

Pattern Attributes#

fractionPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) fractionPatternMatch Description needed
rejectParens fractionPattern Description needed
allowParens fractionPattern Description needed

Match Attributes#

fractionPatternMatch

Attribute Type Description
latex latex Description needed
numerator patternMatch Description needed
denominator patternMatch Description needed

pattern.division#

patterns.division(dividend::pattern, divisor::pattern)::divisionPattern

Pattern Attributes#

divisionPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) divisionPatternMatch Description needed
rejectParens divisionPattern Description needed
allowParens divisionPattern Description needed

Match Attributes#

divisionPatternMatch

Attribute Type Description
latex latex Description needed
divisor patternMatch Description needed
dividend patternMatch Description needed

pattern.radical#

patterns.radical(radicand::pattern, degree::pattern)::radicalPattern

Pattern Attributes#

radicalPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) radicalPatternMatch Description needed
rejectParens radicalPattern Description needed
allowParens radicalPattern Description needed

Match Attributes#

radicalPatternMatch

Attribute Type Description
latex latex Description needed
radicand patternMatch Description needed
degree patternMatch Description needed

pattern.overLine#

Description Needed

pattern.overArc#

Description Needed

pattern.overRightArrow#

Description Needed

pattern.overLeftArrow#

Description Needed

pattern.difference#

patterns.difference(minuend::pattern, subtrahend::pattern)::pattern

Pattern Attributes#

differencePattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) differencePatternMatch Description needed
rejectParens differencePattern Description needed
allowParens differencePattern Description needed

Match Attributes#

differencePatternMatch

Attribute Type Description
latex latex Description needed
minuend patternMatch Description needed
subtrahend patternMatch Description needed

pattern.repeat#

Description Needed

pattern.optional#

Description Needed

pattern.functionCall#

Description Needed

pattern.prime#

Description Needed

pattern.exponent#

patterns.exponent(base::pattern, exponent::pattern)::exponentPattern

Pattern Attributes#

exponentPattern

Attribute Type Description
matches(ltx::latex) boolean Description needed
parse(ltx::latex) exponentPatternMatch Description needed
rejectParens exponentPattern Description needed
allowParens exponentPattern Description needed

Match Attributes#

exponentPatternMatch

Attribute Type Description
latex latex Description needed
base patternMatch Description needed
exponent patternMatch Description needed

pattern.comparator#

Description Needed

pattern.comparatorChain#

Description Needed

pattern.symbol#

Description Needed