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

learn more… | top users | synonyms

47
votes
2answers
9k views

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

Boggle is a word game played with 16 dice and a 4x4 tray. This question is inspired by a Stack Overflow question about Boggle that I decided to solve using Mathematica. In addition to Mathematica, I ...
35
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
1k 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
682 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
262 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
259 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: ...
10
votes
5answers
576 views

How to deal with recursion formula in Mathematica?

In engineering problems, I am always seeing many recursion formula. For instance, In the book "The NURBS book", I discovered many recursion formula Fibonacci ...
9
votes
1answer
665 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
927 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 ...
6
votes
3answers
264 views

Nested list to graph

The following nested list can be regarded as a representation of a (tree) graph: ...
4
votes
1answer
505 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
1answer
218 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
2answers
226 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
436 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
123 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 ...
24
votes
3answers
825 views

Smooth Peter de Jong attractor

Today I was playing with Peter de Jong attractor. At the bottom of the page I've linked there are beautiful examples like: My attempts are not so great: It is around 10^5 points. For more than ...
22
votes
7answers
609 views

Alternative ways to implement a triangular recursion

Triangular recursions are a class of algorithms that frequently turn up in computational mathematics. These recursions are expressible in the general form ...
6
votes
1answer
247 views

Speeding up the non-negativity algorithm for multilinear function with interval probabilities?

The algorithm is a branch-and-bound algorithm that calculates the non-negativity of a multilinear function with interval probabilities. The lines 9-11 has an optimization that is explained on the ...
11
votes
7answers
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) & ...
8
votes
1answer
282 views

How can I define a Listable function only apply to a vector?

Consider the function f which should behave as f[{{1, 2}, {3, 4}, {5, 6, 7}}] ...
9
votes
2answers
318 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$$ ...
7
votes
2answers
284 views

Recursive partitioning: Is there a better way to do this?

Consider the following function, which recursively splits a list into equal length sublists (the length of the list is assumed to be a power of the second argument): ...
9
votes
2answers
396 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 ...
8
votes
4answers
406 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 ...
5
votes
2answers
279 views
4
votes
3answers
204 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
802 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
310 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
280 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
1answer
105 views

How to find a recurrence relation for a sequence?

I have a sequence given by an explicit formula for n-th term: ...
5
votes
2answers
178 views

Plotting two recursive functions

I'm trying to plot two recursive functions (p and θ) on a map. So far I have: ...
5
votes
1answer
217 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
4answers
253 views

Can the general term my recurrence equations be written with Floor or Mod?

I want to know the formula for the general term of the following recurrence system. I guess it can be written with Floor or Mod. ...
5
votes
1answer
253 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
119 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
2answers
202 views

A problem about recursion formula of deCasteljau algorithm

I use mathematica to realize the deCasteljaul algorithm $$\vec{P}_{k,i}(u_0)=(1-u_0)\vec{P}_{k-1,i}(u_0)+u_0\vec{P}_{k-1,i+1}(u_0)$$ The graphics that deCasteljaul algorithm generated as below: ...
3
votes
0answers
313 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
173 views

How can I get the general term of this recurrence equations?

Following is the recurrence relation: a[1] = 1; a[n_] := a[n - a[n - 1]] + 1 Array[a, 28] I tried to use RSolve, but it ...
3
votes
1answer
212 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
212 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
115 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
240 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 ...