Skip to content

Commit

Permalink
https://github.com/crowlogic/arb4j/issues/253
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlogic committed Dec 5, 2024
1 parent e1c2b9c commit a7dc232
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 105 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ try (Real x = new Real("25", 128)) {

#### Expression Compiler
- The [arb.expressions](https://github.com/crowlogic/arb4j/tree/master/src/main/java/arb/expressions) package in arb4j includes tools for compiling mathematical [expressions](https://github.com/crowlogic/arb4j/blob/master/src/main/java/arb/expressions/Expression.java) directly into Java bytecode, saving milleniums of development time, reducing the need to laborously and tediously write new code for each different formula to be evaluated whilst also ensuring efficiency and correctness; it would be challenging to write code manually that would significantly outperform the generated code
##### ExpressionAnalyzer
The [ExpressionAnalyzer](https://github.com/crowlogic/arb4j/tree/master/src/main/java/arb/viz/ExpressionAnalyzer.java) provides a tree-list view that shows the abstract-syntex-tree that constitutes
##### Expressor
The [Expressor](https://github.com/crowlogic/arb4j/tree/master/src/main/java/arb/expressions/viz/Expressor.java) program provides a tree-list view that shows the abstract-syntex-tree that constitutes
a given expression and the intermediate values that combine to produce a given result.

![Screenshot from 2024-08-25 21-42-44](https://github.com/user-attachments/assets/cd1d71de-bcef-4be6-b25a-3c41293de158)
Expand Down
16 changes: 16 additions & 0 deletions expressions/P.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--- !!arb.expressions.SerializedExpression
coDomain: arb.RationalFunction
context:
input:
- arb.Integer
- 2
β:
- arb.Real
- '-0.5'
α:
- arb.Real
- '-0.5'
domain: arb.Integer
expression: n➔when(n=0,1,n=1,(C(1)*x-β+α)/2.0,else,(A(n)*P(n-1)-B(n)*P(n-2))/E(n))
function: arb.functions.rational.RationalFunctionSequence
...
36 changes: 26 additions & 10 deletions src/main/java/arb/expressions/nodes/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,12 @@ public Node<D, R, F> mul(Node<D, R, F> multiplicand)

public Node<D, R, F> pow(int i)
{
return pow(LiteralConstantNode.of(expression, i));
return pow(nodeOf(i));
}

public Node<D, R, F> nodeOf(int i)
{
return LiteralConstantNode.of(expression, i);
}

public Node<D, R, F> neg()
Expand All @@ -280,12 +285,12 @@ public Node<D, R, F> neg()

public Node<D, R, F> sub(int i)
{
return sub(LiteralConstantNode.of(expression, i));
return sub(nodeOf(i));
}

public Node<D, R, F> div(int i)
{
return div(LiteralConstantNode.of(expression, i));
return div(nodeOf(i));
}

public Node<D, R, F> pow(String exponent)
Expand All @@ -296,23 +301,34 @@ public Node<D, R, F> pow(String exponent)

public Node<D, R, F> cos()
{
return new FunctionNode<>("cos",
return apply("cos");
}

public Node<D, R, F> apply(String functionName)
{
return new FunctionNode<>(functionName,
this,
expression);
}

public Node<D, R, F> sin()
{
return new FunctionNode<>("sin",
this,
expression);
return apply("sin");
}

public Node<D, R, F> sqrt()
{
return new FunctionNode<>("sqrt",
this,
expression);
return apply("sqrt");
}

public Node<D, R, F> tan()
{
return apply("tan");
}

public Node<D, R, F> tanh()
{
return apply("tanh");
}

}
6 changes: 4 additions & 2 deletions src/main/java/arb/expressions/nodes/unary/FunctionNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ private Node<D, R, F> differentiateBuiltinFunction()
switch (functionName)
{
case "arcsin":
var one = expression.newLiteralConstant(1);
var one = nodeOf(1);
return one.div(one.sub(arg.pow(2)).sqrt());
case "sin":
return arg.cos();
Expand All @@ -474,7 +474,9 @@ private Node<D, R, F> differentiateBuiltinFunction()
case "exp":
return this;
case "log":
return expression.newLiteralConstant(1).div(arg);
return nodeOf(1).div(arg);
case "tanh":
return nodeOf(1).sub(arg.tanh().pow(2));
default:
throw new UnsupportedOperationException("Derivative not implemented for function: " + functionName);
}
Expand Down
Loading

0 comments on commit a7dc232

Please sign in to comment.