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

Update units context to include differentiation #1177

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

Conversation

dpvc
Copy link
Member

@dpvc dpvc commented Jan 11, 2025

The main purpose of this PR is to add the ability to differentiate formulas with units.

I also made a pass over the documentation, and corrected some errors, moved the section on removing units from a context to earlier (right after adding them), and added a section on the new differentiation features.

In the past, the D() method provided for multiple derivatives like D(2) or D(2, "x"), but only allowed one to be specified. I have extended the D() method to allow more than one numeric repeat operation, so $f->D(2, "x", 3, "y") would be $d\over dx^2 dy^3$, and is equivalent to $f->D("x", "x", "y", "y", "y") or $f->D(2, "x")->D(3, "y"). This should not affect existing problems, as it is an extension of the syntax, not a change.

While working on this, I noticed that I had handled abs() for arguments that are numbers with units, but didn't handle the |...| syntax for that. So I've added that here. That also required adding another option to the contextExtensions.pl extension mechanism to override the list classes (|...| are handled using the list delimiters infrastructure).

Finally, I fixed an error with abs() with number-with-units arguments where formula comparisons would fail (the type was not being set properly), simplified the evaluation of trig functions with angle units by removing unneeded tests that are already made in the _check() function, and added a quantity method to the Formula-with-Units class to match the one in the Number-with-Units class.

with units, the MathObjects library needs to know the units of the
variable you are differentiating by. For example, if you have

$s = Compute("(3t^2 - 2x) m");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want $s = Compute("(3t^2 - 2t) m");

->assignUnits(
t => "s",
s => "m"
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, with this example, I think adding ->variables->are(t => Real, s=> Real); will help.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind. I guess the assignUnits will defined the variables.

@pstaabp
Copy link
Member

pstaabp commented Jan 28, 2025

Overall, this looks great. I was testing the higher-order derivatives with the non-realistic function

Context('Units')->withUnitsFor('length','mass')->assignUnits(
        x => 'm',
        y => "ft",
    z => 'kg'
    );

$z = Compute("(3x^2 * y^4) kg");

$dz = $z->D('x',3,'y');

and the result was 6x * 24y kg/(m ft^3) as expected. I was thinking that using ->reduce would change this to 144 x * y kg/(m ft^3), but left it unchanged. Is this because the reduction rules don't automatically apply to these objects? Not a big deal, but just curious.

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