0
$\begingroup$

Imagine we have an expression that contains both integer ($y$, $y^3$, $y^8$ etc.) and non-integer powers ($y^{1+\alpha}$, $y^{2\,\alpha}$, $y^{2+3\,\alpha}$) of the variable $y$, where $\alpha\in\mathbb{R}$, $\alpha>0$.

Simple example

expr = (-1 + (-1 + x) y) (2 + x y + y^α);

I want to rewrite this as

-2 + (-2 + x) y + (-1 + x) x y^2 - y^α + (-1 + x) y^(1 + α)

A more complicated expression, closer to what I have in reality:

expr=(-1 + (-1 + x) y + (-1 + x - x^2) y^2) ((1 + x)^4 (1 + y^2 a2[x] + 
    y^α sna[x])^2 ((1 + (-1 + x) y) (1 + 
    1/2 (-1 + x) x (-1 + (-1 + x) x (6 + x (-8 + 3 x))) y + 
    y^α sna[x]) (1 + 1/2 (-1 + x) x (1 + (-1 + x) x (-14 + x (-8 + 21 x))) y + 
    y^α sna[x]) + (1 +1/2 x (-1 + x (35 + x (-76 + x (23 + (46 - 27 x) x)))) y + 
    y^α sna[x]) (3 + 1/2 (-4 + 5 x - 3 x^2 - 20 x^3 + 73 x^4 - 78 x^5 + 27 x^6) y +
    y^α (3 + 2 (-1 + x) y) sna[x])));

where sna[x] is some real function of another variable x that we do not care about.

As in the simple example - I want to Collect all powers of y - both integer and non-integer, so the final expression will be something like

$\sum_{n=0}^{N}\,a_n(x)\,y^{n}+\sum_{k=0}^{K}b_k(x)\,y^{k+\alpha}+\sum_{j=0}^{J}c_j(x)\,y^{j+2\,\alpha}+...$ and so on depending on what is the highest multiple of $\alpha$ in an exponent, for some integers $N$, $K$, $J$.

I tried

Block[{$Assumptions = α > 0 && α ∈ Reals}, 
 Collect[expr, y, Simplify]]

and

Block[{$Assumptions = α > 0 && α ∈ Reals}, 
 Collect[expr, {y, y^α}, Simplify]]

but they don't work.

UPDATE: Possible solution. Taking the more complicated example, then we do

Block[{$Assumptions = α > 0 && α ∈ Reals}, 
   kek = Collect[expr, {y, y^α}, Simplify]];
kekList = List @@ kek;
Block[{$Assumptions = α > 0 && α ∈ Reals}, 
   kek2 = If[StringContainsQ[ToString[Exponent[#, y]], "Max"], 
       List @@ Collect[ExpandAll[#], y^α, Simplify], 
       Collect[ExpandAll[#], y^α, Simplify]] & /@ kekList];
kek3 = SortBy[Flatten[kek2], Exponent[#, y] &];

and kek3 is what I want. A quick check on the exponents

Exponent[kek3, y]
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, α, 2 α, 3 α, 4 α, 1 + α, 2 + α, 3 + α, 4 + α, 5 + α, 6 + α, 7 + α, 8 + α, 1 + 2 α, 2 + 2 α, 3 + 2 α, 4 + 2 α, 5 + 2 α, 6 + 2 α, 7 + 2 α, 1 + 3 α, 2 + 3 α, 3 + 3 α, 4 + 3 α, 5 + 3 α, 1 + 4 α, 2 + 4 α, 3 + 4 α}

I am not going to post what kek3 is itself, as it is very long. The above also works with the simple example.

Now, this is the brute force way that is not efficient. What is the Mathematica way to do it, as my real expressions are way longer and this will take centuries to evaluate.

$\endgroup$
  • $\begingroup$ Could you perhaps come up with a simpler expression to work on, and show the exact form (not the general formula) you would like it transformed to? $\endgroup$ – MarcoB Dec 4 '19 at 17:55
  • $\begingroup$ @MarcoB I added a simpler example $\endgroup$ – ThunderBiggi Dec 4 '19 at 19:58
  • $\begingroup$ Comes close: In[42]:= expr = (-1 + (-1 + x) y) (2 + x y + y^a); Collect[expr, {y^a, y}, Factor] Out[43]= -2 + (-2 + x) y + (-1 + x) x y^2 + y^a (-1 + (-1 + x) y) $\endgroup$ – Daniel Lichtblau Dec 4 '19 at 20:23
  • $\begingroup$ @DanielLichtblau I tried that. Is there a way to specify a general form, like when one does a replacement someExpr/.{x^(1+k_):>x^k} for example. $\endgroup$ – ThunderBiggi Dec 4 '19 at 21:04
  • 1
    $\begingroup$ I think the requirement changedfor the general case. the last time I looked it was as I described in prior comments. Now it is not. It is unrealistic to expect people to foresee such changes. For what it is worth, the earlier requirement would be the easier to meet. $\endgroup$ – Daniel Lichtblau Dec 4 '19 at 23:23
3
$\begingroup$

Does the following do what you want:

expr=(-1+(-1+x) y) (2+x y+y^α);
Collect[expr, y^_.]

-2 + (-2 + x) y + (-x + x^2) y^2 - y^α + (-1 + x) y^(1 + α)

and:

expr=(-1 + (-1 + x) y + (-1 + x - x^2) y^2) ((1 + x)^4 (1 + y^2 a2[x] + 
y^α sna[x])^2 ((1 + (-1 + x) y) (1 + 
1/2 (-1 + x) x (-1 + (-1 + x) x (6 + x (-8 + 3 x))) y + 
y^α sna[x]) (1 + 1/2 (-1 + x) x (1 + (-1 + x) x (-14 + x (-8 + 21 x))) y + 
y^α sna[x]) + (1 +1/2 x (-1 + x (35 + x (-76 + x (23 + (46 - 27 x) x)))) y + 
y^α sna[x]) (3 + 1/2 (-4 + 5 x - 3 x^2 - 20 x^3 + 73 x^4 - 78 x^5 + 27 x^6) y +
y^α (3 + 2 (-1 + x) y) sna[x])));

res = Collect[expr, y^_.];
Short[res, 5] //TeXForm

$-16 (x+1)^4 \text{sna}(x) y^{\alpha }-24 (x+1)^4 \text{sna}(x)^2 y^{2 \alpha }-16 (x+1)^4 \text{sna}(x)^3 y^{3 \alpha }-4 (x+1)^4 \text{sna}(x)^4 y^{4 \alpha }+\langle\langle 30\rangle\rangle +(x+1)^4 \left(\frac{333 x^{12}}{2}-726 x^{11}+911 x^{10}+470 x^9-\frac{4537 x^8}{2}+2235 x^7-887 x^6+105 x^5-\frac{99 x^4}{2}+59 x^3-\frac{33 x^2}{2}-2 x-8 \text{a2}(x)-1\right) y^2+(x+1)^4 \left(15 x^6+2 x^5-95 x^4+124 x^3-47 x^2+2 x-1\right) y-4 (x+1)^4$

If you want an output where the powers of α are pulled out, you could do:

Short[Collect[expr, {y^(_. α), y}], 5] //TeXForm

$\left((x+1)^4 \left(15 \text{a2}(x)^2 \text{sna}(x) x^9-44 \text{a2}(x)^2 \text{sna}(x) x^8+11 \text{a2}(x)^2 \text{sna}(x) x^7+127 \text{a2}(x)^2 \text{sna}(x) x^6-263 \text{a2}(x)^2 \text{sna}(x) x^5+262 \text{a2}(x)^2 \text{sna}(x) x^4-140 \text{a2}(x)^2 \text{sna}(x) x^3+33 \text{a2}(x)^2 \text{sna}(x) x^2-\text{a2}(x)^2 \text{sna}(x) x\right) y^8+(x+1)^4 \left(-\frac{63}{2} \text{a2}(x) \text{sna}(x) x^{15}+\langle\langle 31\rangle\rangle +6 \text{a2}(x)^2 \text{sna}(x)\right) y^7+(x+1)^4 (\langle\langle 1\rangle\rangle ) y^6+\langle\langle 3\rangle\rangle +\langle\langle 1\rangle\rangle +(x+1)^4 \left(45 \text{sna}(x) x^6+\langle\langle 9\rangle\rangle \right) y-16 (x+1)^4 \text{sna}(x)\right) y^{\alpha }+\langle\langle 14\rangle\rangle$

Here's a version where I set all of the coefficients to 1:

Collect[expr, {y^(_. α), y}, 1&]

1 + y + y^2 + y^3 + y^4 + y^5 + y^6 + y^7 + y^8 + y^9 + y^(4 α) (1 + y + y^2 + y^3) + y^(3 α) (1 + y + y^2 + y^3 + y^4 + y^5) + y^(2 α) (1 + y + y^2 + y^3 + y^4 + y^5 + y^6 + y^7) + y^α (1 + y + y^2 + y^3 + y^4 + y^5 + y^6 + y^7 + y^8)

| improve this answer | |
$\endgroup$
  • $\begingroup$ It is what I want and is faster than what I proposed in my question. Do you think it is possible to speed this up? $\endgroup$ – ThunderBiggi Dec 5 '19 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.