I am using RSolve to find solutions, but I am only interested in rational solutions. For example here: code

And here is the input code:

input[x_,y_] := (2x^3+13x^2+22x+8)y[x+3]-(2x^3+11x^2+18x+9)y[x+2]+(2x^3+x^2-6x)y[x+1]-(2x^3-x^2-2x+1)y[x]
solutions = RSolve[input[x, y] == 0, y[x], x]

I am only interested in the first term, I would prefer that the result was only $\frac{(3-2x)c_1}{3(-1+x^2)}$. I looked around and I could not find a solution, since RationalQ does not exist.

To further clarify, I don't know how to strip the hypergeometric part. Setting any $c$ to 0 would not work, since then in one other example I could have:

input[x_, y_] := y[x + 2] - 6 y[x + 1] + 9 y[x];
RSolve[input[x, y] == 0, y[x], x]

Which outputs:

$$\left\{\left\{y(x)\to c_1 3^x+c_2 3^x x\right\}\right\}.$$

Setting $c_2$ to zero would remove one solution, which is not what I want.

  • $\begingroup$ Just set $c_2$ and $c_3$ to zero in your solution. $\endgroup$ – yarchik Mar 28 at 12:35
  • $\begingroup$ That's a great idea, but I am interested in general sense, since this would not work if there were for example 2 polynomial solutions $\endgroup$ – klemen kobau Mar 28 at 12:49
  • 2
    $\begingroup$ Why would not that work? Actually, you do not provide any information and yet expect a full answer. Why don't you copy and paste your actual code you are currently having a problem? This is actually a general requirement for posts here--to provide the code. $\endgroup$ – yarchik Mar 28 at 14:28
  • 3
    $\begingroup$ Please post the code used to generate this result. Copy and paste the code in Raw InputForm rather than a picture of the code. $\endgroup$ – Bob Hanlon Mar 28 at 14:29
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Mar 28 at 16:01

I have method to only select rational solutions, but it is a bit clumsy and probably can be improved. Here is my code:

ClearAll[onlyrationals, splitC, polynomialQ, rationalQ];

onlyrationals[solns_, vars_] := With[{k = Keys[#][[1]]},
   {k -> splitC[Values@#[[1]], vars]}] & /@ solns;

splitC[expr_, vars_] := Module[{cv, cx, c0, cvars = Sort@
   Reap[Map[If[Head[#] === C, Sow[#], #] &, expr, Infinity]][[2, 1]]},
   c0 = expr - Sum[Coefficient[expr, cv] cv, {cv, cvars}] // Simplify;
   c0 + Sum[If[rationalQ[cx = Coefficient[expr, cv], vars], cv cx, 0],
     {cv, cvars}]];

polynomialQ[expr_, vars_] := Module[{x, n}, Which[
    MatchQ[expr, Power[x_ /; polynomialQ[x, vars], 
      n_ /; IntegerQ[n] && Positive[n]]], True,
    Head@expr === Times, And @@ Table[polynomialQ[x, vars], {x, List @@ expr}],
    Head@expr === Plus,  And @@ Table[polynomialQ[x, vars], {x, List @@ expr}],
    Head@expr === Minus, polynomialQ[-expr, vars],
    Head@expr === Symbol, MemberQ[vars, expr],
    NumberQ@expr, True,   True, False]];

rationalQ[expr_, vars_] :=  polynomialQ[Numerator@expr, vars] && 
   polynomialQ[Denominator@expr, vars];

You would use the code like this:

solutions = RSolve[b[x + 2] - 3 b[x + 1] + 2 b[x] + 4 == 0, b[x], x]
(* {{b[x] -> 4*(1 + x) + C[1] + 2^x*C[2]}} *)
onlyrationals[solutions, {x}]
(* {{b[x] -> 4*(1 + x) + C[1]}} *)
  • $\begingroup$ This does not give me the expected solution, but it answers my question. I will accept this since it is a good step in the right direction $\endgroup$ – klemen kobau Mar 29 at 19:43
  • $\begingroup$ @klemenkobau Thanks for that helpful comment! I have modified my code for better solution, but it still does not completely do the job. :-( $\endgroup$ – Somos Mar 29 at 19:52
  • $\begingroup$ @klemenkobau After much work I think my code does what you want. $\endgroup$ – Somos Mar 30 at 0:10
  • $\begingroup$ uff I will have to go through this, thank you for your work, I will also try to solve it myself using your example $\endgroup$ – klemen kobau Mar 30 at 20:54

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