Ask Your Question

Revision history [back]

sage: import numpy as np
sage: a = np.array([[1.r, 2.r, 3.r],   # use 'r' to input python floats (which numpy
                    [4.r, 5.r, -2.r],  # converts to numpy floats) rather than Sage
                    [6.r, 7.r, 9.r]])  # reals (which numpy would treat as objects)
sage: print a
[[ 1.  2.  3.]
 [ 4.  5. -2.]
 [ 6.  7.  9.]]
sage: a[1] = np.ones(3) # replace row 1 by ones
sage: print a
[[ 1.  2.  3.]
 [ 1.  1.  1.]
 [ 6.  7.  9.]]
sage: a[:,1] = np.zeros(3) # replace column 1 by zeros
sage: print a
[[ 1.  0.  3.]
 [ 1.  0.  1.]
 [ 6.  0.  9.]]
sage: a[0] = [3.,3.,6.] # replace row 0 by [3., 3., 6.] ('r' no longer needed)
sage: print a
[[ 3.  3.  6.]
 [ 1.  0.  1.]
 [ 6.  0.  9.]]
sage: type(a[0,1]) # Sage reals were converted to numpy floats
<type 'numpy.float64'>
sage: import numpy as np
sage: a = np.array([[1.r, np.array(
            [[1.r, 2.r, 3.r],   # use 'r' to input python floats (which numpy
              [4.r, 5.r, -2.r],  # converts to numpy floats) rather than Sage
              [6.r, 7.r, 9.r]])  # reals (which numpy would treat as objects)
sage: print a
[[ 1.  2.  3.]
 [ 4.  5. -2.]
 [ 6.  7.  9.]]
sage: a[1] = np.ones(3) # replace row 1 by ones
sage: print a
[[ 1.  2.  3.]
 [ 1.  1.  1.]
 [ 6.  7.  9.]]
sage: a[:,1] = np.zeros(3) # replace column 1 by zeros
sage: print a
[[ 1.  0.  3.]
 [ 1.  0.  1.]
 [ 6.  0.  9.]]
sage: a[0] = [3.,3.,6.] # replace row 0 by [3., 3., 6.] ('r' no longer needed)
sage: print a
[[ 3.  3.  6.]
 [ 1.  0.  1.]
 [ 6.  0.  9.]]
sage: type(a[0,1]) # Sage reals were converted to numpy floats
<type 'numpy.float64'>
sage: import numpy as np
sage: a = np.array(
            [[1.r, 2.r, 3.r],   # use 'r' to 

To initialize the array, input python floats (which numpy (with trailing r): numpy converts them to numpy floats while it would treat Sage reals as Python objects.

sage: a = np.array([[1.r, 2.r, 3.r], [4.r, 5.r, -2.r],  # converts to numpy floats) rather than Sage
             [6.r, -2.r],[6.r, 7.r, 9.r]])  # reals (which numpy would treat as objects)
9.r]])
sage: print a
[[ 1.  2.  3.]
 [ 4.  5. -2.]
 [ 6.  7.  9.]]
sage: a[1] = np.ones(3) # replace row 

Replace line 1 by ones ones.

sage: a[1] = np.ones(3)
sage: print a
[[ 1.  2.  3.]
 [ 1.  1.  1.]
 [ 6.  7.  9.]]
sage: a[:,1] = np.zeros(3) # replace 

Replace column 1 by zeros zeros.

sage: a[:,1] = np.zeros(3)
sage: print a
[[ 1.  0.  3.]
 [ 1.  0.  1.]
 [ 6.  0.  9.]]
sage: a[0] = [3.,3.,6.] # replace row 

Replace line 0 by [3., 3., 6.] ('r' chosen values. Trailing r no longer needed) sage: print a [[ 3. 3. 6.] [ 1. 0. 1.] [ 6. 0. 9.]] sage: type(a[0,1]) # needed, numpy converts Sage reals were converted to numpy floats <type 'numpy.float64'> the type of other elements of the array.

sage: a[0] = [3.,3.,6.]
sage: print a
[[ 3.  3.  6.]
 [ 1.  0.  1.]
 [ 6.  0.  9.]]