Skip to content

Commit

Permalink
differentiation the addition operator
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlogic committed Nov 14, 2024
1 parent 3ee3bc3 commit 01f9eb5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
24 changes: 14 additions & 10 deletions src/main/java/arb/expressions/nodes/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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. <br>
* 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. <br>
* <br>
*
* The advantage to not reusing nodes is that it would allow the production of a
Expand Down Expand Up @@ -142,7 +141,7 @@ public Class<?> getGeneratedType()
* or {@link Integer}
*/
public abstract <C> Class<? extends C> 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
Expand All @@ -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);
Expand All @@ -177,9 +175,8 @@ public Class<?> generateCastTo(MethodVisitor methodVisitor, Class<?> type)
return generatedType;
}

public abstract <E, S, G extends Function<? extends E, ? extends S>>
Node<D, R, F>
substitute(String variable, Node<E, S, G> arg);
public abstract <E, S, G extends Function<? extends E, ? extends S>> Node<D, R, F> substitute(String variable,
Node<E, S, G> arg);

public boolean isVariable()
{
Expand Down Expand Up @@ -235,4 +232,11 @@ public boolean isPossiblyNegative()
return false;
}

public Node<D, R, F> add(Node<D, R, F> addend)
{
return new AdditionNode<>(expression,
this,
addend);
}

}
3 changes: 1 addition & 2 deletions src/main/java/arb/expressions/nodes/binary/AdditionNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public boolean isCommutative()
@Override
public Node<D, R, F> differentiate(VariableNode<D, R, F> variable)
{
assert false : "TODO: Auto-generated method stub";
return null;
return left.differentiate(variable).add(right.differentiate(variable));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion todoList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.<init>(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 [email protected]/junit.framework.TestCase.runTest(TestCase.java:177)at [email protected]/junit.framework.TestCase.runBare(TestCase.java:142)at [email protected]/junit.framework.TestResult$1.protect(TestResult.java:122)at [email protected]/junit.framework.TestResult.runProtected(TestResult.java:142)at [email protected]/junit.framework.TestResult.run(TestResult.java:125)at [email protected]/junit.framework.TestCase.run(TestCase.java:130)at [email protected]/junit.framework.TestSuite.runTest(TestSuite.java:241)at [email protected]/junit.framework.TestSuite.run(TestSuite.java:236)at [email protected]/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.<init>(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 [email protected]/junit.framework.TestCase.runTest(TestCase.java:177)at [email protected]/junit.framework.TestCase.runBare(TestCase.java:142)at [email protected]/junit.framework.TestResult$1.protect(TestResult.java:122)at [email protected]/junit.framework.TestResult.runProtected(TestResult.java:142)at [email protected]/junit.framework.TestResult.run(TestResult.java:125)at [email protected]/junit.framework.TestCase.run(TestCase.java:130)at [email protected]/junit.framework.TestSuite.runTest(TestSuite.java:241)at [email protected]/junit.framework.TestSuite.run(TestSuite.java:236)at [email protected]/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)

0 comments on commit 01f9eb5

Please sign in to comment.