From 01f9eb5ac15ce200731f3efdb293360abc29e79e Mon Sep 17 00:00:00 2001 From: Stephen Crowley Date: Wed, 13 Nov 2024 19:52:13 -0600 Subject: [PATCH] differentiation the addition operator https://github.com/crowlogic/arb4j/issues/253 --- src/main/java/arb/expressions/nodes/Node.java | 24 +++++++++++-------- .../nodes/binary/AdditionNode.java | 3 +-- todoList.txt | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/arb/expressions/nodes/Node.java b/src/main/java/arb/expressions/nodes/Node.java index eaf96d03e..76da69cfc 100644 --- a/src/main/java/arb/expressions/nodes/Node.java +++ b/src/main/java/arb/expressions/nodes/Node.java @@ -3,7 +3,6 @@ import static arb.expressions.Compiler.checkClassCast; import static arb.expressions.Compiler.invokeSetMethod; import static arb.expressions.Compiler.loadBitsParameterOntoStack; -import static arb.expressions.Compiler.loadThisOntoStack; import java.util.List; import java.util.function.Consumer; @@ -110,9 +109,9 @@ public Class getGeneratedType() * own result, thus saving heap allocations. The method should be overridden by * every subclass of {@link Node} to provide an appropriate response. For * {@link LiteralConstantNode} and {@link VariableNode} nodes, they would always - * return false (since we cannot reuse them). For {@link BinaryOperationNode} nodes - * like {@link AdditionNode}, a method of determining whether they're reusable based - * on the state of their operands would be in order.
+ * return false (since we cannot reuse them). For {@link BinaryOperationNode} + * nodes like {@link AdditionNode}, a method of determining whether they're + * reusable based on the state of their operands would be in order.
*
* * The advantage to not reusing nodes is that it would allow the production of a @@ -142,7 +141,7 @@ public Class getGeneratedType() * or {@link Integer} */ public abstract Class type(); - + /** * TODOL Instantiates the target type instance then calls set on it with the * source instance then sets generatedType to to the requested type so that @@ -167,8 +166,7 @@ public Class generateCastTo(MethodVisitor methodVisitor, Class type) + " and casting to " + type + " is requested"; - assert !generatedType.equals(type) : String.format("tried to cast from and to the same type %s\n", - generatedType); + assert !generatedType.equals(type) : String.format("tried to cast from and to the same type %s\n", generatedType); checkClassCast(methodVisitor, generatedType); expression.allocateIntermediateVariable(methodVisitor, type); Compiler.swap(methodVisitor); @@ -177,9 +175,8 @@ public Class generateCastTo(MethodVisitor methodVisitor, Class type) return generatedType; } - public abstract > - Node - substitute(String variable, Node arg); + public abstract > Node substitute(String variable, + Node arg); public boolean isVariable() { @@ -235,4 +232,11 @@ public boolean isPossiblyNegative() return false; } + public Node add(Node addend) + { + return new AdditionNode<>(expression, + this, + addend); + } + } diff --git a/src/main/java/arb/expressions/nodes/binary/AdditionNode.java b/src/main/java/arb/expressions/nodes/binary/AdditionNode.java index b85d71ce5..8149d838a 100644 --- a/src/main/java/arb/expressions/nodes/binary/AdditionNode.java +++ b/src/main/java/arb/expressions/nodes/binary/AdditionNode.java @@ -51,8 +51,7 @@ public boolean isCommutative() @Override public Node differentiate(VariableNode variable) { - assert false : "TODO: Auto-generated method stub"; - return null; + return left.differentiate(variable).add(right.differentiate(variable)); } @Override diff --git a/todoList.txt b/todoList.txt index 0fed46c69..6ddad830d 100644 --- a/todoList.txt +++ b/todoList.txt @@ -6,4 +6,4 @@ exponentiation: handle negative powers requiring codomain capable of expressing todo: mouse wheel zooms control or whole scene java.lang.NullPointerException: Cannot read field "variables" because the return value of "arb.expressions.viz.Expressor.getCurrentContext()" is null expr compiler: fractional derivatices -java.lang.AssertionError: TODO: Auto-generated method stubat arb4j/arb.expressions.nodes.binary.AdditionNode.differentiate(AdditionNode.java:54)at arb4j/arb.expressions.nodes.DerivativeNode.(DerivativeNode.java:91)at arb4j/arb.expressions.Expression.evaluate(Expression.java:587)at arb4j/arb.expressions.Expression.exponentiate(Expression.java:721)at arb4j/arb.expressions.Expression.exponentiateMultiplyAndDivide(Expression.java:751)at arb4j/arb.expressions.Expression.resolve(Expression.java:1752)at arb4j/arb.expressions.Expression.parseRoot(Expression.java:1585)at arb4j/arb.functions.Function.parse(Function.java:197)at arb4j/arb.expressions.Compiler.compile(Compiler.java:171)at arb4j/arb.expressions.Compiler.express(Compiler.java:254)at arb4j/arb.expressions.Compiler.express(Compiler.java:228)at arb4j/arb.expressions.Compiler.compile(Compiler.java:135)at arb4j/arb.functions.Function.instantiate(Function.java:119)at arb4j/arb.functions.rational.RationalNullaryFunction.express(RationalNullaryFunction.java:29)at arb4j/arb.functions.rational.RationalNullaryFunction.express(RationalNullaryFunction.java:39)at arb4j/arb.expressions.ExpressionTest.testRationalNullaryDerivative(ExpressionTest.java:59)at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)at java.base/java.lang.reflect.Method.invoke(Method.java:580)at junit@4.13.2/junit.framework.TestCase.runTest(TestCase.java:177)at junit@4.13.2/junit.framework.TestCase.runBare(TestCase.java:142)at junit@4.13.2/junit.framework.TestResult$1.protect(TestResult.java:122)at junit@4.13.2/junit.framework.TestResult.runProtected(TestResult.java:142)at junit@4.13.2/junit.framework.TestResult.run(TestResult.java:125)at junit@4.13.2/junit.framework.TestCase.run(TestCase.java:130)at junit@4.13.2/junit.framework.TestSuite.runTest(TestSuite.java:241)at junit@4.13.2/junit.framework.TestSuite.run(TestSuite.java:236)at junit@4.13.2/org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:93)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:530)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:758)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:453)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:211) +java.lang.AssertionError: TODOat arb4j/arb.expressions.nodes.binary.MultiplicationNode.differentiate(MultiplicationNode.java:64)at arb4j/arb.expressions.nodes.binary.AdditionNode.differentiate(AdditionNode.java:54)at arb4j/arb.expressions.nodes.binary.AdditionNode.differentiate(AdditionNode.java:54)at arb4j/arb.expressions.nodes.binary.AdditionNode.differentiate(AdditionNode.java:54)at arb4j/arb.expressions.nodes.DerivativeNode.(DerivativeNode.java:91)at arb4j/arb.expressions.Expression.evaluate(Expression.java:587)at arb4j/arb.expressions.Expression.exponentiate(Expression.java:721)at arb4j/arb.expressions.Expression.exponentiateMultiplyAndDivide(Expression.java:751)at arb4j/arb.expressions.Expression.resolve(Expression.java:1752)at arb4j/arb.expressions.Expression.parseRoot(Expression.java:1585)at arb4j/arb.functions.Function.parse(Function.java:197)at arb4j/arb.expressions.Compiler.compile(Compiler.java:171)at arb4j/arb.expressions.Compiler.express(Compiler.java:254)at arb4j/arb.expressions.Compiler.express(Compiler.java:228)at arb4j/arb.expressions.Compiler.compile(Compiler.java:135)at arb4j/arb.functions.Function.instantiate(Function.java:119)at arb4j/arb.functions.rational.RationalNullaryFunction.express(RationalNullaryFunction.java:29)at arb4j/arb.functions.rational.RationalNullaryFunction.express(RationalNullaryFunction.java:39)at arb4j/arb.expressions.ExpressionTest.testRationalNullaryDerivative(ExpressionTest.java:59)at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)at java.base/java.lang.reflect.Method.invoke(Method.java:580)at junit@4.13.2/junit.framework.TestCase.runTest(TestCase.java:177)at junit@4.13.2/junit.framework.TestCase.runBare(TestCase.java:142)at junit@4.13.2/junit.framework.TestResult$1.protect(TestResult.java:122)at junit@4.13.2/junit.framework.TestResult.runProtected(TestResult.java:142)at junit@4.13.2/junit.framework.TestResult.run(TestResult.java:125)at junit@4.13.2/junit.framework.TestCase.run(TestCase.java:130)at junit@4.13.2/junit.framework.TestSuite.runTest(TestSuite.java:241)at junit@4.13.2/junit.framework.TestSuite.run(TestSuite.java:236)at junit@4.13.2/org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:93)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:530)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:758)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:453)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:211)