a more general formatting function for formulas

a more general formatting function for formulas

by Vincent Guirardel -
Number of replies: 3

Hello,

I'm new to this forum, so I hope this is the right place to ask this question (this is a feature request for the formulas question type).

There is the function poly which provides tex format for a polynomial in a single variable.

A more general version with several variables involved would be very convenient (i have in mind linear algebra):

Input: list of coefficients , list of variables (strings) (same size).

Output a nicely formatted linear combination.

Example:

format([3,-2,1,0,7],["x","y","z","t","x^2"]) would yield    3x-2y+z+7x^2

I kind of manage to do this by hand but this is heavy. Does such a function already exist ? If not, that would be great to include it.

BTW, I don't know how to find out which version of Moodle we use at the univesity...


Average of ratings: -
In reply to Vincent Guirardel

Re: a more general formatting function for formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Vincent,

Thank you for a very interesting suggestion. I put a note on the poly() page so as not to forget. I hope that this function can be implemented in the near future.

Although it is not yet officially approved for Moodle 3.7 and 3.8, the Formulas question plugin works correctly with these versions (and earlier versions).

In reply to Dominique Bauer

Re: a more general formatting function for formulas

by Vincent Guirardel -
Thanks Dominique.

Here a proposition of code for the function, together with a collection of tests. I hope it helps.

I am not sure how the function should be called, I used "lincomb" for linear combination.
Compared to the initial suggestion, I added the possibility to insert "&" (or anything) between the terms for alignment within a \begin{matrix}\end{matrix}:
example:
lincomb("&", [1,2,0,3],["x","y","z","t"] )
returns
"x&+2y&&+3t"

I just looked at the code for the function "poly", and I adapted it to this similar context.
I did perform a collection of tests, but not "inside moodle" as I don't have it installed on a machine of mine.

Attached is a file that can be executed in php to performs the tests. It contains the code that would need to be included in the file "variables.php" as comments at the beginning of the file.

In reply to Vincent Guirardel

Re: a more general formatting function for formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Vincent,

Thanks for this code. We will definitely take a look at it.