The series
- The series is: $\text{expr}_{1} \pm \text{expr}_{2} ...$
- $\pm$ represents plus or minus sign.
- The number of terms in the series can be 1 or more than 1.
- Each term $\text{expr}_{n}$ is an expression, and not necessarily $ax^{n}$
Use Case
- I want to transform the series by:
- Writing a rule that split the series into 1st and remaining terms.
- Writing another rule that transform the first term.
- Using the two rule to transform all terms of the series.
Current Goal
I want to split the series into:
- the first term, and
- the remaining terms
Constraint
- I only want the pattern to match for once.
- There are several ways to split the expression into 2 parts
- I only want a split that keeps the first term as simple as possible.
Test
I will use polynomials as test cases.
rule[expr_] = expr /. {Shortest[a_] + b__ :> a} test1 = x - 2x^{2} + 3x^{3}; rule[test1] test2 = -x^{4} + 2x^{2} + 3x^{3}; rule[test2] test3 = -x; rule[test3] test4 := -2 x^3 + Sum[Log[x], {n, 1, L}] Expected Result
- $x$
- $-x^{4}$
- $-x$
- $-2x^3$
Actual Result
$x - 2 x^2 + 3 x^3$
$2 x^2 + 3 x^3 - x^4$
$-x$
$L \log (x)-2 x^3$
Question
The rule matches with the whole series instead of the shortest first term.
How to fix the rule?
Thanks.
Part[expr, 1]doesn't get the first term. The doc might be a lie: reference.wolfram.com/language/tutorial/… $\endgroup$