Closed crowlogic closed 4 months ago
Designing a syntax for expressing mathematical operations in programming languages that do not support the native mathematical notation can be challenging. The goal is to create a syntax that is both readable and expressive within the constraints of the language. In your case, you're aiming to represent an indefinite or definite integral in Java, a language that does not naturally support the vertical positioning of expressions, such as superscripts and subscripts, commonly used in mathematical notation.
Given Java's limitations in representing such expressions directly, your approach involves designing a string-based representation that can be parsed by your compiler or interpreter to perform the desired mathematical operations. The examples you've provided aim to represent the integral of a constant function over an interval, which is a straightforward mathematical concept but requires careful consideration for syntactical clarity in implementation.
Between the two examples you've provided:
var integral = Function.expres("∫1dx∈(0..∞)");
var integral = Function.expres("∫1d{x={0..∞}");
The first option is closer to conventional mathematical notation and might be more intuitive for someone familiar with integrals. However, it uses the symbol ∈
(element of), which could be misleading since ∈
traditionally denotes element membership in set theory rather than defining an interval for integration.
The second option attempts to clarify the interval by using {0..∞}
, but the use of d{x={0..∞}
might be confusing due to the unconventional placement of braces and equals sign.
Considering clarity and the need to avoid ambiguity, neither option is ideal as presented. A more clear and conventional approach might involve directly stating the limits of integration and clearly separating the differential part of the integral. Here's a suggested alternative:
var integral = Function.expres("∫(0,∞) 1 dx");
This syntax:
(0,∞)
from the integrand 1
and the differential part dx
.∈
in contexts where they could be misinterpreted.If the integration operation is a central feature of your language or library, consider implementing a more robust parsing mechanism that can handle variations in syntax while providing clear error messages for unsupported formats. This approach allows users more flexibility in how they express integrals and other mathematical operations while maintaining the readability and integrity of the code.
java.lang.AssertionError: todo: parse integrand and variable of integration then integrate it over 2 to 4
at arb4j/arb.expressions.nodes.Integral.<init>(Integral.java:83)
at arb4j/arb.expressions.Expression.evaluate(Expression.java:721)
at arb4j/arb.expressions.Expression.exponentiate(Expression.java:842)
at arb4j/arb.expressions.Expression.exponentiateMultiplyAndDivide(Expression.java:881)
at arb4j/arb.expressions.Expression.resolveArithmetic(Expression.java:1613)
at arb4j/arb.expressions.Expression.parseRoot(Expression.java:1494)
at arb4j/arb.expressions.Compiler.express(Compiler.java:153)
at arb4j/arb.expressions.Compiler.express(Compiler.java:170)
at arb4j/arb.expressions.Compiler.express(Compiler.java:105)
at arb4j/arb.expressions.Expression.compile(Expression.java:193)
at arb4j/arb.expressions.Expression.instantiate(Expression.java:231)
at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:655)
at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:649)
at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:628)
at arb4j/arb.expressions.ExpressionTest.testIntegralOfAConstant(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)
java.lang.AssertionError: todo: evaluate compiled integrand x➔1*x at x=2 and subtract from that the value at x=4
at arb4j/arb.expressions.nodes.Integral.generate(Integral.java:102)
at arb4j/arb.expressions.Expression.generateEvaluationMethod(Expression.java:1029)
at arb4j/arb.expressions.Expression.compile(Expression.java:525)
at arb4j/arb.expressions.Compiler.express(Compiler.java:154)
at arb4j/arb.expressions.Compiler.express(Compiler.java:170)
at arb4j/arb.expressions.Compiler.express(Compiler.java:105)
at arb4j/arb.expressions.Expression.compile(Expression.java:193)
at arb4j/arb.expressions.Expression.instantiate(Expression.java:231)
at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:655)
at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:649)
at arb4j/arb.functions.real.RealFunction.express(RealFunction.java:628)
at arb4j/arb.expressions.ExpressionTest.testIntegralOfAConstant(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)
package arb.functions.real;
import arb.Integer;
import arb.Real;
public class simpleIntegral implements
RealFunction
{
private boolean isInitialized;
Integer cℤ2;
Integer cℤ1;
Integer cℤ3;
public Real valueℝ1;
public Real integralDifferenceℝ1;
public final integralℝ1 integralℝ1 = new integralℝ1();
public Class rangeType()
{
return Real.class;
}
public Real evaluate(Real in, int order, int bits, Real result)
{
return integralℝ1.evaluate(cℤ3, bits, valueℝ1)
.sub(integralℝ1.evaluate(cℤ2, bits, valueℝ1), bits, result);
}
public simpleIntegral()
{
cℤ2 = new Integer("2");
cℤ1 = new Integer("1");
cℤ3 = new Integer("4");
valueℝ1 = new Real();
integralDifferenceℝ1 = new Real();
}
public void initialize()
{
if (isInitialized)
{
throw new AssertionError("Already initialized");
}
else
{
isInitialized = true;
}
}
public void close()
{
cℤ2.close();
cℤ1.close();
cℤ3.close();
valueℝ1.close();
integralDifferenceℝ1.close();
}
@Override
public String toString()
{
return "∫x➔1dx∈(2,4)";
}
}
Description Resource Path Location Type
The method evaluate(Real, int, Real) in the type Function<Real,Real> is not applicable for the arguments (Integer, int, Real) simpleIntegral.java /arb4j/src/main/java/arb/functions/real line 24 Java Problem
The method evaluate(Real, int, Real) in the type Function<Real,Real> is not applicable for the arguments (Integer, int, Real) simpleIntegral.java /arb4j/src/main/java/arb/functions/real line 25 Java Problem