# Best way to parse expanded quadratic expressions

I am keen to be able to parse quadratic expressions like x²-7x+6 and extract the coefficients as a = 1, b = -7, c = 6.

I saw there is the parabola/conic type and so far the below is the best I have come up, but the problem for my purposes is that I don’t want students to be able to type in `(x - 6)(x - 1)` and have Desmos do the expansion for us.

Has anyone written some CL code that parses expanded quadratic expressions and returns the coefficients?

``````negate = simpleFunction(`-x`)

fn = conic(exp.latex)
a = negate.evaluateAt(fn.getCoefficient(3,1))
b = negate.evaluateAt(fn.getCoefficient(2,1))
c = negate.evaluateAt(fn.getCoefficient(1,1))
``````

I just use Calculus to find this

1 Like

I’m not sure how to use calculus to do it in a simpler way than the five lines of code above, but actually the more fundamental issue I’m facing is that I realised I want to parse expanded expressions to extract the coefficients (it’s for correctness-checking on questions where students are required to expand brackets).

I really like the conic thing for what I think your purpose is, it’s just i typically use the calculus stuff to do some other things too.

I guess i’m not sure how your solution isn’t a solution. Seems to work, no? I think i must not understand the problem.

1 Like

Yep I wasn’t super clear in the original question. If I ask them to expand (x - 6)(x - 1) I want them to give me the answer x²-7x+6, but if I just check that they have got the right function (which the calculus solution and conic solutions both would do) then I would end up automatically classifying them as correct if they literally just typed (x - 6)(x - 1) in the answer.

So a more extreme way around it, and probably what I’ll do rather than properly parsing is just check literally for whether the latex is `x^2 - 7x + 6`. But it does mean that I would mark them incorrect for `6 -7x + x^2`.

I guess I was hoping someone had already done the hard job of writing code the parses expanded quadratics and extracted the coefficients.

Here are a couple examples. You can combine either your conic method or using test values to check the student answer against the correct function, combined with a structure check to see that it is in the form of a trinomial. The first two screens are more restrictive, meaning that it must see x^2 + expression + expression to be correct, and the last screen just needs to see expression + expression + expression. The problem with that being that a student could potentially write the answer as like (x-6)(x-1) + 1 + -1 and that would mark correct. The first two screens should work fine, and would only need modification if you needed it to parse ax^2 instead of just x^2. Does this do what you are looking for? Using repeat and allowNegativeTerms patterns, and Conics, to check quadratics • Activity Builder by Desmos

Thanks for this - the first two screens definitely look promising in terms of parsing using patterns - you are way ahead of me in terms of understanding how to use the patterns/parsing functionality!

no problem - let me know if you have any questions about it. This set of videos will help you get started.

Oh i misunderstood.

Here’s something that’ll do what you want as well: Expanding & Simplifying | Components • Activity Builder by Desmos