2 * Introduction to solve_rec::
3 * Functions and Variables for solve_rec::
6 @node Introduction to solve_rec, Functions and Variables for solve_rec, Package solve_rec, Package solve_rec
7 @section Introduction to solve_rec
9 @code{solve_rec} is a package for solving linear recurrences with polynomial
12 A demo is available with @code{demo("solve_rec");}.
17 (%i1) load("solve_rec")$
19 (%i2) solve_rec((n+4)*s[n+2] + s[n+1] - (n+1)*s[n], s[n]);
23 (%o2) s = -------------------- + ---------------
24 n (n + 1) (n + 2) (n + 1) (n + 2)
28 @opencatbox{Categories:}
29 @category{Linear recurrences}
30 @category{Share packages}
31 @category{Package solve_rec}
34 @node Functions and Variables for solve_rec, , Introduction to solve_rec, Package solve_rec
35 @section Functions and Variables for solve_rec
37 @anchor{harmonic_number}
38 @deffn {Function} harmonic_number (@var{x})
39 When @var{x} is positive integer @math{n}, @code{harmonic_number} is
40 the @math{n}'th harmonic number. More generally,
41 @code{harmonic_number(x) = psi[0](x+1) + %gamma}. (See @ref{polygamma}).
44 @c load("simplify_sum")$
45 @c harmonic_number(5);
47 @c float(harmonic_number(sqrt(2)));
48 @c float(psi[0](1+sqrt(2)))+%gamma;
51 (%i1) load("simplify_sum")$
53 (%i2) harmonic_number(5);
59 (%i3) sum(1/k, k, 1, 5);
65 (%i4) float(harmonic_number(sqrt(2)));
66 (%o4) %gamma + 0.6601971549171388
69 (%i5) float(psi[0](1+sqrt(2)))+%gamma;
70 (%o5) %gamma + 0.6601971549171388
75 @anchor{harmonic_to_psi}
76 @deffn {Function} harmonic_to_psi (@var{x})
77 Converts expressions with @code{harmonic_number} to the equivalent
78 expression involving @code{psi[0]} (see @ref{polygamma}).
81 @c load("simplify_sum")$
82 @c harmonic_to_psi(harmonic_number(sqrt(2)));
85 (%i1) load("simplify_sum")$
87 (%i2) harmonic_to_psi(harmonic_number(sqrt(2)));
88 (%o2) psi (sqrt(2) + 1) + %gamma
95 @deffn {Function} reduce_order (@var{rec}, @var{sol}, @var{var})
97 Reduces the order of linear recurrence @var{rec} when a particular solution
98 @var{sol} is known. The reduced reccurence can be used to get other solutions.
104 (%i3) rec: x[n+2] = x[n+1] + x[n]/n;
111 (%i4) solve_rec(rec, x[n]);
112 WARNING: found some hypergeometrical solutions!
117 (%i5) reduce_order(rec, n, x[n]);
129 (%o6) (- n - 2) %u - %u
133 (%i6) solve_rec((n+2)*%u[n+1] + %u[n], %u[n]);
137 (%o6) %u = ----------
140 So the general solution is
145 %k n > -------- + %k n
152 @opencatbox{Categories:}
153 @category{Package solve_rec}
158 @anchor{simplify_products}
159 @defvr {Option variable} simplify_products
160 Default value: @code{true}
162 If @code{simplify_products} is @code{true}, @code{solve_rec} will try to
163 simplify products in result.
165 See also: @mrefdot{solve_rec}
167 @opencatbox{Categories:}
168 @category{Package solve_rec}
173 @anchor{simplify_sum}
174 @deffn {Function} simplify_sum (@var{expr})
176 Tries to simplify all sums appearing in @var{expr} to a closed form.
178 To use this function first load the @code{simplify_sum} package with
179 @code{load("simplify_sum")}.
184 @c load("simplify_sum")$
185 @c sum(binomial(n+k,k)/2^k, k, 1, n) + sum(binomial(2*n, 2*k), k, 1,n);
189 (%i1) load("simplify_sum")$
191 (%i2) sum(binomial(n+k,k)/2^k, k, 1, n) + sum(binomial(2*n, 2*k), k, 1,n);
194 \ binomial(n + k, k) \
195 (%o2) > ------------------ + > binomial(2 n, 2 k)
201 (%i3) simplify_sum(%);
207 @opencatbox{Categories:}
208 @category{Package solve_rec}
209 @category{Sums and products}
210 @category{Simplification functions}
216 @deffn {Function} solve_rec (@var{eqn}, @var{var}, [@var{init}])
217 Solves for hypergeometrical solutions to linear recurrence @var{eqn} with
218 polynomials coefficient in variable @var{var}. Optional arguments @var{init}
219 are initial conditions.
221 @code{solve_rec} can solve linear recurrences with constant coefficients,
222 finds hypergeometrical solutions to homogeneous linear recurrences with
223 polynomial coefficients, rational solutions to linear recurrences with
224 polynomial coefficients and can solve Ricatti type recurrences.
226 Note that the running time of the algorithm used to find hypergeometrical
227 solutions is exponential in the degree of the leading and trailing
230 To use this function first load the @code{solve_rec} package with
231 @code{load("solve_rec");}.
233 Example of linear recurrence with constant coefficients:
237 (%i2) solve_rec(a[n]=a[n-1]+a[n-2]+n/2^n, a[n]);
239 (sqrt(5) - 1) %k (- 1)
241 (%o2) a = ------------------------- - ----
247 + ------------------ - ----
253 Example of linear recurrence with polynomial coefficients:
257 (%i7) 2*x*(x+1)*y[x] - (x^2+3*x-2)*y[x+1] + (x-1)*y[x+2];
259 (%o7) (x - 1) y - (x + 3 x - 2) y + 2 x (x + 1) y
263 (%i8) solve_rec(%, y[x], y[1]=1, y[3]=3);
271 Example of Ricatti type recurrence:
275 (%i2) x*y[x+1]*y[x] - y[x+1]/(x+2) + y[x]/(x-1) = 0;
278 (%o2) x y y - ------ + ----- = 0
281 (%i3) solve_rec(%, y[x], y[3]=5)$
283 (%i4) ratsimp(minfactorial(factcomb(%)));
286 (%o4) y = - -------------------------------------------------
288 5 x - 3 x - 25 x + 15 x + 20 x - 12 x - 1584
293 See also: @mrefcomma{solve_rec_rat} @mref{simplify_products} and @mrefdot{product_use_gamma}
295 @opencatbox{Categories:}
296 @category{Package solve_rec}
301 @anchor{solve_rec_rat}
302 @deffn {Function} solve_rec_rat (@var{eqn}, @var{var}, [@var{init}])
304 Solves for rational solutions to linear recurrences. See solve_rec for
305 description of arguments.
307 To use this function first load the @code{solve_rec} package with
308 @code{load("solve_rec");}.
314 (%i1) (x+4)*a[x+3] + (x+3)*a[x+2] - x*a[x+1] + (x^2-1)*a[x];
315 (%o1) (x + 4) a + (x + 3) a - x a
322 (%i2) solve_rec_rat(% = (x+2)/(x+1), a[x]);
324 (%o2) a = ---------------
330 See also: @mrefdot{solve_rec}
332 @opencatbox{Categories:}
333 @category{Package solve_rec}
338 @anchor{product_use_gamma}
339 @defvr {Option variable} product_use_gamma
340 Default value: @code{true}
342 When simplifying products, @code{solve_rec} introduces gamma function
343 into the expression if @code{product_use_gamma} is @code{true}.
345 See also: @mrefcomma{simplify_products} @mrefdot{solve_rec}
347 @opencatbox{Categories:}
348 @category{Package solve_rec}
353 @anchor{summand_to_rec}
354 @deffn {Function} summand_to_rec @
355 @fname{summand_to_rec} (@var{summand}, @var{k}, @var{n}) @
356 @fname{summand_to_rec} (@var{summand}, [@var{k}, @var{lo}, @var{hi}], @var{n})
358 Returns the recurrence satisfied by the sum
372 where summand is hypergeometrical in @var{k} and @var{n}. If @var{lo} and @var{hi}
373 are omitted, they are assumed to be @code{lo = -inf} and @code{hi = inf}.
375 To use this function first load the @code{simplify_sum} package with
376 @code{load("simplify_sum")}.
381 (%i1) load("simplify_sum")$
383 (%i2) summand: binom(n,k);
387 (%i3) summand_to_rec(summand,k,n);
392 (%i7) summand: binom(n, k)/(k+1);
398 (%i8) summand_to_rec(summand, [k, 0, n], n);
399 (%o8) 2 (n + 1) sm - (n + 2) sm = - 1
404 @opencatbox{Categories:}
405 @category{Package solve_rec}