![]() ![]() The Fibonacci example would be extended by adding this: fib = 0 This is not possible with arrays, but it is possible with functions. You also asked about returning 0 for all negative indices. In the end it will easier to manage, but it does have a learning curve. However, what I showed you with the Fibonacci example is probably more suitable for this type of calculation than arrays. Notice that arrays are indexed using ], not. First the array would need to be pre-allocated, as in arr = ConstantArray If you decide to use arrays (they're called lists in Mathematica) to do this computation, then Do might be a little easier than While. I should have spelt out explicitly that using Table is not suitable for computing recursive relations because when computing the ith element of the array, it is not possible to refer back to the i-1th element. There's also RSolve for solving recursion relations symbolically. This technique is called memoization and you'll find a lot of information about it by googling.į := f = f + fĭoing this requires some familiarity with Mathematica, and if you're a beginner, I recommend going through at least a few tutorials first: Notice that this implementation uses = to cache every result, thus avoiding the exponential complexity that would result from the double recursion. Now try fib then check the definition of fib using ?fib and see how values got cached. The recursion, with caching: fib := (fib = fib + fib) These are needed to stop the recursion and avoid an infinite loop: fib = 0 The example which is usually given is Fibonacci:įirst clear the function definition and any cached values: Clear To compute a recursive relation, the easiest way is to define a function recursively. Without having something more concrete to work with people are groping and trying to figure out how to correctly "do some stuff with some stuff." ![]() If you aren't really certain that someone else who knows almost nothing about what is in your head will be able to read that list and be able to give you the right answer on the first try then maybe you could think of another way you could write down a set of directions on a piece of paper, hand it to someone who knows nothing about anything except how to follow directions, they would take that sheet of paper and come back in an hour with exactly what it is that you need done. If you could do that and the pattern of what you really wanted to accomplish would be clear to someone after reading this then someone might be able to come up with a few lines of code that would do it. ![]() Would it be possible for you to manually list the entries for a 4 by 4 example? Perhaps something like 1,1, function or expression to use for this entryġ,2, function or expression to use for this entryĤ,3, function or expression to use for this entryĤ,4, function or expression to use for this entry
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |