1

Suppose I wish to define a recursive function theta whose functionality should be apparent below.

The following definition will work.

theta[0] = 0;               
theta[i_ ] := theta[i-1] + 1

However, this will not work.

theta[0] = 0;               
theta[i_ + 1] := theta[i] + 1

My question is, is it possible to make something like the second definition work, where I can define the function based on the i+1 term instead of the i term?

I understand that they are mathematically equivalent, but I am curious about whether Mathematica will permit something like the second syntax.

2 Answers 2

1

It is perfectly feasible to make your second definition work if you understand that default automatic simplifications are done, often before you can get control, and if you then use your definition with appropriate parameters that match your definition.

Example

In[1]:= theta[i_ + 1] := Sin[i]+1;
theta[a + 1]

Out[2]= 1+Sin[a]

but then you probably expect to use this as

In[3]:= theta[8]

Out[3]= theta[8]

and that fails because you defined a function that matches the sum of something and one, but gave it just an integer and you have no definition that matches that. Even this fails

In[4]:= theta[7 + 1]

Out[4]= theta[8]

because the default automatic rules turn the sum of two integers into an integer and you are back to the previous case.

It is sometimes said that Mathematica does "structural" matching, if two structure of two expressions match the Mathematica accepts this as a match. This is very different from the sort of matching that anyone with a bit of mathematical maturity would use. A decade or more ago someone wrote up an article in the Mathematica Journal showing that it would be possible to use a more mathematical version of matching within Mathematica. I think that was completely ignored and nothing more was ever done with that. It would be nice if someone with the skill needed could bring that code up to the current version of Mathematica, but I think this might be a substantial challenge.

Sign up to request clarification or add additional context in comments.

Comments

0

There is always "a way". For example:

ClearAll[a];
a[i_] = a[i] /.  First@RSolve[{a[i + 1] == a[i] + 1, a[0] == 0}, a[i], i]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.