Skip to content

Commit

Permalink
allow second argument value in Interpolation / markdown docs
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Jun 22, 2024
1 parent 90d66e9 commit c0042e9
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 22 deletions.
32 changes: 32 additions & 0 deletions symja_android_library/doc/functions/Interpolation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Interpolation

```
Interpolation(data-list)
```

> return an `InterpolationFunction` for the `data-list`.
```
Interpolation(data-list, point)
```

> return the value for a given `point` interpolating the `data-list`.


### Examples

```
>> ipf=Interpolation(Table({x, Exp(4/(1+x^2))}, {x, 0, 3, 0.5})); ipf(2.5)
1.73624
>> Interpolation(Table({x, Exp(4/(1+x^2))}, {x, 0, 3, 0.5}), 2.5)
1.73624
>> Exp(4/(1 + 2.5^2))
1.73624
```

### Related terms
[InterpolatingFunction](InterpolatingFunction.md)

Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,66 @@
import org.hipparchus.analysis.interpolation.FieldHermiteInterpolator;
import org.matheclipse.core.eval.Errors;
import org.matheclipse.core.eval.EvalEngine;
import org.matheclipse.core.eval.interfaces.AbstractEvaluator;
import org.matheclipse.core.eval.interfaces.AbstractFunctionOptionEvaluator;
import org.matheclipse.core.eval.interfaces.IFunctionEvaluator;
import org.matheclipse.core.eval.util.OptionArgs;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.expression.ImplementationStatus;
import org.matheclipse.core.expression.S;
import org.matheclipse.core.expression.data.InterpolatingFunctionExpr;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IASTAppendable;
import org.matheclipse.core.interfaces.IBuiltInSymbol;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.core.interfaces.ISymbol;

public class Interpolation extends AbstractEvaluator {
public class Interpolation extends AbstractFunctionOptionEvaluator {

public Interpolation() {}

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
public IExpr evaluate(IAST ast, int argSize, IExpr[] options, EvalEngine engine,
IAST originalAST) {
int[] dims = ast.arg1().isMatrix();
if (dims != null && dims[0] > 2 && dims[1] >= 2) {
String method = "";
final OptionArgs options = new OptionArgs(ast.topHead(), ast, 2, engine);
IExpr option = options.getOption(S.Method);
if (option.isPresent()) {
IExpr option = options[0];
if (option.isPresent() && option != S.Automatic) {
method = option.toString();
}
if (!method.isEmpty()) {
// TODO: if ("Spline".equals(method)) {
if ("Hermite".equals(method)) {
return hermiteInterpolate((IAST) ast.arg1(), dims, engine);
IExpr temp = interpolation(ast, dims, method, engine);
if (temp instanceof InterpolatingFunctionExpr) {
if (ast.isAST2()) {
InterpolatingFunctionExpr ipf = (InterpolatingFunctionExpr) temp;
return F.unaryAST1(ipf, ast.arg2());
}
return Errors.printMessage(ast.topHead(), "optx", F.list(S.Method, ast), engine);
}
if (ast.isAST1()) {
if (dims[1] >= 2) {
int rowsSize = dims[0];
if (rowsSize >= 4) {
return piecewisePolynomialInterpolate(ast, rowsSize, engine);
}
}
return temp;
}
return F.NIL;
}

return F.NIL;
public IExpr interpolation(IAST ast, int[] dims, String method, EvalEngine engine) {
if (!method.isEmpty()) {
// TODO: if ("Spline".equals(method)) {
if ("Hermite".equalsIgnoreCase(method)) {
return hermiteInterpolate((IAST) ast.arg1(), dims, engine);
}
return Errors.printMessage(ast.topHead(), "optx", F.list(S.Method, ast), engine);
}
if (ast.argSize() == 1 || ast.argSize() == 2) {
if (dims[1] >= 2) {
int rowsSize = dims[0];
if (rowsSize >= 4) {
return piecewisePolynomialInterpolate(ast, rowsSize, engine);
}
}
}
return F.NIL;
}

public IExpr piecewisePolynomialInterpolate(final IAST ast, int rowsSize, EvalEngine engine) {
final IAST function = ast;
if (function.isAST1()) {
if (function.argSize() == 1 || function.argSize() == 2) {

if (rowsSize >= 4) {
IAST matrix = (IAST) function.arg1();
Expand Down Expand Up @@ -133,6 +144,15 @@ public int status() {

@Override
public int[] expectedArgSize(IAST ast) {
return IFunctionEvaluator.ARGS_1_5;
return IFunctionEvaluator.ARGS_1_2;
}

@Override
public void setUp(final ISymbol newSymbol) {
setOptions(newSymbol, //
new IBuiltInSymbol[] {//
S.Method}, //
new IExpr[] {//
S.Automatic});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Interpolation

```
Interpolation(data-list)
```

> return an `InterpolationFunction` for the `data-list`.
```
Interpolation(data-list, point)
```

> return the value for a given `point` interpolating the `data-list`.


### Examples

```
>> ipf=Interpolation(Table({x, Exp(4/(1+x^2))}, {x, 0, 3, 0.5})); ipf(2.5)
1.73624
>> Interpolation(Table({x, Exp(4/(1+x^2))}, {x, 0, 3, 0.5}), 2.5)
1.73624
>> Exp(4/(1 + 2.5^2))
1.73624
```

### Related terms
[InterpolatingFunction](InterpolatingFunction.md)

Original file line number Diff line number Diff line change
Expand Up @@ -11006,6 +11006,12 @@ public void testIntegerPartitions() {

@Test
public void testInterpolation() {
checkNumeric("Interpolation(Table({x, Exp(4/(1+x^2))}, {x, 0, 3, 0.5}), 2.5)", //
"1.7362439627994641");
checkNumeric("ipf=Interpolation(Table({x, Exp(4/(1+x^2))}, {x, 0, 3, 0.5})); ipf(2.5) ", //
"1.7362439627994641");
checkNumeric("Exp(4/(1 + 2.5^2))", //
"1.7362439627994641");
// print message: InterpolatingFunction: Input value {10} lies outside the range of data in the
// interpolating
// function. Extrapolation will be used.
Expand Down

0 comments on commit c0042e9

Please sign in to comment.