Hi Ben and Justin, On 15 Mrz., 05:38, "Justin C. Walker" <[email protected]> wrote: > On Mar 14, 2011, at 21:24 , Ben123 wrote: > > I was hoping I wouldn't need to make that many changes to the python > > code, but this would seem to indicate Sage and Python aren't 1-to-1.
Perhaps you think of a matrix as a list of lists, but this is not the case: A matrix is a 2-dimensional object, and thus the key needed to access an element is formed by two numbers. When you do M[k] with k = (1,3) or when you do M[1,3], then Python in fact calls M.__getitem__(k): sage: M.__getitem__((1,3)) == M[(1,3)] == M[1,3] True So, "M[1,3]" is simply how Python works. In particular, if you want to read out the value of a mark in the matrix, you should *not* do M[3][4], because M[3] first creates a vector and then ...[4] reads out the 4th item of that vector -- that's a waste of time. Note that slicing etc. also works fine: sage: MS = MatrixSpace(ZZ,4,4) sage: M = MS.random_element() sage: M [ -1 1 -2 0] [ -1 0 -1 -3] [ 1 1 0 0] [-57 1 1 -1] sage: M[1,2:4] [-1 -3] sage: M[1:3,2:4] [-1 -3] [ 0 0] > They definitely are not the same. Among other things, Sage has a preparser > that converts "sage" syntax into python syntax. The way of setting/getting elements of a matrix has nothing to do with the preparser. The preparser is involved when you do things like sage: R.<a,b,c,d> = ZZ[] sage: R Multivariate Polynomial Ring in a, b, c, d over Integer Ring sage: f(x) = sin(x) sage: f x |--> sin(x) Cheers, Simon -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
