maths / moodle-qtype_stack

Stack question type for Moodle
GNU General Public License v3.0
142 stars 149 forks source link

Support for Rules and Patterns #1209

Closed LukeLongworth closed 2 weeks ago

LukeLongworth commented 5 months ago

The set of functions for rules and patterns looks like it would be useful for manipulation of certain symbolic expressions. Should some or all of these functions be unbanned?

Some use case examples in the documentation are:

My own use case that inspired me to look into this (may or may not be possible, I'm not sure):

sangwinc commented 4 months ago

@LukeLongworth, this looks very reasonable to add. Can you please provide examples of exactly what you do need?

sangwinc commented 4 months ago

@LukeLongworth we may have a snag here. If you try just let(sin(a)^2, 1 - cos(a)^2) in Maxima then the return value is a new kind of operator -> which we've not accommodated. Indeed, using ?print you get

((MTEXT SIMP) ((MEXPT SIMP) ((%SIN SIMP) $A) 2) | --> |
 ((MPLUS SIMP) 1 ((MTIMES SIMP) -1 ((MEXPT SIMP) ((%COS SIMP) $A) 2)))) 

To fully support this feature we need to expand the parser (comments @aharjula?), or we insist on a workaround as shown here https://github.com/maths/moodle-qtype_stack/commit/cfd0887d71f5ca97e6df49275b4792d70dc95f3e#diff-aad04ae351ba675f6ae80a98e32ef7c4b57f41d6e0c614ec9f6d84e3b0d8ae28R2944 which avoids the problem of this return value in the Maxima -> PHP connection.

aharjula commented 4 months ago

Well, wrapping it and piping the return value out to some bit bucket instead of returning it would be the workaround for now. You can naturally add a new operator to the current parser, but:

  1. The current parser is not really configurable, so the operator would also exist for the students, and if it needs to exist for them, then it might affect some parsing error messages.
  2. Is it just a return value in an interesting form, or is it really an operator that can be used? Do we really want to parse things that could never be input?

I would rather start investing in the next-gen parser with its configurable lexers so that one could when needed, use a lexer that accepts that as an operator and otherwise allow simpler ones to be used. This would have less vocabulary and, thus, simpler syntax error messages. Basically, if it currently aims to provide localisable syntax lexers, it can easily also adjust and provide a larger syntax parser for return values from the CAS, or author side code, when need be.

sangwinc commented 4 months ago

I agree @aharjula that adapting the parser is too much work for this case, and probably prone to causing more problems. We'll document the use of let0. There is likely to be little error trapping when we forget as the parser throws an exception which will bring everything to an end...

@LukeLongworth could you create some use-cases and perhaps some docs?

sangwinc commented 2 weeks ago

@LukeLongworth I would really like to get this functionality into the next release (19th Nov). Do you have anything to add, e.g. some compelling use-cases?

LukeLongworth commented 2 weeks ago

@sangwinc I haven't been able to experiment with this, as I usually just use the CAS chat in our local install to play around with Maxima.

The thing that originally drove me to investigate this was symbolic matrix algebra. I wanted Maxima to understand that I is an identity matrix, and O is a zero matrix, with appropriate simplification following.

I can also imagine a use case where more complex simplifications could be managed. I have had many headaches with a particular implicit partial differentiation question we have, where rearranging the original equations before differentiating will produce valid answers (ignoring any domain restrictions that get introduced) that STACK won't recognise. One possible solution would be to ask STACK to, for example, look for any expressions of the form t^2*cos(3*y) and replace them with x*z if one of the given equations was x*z = t^2*cos(3*y).

If this functionality gets into the next version of STACK, I would be happy to play around and write some example use cases. 19th of November might even give us enough time to include it in our December update! It would be a rare treat for us to actually be on the latest version for a while.

sangwinc commented 2 weeks ago

Thanks @LukeLongworth, this is in the dev branch, and scheduled for the next release. Please do make use of this and add examples and "know how" here:
https://github.com/maths/moodle-qtype_stack/blob/dev/doc/en/CAS/Rules.md Your matrix example is in the docs and the test cases...