0
$\begingroup$

Two months ago I asked a way to truncate a polynomial to a given degree. @Stitch answered my question by writing a program which contained the following function:

truncate[polynomial_, variable_, maxPower_] := 
  Module[{rules}, 
    rules = CoefficientRules[polynomial, {variable}]; 
    Plus @@ (Times[Power[variable, #[[1, 1]]], #[[2]]] & /@ 
      Select[rules, (#[[1, 1]] <= maxPower) &])]

It's good but it works only when the power of the polynomial is positive and/or is a integer number. When the power is not positive and integer, the function CoefficientRules does not manage to select the coefficient in front of the variable and the output is completly wrong.

Is there a way to generalize this script for any kind of power? In particular I would like to find a way to order polynomial whose variables has generic power alpha, in such a way that there are expressions like

1+ t^alpha + t^(1 + alpha) + x t^(2 alpha) 

So my idea is to tell Mathematica that alpha is between 0 and 1, and then to truncate the polynomial to a given power I choose. Is this possible in someway?

Basically, the first script is also sufficient because I can just choose a particular value of alpha and then reverse-engineer to find what was the corresponding power of the polynomial for a generic alpha. But, if there is also the way to make the script very general, it would be great. I do not know any function in Mathematica which works with real powers, so I do not know how it is possible to generalize CoefficientRules.

$\endgroup$
4
  • $\begingroup$ Maybe use an explicit value for alpha such as EulerGamma? $\endgroup$ Jul 3, 2017 at 17:46
  • $\begingroup$ Just to clarify. You want to truncate based on the exponent, but it might be non-integer? Also, the alpha is not a number, but a range? $\endgroup$
    – Stitch
    Jul 3, 2017 at 22:53
  • $\begingroup$ @Stitch Yes, the Power of the polynomials can be real and "alpha" is the parameter which makes the power real. I have a polynomial where the variable is also "alpha" but I know that alpha is between 0 and 1 so I think that a script which truncate the polynomial for real powers is sufficient because I can always restore the alpha after the truncation if I choose it properly. At the same time if the script is really generic for an alpha parameter would be very great! Thank you very much for the old script $\endgroup$ Jul 4, 2017 at 5:54
  • $\begingroup$ Please let me know if the answer below works or you need something else. $\endgroup$
    – Stitch
    Jul 4, 2017 at 22:26

1 Answer 1

1
$\begingroup$

As a starting point, you can deal with real exponents by using Collect, but you need to rationalize the exponents first:

truncate[polynomial_, variable_, maxPower_] :=
 Module[{rationalizedPolynomial, rules, free},
  rationalizedPolynomial = 
   Collect[polynomial /. {Power[variable, e_: 1] :> 
       Power[variable, Rationalize[e]]}, variable];
  rules = 
   Cases[rationalizedPolynomial, 
    Times[a_: 1, Power[variable, e_: 1]] | 
      Power[variable, e_: 1] :> {a, e}];
  free = DeleteCases[rationalizedPolynomial, 
    Times[_, Power[variable, _ : 1]] | Power[variable, _ : 1]];
  Plus @@ ({free}~
     Join~(Times[Power[variable, N@#1[[2]]], #1[[1]]] & /@ 
       Select[rules, (#[[2]] <= maxPower) &]))
  ]

or by manually combining the terms:

truncate[polynomial_, variable_, maxPower_] :=
 Module[{powers, groups, rules, free},
  powers = 
   Cases[polynomial, 
    Times[a_: 1, Power[variable, e_: 1]] | 
      Power[variable, e_: 1] :> {a, e}];
  groups = GroupBy[powers, #[[2]] &];
  rules = 
   Map[Merge[Total], Map[Rule[#[[2]], #[[1]]] &, groups, {2}]] // 
      Values // Normal // Flatten;
  free = DeleteCases[polynomial, 
    Times[_, Power[variable, _ : 1]] | Power[variable, _ : 1]];
  Plus @@ ({free}~
     Join~(Times[Power[variable, N@#1[[1]]], #1[[2]]] & /@ 
       Select[rules, (#[[1]] <= maxPower) &]))
  ]

Using either of them as before:

p = 1 + y + 2 x + x^2 + x^0.8 + x^2.91 + 3 x^2.5 + y x^2.1 + y x^0.8;
truncate[p, x, .9]

1 + y + x^0.8 (1 + y)

$\endgroup$
4
  • $\begingroup$ Both the algorithms work very well. The only thing that does not work is the fact that sometimes I have a power elevated to a real power [example: (t^2)^(1+0.3334)] and Mathematica does not want to simplify the expression making the product of the exponents. This is a crucial fact since your algorithms eliminate the external power and not the product of the two powers. So I have some truncation which is not properly corrected and I think that sometimes Mathematica could have truncate something that maybe should not have to be truncated. $\endgroup$ Jul 5, 2017 at 6:47
  • $\begingroup$ If you have some suggestion to force the multiplication of the power, very good, otherwise I will try to simplify manually the not needed powers after maybe a truncation bigger than what I need to avoid unwanted truncations. Thank you very much for your help so far, you have been very helpful to me! $\endgroup$ Jul 5, 2017 at 6:50
  • $\begingroup$ Oh! I did it! I manage to enforce the multiplication of the exponents! Ok, now I have solved my problem and I can restore the generic "alpha" after the truncation just choosing it properly so that there is not ambiguity in the power series. Thank you very much! You were really really really helpful!! $\endgroup$ Jul 5, 2017 at 10:03
  • $\begingroup$ @AlessandroMininno No problem! Glad it does what you need. Let me know if you have other questions. $\endgroup$
    – Stitch
    Jul 15, 2017 at 17:14

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.