Splits an expression into two parts if possible - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-08-20T11:13:48Z https://mathematica.stackexchange.com/feeds/question/186438 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://mathematica.stackexchange.com/q/186438 0 Splits an expression into two parts if possible R zu https://mathematica.stackexchange.com/users/61476 2018-11-21T14:33:33Z 2018-11-21T23:43:18Z <p><strong>The series</strong></p> <ul> <li>The series is: <span class="math-container">$\text{expr}_{1} \pm \text{expr}_{2} ...$</span></li> <li><span class="math-container">$\pm$</span> represents plus or minus sign.</li> <li>The number of terms in the series can be 1 or more than 1.</li> <li>Each term <span class="math-container">$\text{expr}_{n}$</span> is an expression, and not necessarily <span class="math-container">$ax^{n}$</span></li> </ul> <p><strong>Use Case</strong></p> <ul> <li>I want to transform the series by: <ul> <li>Writing a rule that split the series into 1st and remaining terms.</li> <li>Writing another rule that transform the first term.</li> <li>Using the two rule to transform all terms of the series.</li> </ul></li> </ul> <p><strong>Current Goal</strong></p> <ul> <li><p>I want to split the series into:</p> <ul> <li>the first term, and </li> <li>the remaining terms</li> </ul></li> </ul> <p><strong>Constraint</strong></p> <ul> <li>I only want the pattern to match for once. <ul> <li>There are several ways to split the expression into 2 parts</li> <li>I only want a split that keeps the first term as simple as possible.</li> </ul></li> </ul> <p><strong>Test</strong></p> <p>I will use polynomials as <em>test cases</em>.</p> <pre><code>rule[expr_] = expr /. {Shortest[a_] + b__ :&gt; 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}] </code></pre> <p><strong>Expected Result</strong></p> <ul> <li><span class="math-container">$x$</span> </li> <li><span class="math-container">$-x^{4}$</span> </li> <li><span class="math-container">$-x$</span></li> <li><span class="math-container">$-2x^3$</span></li> </ul> <p><strong>Actual Result</strong></p> <ul> <li><p><span class="math-container">$x - 2 x^2 + 3 x^3$</span></p></li> <li><p><span class="math-container">$2 x^2 + 3 x^3 - x^4$</span></p></li> <li><p><span class="math-container">$-x$</span></p></li> <li><p><span class="math-container">$L \log (x)-2 x^3$</span></p></li> </ul> <p><strong>Question</strong></p> <p>The rule matches with the whole series instead of the shortest first term.</p> <p>How to fix the rule?</p> <p>Thanks.</p> https://mathematica.stackexchange.com/questions/186438/-/186442#186442 1 Answer by AccidentalFourierTransform for Splits an expression into two parts if possible AccidentalFourierTransform https://mathematica.stackexchange.com/users/34893 2018-11-21T15:19:42Z 2018-11-21T15:19:42Z <p>First of all, don't use braces around exponents.</p> <p>Second,</p> <pre><code>SetAttributes[rule, HoldAll]; rule[exp_] := ({HoldForm[exp] /. Plus -&gt; Sequence})[[1, 1]]; rule[x - 2 x^2 + 3 x^3] rule[-x^4 + 2 x^2 + 3 x^3] rule[-x] (* x -x^4 -x *) </code></pre> https://mathematica.stackexchange.com/questions/186438/-/186464#186464 0 Answer by R zu for Splits an expression into two parts if possible R zu https://mathematica.stackexchange.com/users/61476 2018-11-21T23:43:18Z 2018-11-21T23:43:18Z <p>Recursively apply rule to each term.</p> <pre><code>f[exp_] := exp //. {Plus[x1_, x2__] :&gt; f[x1] + f[x2]} </code></pre>