Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a parserLogb.pl macro for logarithms with base b. #1147

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

drgrice1
Copy link
Member

@drgrice1 drgrice1 commented Nov 24, 2024

This is similar to the parserRoot.pl macro. To enable the usage of the logb function in the current context call Parser::Logb->Enable.

Then you can call $n = logb(3, 5) to get the value of the logarithm with base 3 evaluated at 5, or $ans = Compute("logb(3, 5)") to use as an answer.

You can also call Parser::Logb->EnableComplex to allow negative bases and evaluation at negative numbers. Note that attempting to use a negative base or evaluate at a negative number would otherwise produce an error message.

An answer in this context will automatically have the MathQuill option logsChangeBase => 0 set. To facilitate this without the need to have numerous special cases for contexts in the ENDDOCUMENT method of PG.pl, there is a new mathQuillOpts context flag. The default value for this flag is a reference to a hash with no keys. Any context can set keys in this hash, and those will be tranferred to the MathQuill options for any answer rule in the context. This is now used by the the parserRoot.pl macro to set rootsAreExponents => 0, and by the new parserLogb.pl macro to set logsChangeBase => 0.

To facilitate students entering a logarithm with a base, if the logsChangeBase option is 0 (or not set), then a subscript button is added to the MathQuill toolbar.

Note that MathQuill is already set up to output the text logb(b, x) if log_b(x) is entered.

@drgrice1 drgrice1 force-pushed the log-base-b-function branch 2 times, most recently from af19abe to cc0123a Compare November 29, 2024 13:17
@drgrice1 drgrice1 force-pushed the log-base-b-function branch from cc0123a to bf63330 Compare December 3, 2024 21:22
@drgrice1 drgrice1 force-pushed the log-base-b-function branch from bf63330 to 9d0ec6e Compare December 11, 2024 02:00
@Alex-Jordan
Copy link
Contributor

I find that I have to set

Context()->flags->set(mathQuillOpts => {});

before I run Parser::Logb->Enable;, or else I get an error like:

Can't use an undefined value as a HASH reference at line 92 of [PG]/macros/parsers/parserLogb.pl
   from within Parser::Logb::Enable called at line 6 of undefined

Here is the file I am testing with:

DOCUMENT();

loadMacros(qw(PGstandard.pl PGML.pl parserLogb.pl));

#Context()->flags->set(mathQuillOpts => {});
Parser::Logb->Enable;
$n = logb(3, 5);

BEGIN_PGML
Enter a value for [`[$n]`].

[_]{$n}{5}
END_PGML

ENDDOCUMENT();

@Alex-Jordan
Copy link
Contributor

Something came up today where a person used lg(), meaning the logarithm base 2. So it comes to mind when looking atht his. We have log(), log10(), and ln(). Is it appropriate to add lg()? And if so, is it appropriate to include that as part of this macro?

@drgrice1 drgrice1 force-pushed the log-base-b-function branch from 9d0ec6e to b604fd6 Compare December 11, 2024 13:19
@drgrice1
Copy link
Member Author

I tested with your sample code, and I do not see any errors or warnings about undefined HASH references. The mathQuillOpts context flag should always be defined to be a reference to an empty hash. It is set in lib/Parser/Context/Default.pm.

I am not sure that I agree to adding lg for a logarithm with base 2. That notation seems to be primarily a computer science notation, and it seems to be rather heavily disputed as to whether it is the logarithm with base 2 or base 10 (see for instance https://en.wikipedia.org/wiki/Binary_logarithm#Notation). This macro is designed to handle logarithms with explicit bases given. Special notations like log, ln, log10 and something like lg are not the intent for this macro.

@drgrice1
Copy link
Member Author

Ahh, you may need to restart the webwork2 app to get the changes in this pull request to take effect (if you are not using morbo). The change in Default.pm occurs when the safe compartment is compiled, and that happens when the webwork2 app is first started. That may be why you got the error about the undefined value used as a HASH reference.

@Alex-Jordan
Copy link
Contributor

Yeah, oops. I had not restarted webwork2. This looks good and works in all the ways.

@drgrice1 drgrice1 force-pushed the log-base-b-function branch from b604fd6 to b08d196 Compare December 17, 2024 12:48
This is similar to the `parserRoot.pl` macro.  To enable the usage of
the `logb` function in the current context call `Parser::Logb->Enable`.

You can call `$n = logb(3, 5)` to get the value of the logarithm with
base 3 evaluated at 5, or `$ans = Compute("logb(3, 5)")` to  use as an
answer.

You can also call `Parser::Logb->EnableComplex` to allow negative bases
and evaluation at negative numbers. Note that attempting to use a
negative base or evaluate at a negative number would otherwise produce
an error message.

An answer in this context will automatically have the MathQuill option
`logsChangeBase => 0` set.  To facilitate this without the need to have
numerous special cases for contexts in the `ENDDOCUMENT` method of
`PG.pl`, there is a new `mathQuillOpts` context flag.  The default value
for this flag is a reference to a hash with no keys.  Any context can
set keys in this hash, and those will be tranferred to the MathQuill
options for any answer rule in the context.  This is now used by the
the `parserRoot.pl` macro to set `rootsAreExponents => 0`, and by the
new `parserLogb.pl` macro to set `logsChangeBase => 0`.

To facilitate students entering a logarithm with a base, if the
`logsChangeBase` option is 0 (or not set), then a subscript button is
added to the MathQuill toolbar.
@drgrice1 drgrice1 force-pushed the log-base-b-function branch from b08d196 to 241bcb1 Compare December 18, 2024 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants