NumPy .edu

[Pages:17]NumPy

Computational Physics NumPy

Outline

Some Leftovers

Get people on line! Write a function / Write a script

NumPy

NumPy Arrays; Indexing; Iterating Creating Arrays Basic Operations Copying Linear Algebra

Calculate Something

Basic python doesn't know much ...

We must "import" useful functions from the "math" module:

> from math import sin imports the "sin" function

To call the sin function: sin(theta)

> from math import * imports all math functions

To call the sin function: sin(theta)

} > import math

imports the math module

To call the sin function: math.sin(theta)

My Advice: USE

> import math as m

imports math, calls it "m"

THESE

To call the sin function: m.sin(theta)

Write a function

In [10]: import math

In [11]: def sind(theta): ....: t = theta/180.*math.pi ....: return(math.sin(t)) ....:

In [12]: sind(90) Out[12]: 1.0

Return when done with defi nition

Write a script

Use Text Editor to create a fi le with python commands.

In this example: myscript.py

##if filele: :mmyyssccrirpipt.tp.pyy AA=='H'HeellloloWWoorlrdld!'!' pprirnint tAA

To run the script enter:

In [13]: run myscript.py Out[13]: Hello World!

Try some stuff out!

Defne some numbers and operate on them (+,-,*,/)

Build a long string by concatenating short strings

Select substrings in your long string using the : operator.

Write a "degree input" function to compute cosine.

Write a fle with all trig functions ready to accept degree arguments (e.g. sind, cosd, tand, atand, atan2d)

About NumPy

Python Lists

Lists can have any type of data

Locations of List items in memory is not predictable

This limits mathematical use of Lists as arrays.

NumPy Arrays

All data have same type

All data are together in memory

List Data NumPy Array Data

Learning about NumPy

See: wiki.Tentative_NumPy_Tutorial

NumPy creates a homogeneous, multidimensional, array.

Elements all have same type.

Each dimension of the array is called an axis.

Number of axes is called the "rank" of the array.

Positive numbers are used to index the data in the array, beginning with index 0, with one number for each axis of the array.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download