On Wed, 30 Mar 2011 at 11:58AM -0700, ObsessiveMathsFreak wrote: > I'm just wondering if there is a canonical (i.e. convienient(i.e. > lazy)) way to define simple sequences and series in sage. In > particular, is there a standard way to define recursive series? > > Suppose for example that You wanted to define the series a_n=1/n^2. Is > there a way to do this without writing a for loop?
def a(n): return 1/n^2 Or, if you really want it to fit on a single line: a = lambda n: 1/n^2 > And moreover, suppose you wanted to define the recursive sequences > a_n=(n^2+2)a_{n-1}. Is there a way to do this automatically? There's no automatic way that I know of, but it's easy enough with a regular Python function: def a(n): if n == 0: return whatever else: return (n^2 + 2) * a(n - 1) I can't imagine it getting much simpler than that. If you are calling that function a lot, you can speed it up by using the @cached_function decorator. Dan -- --- Dan Drake ----- http://mathsci.kaist.ac.kr/~drake -------
signature.asc
Description: Digital signature