On Monday, February 15, 2016 at 7:09:19 AM UTC-8, Harald Schilly wrote:
>
> That's your problem, using "range".
>

That's *our* problem too (and probably what set Jeremy on investigating 
this: *we're* executing the matrix construction with a "range".

sage: g=lambda i,j: 0 if j==0 else i/j
sage: matrix(3,3,g)
[0 0 0]
[0 1 0]
[0 2 1]

That's due to line 608 in sage.matrix._matrix_constructor

            f = args[0]
            args[0] = [[f(i,j) for j in range(ncols)] for i in range(nrows)]

The difference between the two fragments that Jeremy posted is that in the 
example where he uses "e", the function is defined at top level, so the p,q 
are global variables, whereas in the first example the "lambda i,j: ..." is 
inside the "lambda p,q:...", so the second one is a closure (you're 
actually getting the p,q you were aiming for)

Example:

sage: def e(i,j):
....:         if i==j:
....:                 return q*(p-1)/p
....:         else:
....:                 return -q/p
....:     
sage: f = lambda p,q: matrix(p-1, p-1,e)
sage: 
sage: p,q=1000,1000
sage: f(3,2)
[999  -1]
[ -1 999]
sage: del p,q
sage: f(3,2)
NameError: global name 'q' is not defined


-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to