[sage-devel] Re: tutorial on Python functional programming for mathematicians
On Dec 15, 4:34 am, mhampton hampto...@gmail.com wrote: Thanks for pointing that out. I was looking for svd, lower-case, and missed it. actually, me too. Is there a reason why it is uppercase? I'm for renaming it to lowercase and adding an uppercase synonym for backwards compatibility. h -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
[sage-devel] Re: tutorial on Python functional programming for mathematicians
Harald Schilly wrote: On Dec 15, 4:34 am, mhampton hampto...@gmail.com wrote: Thanks for pointing that out. I was looking for svd, lower-case, and missed it. actually, me too. Is there a reason why it is uppercase? I'm for renaming it to lowercase and adding an uppercase synonym for backwards compatibility. h It has caught me before too. I'd rather that it was lowercase. So...submit a patch! :) Thanks, Jason -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
[sage-devel] Re: tutorial on Python functional programming for mathematicians
With respect to some of this discussion, see also the following sage- support request: http://groups.google.com/group/sage-support/browse_thread/thread/9e6475494f85cd53 - kcrisman -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
[sage-devel] Re: tutorial on Python functional programming for mathematicians
mhampton wrote: If you convert to numpy matrices, then Sage is pretty competitive with matlab. We still have some room for improvement in making it easy though - despite Jason Grout's improvements, a matrix over RDF is missing some methods I'd like, such as the singular value decomposition. As an example, to extend one of Minh's examples to get the smallest singular value of a bunch of matrices you'd have to do something like: sage: rand_row = lambda n: [randint(1, 10) for i in xrange(n)] sage: rand_mat = lambda nrows, ncols: [rand_row(ncols) for i in xrange (nrows)] sage: rows = [randint(1, 10) for i in xrange(10)] sage: cols = [randint(1, 10) for i in xrange(10)] sage: M = map(rand_mat, rows, cols) sage: M = map(matrix, M) sage: from numpy.linalg import svd sage: smallest_singular_values = lambda x: min(svd(x.numpy())[1]) sage: ssvM = map(smallest_singular_values,M) Or perhaps I'm missing something and that import isn't necessary. Use .SVD(): sage: a=random_matrix(RDF,4) sage: a [-0.0589149680447 -0.171553538689 0.0504191493106 -0.728358759815] [ 0.939727693257 -0.367536042534 -0.721360348176 0.0692413755066] [ -0.265450259774 -0.0414843551839 0.329102195484 -0.152151468706] [ 0.166692127509 -0.107846656469 -0.964097402323 -0.752787313591] sage: a.SVD() ( [ 0.136357448911 0.618194289876 0.67734122822 -0.374768363984] [ 0.678366688256 -0.550596271759 0.457486597867 0.165433958308] [-0.204023508917 0.263478231509 0.295912033247 0.895203718664] [ 0.692531364055 0.495236092193 -0.494318664329 0.175472450502], [ 1.590466103720.00.00.0] [ 0.0 1.052460858250.00.0] [ 0.00.0 0.4550981935190.0] [ 0.00.00.0 0.064329905844], [ 0.502396175818 -0.514242277172 0.503316286102 -0.479401375714] [-0.213107477417 0.0303771634211 -0.534627925294 -0.81721197526] [-0.765363106349 0.0357282483633 0.611065745739-0.198850351] [-0.341178254043 -0.85636183975 -0.295709911949 0.250594186611] ) sage: What apparently is missing is that matrix/matrix_double_dense.pyx has not been converted to rest yet, so the SVD function does not show up in the reference manual. Thanks, Jason -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
[sage-devel] Re: tutorial on Python functional programming for mathematicians
Jason Grout wrote: Use .SVD(): I should mention that .SVD just calls numpy/scipy for the actual computation. Jason -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
[sage-devel] Re: tutorial on Python functional programming for mathematicians
Thanks for pointing that out. I was looking for svd, lower-case, and missed it. -Marshall On Dec 14, 6:33 pm, Jason Grout jason-s...@creativetrax.com wrote: Jason Grout wrote: Use .SVD(): I should mention that .SVD just calls numpy/scipy for the actual computation. Jason -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
[sage-devel] Re: tutorial on Python functional programming for mathematicians
If you convert to numpy matrices, then Sage is pretty competitive with matlab. We still have some room for improvement in making it easy though - despite Jason Grout's improvements, a matrix over RDF is missing some methods I'd like, such as the singular value decomposition. As an example, to extend one of Minh's examples to get the smallest singular value of a bunch of matrices you'd have to do something like: sage: rand_row = lambda n: [randint(1, 10) for i in xrange(n)] sage: rand_mat = lambda nrows, ncols: [rand_row(ncols) for i in xrange (nrows)] sage: rows = [randint(1, 10) for i in xrange(10)] sage: cols = [randint(1, 10) for i in xrange(10)] sage: M = map(rand_mat, rows, cols) sage: M = map(matrix, M) sage: from numpy.linalg import svd sage: smallest_singular_values = lambda x: min(svd(x.numpy())[1]) sage: ssvM = map(smallest_singular_values,M) Or perhaps I'm missing something and that import isn't necessary. -Marshall On Dec 13, 3:31 am, David Joyner wdjoy...@gmail.com wrote: On Sat, Dec 12, 2009 at 11:09 PM, Minh Nguyen nguyenmi...@gmail.com wrote: Hi David, On Sun, Dec 13, 2009 at 11:20 AM, David Joyner wdjoy...@gmail.com wrote: SNIP I think this would fit nicely in the constructions document. Adding an example of how to use map for Sage matrices might be worth thinking about. I have provided some examples on using lambda and map() to construct matrices. Nice examples, thanks. I think one person asked me about that recently and it would have been nice to point them to something. Do you still remember the question? I don't know what's required so the matrix examples are rather simple. Here is a related non-example, which tries to create ain integer (-1,1) matrix from a GF(2) matrix in the (wrong but) obvious way: sage: A = random_matrix(GF(2), 2, 3) sage: A [0 1 1] [0 1 0] sage: f = lambda x: 2*ZZ(x)-1 sage: map(f,A) --- TypeError Traceback (most recent call last) One right way is to use the list of elements: sage: A = random_matrix(GF(2), 2, 3); A [0 1 0] [0 0 1] sage: f = lambda x: 2*ZZ(x)-1 sage: B = matrix(ZZ, 2, 3, map(f, A.list())); B [-1 1 -1] [-1 -1 1] The actual question I don't remember exactly. But we were arguing about matlab vs Sage. Going form a vague memory: My colleague was arguing that one should not teach a course in scientific programming using Sage because it was so slow in doing some very simple things. Apparently if A is a vector or matrix in matlab (ie, an array or real numbers) and f is a function (eg, a sin or a polynomial), then f(A) is both easy for the student to write and also super optimized. He doubted Sage could do that. So for large sized arrays with thousands of entries, matlab would leave Sage in the dust. My only argument, if I remember, was that his argument did not make intuitive sense. I said that though I didn't know off the top of my head how to write down the evaluation of a function on a matrix in an optimized way, there must exist one. With scipy used in industry for serious number-crunching, it must be possible. Of course an example would be more convincing that an existence argument:-) An example which would be both easy for the student to perform (and also fast) would be nice, -- Regards Minh Van Nguyen -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group athttp://groups.google.com/group/sage-devel URL:http://www.sagemath.org -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
[sage-devel] Re: tutorial on Python functional programming for mathematicians
On Dec 13, 1:02 am, Minh Nguyen nguyenmi...@gmail.com wrote: I have written a draft of a tutorial on functional programming for mathematicians. Very nice, i added a comment and I just repeat it here. I think you should also point to the operator python package, since defining add in a function is an overhead and helpers like itemgetter is also quite common. you should also point to the itertools package. e.g. an inner product is more efficient using sum(imap(operator.mul, vector1, vector2)) according to the itertools page - w/o sage of course ;) h -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
[sage-devel] Re: tutorial on Python functional programming for mathematicians
Thanks a lot for this tutorial. I think it would be great to have it included in the documentation. I did not know anything about functional programming before using sage. To a new mathematical user without python knowledge things like lambda = 4 SyntaxError: invalid syntax lambda? No object 'lambda' currently defined. are very odd. -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
[sage-devel] Re: tutorial on Python functional programming for mathematicians
Minh Nguyen wrote: Hi folks, I have written a draft of a tutorial on functional programming for mathematicians. The tutorial is available on the Sage wiki [1], but you could also find it at Wordpress [2]. This is a redundancy measure in case the wiki is down. I invite you to comments on it or expand it as necessary. If you want, I could open a ticket to add the tutorial to the Constructions document [3] or the Developers' Guide [4]. This is great. Do you think you could add a sentence or two in the filter section about using list comprehensions to do filters, like you did above in the map section? Python people are probably going to see a lot of [f(x) for x in [1..10] if f(x)0] type of statements, so having the connection to filter in their mind would be nice. Thanks, Jason -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
[sage-devel] Re: tutorial on Python functional programming for mathematicians
On Dec 12, 4:24 pm, Jason Grout jason-s...@creativetrax.com wrote: [f(x) for x in [1..10] if f(x)0] This is actually bad style. It means that f gets evaluated twice for all the values that end up in the list. The magma language solves this with modified semantics for the where clause. One would write: [ v : x in [1..10] | v gt 0 where v:=f(x)] The point being that for every iteration of x, the where clause supplies a variable local to the condition which survives into the value computing part. Does python have a similar construct? I guess a nested construction with iterators might come close: [v for v in ( f(x) for x in [1..10] ) if v 0] but I don't know what the overhead is in that construction. -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
[sage-devel] Re: tutorial on Python functional programming for mathematicians
Nils Bruin wrote: On Dec 12, 4:24 pm, Jason Grout jason-s...@creativetrax.com wrote: [f(x) for x in [1..10] if f(x)0] This is actually bad style. It means that f gets evaluated twice for all the values that end up in the list. Good point. The magma language solves this with modified semantics for the where clause. One would write: [ v : x in [1..10] | v gt 0 where v:=f(x)] The point being that for every iteration of x, the where clause supplies a variable local to the condition which survives into the value computing part. Does python have a similar construct? I guess a nested construction with iterators might come close: [v for v in ( f(x) for x in [1..10] ) if v 0] but I don't know what the overhead is in that construction. There's apparently a lot more overhead than I would have imagined: sage: %timeit [v for v in (x for x in range(10) ) if v 0] 10 loops, best of 3: 5.4 盜 per loop sage: %timeit [v for v in range(10) if v0] 10 loops, best of 3: 2.98 盜 per loop Interestingly, the builtin filter function isn't much faster than the slow version above: sage: %timeit filter(lambda x: x0, range(10)) 10 loops, best of 3: 5.3 盜 per loop and a hand-coded filter is about the same speed: 1 def fltr(lst): 2 result=[] 3 for x in lst: 4 v = x 5 if v0: 6 result.append(v) 7 return result sage: %timeit fltr(range(10)) 10 loops, best of 3: 5.18 盜 per loop Thanks, Jason -- Jason Grout -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org
Re: [sage-devel] Re: tutorial on Python functional programming for mathematicians
Hi Jason, On Sun, Dec 13, 2009 at 11:24 AM, Jason Grout jason-s...@creativetrax.com wrote: SNIP Do you think you could add a sentence or two in the filter section about using list comprehensions to do filters, like you did above in the map section? The tutorial is updated with such an example. Say the problem is to generate all positive integers between 1 and 20, inclusive, each of which is relatively prime to 20. The conditional involves testing whether two integers are coprime to each other. First, a solution is provided using list comprehension. A second solution is provided using filter(). -- Regards Minh Van Nguyen -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org