For questions about defining recursive functions, recursive algorithms and solving recursive equations.

learn more… | top users | synonyms

42
votes
2answers
9k views

How can I use Mathematica's graph functions to cheat at Boggle?

This question is inspired by a Stack Overflow question that I decided to solve using Mathematica. In addition to Mathematica, I thought I'd use some of the new version's graph-related functionality, ...
33
votes
4answers
2k views

How can I implement dynamic programming for a function with more than one argument?

Dynamic programming is a technique for avoiding the repeated computation of the same values in a recursive program. Each value computed is immediately stored. If the value is needed again, it is ...
24
votes
4answers
908 views

What tools can help in realizing tail recursion?

I had nice discussions with Leonid and Rojo that got me interested in tail recursion. Tail recursion is not always easy to realize with Mathematica, so it would be nice to have some tools to help with ...
17
votes
3answers
592 views

How to clear parts of a memoized function?

I have a function of two variables, e.g.: f[a_, b_] := f[a, b] = something f[a - 1, b - 1] etc With the above code I used the concept of memoization to speed up ...
7
votes
0answers
236 views

Compile recursive function modifying global variables

How to compile recursive formula when it relies on more than a few global variables (global to the topmost compiled function)? It is unreasonable to pass on all such variables to each recursive ...
6
votes
5answers
252 views

How to nest my own “times” function to get powers

I have a "times" function. I'd like to create a power function using it. It should look like this for an 6th power: ...
9
votes
1answer
603 views

Compiling a recursive formula

My question is related to computing what is called "invariant measure" for a particular well known fractal - the Sierpinski triangle. We have an array m of four two by two matrices, say ...
7
votes
1answer
826 views

Efficient backtracking with Mathematica

Backtracking is a general algorithm for finding all (or some) solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate c ...
4
votes
1answer
463 views

How do I use RSolve to solve a system of recurrence relations?

I am trying to solve a system of recurrence relations as follows. ...
5
votes
2answers
208 views

Update a function avoiding infinite recursion

I am quite new to Mathematica and not completely familiar with functional programming. I am currently working with a function (call it foo) and wish to change its behaviour, for example, by adding 1 ...
3
votes
4answers
310 views

Recursive function for calculating the arithmetic mean of a vector

I want to write recursive function for calculating the arithmetic mean of a vector. For $X(n)=\{1,2,3,\ldots, n\}$, the formula is ${\rm Mean}(X(n))=((n-1)\, {\rm Mean}((X(n-1))+X_n)/n$. My ...
2
votes
2answers
117 views

Recursion doesn't happen any more or doesn't happen correctly if I store the recursive relationship in a variable first

I know the following code will form a recursion: Clear["Global`*"] u[i_, n_] := u[i - 1, n] + u[i, n - 1] u[0, n_] := n u[i_, 0] := i u[2, 2] (* => 8 *) But ...
5
votes
0answers
196 views

Speeding up multilinear PRA branch-and-bound algorithm with worst-case exponential time scenario with respect to basic events

The algorithm is a branch-and-bound algorithm that calculates dominances for PRA, probalistic risk assessment. The task was to find faster ways to do it in numerical software such as Matlab but we ...
9
votes
2answers
294 views

Recursive Integral for Volume of $n$-Ball

The volume of an $n$-ball (the $(n+1)$-dimensional analogue of a disk) of radius $r$ can be found by the following integral recurrence: $$V_0(r)=2r$$ ...
9
votes
6answers
2k views

Recursive function with if-statement

I am trying to represent the following function definition in Mathematica: $$\begin{align*} f(1)&=1 \\ f(2n)&= \begin{cases}f(n) & \text{if}\space n\equiv0\pmod{2} \\ 2f(n) & ...
5
votes
3answers
225 views

Nested list to graph

The following nested list can be regarded as a representation of a (tree) graph: ...
8
votes
4answers
378 views

Partitioning a list by recursion (Programming Paradigms via Mathematica (A First Course))

I am working through the course Programming Paradigms via Mathematica. One of the homework exercises for the section Recursion I: Passing the Buck is as follows: Use recursion to partition a ...
4
votes
3answers
156 views

How to solve an integral equation by iteration method? [duplicate]

How I can obtain $n^{th}$ approximation of the following equation $f(t)=t+\int_0^tds f(s)$ by iteration method?
3
votes
1answer
742 views

Solve pair of recurrence relations

[Corrected equations and added simple example] Can you solve a system of (loosely) coupled recurrence relations like this in Mathematica somehow? ...
1
vote
1answer
245 views

Finding n-th term of a sequence of functions

Lets say, I have the following series of functions, $f_1=a+1$ $f_2=a^2+2a+1$ $f_3=a^3+3a^2+3a+1$ Assume that $f_4, f_5,$ and $f_6$ are also known. However $f_n$ is not known as a function of $n$. ...
8
votes
5answers
271 views

Wrapping a function in count

I am working through the Programming Paradigms via Mathematica (A First Course) and am attempting to answer the following: Use recursion to count how many times the argument can have its square ...
7
votes
2answers
372 views

How to deduce a generator formula for a polynomial sequence?

Consider a polynomial sequence $\{p_n\}$ generated by some (simple) rule: $$ \begin{array}{l} p_1(x)=x \\ p_2(x)=2 x-x^2 \\ p_3(x)= x^3-3 x^2+3 x \\ p_4(x)=-x^4+4 x^3-6 x^2+4 x \\ p_5(x)= x^5-5 ...
5
votes
1answer
192 views

Setting $RecursionLimit crashes Mathematica 10

I have encountered what I shall presume is a bug: setting $RecursionLimit repeatably crashes the Mathematica 10 kernel: ...
5
votes
1answer
190 views

How to deal with too much recursion

I have put together a very simple climate model based around four equations which define the state at time t based on time t-1, ...
5
votes
1answer
222 views

Build recursion in parallel?

Problem Let's define a simple recursion. f[1] = 1; f[n_] := f[n] = f[n - 1]*n; If I evaluate f in parallel ...
4
votes
0answers
113 views

Efficient Generation of Subgraphs (or, unfriend a friend a day)

NB: If you're not interested in the back-story, you could simply jump forward to the code which I'm seeking to optimize. Also, I believe this code would only work for MMA 8.0 and above. My friends ...
3
votes
0answers
281 views

Solving recursion relations using Mathematica

I want to solve the recursion relation given in equation 2.7(a/b) on page $6$ of this paper. (..the initial seed is $F_1 = G_1 = 1$ and the functions $\alpha$ and $\beta$ are defined on page $5$ in ...
3
votes
1answer
204 views

Avoid crash in recursive Dynamic

With the following Dynamic cell, I can reproducibly crash Mathematica, so do not try this unless you have saved all your work! ...
2
votes
1answer
180 views

calculating a sequence of functions using iteration

I am trying to compute a sequence of functions using iteration and keep running into problems trying to use built in looping commands because of the recursive nature of the definition. The code below ...
0
votes
1answer
106 views

Explicit, closed formula for recursive integral as a function of the recursive parameter

This is a follow-up of this question/answer. I'm working on a recursive integral more complicated than the one on the linked question, but this one can be used as an example. I would like to obtain ...
0
votes
2answers
229 views

Recursion on a moving window

I need to apply some computations to a moving window of $N$ items in a time series and I am struggling with doing recursion and shifting the considered window. To illustrate, please consider the ...