Skip to content

parse derivatives instead of just relying on tree #887

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

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

Conversation

drdrew42
Copy link
Member

@drdrew42 drdrew42 commented Jul 26, 2023

Taking derivatives using the D method ends up creating formulas that refuse to reduce (e.g. constant products appearing in the derivative of a polynomial).

This PR modifies the behavior of the D method -- preserving the existing tree structure of the derivative, but now processing the result of that process (respecting the existing context flags for free).

@@ -39,7 +39,11 @@ sub Parser::D {
return (0 * $self)->reduce('0*x' => 1) unless defined $self->{variables}{$x};
$f = $f->D($x);
}
return $self->new($f);
my $deriv = $self->new($f);
$deriv->{string} = $deriv->{tree}->string;
Copy link
Member

Choose a reason for hiding this comment

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

This line means that any constants are clipped to 6 significant digits (the default number format), so the result may have different from the original.

You may also want to add a new reduction rule that controls doing this doing this, so it can be turned off, if desired.

@Alex-Jordan
Copy link
Contributor

I think we can close this. The specific instance cited (e.g. constant products appearing in the derivative of a polynomial) has already been addressed by #1025. Is there an example of something different that this would still help with?

In case this should be kept, replying to @dpvc's comment: could the string method at the new line 43 take an argument that briefly changes the number format to use more precision? Say 17 significant digits?

@dpvc
Copy link
Member

dpvc commented May 21, 2025

The string methods already take optional parameters, so you can't use that approach to adjust the precision. But there is a context flag for the number format, and an expression can override that using its format property, so you could use

	my $deriv = $self->new($f);
	$deriv->{format} = '%.17g';
	$deriv->{string} = $deriv->{tree}->string;
	delete $deriv->{format};

in order to get the higher precision. I doubt it is necessary, but you could save any old format value and replace it, if there is one.

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.

3 participants