From 8e476713104d453058342fe242e1bd5c098974fa Mon Sep 17 00:00:00 2001 From: axexlck Date: Mon, 4 Nov 2024 21:07:31 +0100 Subject: [PATCH] Improve `CoefficientList` for `SeriesData` arguments --- .../doc/functions/CoefficientList.md | 6 +++++ .../core/builtin/PolynomialFunctions.java | 12 ++++++--- .../doc/functions/CoefficientList.md | 6 +++++ .../core/system/LowercaseTestCase.java | 25 +++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/symja_android_library/doc/functions/CoefficientList.md b/symja_android_library/doc/functions/CoefficientList.md index 60326b2105..d2a85d3aed 100644 --- a/symja_android_library/doc/functions/CoefficientList.md +++ b/symja_android_library/doc/functions/CoefficientList.md @@ -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. diff --git a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/PolynomialFunctions.java b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/PolynomialFunctions.java index 3f57e06f35..8b7261d938 100644 --- a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/PolynomialFunctions.java +++ b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/PolynomialFunctions.java @@ -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; @@ -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); } @@ -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> polyList = @@ -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)); diff --git a/symja_android_library/matheclipse-core/src/main/resources/doc/functions/CoefficientList.md b/symja_android_library/matheclipse-core/src/main/resources/doc/functions/CoefficientList.md index 60326b2105..d2a85d3aed 100644 --- a/symja_android_library/matheclipse-core/src/main/resources/doc/functions/CoefficientList.md +++ b/symja_android_library/matheclipse-core/src/main/resources/doc/functions/CoefficientList.md @@ -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. diff --git a/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java b/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java index b36b01050f..4f63019beb 100644 --- a/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java +++ b/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java @@ -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)", // @@ -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)", // @@ -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}");