Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlogic committed Jan 28, 2024
1 parent 744e49c commit fec6f60
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 32 deletions.
18 changes: 16 additions & 2 deletions src/main/java/arb/Integer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,24 @@
public class Integer implements
AutoCloseable,
Comparable<Integer>,
Ring<Integer>
Ring<Integer>,
HasName
{

public Real factorial( int bits, Real result)
public String name;

public String getName()
{
return name;
}

public Integer setName(String name)
{
this.name = name;
return this;
}

public Real factorial(int bits, Real result)
{
arblib.arb_fac_ui(result, getUnsignedValue(), bits);
return result;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/arb/expressions/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map.Entry;
import java.util.stream.Stream;

import arb.HasName;
import arb.Real;
import arb.expressions.nodes.Node;
import arb.expressions.nodes.Variable;
Expand Down Expand Up @@ -69,7 +70,7 @@ public Context(FunctionMappings funcs)
this.functions = funcs;
}

public Context(Real... vars)
public Context(HasName... vars)
{
this(new Variables(vars));
}
Expand Down
49 changes: 40 additions & 9 deletions src/main/java/arb/expressions/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
public class Expression<D, R, F extends Function<D, R>> implements
Typesettable
{
private static final char[] SUBSCRIPT_CHARACTERS = new char[]
{ '₀', '₁', '₂', '₃', '₄', '₅', '₆', '₇', '₈', '₉', 'ₐ', 'ₑ', 'ₒ', 'ₓ', 'ₔ', 'ₕ', 'ₖ', 'ₗ', 'ₘ', 'ₙ', 'ₚ', 'ₛ',
'ₜ' };

private static final String IS_INITIALIZED = "isInitialized";

private static final String nameOfInitializerFunction = "initialize";
Expand Down Expand Up @@ -701,7 +705,7 @@ public Node<D, R, F> evaluate(int depth) throws ExpressionCompilerException
}
else if (Parser.isNumeric(character))
{
node = parseNumber(depth, startPos);
node = evaluateNumber(depth, startPos);
assert node != null : "parseNumber returned null";
}
else if (Parser.isLatinOrGreek(character, false))
Expand Down Expand Up @@ -775,14 +779,42 @@ public Node<D, R, F> exponentiate(int depth) throws ExpressionCompilerException
return exponentiate(depth, evaluate(depth));
}

public Reference parseName(int depth, int startPos)
public Reference evaluateName(int depth, int startPos)
{
while (isLatinOrGreek(character, true))
{
nextCharacter();
}
String identifier = expression.substring(startPos, position).trim();
String index = null;
String index = evaluateConditionalSquareBracketedIndex(depth);
if (index == null)
{
index = evaluateConditionalSubscriptedIndex(depth);
}
return new Reference(identifier,
index);
}

private String evaluateConditionalSubscriptedIndex(int depth)
{
StringBuilder index = new StringBuilder();

while (nextCharacterIsSubscript(depth))
{
index.append(previousCharacter);
}

return index.length() > 0 ? index.toString() : null;
}

private boolean nextCharacterIsSubscript(int depth)
{
return nextCharacterIs(depth, SUBSCRIPT_CHARACTERS);
}

private String evaluateConditionalSquareBracketedIndex(int depth)
{
String index = null;
if (nextCharacterIs(depth, '['))
{
int indexPosition = position;
Expand All @@ -793,12 +825,10 @@ public Reference parseName(int depth, int startPos)
}
index = expression.substring(indexPosition, position - 1);
}

return new Reference(identifier,
index);
return index;
}

public Node<D, R, F> parseNumber(int depth, int startPos)
public Node<D, R, F> evaluateNumber(int depth, int startPos)
{
while (isNumeric(character))
{
Expand Down Expand Up @@ -946,14 +976,15 @@ public String reserveIntermediateVariable(MethodVisitor methodVisitor, int depth
public Node<D, R, F> resolveFunctionInvocationOrVariableReference(int depth,
int startPos) throws ExpressionCompilerException
{
Reference reference = parseName(depth, startPos);
Reference reference = evaluateName(depth, startPos);
boolean isFunction = nextCharacterIs(depth, '(');

if (isFunction)
{
if ("when".equals(reference.name))
{
return new When<>(this, depth);
return new When<>(this,
depth);
}
else
{
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/arb/expressions/nodes/binary/Addition.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

/**
* arb4j is made available under the terms of the Business Source License™ v1.1
* ©2023 which can be found in the root directory of this project in a file
* ©2024 which can be found in the root directory of this project in a file
* named License.pdf, License.txt, or License.tm which are the pdf, text, and
* TeXmacs format of the same document respectively.
* TeXmacs formatted of the same document respectively.
*/
public class Addition<D, R, F extends Function<D,R>> extends
BinaryOperation<D, R, F>
public class Addition<D, R, F extends Function<D, R>> extends
BinaryOperation<D, R, F>
{

@Override
public String typeset()
{
return format("%s + %s", left.typeset(), right.typeset());
}

public Addition(Expression<D, R, F> expression, Node<D, R, F> left, Node<D, R, F> right, int depth)
{
super(expression,
Expand All @@ -31,7 +31,4 @@ public Addition(Expression<D, R, F> expression, Node<D, R, F> left, Node<D, R, F
depth);
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public BinaryOperation(Expression<D, R, F> expression,
else
{
throw new ExpressionCompilerException("Unhandled fill-in of left-hand-side of binary "
+ "operation when the right hand side is of type " + right.type() + ", this is where -x is translated to 0-x. that is what is meant by fill-in");
+ "operation when the right hand side is of type " + right.type()
+ ", this is where -x is translated to 0-x. that is what is meant by fill-in");
}
}
assert left != null && right != null : "one or more of the operands to this were missing: " + this
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/arb/expressions/nodes/binary/Division.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* named License.pdf, License.txt, or License.tm which are the pdf, text, and
* TeXmacs format of the same document respectively.
*/
public class Division<D, R, F extends Function<D,R>> extends
BinaryOperation<D, R, F>
public class Division<D, R, F extends Function<D, R>> extends
BinaryOperation<D, R, F>
{
public Division(Expression<D, R, F> expression, Node<D, R, F> left, Node<D, R, F> right, int depth)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* TeXmacs format of the same document respectively.
*/
public class Exponentiation<D, R, F extends Function<D, R>> extends
BinaryOperation<D, R, F>
BinaryOperation<D, R, F>
{

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* TeXmacs format of the same document respectively.
*/
public class Multiplication<D, R, F extends Function<D, R>> extends
BinaryOperation<D, R, F>
BinaryOperation<D, R, F>
{
public Multiplication(Expression<D, R, F> expression, Node<D, R, F> left, Node<D, R, F> right, int depth)
{
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/arb/expressions/nodes/binary/Subtraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
* named License.pdf, License.txt, or License.tm which are the pdf, text, and
* TeXmacs format of the same document respectively.
*/
public class Subtraction<D, R, F extends Function<D,R>> extends
BinaryOperation<D, R, F>
public class Subtraction<D, R, F extends Function<D, R>> extends
BinaryOperation<D, R, F>
{

@Override
public String typeset()
{
return format("%s - %s", left.typeset(), right.typeset());
}

public Subtraction(Expression<D, R, F> expression, Node<D, R, F> left, Node<D, R, F> right, int depth)
{
super(expression,
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/arb/expressions/nodes/nary/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public Product(Expression<D, R, F> expression, int depth)
{
super(expression,
depth);
assert false : "TODO: construct Product whose syntax is like this ∏αₖ₍ₙ₎{k=1…p} where αₖ₍ₙ₎=α[k].risingFactorial(n) in this example";
Node<D, R, F> node = expression.evaluate(depth + 1);

assert false : "TODO: construct Product... node=" + node;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public class HypergeometricFunctionSequence implements
Function<Integer, RealPolynomial>
{

@Override
public void close()
{
p.close();
q.close();
α.close();
β.close();
}

public static void main(String... args)
{
try ( var F = new HypergeometricFunctionSequence(3,
Expand All @@ -36,13 +45,18 @@ public static void main(String... args)

public final Real α, β;

public final Integer p, q;

public final Function<Integer, RealPolynomial> F;

public static final String Fdef = "n➔when(n=0,1,else,x*F(n-1)*(∏α₍ₖ₎{k=1…p}/∏β₍ₖ₎{k=1…q}))";
public static final String Fdef = "n➔when(n=0,1,else,x*F(n-1)*(∏αₖ₍ₙ₋₁₎{k=1…p}/∏βₖ₍ₙ₋₁₎{k=1…q}))";

@SuppressWarnings("resource")
public HypergeometricFunctionSequence(int p, int q)
{
context = new Context(α = Real.newVector(p).setName("α"),
context = new Context(this.p = new Integer(p).setName("p"),
this.q = new Integer(q).setName("q"),
α = Real.newVector(p).setName("α"),
β = Real.newVector(q).setName("β"));
F = Function.express(Integer.class, RealPolynomial.class, "F", Fdef, context);
}
Expand Down

0 comments on commit fec6f60

Please sign in to comment.