Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Numpy: Dense Numerical Matrices, Study notes of Mathematics

An overview of Numpy arrays, which are vastly different than Sage matrices. Numpy arrays have many similarities with matlab arrays and can have any number of dimensions. They can be initialized with a specified data type and can be reshaped, sliced, and have various attributes such as flags, shape, strides, ndim, data, size, itemsize, ctypes, and __array_interface__.

What you will learn

  • How can Numpy arrays be initialized with a specified data type?
  • What are the similarities between Numpy arrays and matlab arrays?
  • What are some of the attributes of Numpy arrays?
  • What are Numpy arrays and how do they differ from Sage matrices?

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

birkinshaw
birkinshaw 🇺🇸

4.7

(9)

239 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sage Worksheet: math 480 - 20080519 - numpy http://localhost:8000/home/admin/152/print
1 of 22 5/19/08 12:10 PM
math 480 - 20080519 - numpy
%hide
%html
<font size=+1>
<h1>Math 480 -- 20080519 -- numpy</h1>
<h1>Numpy: Dense Numerical Matrices</h1>
<h2>Documentation</h2>
The <a target="_new" href="numpybook.pdf">Guide to NumPy</a> is
Travis Oliphant's book about numpy. The description of
functions in this worksheet draws heavily on that.
<h2>Introduction</h2>
Numpy arrays are <i>vastly different</i> than Sage matrices, to put
it mildly. Essentially
every rule, constraint, convention, etc., is different. They
server a very different purpose.
They are meant to be the ultimate Python general $n$-dimensional
dense array object. In contrast, Sage matrices are meant to be
very mathematical objects with clear mathematically meaningful
semantics and very fast algorithms mainly for algebraic
computations.
<br><br>
Numpy array have many similarities with matlab arrays, given that
they have very similar computational domains.
See <a href="http://www.scipy.org/NumPy_for_Matlab_Users">Numpy for
Matlab Users</a> for a table that compares
and contrasts their functions. That page also has an excellent
table showing equivalent commands in Matlab and Numpy.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Numpy: Dense Numerical Matrices and more Study notes Mathematics in PDF only on Docsity!

math 480 - 20080519 - numpy

%hide

%html

Math 480 -- 20080519 -- numpy

Numpy: Dense Numerical Matrices

Documentation

The Guide to NumPy is

Travis Oliphant's book about numpy. The description of

functions in this worksheet draws heavily on that.

Introduction

Numpy arrays are vastly different than Sage matrices, to put

it mildly. Essentially

every rule, constraint, convention, etc., is different. They

server a very different purpose.

They are meant to be the ultimate Python general $n$-dimensional

dense array object. In contrast, Sage matrices are meant to be

very mathematical objects with clear mathematically meaningful

semantics and very fast algorithms mainly for algebraic

computations.



Numpy array have many similarities with matlab arrays, given that

they have very similar computational domains.

See Numpy for

Matlab Users for a table that compares

and contrasts their functions. That page also has an excellent

table showing equivalent commands in Matlab and Numpy.

Math 480 -- 20080519 -- numpy

Numpy: Dense Numerical

Matrices

Documentation

The Guide to NumPy is Travis Oliphant's book about numpy. The

description of functions in this worksheet draws heavily on that.

Introduction

Numpy arrays are vastly different than Sage matrices, to put it mildly.

Essentially every rule, constraint, convention, etc., is different. They

server a very different purpose. They are meant to be the ultimate

Python general -dimensional dense array object. In contrast, Sage

matrices are meant to be very mathematical objects with clear

mathematically meaningful semantics and very fast algorithms mainly

for algebraic computations.

Numpy array have many similarities with matlab arrays, given that they

have very similar computational domains. See Numpy for Matlab Users

for a table that compares and contrasts their functions. That page also

has an excellent table showing equivalent commands in Matlab and

Numpy.

import numpy

%hide

%html

Create a numpy array as follows. For numeric types, one can and

n

s += 'r'*ncols + '}\n'

for i in range(nrows):

s += ' & '.join([latex(A[i,j]) for j in range(ncols)]) +

'\\\n'

s += r'\end{array}\right)'

html('$%s$'%s)

shownp(Z)

shownp(numpy.dot(Z,Z))

An 1-d array

A = numpy.array([1..10])

shownp(A)

%hide

%html

Numpy arrays can have any number of dimensions!

Think of these higher dimensional arrays as arrays of arrays (of

arrays...). They are useful, e.g., in image processing, where

there is one array for each color channel.

Numpy arrays can have any number of dimensions! Think of these

higher dimensional arrays as arrays of arrays (of arrays...). They are

useful, e.g., in image processing, where there is one array for each color

channel.

p

2 + 3

Ò

Ó

3

2

1 A 0 B B B B B B @

B

p

2 + 3

Ñ 2

p

2 + 3

Ñ 2

C

A

p

2 + 3

Ñ

Ò

0 B B B B B B B B B B B B B B B B @

1 C C C C C C C C C C C C C C C C A

M = numpy.array([[[1,2], [3,4]], [[1/3,5], [10,2]], [[8,1],

[-5,2]]], dtype=int)

M

array([[[ 1, 2],

[ 3, 4]],

[[ 0, 5],

[10, 2]],

[[ 8, 1],

[-5, 2]]])

The actual array entries are NOT Python int's!

type(M[0,0,0])

<type 'numpy.int32'>

M[0,0,0].item()

shownp(M)

dtype: A data-type object that fully describes each

fixed-length item in the array.

The dtype attribute can be set to anything that can be

interpreted as a data type (numpy includes 21 of these).

Setting this attribute allows you to change the interpretation of

the data in the array. The new data type must be compatible with

the array%u2019s

current data type.

M.dtype

dtype('int32')

M.dtype = float

shownp(M)

Ò

Ó

Ò

Ó

Ò

À 5

Ó

Ò

4 : 24399158242 e À 314

8 : 48798316534 e À 314

Ó

Ò

1 : 06099789548 e À 313

4 : 24399158687 e À 314

Ó

Reshape makes a reshaped copy:

shownp(M.reshape((4,3)))

M.shape

strides: (settable) tuple showing how many bytes must be jumped

in

the data segment to get from one entry to the next

M.strides

ndim: (not settable) number of dimensions in array

M.ndim

data: (settable) bu%uFB00er object loosely wrapping the array

data

(only works for single-segment arrays)

d = M.data; d

<read-write buffer for 0x7cc3470, size 48, offset 0 at 0x7fda800>

d[0:10]

'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00'

type(d)

<type 'buffer'>

size: (not settable) total number of elements

M.size

itemsize: (settable) one-dimensional, indexable iterator

object that acts somewhat like a 1-d array

M.flat

<numpy.flatiter object at 0xe83e00>

len(M.flat)

list(M.flat)

[1, 2, 3, 4, 0, 5, 10, 2, 8, 1, -5, 2]

M.flat[0] = 20090283

B

B

À 5

C

C

A

shownp(M)

ctypes: (not settable) object to simplify the interaction of

this array with the ctypes module

M.ctypes

<numpy.core._internal._ctypes object at 0x7fda7d0>

type(M.ctypes)

<class 'numpy.core._internal._ctypes'>

M.ctypes.data

array_interface: (not settable) dictionary with keys (data,

typestr, descr, shape, strides) for compliance with Python side

of array protocol

M.array_interface

{'descr': [('', '<i4')], 'strides': None, 'shape': (3, 2, 2),

'version': 3, 'typestr': '<i4', 'data': (130823360, False)}

array_struct: (not settable) array interface on C-level

M.array_struct

<PyCObject object at 0x7a16740>

array_priority: (not settable) always 0.0 for base type

ndarray

M.array_priority

%hide

%html

Ò

Ó

Ò

Ó

Ò

À 5

Ó

type(A[0,0])

<type 'numpy.float64'>

A[0,0] = 10; shownp(A)

A.flags

C_CONTIGUOUS : True

F_CONTIGUOUS : False

OWNDATA : True

WRITEABLE : True

ALIGNED : True

UPDATEIFCOPY : False

A.flags['WRITEABLE'] = False

A.flags

C_CONTIGUOUS : True

F_CONTIGUOUS : False

OWNDATA : True

WRITEABLE : False

ALIGNED : True

UPDATEIFCOPY : False

A[0,0] = 10

Traceback (click to the left for traceback)

...

RuntimeError: array is not writeable

Mutability (writeability) is not strictly enforced once set,

since you can trivially change it to true, make a write, then

change the flag back.

before = A.flags['WRITEABLE']

A.flags['WRITEABLE'] = True

A[0,0] = 20

A.flags['WRITEABLE'] = before

shownp(A)

This is different than Sage matrix mutability!

B = matrix(RDF, 3, 3, [1..9])

B.set_immutable()

A

A

A

B can't be changed back to being mutable...

B[0,0] = 20

Sage is much more about enforcing rigor, etc., since a lot of

subtle

mathematical errors in complicated algebraic algorithms can arise

from the lax (but more user-friendly?) numpy approach.

Traceback (click to the left for traceback)

...

ValueError: matrix is immutable; please change a copy instead (use

self.copy()).

numpy arrays can be serialized and saved to disk

save(M, DATA+'M.sobj')

print M.dumps()

(M == loads(M.dumps())).all()

€cnumpy.core.multiarray

_reconstruct

qcnumpy

ndarray

qK?…U‡Rq(KKKK‡cnumpy

dtype

qUi4K?K‡Rq(KU<NNNJÿÿÿÿJÿÿÿÿK?tb‰U0?ʏ\

33;????????\

3;????????&\

????????\

5533;??ûÿÿÿ???tb.

True

load(DATA+'M.sobj')

array([[[ 1, 2],

[ 3, 4]],

[[ 0, 5],

[10, 2]],

[[ 8, 1],

[-5, 2]]])

%hide

%html

Converting between Numpy and Sage Matrices

Converting between Numpy and

type(B)

<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>

M = matrix(CDF, 3, [1,2,3.4, 1.2, 2.5, -1.3, 2+I,2-I,e]); M

[ 1.0 2.0 3.4]

[ 1.2 2.5 -1.3]

[ 2.0 + 1.0I 2.0 - 1.0I 2.71828182846]

A = M.numpy(); A

array([[ 1.00000000+0.j, 2.00000000+0.j, 3.40000000+0.j],

[ 1.20000000+0.j, 2.50000000+0.j, -1.30000000+0.j],

[ 2.00000000+1.j, 2.00000000-1.j, 2.71828183+0.j]])

A.dtype

dtype('complex128')

%hide

%html

Numpy Arrays Having an Amazingly Rich Range of

Functionality

Numpy Arrays Having an

Amazingly Rich Range of

Functionality

A = random_matrix(RDF,3).numpy()

shownp(A)

max: return the largest value in A. Do not use max(A)...

A.max()

max(A)

À 0 : 621060987001

À 0 : 900640346182

À 0 : 152539619277

À 0 : 555126544917

À 0 : 602351280862

A

Traceback (click to the left for traceback)

...

ValueError: The truth value of an array with more than one element

is ambiguous. Use a.any() or a.all()

min: Return the smallest value in A. Much better than min(A)

A.min()

ptp: Difference of the largest to the smallest value of A

A.ptp()

clip: Return a new array where any element in A less than min is

set to min

and any element less than max is set to max.

shownp(A.clip(min=-0.5, max=0.5))

conj: return a new array with each of the elements of the input

array

replaced by their complex conjugate

B = random_matrix(CDF, 3).numpy()

print "Matrix B = ",

shownp(B)

print "\nComplex conjugate = ",

shownp(B.conj())

Matrix B =

Complex conjugate =

shownp(A)

shownp(A.round())

shownp(A)

A.trace()

À 0 : 500000000000000

À 0 : 500000000000000

À 0 : 152539619277

À 0 : 500000000000000

À 0 : 500000000000000

A

(0.476948747614-0.694197532337j)

(-0.106544963575-0.879795738338j)

(-0.37540094272+0.835285088525j)

(0.476948747614+0.694197532337j)

(-0.106544963575+0.879795738338j)

(-0.37540094272-0.835285088525j)

À 0 : 621060987001

À 0 : 900640346182

À 0 : 152539619277

À 0 : 555126544917

À 0 : 602351280862

A

À 1 : 0

À 1 : 0

À 0 : 0

À 1 : 0

À 1 : 0

A

shownp(A)

A.var()

std -- return the standard deviation of the entries in the matrix

This is the square root of the variance.

shownp(A)

A.std()

sqrt(A.var())

prod -- product of all entries of self.

A.prod()

all -- all entries bool(...) to True

A.all()

True

B = A.copy(); B[0,0] = 0

B.all()

False

all -- any entry bool(...) to True

A.any()

True

%hide

%html

Summary of Array Calculation Methods

Summary of Array Calculation Methods

(a )

N

X

NÀ 1

i=

i

À Ö

2

À 0 : 621060987001

À 0 : 900640346182

À 0 : 152539619277

À 0 : 555126544917

À 0 : 602351280862

A

À 0 : 621060987001

À 0 : 900640346182

À 0 : 152539619277

À 0 : 555126544917

À 0 : 602351280862

A

Lots of other functions that aren't methods!

import numpy

numpy.cov -- compute the covariance matrix of data

shownp(numpy.cov(A))

shownp(numpy.corrcoef(A))

À 0 : 0620073609308

À 0 : 0620073609308

A

%hide

%html

Array Slicing

Numpy (and Sage) both have sophisticated array slicing somewhat

like Matlab.

Array Slicing

Numpy (and Sage) both have sophisticated array slicing somewhat like

Matlab.

shownp(A)

shownp(A[:2,])

À 0 : 621060987001

À 0 : 900640346182

À 0 : 152539619277

À 0 : 555126544917

À 0 : 602351280862

A

Ò

À 0 : 621060987001

À 0 : 152539619277

À 0 : 602351280862

Ó

shownp(A[:3,0:2])

shownp(A[1:3,0:2])

shownp(A[1:3,1:3])

B = matrix(A); show(B) # sage matrix

show(B[1:3, 1:3])

show(B[:3, 0:2])

%hide

%html

A key difference between numpy and sage slicing, is that in

Sage the slices are new copies, but in numpy they are references

into the original matrix!

A key difference between numpy and sage slicing, is that in Sage the

slices are new copies, but in numpy they are references into the original

matrix!

shownp(A)

B = A[0:2,0:2]

À 0 : 621060987001

À 0 : 900640346182

À 0 : 152539619277

À 0 : 555126544917

A

Ò

À 0 : 621060987001

À 0 : 900640346182

À 0 : 152539619277

À 0 : 555126544917

Ó

Ò

À 0 : 152539619277

À 0 : 555126544917

À 0 : 602351280862

Ó

À 0 : 621060987001

À 0 : 900640346182

À 0 : 152539619277

À 0 : 555126544917

À 0 : 602351280862

A

Ò

À 0 : 152539619277

À 0 : 555126544917

À 0 : 602351280862

Ó

À 0 : 621060987001

À 0 : 900640346182

À 0 : 152539619277

À 0 : 555126544917

A