From 6a93d0b8090153bf3f30215cc4000b688426f9a3 Mon Sep 17 00:00:00 2001 From: Stephen Crowley Date: Mon, 2 Dec 2024 11:05:13 -0600 Subject: [PATCH] =?UTF-8?q?public=20void=20testDerivativeOfLogarithm()=20?= =?UTF-8?q?=20=20{=20=20=20=20=20var=20x=20=3D=20RealFunction.parse("?= =?UTF-8?q?=E2=88=82ln(x)/=E2=88=82x");=20=20=20=20=20assertEquals("1/x",?= =?UTF-8?q?=20x.rootNode.toString());=20=20=20}=20https://github.com/crowl?= =?UTF-8?q?ogic/arb4j/issues/253?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/arb/expressions/nodes/unary/FunctionNode.java | 4 ++++ src/test/java/arb/expressions/ExpressionTest.java | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/main/java/arb/expressions/nodes/unary/FunctionNode.java b/src/main/java/arb/expressions/nodes/unary/FunctionNode.java index af6864ecc..d84fc07cb 100644 --- a/src/main/java/arb/expressions/nodes/unary/FunctionNode.java +++ b/src/main/java/arb/expressions/nodes/unary/FunctionNode.java @@ -26,6 +26,7 @@ import arb.expressions.Expression; import arb.expressions.FunctionMapping; import arb.expressions.Parser; +import arb.expressions.nodes.LiteralConstantNode; import arb.expressions.nodes.Node; import arb.expressions.nodes.VariableNode; import arb.functions.Function; @@ -473,6 +474,9 @@ private Node differentiateBuiltinFunction() expression).neg(); // derivative of cos is -sin case "exp": return this; // derivative of exp is exp + case "log": + return new LiteralConstantNode<>(expression, + "1").div(arg); // Add other built-in function derivatives default: throw new UnsupportedOperationException("Derivative not implemented for function: " + functionName); diff --git a/src/test/java/arb/expressions/ExpressionTest.java b/src/test/java/arb/expressions/ExpressionTest.java index eb5ffc6f9..753a72a9e 100644 --- a/src/test/java/arb/expressions/ExpressionTest.java +++ b/src/test/java/arb/expressions/ExpressionTest.java @@ -31,6 +31,13 @@ public class ExpressionTest extends TestCase { + public void testDerivativeOfLogarithm() + { + var x = RealFunction.parse("∂ln(x)/∂x"); + assertEquals("1/x", x.rootNode.toString()); + } + + public void testDerivativeOfExponentialFunction() { var x = RealFunction.parse("∂exp(x)/∂x");