aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2021-11-10 15:03:24 -0700
committerJohn Denker <jsd@av8n.com>2021-11-10 15:03:24 -0700
commitd8e30f21278e2326b09097f99a8e5423112a8e8e (patch)
treee2f7c94bc6842c21f2184feb76b9ee7d11063230
parentcb3ef4ea0cfffb9236951a24defebb7edd0ab0c9 (diff)
more complete set of math functions
-rw-r--r--parser.php52
1 files changed, 41 insertions, 11 deletions
diff --git a/parser.php b/parser.php
index b5bbaba..fd58f4c 100644
--- a/parser.php
+++ b/parser.php
@@ -1139,6 +1139,18 @@ class qtype_algebra_parser_special extends qtype_algebra_parser_term {
);
}
+/**
+ * implement math functions that aren't php builtins
+ */
+function log2($arg)
+{
+ return log($arg, 2);
+}
+
+function ln($arg)
+{
+ return log($arg);
+}
/**
* Class representing a function in an algebraic expression.
@@ -1158,6 +1170,9 @@ class qtype_algebra_parser_function extends qtype_algebra_parser_term {
* @param $text string matching the function's regular expression
*/
public function __construct($text) {
+ if ($text == 'log') {
+ throw new parser_exception(get_string('deprecatedlog', 'qtype_algebra'));
+ }
if (!function_exists($text) and !array_key_exists($text, self::$fnmap)) {
throw new parser_exception(get_string('undefinedfunction', 'qtype_algebra', $text));
}
@@ -1284,14 +1299,21 @@ class qtype_algebra_parser_function extends qtype_algebra_parser_term {
// Static class properties.
const NARGS = 1;
- public static $fnmap = array ('ln' => 'log',
- 'log' => 'log10'
- );
- public static $texmap = array('asin' => '\\sin^{-1}',
- 'acos' => '\\cos^{-1}',
- 'atan' => '\\tan^{-1}',
- 'sqrt' => '\\sqrt'
+ /* fnmap is not needed, since all needed
+ * functions are implemented without pseudonyms
+ */
+ public static $fnmap = array ();
+ public static $texmap = array('sqrt' => '\\sqrt',
+ 'log2' => '\\log_{2}',
+ 'log10' => '\\log_{10}',
+ 'asin' => '\\sin^{-1}',
+ 'acos' => '\\cos^{-1}',
+ 'atan' => '\\tan^{-1}',
+ 'asinh' => '\\sinh^{-1}',
+ 'acosh' => '\\cosh^{-1}',
+ 'atanh' => '\\tanh^{-1}',
);
+
// List of functions requiring special brackets.
public static $bracketmap = array ('sqrt'
);
@@ -1404,18 +1426,26 @@ class qtype_algebra_parser {
'e'
);
- // Functions which the parser will understand. These should all be standard PHP math functions.
+ // Functions which the parser will understand.
+ // You have to implement any that are not standard PHP math functions.
public static $functions = array ('sqrt',
+ 'exp',
+ 'log2',
'ln',
'log',
- 'cosh',
+ 'log10',
'sinh',
+ 'cosh',
+ 'tanh',
'sin',
'cos',
'tan',
'asin',
'acos',
- 'atan'
+ 'atan',
+ 'asinh',
+ 'acosh',
+ 'atanh',
);
// Array to define the priority of the different operations. The parser implements the standard BODMAS priority:
@@ -1461,7 +1491,7 @@ class qtype_algebra_parser {
}
/**
- * Parses a given string containing an algebric epxression and returns the corresponding parse tree.
+ * Parses a given string containing an algebric expression and returns the corresponding parse tree.
*
* This method loops over the string using the regular expressions in the token map to break down the
* string into tokens. These tokens are arranged into a structured stack, taking account of the