From: dpvc v. a. <we...@ma...> - 2005-08-13 01:39:02
|
Log Message: ----------- Added differentiation of (single-variable) functions created with parserFunction. Modified Files: -------------- pg/macros: parserFunction.pl Revision Data ------------- Index: parserFunction.pl =================================================================== RCS file: /webwork/cvs/system/pg/macros/parserFunction.pl,v retrieving revision 1.1 retrieving revision 1.2 diff -Lmacros/parserFunction.pl -Lmacros/parserFunction.pl -u -r1.1 -r1.2 --- macros/parserFunction.pl +++ macros/parserFunction.pl @@ -78,7 +78,7 @@ @_, class => 'parserFunction', argCount => scalar(@argNames), argNames => [@argNames], argTypes => [@argTypes], function => $formula->perlFunction(undef,[@argNames]), - type => $formula->typeRef, + formula => $formula, type => $formula->typeRef, } ); main::PG_restricted_eval("sub main::$name {Parser::Function->call('$name',\@_)}"); @@ -120,6 +120,20 @@ } # +# Compute the derivative of (single-variable) functions +# using the chain rule. +# +sub D { + my $self = shift; my $def = $self->{def}; + $self->Error("Can't differentiate function '%s'",$self->{name}) + unless $def->{argCount} == 1; + my $x = $def->{argNames}[0]; + my $Df = $def->{formula}->D($x); + my $g = $self->{params}[0]; + return (($Df->substitute($x=>$g))*($g->D(@_)))->{tree}->reduce; +} + +# # Get the name for a number # sub NameForNumber { |