Skip to content

Commit

Permalink
Improve CoefficientList for SeriesData arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Nov 4, 2024
1 parent 58235e2 commit 8e47671
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
6 changes: 6 additions & 0 deletions symja_android_library/doc/functions/CoefficientList.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ See:
>> CoefficientList(a+c*x^2, x)
{a,0,c}
>> CoefficientList((x + y)^3, z)
{(x+y)^3}
>> CoefficientList(Series(2*x, {x, 0, 9}), x)
{0,2}
```

In the next line `Coefficient` returns the coefficient of a particular term of a polynomial. In this case `(-210*c^2 * x^2*y*z^2)` is a term of `(c*x-2*y+z)^7` after it's expanded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matheclipse.core.basic.Config;
import org.matheclipse.core.basic.OperationSystem;
import org.matheclipse.core.convert.JASConvert;
import org.matheclipse.core.convert.JASIExpr;
import org.matheclipse.core.convert.JASModInteger;
Expand Down Expand Up @@ -303,8 +302,13 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (ast.arg1().isList()) {
return arg1.mapThread(ast, 1);
}
IExpr expr = F.evalExpandAll(arg1, engine).normal(false);
IAST list = ast.arg2().makeList();
IExpr expr = arg1.normal(false);
if (expr.isAST() && !expr.isFree(x -> list.indexOf(x) > 0, true)) {
expr = F.evalExpandAll(arg1, engine);
}
expr = engine.evaluate(expr.normal(false));

return coefficientList(expr, list);
}

Expand Down Expand Up @@ -1684,7 +1688,7 @@ private static IAST computeGroebnerBasis(IAST listOfPolynomials, IAST listOfVari
return F.NIL;
}
varList.add((ISymbol) listOfVariables.get(i));
pvars[i - 1] = ((ISymbol) listOfVariables.get(i)).toString();
pvars[i - 1] = listOfVariables.get(i).toString();
}

List<GenPolynomial<BigRational>> polyList =
Expand Down Expand Up @@ -2072,7 +2076,7 @@ public IExpr numericFunction(IAST ast, final EvalEngine engine) {
// https://en.wikipedia.org/wiki/Laguerre_polynomials#Physicist_Scaling_Convention
// (Gamma(l+n+1)*Hypergeometric1F1(-n,l+1,z))/(Gamma(l+1)*n!)
INumber nNumber = n;
IExpr v1 = ((INumber) l).plus(F.C1);
IExpr v1 = l.plus(F.C1);
return F.Times(F.Power(F.Factorial(n), F.CN1), F.Power(F.Gamma(v1), F.CN1),
F.Gamma(F.Plus(n, v1)), F.Hypergeometric1F1(nNumber.negate(), v1, z));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ See:
>> CoefficientList(a+c*x^2, x)
{a,0,c}
>> CoefficientList((x + y)^3, z)
{(x+y)^3}
>> CoefficientList(Series(2*x, {x, 0, 9}), x)
{0,2}
```

In the next line `Coefficient` returns the coefficient of a particular term of a polynomial. In this case `(-210*c^2 * x^2*y*z^2)` is a term of `(c*x-2*y+z)^7` after it's expanded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2732,6 +2732,23 @@ public void testCoefficient() {

@Test
public void testCoefficientList() {

check("CoefficientList(Series(2*x, {x, 0, 9}), x)", //
"{0,2}");
check("Series(2*x, {x, 0, 9})", //
"2*x+O(x)^10");

check("CoefficientList((x + y)^3, z)", //
"{(x+y)^3}");
check("CoefficientList((x - 2 y + 3 z)^3, {x, y, z})", //
"{{{0,0,0,27},{0,0,-54,0},{0,36,0,0},{-8,0,0,0}},{{0,0,27,0},{0,-36,0,0},{12,0,0,\n" //
+ "0},{0,0,0,0}},{{0,9,0,0},{-6,0,0,0},{0,0,0,0},{0,0,0,0}},{{1,0,0,0},{0,0,0,0},{0,\n" //
+ "0,0,0},{0,0,0,0}}}");

check("CoefficientList(Series(Log(1-x), {x, 0, 9}),x)", //
"{0,-1,-1/2,-1/3,-1/4,-1/5,-1/6,-1/7,-1/8,-1/9}");


// check("1.0 * 10.0* x^9", //
// "10.0*x^9");
check("Expand((1.0 + x)^10)", //
Expand Down Expand Up @@ -15989,6 +16006,12 @@ public void testNorm() {

@Test
public void testNormal() {
check("s=Series(Exp(x), {x, 0, 5})", //
"1+x+x^2/2+x^3/6+x^4/24+x^5/120+O(x)^6");
check("Normal(s)", //
"1+x+x^2/2+x^3/6+x^4/24+x^5/120");
check("Normal(Pi)", //
"Pi");
check("Normal( ConditionalExpression( 1, Element(a,Reals)&&b>0&&n>0 ) + z^3 )", //
"1+z^3");
check("Normal( ConditionalExpression( 1, Element(a,Reals)&&b>0&&n>0 ) + z^3, ByteArray)", //
Expand Down Expand Up @@ -20332,6 +20355,8 @@ public void testRealDigits() {

check("RealDigits(0)", //
"{{0},1}");
check("RealDigits(0,20)", //
"{{0},1}");
check("RealDigits(0.1, 2)", //
"{{1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,\n" //
+ "0,1,1,0,0,1,1,0,0,1,1,0,0,1},-3}");
Expand Down

0 comments on commit 8e47671

Please sign in to comment.