On 30 mar, 20:58, 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? > > 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?
To complement other answers to your questions: you can use "for" in shorter constructs than for loops. List of first n terms: sage: n = 12; [1/k^2 for k in xrange(1,n+1)] [1, 1/4, 1/9, 1/16, 1/25, 1/36, 1/49, 1/64, 1/81, 1/100, 1/121, 1/144] As a function: sage: def first_n_terms(n): ....: return [1/k^2 for k in xrange(1,n+1)] ....: sage: first_n_terms(12) [1, 1/4, 1/9, 1/16, 1/25, 1/36, 1/49, 1/64, 1/81, 1/100, 1/121, 1/144] Iterator for first n terms: sage: def first_n_terms(n): ....: return (1/k^2 for k in xrange(1,n+1)) ....: sage: first_n_terms(12) <generator object <genexpr> at 0xae62170> Partial sum: sage: n = 20; sum(1/k^2 for k in xrange(1,n+1)) 17299975731542641/10838475198270720 As a function: sage: def partial_sum(n): ....: return sum(1/k^2 for k in xrange(1,n+1)) ....: sage: partial_sum(20) 17299975731542641/10838475198270720 -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org