Pattern Matching for Polynomial with Coefficient

4 views
Skip to first unread message

Josh Joubert

unread,
Apr 17, 2026, 5:03:25 AM (6 days ago) Apr 17
to Numbas Users
Good day

Working with a gap fill mathematical expression.

For context, this is a high school activity working on developing products in the form of a quadratic trinomial.

I want to match for the product of a monomial and a polynomial with the following specifications:
  • form 𝑥(𝑦) where 𝑥 is a monomial and 𝑦 is a polynomial
  • 𝑥 may not be omitted or empty
  • 𝑥 must be a number, unless it contains any number of variables with or without exponents, in which case it may also contain a number

This is my current pattern:
`+-((($n`?($v^($n)`?)`+)`|($n))((`+-($n`?($v^$n)`+)) + (`+-($n`?($v^($n)`?)`+))`+ + (`+-(($n`?($v^$n)`+)`|$n))))

Which NUMBAS renders as:
xwroyyg058nmo2o9c5e.png

From my understanding, the "or number" section (`|($n)) for 𝑥 is causing trouble, though it may also be the quantifiers.

I want the pattern to be able to accept both of the following,
  • -3a^2(6a^2-3ab+10ab-5b^2)
  • 3(6a^2-3ab+10ab-5b^2)

but NOT:
  • 6a^2-3ab+10ab-5b^2

Christian Lawson-Perfect

unread,
Apr 17, 2026, 5:19:16 AM (6 days ago) Apr 17
to numbas...@googlegroups.com
My understanding is you want the expression to be the product of some terms, at least one of which is a monomial.

You might get away with doing something much simpler, by not worrying about exactly what each factor looks like. I think this does what you want:

m_exactly(`!(?+?)`+;monomial * ?`*;other_factors)

The subpattern `!(?+?) is the important bit: it looks for factors which aren't sums. Because you're not capturing the terms of the sum, you don't need the `+- operator. 
So the whole pattern looks for one or more monomial factors, and then allows any number of other factors, which necessarily must be polynomials.

--
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, visit https://groups.google.com/d/msgid/numbas-users/3e8d61a6-e5a3-45ce-b221-e3f1b5782c4dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages