0
$\begingroup$

Consider the following expression that Mathematica returned me after a calculation:

Res=1/77 (-107 3^(1 + k) 7^k + 199 2^(1 + k) 49^k)

As you can see, Mathematica did not really regroup the terms by power of $k$. What I would like is to see something like:

FixedRes=1/77 (-321*21^k + 398*98^k)

More generally I would like to do something like:

$$a_1^k*a_2^{k+2}+a_3^k*a_4^{k+2} \to a_2^2*(a_1*a_2)^k+a_4^2*(a_3*a_4)^k $$

How can I do it? I saw the function "Collect" but it does not solve the same issue I am asking. It is used to regroup terms a sum of terms in power of a fixed variable $x$ as a sum of separate $x^k$. I am somehow asking to regroup terms having a same power as variable.

$\endgroup$
6
  • $\begingroup$ This will be little hard without using Hold or Inactive and such family of commands. Because Mathematica will do automatic rewrite. Here is screen shot !Mathematica graphics You see, it automatically writes $3 (3\times7)^k$ as $3^{1+k} 7^k$ instead of $3\times 21^k$ $\endgroup$
    – Nasser
    Feb 15, 2023 at 14:09
  • $\begingroup$ ps. fixed your latex in title. In Latex * is not used for multiplication. Better to use $\times$ $\endgroup$
    – Nasser
    Feb 15, 2023 at 14:15
  • $\begingroup$ Try: res // Expand // Simplify $\endgroup$ Feb 15, 2023 at 15:21
  • $\begingroup$ @DanielHuber it does not work. They want same output as FixedRes $\endgroup$
    – Nasser
    Feb 15, 2023 at 15:31
  • $\begingroup$ I get, from Res // Expand // Simplify the output: 1/11 7^(-1 + k) (-107 3^(1 + k) + 199 2^(1 + k) 7^k). My version of MMA is 13.2 $\endgroup$ Feb 15, 2023 at 15:51

1 Answer 1

1
$\begingroup$

If you don't care efficiencies,

powerRule1[power_] :=
    {
        IgnoringInactive[x_^(power*k1_.+rest1_.)*y_^(power*k2_.+rest2_.)]:>
            With[ {var = x^k1*y^k2//Simplify},
                If[ IntegerQ[x]||IntegerQ[y],
                    Inactivate[var^power,Power|Sqrt],
                    var^power
                ]
            ]*x^rest1*y^rest2
    };
res//.powerRule1[k]

enter image description here


This is a function I wrote long time ago and have never tested carefully.

powerCollect`kernel[][expr_] :=
    expr//ReplaceRepeated[powerRule`collect[]];
powerCollect`kernel[power_][expr_] :=
    expr//ReplaceRepeated[powerRule`collect[power]];
powerCollect`kernel[power1_,powerRest__][expr_] :=
    powerCollect`kernel[powerRest][
        powerCollect`kernel[power1][expr]
    ];

powerCollect//Options={"wrapper"->Activate};
powerCollect[powers___,opts:OptionsPattern[]][expr_]:=
    powerCollect`kernel[powers][expr]//OptionValue["wrapper"];

powerRule`collect[power_] :=
    {
        IgnoringInactive[x_^(power*k1_.+rest1_.)*y_^(power*k2_.+rest2_.)]:>
            With[ {var = x^k1*y^k2//Simplify},
                If[ IntegerQ[x]||IntegerQ[y],
                    Inactivate[var^power,Power|Sqrt],
                    var^power
                ]
            ]*x^rest1*y^rest2,
        powerRule`mergeNestedPower,
        powerRule`cancelMinus
    };

powerRule`collect[] = {
    IgnoringInactive[x_^(power_*k1_.+rest1_.)*y_^(power_*k2_.+rest2_.)]:>
        With[ {var = x^k1*y^k2//Simplify},
            If[ IntegerQ[x]||IntegerQ[y],
                Inactivate[var^power,Power|Sqrt],
                var^power
            ]
        ]*x^rest1*y^rest2,
    powerRule`mergeNestedPower,
    powerRule`cancelMinus
};

powerRule`mergeNestedPower = 
    IgnoringInactive[(x_^a_)^b_]:>x^(a*b);

powerRule`cancelMinus =
    IgnoringInactive[(-1*rest_.)^(k_Integer*a_.+b_.)]/;EvenQ@k:>
        (-1)^b*rest^(k*a+b);

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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