crowlogic / arb4j

arb4j is a Java API for the arbitrary precision ball arithmetic library found at http://arblib.org
Other
1 stars 0 forks source link

expr compiler: complete Vector node implementation #377

Closed crowlogic closed 4 months ago

crowlogic commented 5 months ago

its a Node which is generated when "[blah,f(x),3,...]" is encountered makes code so that when its evaluated it results in a (vector-instance) of the Real type.. that means it has Real.dim>1 where its ith element is accessed via realElement.get(index) .. in other words, Real can represent both a scalar and a vector

package arb.expressions.nodes;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.objectweb.asm.MethodVisitor;

import arb.documentation.BusinessSourceLicenseVersionOnePointOne;
import arb.documentation.TheArb4jLibrary;
import arb.expressions.ArrayConstructionExample;
import arb.expressions.Expression;
import arb.functions.Function;

/**
 *
 * @see BusinessSourceLicenseVersionOnePointOne © terms of the
 *      {@link TheArb4jLibrary}
 */
public class Vector<D, R, F extends Function<D, R>> extends
                   Node<D, R, F>
{

  @Override
  public String toString()
  {
    return elements.stream()
                   .map(element -> element.typeset() + " of type " + element.type())
                   .collect(Collectors.joining(",", "[", "]"));
  }

  private ArrayList<Node<D, R, F>> elements;

  public Vector(Expression<D, R, F> expression)
  {
    super(expression);
    elements = new ArrayList<>();
    do
    {
      var element = expression.resolve();
      elements.add(element);
    }
    while (expression.nextCharacterIs(','));
    expression.require(']');

  }

  @Override
  public Node<D, R, F> integral(String variable)
  {
    assert false : "TODO: Auto-generated method stub";
    return null;
  }

  @Override
  public List<Node<?, ?, ?>> getBranches()
  {
    assert false : "TODO: Auto-generated method stub";
    return null;
  }

  @Override
  public boolean isLeaf()
  {
    assert false : "TODO: Auto-generated method stub";
    return false;
  }

  /**
   * See {@link ArrayConstructionExample} for how to do this
   * 
   */
  @Override
  public MethodVisitor generate(MethodVisitor mv, Class<?> resultType)
  {
    elements.stream().forEach(element ->
    {
      System.err.println("Vector.generating " + element);
      element.generate(mv, resultType);

    });
    assert false : "TODO: arrange the elemenets so that when new Real(Real... elements) is called it works as expected.. write a unit test for this feature and then decompile the test to see the bytecodes";
    return null;
  }

  @Override
  public boolean isReusable()
  {
    assert false : "TODO: Auto-generated method stub";
    return false;
  }

  @Override
  public MethodVisitor prepareStackForReuse(MethodVisitor mv)
  {
    assert false : "TODO: Auto-generated method stub";
    return null;
  }

  @Override
  public String typeset()
  {
    assert false : "TODO: Auto-generated method stub";
    return null;
  }

  @Override
  public <C> Class<? extends C> type()
  {
    assert false : "TODO: Auto-generated method stub";
    return null;
  }

  @Override
  public boolean hasSingleLeaf()
  {
    assert false : "TODO: Auto-generated method stub";
    return false;
  }

}
crowlogic commented 5 months ago
java.lang.AssertionError: TODO: cast element at index 3 expressed by class arb.expressions.nodes.LiteralConstant which generated a result of type class arb.Integer and not the requested result type class arb.Real

    at arb4j/arb.expressions.nodes.Vector.lambda$1(Vector.java:91)
    at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1708)
    at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762)
    at arb4j/arb.expressions.nodes.Vector.generate(Vector.java:81)
    at arb4j/arb.expressions.nodes.unary.HypergeometricFunction.generate(HypergeometricFunction.java:31)
    at arb4j/arb.expressions.Expression.generateEvaluationMethod(Expression.java:1013)
    at arb4j/arb.expressions.Expression.compile(Expression.java:512)
    at arb4j/arb.expressions.Compiler.express(Compiler.java:172)
    at arb4j/arb.expressions.Compiler.express(Compiler.java:188)
    at arb4j/arb.expressions.Compiler.express(Compiler.java:123)
    at arb4j/arb.expressions.Expression.compile(Expression.java:180)
    at arb4j/arb.expressions.Expression.instantiate(Expression.java:218)
    at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:656)
    at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:650)
    at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:629)
    at arb4j/arb.expressions.ExpressionTest.testHypergeometricFuntion(ExpressionTest.java:24)
    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:529)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:757)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
crowlogic commented 5 months ago

Yes, if your RealPolynomial class already supports addition and subtraction, and you have an expression compiler capable of handling transformations, then you're in a good position to apply the substitution rule directly to transform your polynomial according to the specified transformation (1/2 - x/2).

Given the transformation (1/2 - x/2) and assuming the expression compiler can convert expressions into functions from RealPolynomial to RealPolynomial, you can proceed as follows:

  1. Compile the Transformation: Use the expression compiler to create a Function<RealPolynomial, RealPolynomial> that embodies the transformation (f(x) = 1/2 - x/2). This function, when applied to a RealPolynomial, will return a new RealPolynomial that represents the result of applying (f) to each term.

  2. Evaluate the Original Polynomial: Calculate the original polynomial as usual, obtaining a RealPolynomial that represents the hypergeometric function or any other function's series expansion.

  3. Apply the Transformation: Use the compiled transformation function on the calculated polynomial. This effectively applies (f(x) = 1/2 - x/2) to the polynomial, transforming it according to the substitution rule.

This approach abstracts away the manual steps of applying the transformation by leveraging the expression compiler's ability to parse and apply mathematical expressions directly to polynomials. The key here is the correct implementation of the transformation function, ensuring it properly applies polynomial composition as per the substitution rule.

For example, assuming you have the capability to compile expressions into functions:

// Hypothetical code showing the concept
Function<RealPolynomial, RealPolynomial> transformation = 
    RealPolynomialFunctionCompiler.compile("1/2 - x/2");

RealPolynomial originalPolynomial = calculateHypergeometricOrOther(); // However you obtain this
RealPolynomial transformedPolynomial = transformation.apply(originalPolynomial);

This process should give you the transformed polynomial as if you manually substituted (1/2 - x/2) into each term of the original polynomial. Ensure that your expression compiler and the Function<RealPolynomial, RealPolynomial> interface are correctly implemented to handle these operations on polynomials.

crowlogic commented 4 months ago
java.lang.VerifyError: Operand stack underflow
Exception Details:
  Location:
    sub808.evaluate(Ljava/lang/Object;IILjava/lang/Object;)Ljava/lang/Object; @69: areturn
  Reason:
    Attempt to pop empty stack.
  Current Frame:
    bci: @69
    flags: { }
    locals: { 'sub808', 'java/lang/Object', integer, integer, 'java/lang/Object' }
    stack: { }
  Bytecode:
    0000000: 1003 bd00 0b59 1000 2ab4 000f c000 112a
    0000010: b400 155f b600 1953 5910 012a b400 1cc0
    0000020: 0011 2ab4 001f 5fb6 0019 5359 1002 2ab4
    0000030: 000f c000 112a b400 225f b600 1953 bb00
    0000040: 0b5f b700 26b0                         

    at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3549)
    at java.base/java.lang.Class.getConstructor0(Class.java:3754)
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2930)
    at arb4j/arb.expressions.Expression.getInstance(Expression.java:1224)
    at arb4j/arb.expressions.Expression.instantiate(Expression.java:1306)
    at arb4j/arb.expressions.Expression.instantiate(Expression.java:229)
    at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:659)
    at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:653)
    at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:632)
    at arb4j/arb.expressions.ExpressionTest.testVector(ExpressionTest.java:27)
    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:529)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:757)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
crowlogic commented 4 months ago
java.lang.VerifyError: Operand stack underflow
Exception Details:
  Location:
    sub808.evaluate(Ljava/lang/Object;IILjava/lang/Object;)Ljava/lang/Object; @69: areturn
  Reason:
    Attempt to pop empty stack.
  Current Frame:
    bci: @69
    flags: { }
    locals: { 'sub808', 'java/lang/Object', integer, integer, 'java/lang/Object' }
    stack: { }
  Bytecode:
    0000000: 1003 bd00 0b59 1000 2ab4 000f c000 112a
    0000010: b400 155f b600 1953 5910 012a b400 1cc0
    0000020: 0011 2ab4 001f 5fb6 0019 5359 1002 2ab4
    0000030: 000f c000 112a b400 225f b600 1953 bb00
    0000040: 0b5f b700 26b0                         

    at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3549)
    at java.base/java.lang.Class.getConstructor0(Class.java:3754)
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2930)
    at arb4j/arb.expressions.Expression.getInstance(Expression.java:1224)
    at arb4j/arb.expressions.Expression.instantiate(Expression.java:1306)
    at arb4j/arb.expressions.Expression.instantiate(Expression.java:229)
    at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:659)
    at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:653)
    at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:632)
    at arb4j/arb.expressions.ExpressionTest.testVector(ExpressionTest.java:27)
    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 org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:757)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)