PythonQ 240-8XX User Manual

Browse online or download User Manual for Welding System PythonQ 240-8XX. Numerical Python [en]

  • Download
  • Add to my manuals
  • Print

Summary of Contents

Page 1 - Numerical Python

c! www.simula.no/˜hplNumerical PythonNumerical Python – p. 235/728

Page 2 - Contents

c! www.simula.no/˜hplWarning: arange is dangerousarange’s upper limit may or may not be included (due to round-offerrors)Better to use a safer method:

Page 3 - More info

c! www.simula.no/˜hplArray construction from a Python listarray(list, [datatype]) generates an array from a list:>>> pl = [0, 1.2, 4, -9.1, 5

Page 4 - Numerical Python (NumPy)

c! www.simula.no/˜hplFrom “anything” to a NumPy arrayGiven an object a,a=asarray(a)converts a to a NumPy array (if possible/necessary)Arrays can be or

Page 5

c! www.simula.no/˜hplChanging array dimensions>>> a = array([0, 1.2, 4, -9.1, 5, 8])>>> a.shape = (2,3) # turn a into a 2x3 matrix&g

Page 6 - Resulting plot

c! www.simula.no/˜hplArray initialization from a Python f u n ction>>> def myfunc(i, j):... return (i+1)*(j+4-i)...>>> # make 3x6 ar

Page 7 - Making arrays

c! www.simula.no/˜hplBasic array indexingNote: all integer indices in Python start at 0!a=linspace(-1,1,6)a[2:4] = -1 # set a[2] and a[3] equal to -1a

Page 8

c! www.simula.no/˜hplMore advanced array indexing>>> a = linspace(0, 29, 30)>>> a.shape = (5,6)>>> aarray([[ 0., 1., 2., 3.

Page 9

c! www.simula.no/˜hplSlices refer the array dataWith a as list, a[:] makes a copy of the dataWith a as array, a[:] is a reference to the data>>&

Page 10 - Warning: arange is dangerous

c! www.simula.no/˜hplLoops over arrays (1)Standard loop over each element:for i in xrange(a.shape[0]):for j in xrange(a.shape[1]):a[i,j] = (i+1)*(j+1)

Page 11 - ! www.simula.no/˜hpl

c! www.simula.no/˜hplLoops over arrays (2)View array as one-dimensional and iterate over all elements:for e in a.ravel():print eUse ravel() only when

Page 12 - Given an object a

c! www.simula.no/˜hplContentsEfficient array computing in PythonCreating arraysIndexing/slicing arraysRandom numbersLinear algebraPlottingNumerical Pyt

Page 13 - Changing array dimensions

c! www.simula.no/˜hplArray computat ion sArithmetic operations can be used with arrays:b=3*a-1 #aisarray,bbecomesarray1) compute t1 = 3*a,2)computet2=

Page 14

c! www.simula.no/˜hplStandard math functions can take array arguments#letbbeanarrayc=sin(b)c=arcsin(c)c=sinh(b)#samefunctionsforthecosandtanfamiliesc=

Page 15 - Basic array indexing

c! www.simula.no/˜hplOther useful array operations#aisanarraya.clip(min=3, max=12) # clip elementsa.mean(); mean(a) # mean valuea.var(); var(a) # vari

Page 16 - More advanced array indexing

c! www.simula.no/˜hplMore useful array met hods and attributes>>> a = zeros(4) + 3>>> aarray([ 3., 3., 3., 3.]) # float data>>

Page 17 - Slices refer the array data

c! www.simula.no/˜hplModules for curve plotting and 2D/3D visualizationMatplotlib (curve plotting, 2D scalar and vector fields)PyX (PostScript/TeX-like

Page 18 - Loops over arrays (1)

c! www.simula.no/˜hplCurve plotting with EasyvizEasyviz is a light-weight interface to many plotting packages, using aMatlab-like syntaxGoal: write yo

Page 19 - Loops over arrays (2)

c! www.simula.no/˜hplBasic Easyviz examplefrom scitools.all import*#importnumpyandplottingt=linspace(0,3,51) #51pointsbetween0and3y=t**2*exp(-t**2) #

Page 20 - Array computat ion s

c! www.simula.no/˜hplDecorating the plotplot(t, y)xlabel(’t’)ylabel(’y’)legend(’t^2*exp(-t^2)’)axis([0, 3, -0.05, 0.6]) # [tmin, tmax, ymin, ymax]titl

Page 21

c! www.simula.no/˜hplThe resulting plot 0 0.1 0.2 0.3 0.4 0.5 0.6 0 0.5 1 1.5 2 2.5 3ytMy First Easyviz Demot2*exp(-t2)Numerical Python – p. 262

Page 22 - Other useful array operations

c! www.simula.no/˜hplPlotting several curves in one plotCompare f1(t)=t2e−t2and f2(t)=t4e−t2for t ∈ [0, 3]from scitools.all import*#forcurveplottingde

Page 23

c! www.simula.no/˜hplMore infoCh. 4 in the course bookwww.scipy.orgThe NumPy manualThe SciPy tutorialNumerical Python – p. 237/728

Page 24

c! www.simula.no/˜hplThe resulting plot 0 0.1 0.2 0.3 0.4 0.5 0.6 0 0.5 1 1.5 2 2.5 3ytPlotting two curves in the same plott2*exp(-t2)t4*exp(-t2

Page 25 - Curve plotting with Easyviz

c! www.simula.no/˜hplExample: plot a function given on the command lineTa s k : p l o t ( e. g . ) f(x)=e−0.2xsin(2πx) for x ∈ [0, 4π]Specify f(x) and

Page 26 - Basic Easyviz example

c! www.simula.no/˜hplPlotting 2D scalar fieldsfrom scitools.all import*x=y=linspace(-5,5,21)xv, yv = ndgrid(x, y)values = sin(sqrt(xv**2+yv**2))surf(xv

Page 27 - Decorating the plot

c! www.simula.no/˜hplAdding plot featu res#Matlabstylecommands:setp(interactive=False)surf(xv, yv, values)shading(’flat’)colorbar()colormap(hot())axis

Page 28 - The resulting plot

c! www.simula.no/˜hplThe resulting plot-1-0.8-0.6-0.4-0.2 0 0.2 0.4 0.6 0.8 1-6-4-2 0 2 4-4-2 0 2 4 6-1.5-1-0.5 0 0.5 1 1.5Numerical Python – p. 268/7

Page 29

c! www.simula.no/˜hplOther commands for visualizing 2D scalar fieldscontour (standard contours)), contourf (filled contours),contour3 (elevated contours

Page 30

c! www.simula.no/˜hplCommands for visualizing 3D fieldsScalar fields:isosurfaceslice_ (colors in slice plane),contourslice (contours in slice plane)Vect

Page 31

c! www.simula.no/˜hplMore info about EasyvizAplaintextversionoftheEasyvizmanual:pydoc scitools.easyvizThe HTML version:http://code.google.com/p/scitoo

Page 32 - Plotting 2D scalar fields

c! www.simula.no/˜hplNumerical Python (NumPy)NumPy enables efficient numerical computing in PythonNumPy is a package of modules, which offers efficient

Page 33 - Adding plot featu res

c! www.simula.no/˜hplAtasteofNumPy:aleast-squaresprocedurex=linspace(0.0,1.0,n) #coordinatesy_line = -2*x+3y=y_line+random.normal(0,0.25,n) #linewithn

Page 34

c! www.simula.no/˜hplResulting plot 1 1.5 2 2.5 3 3.5 0 0.2 0.4 0.6 0.8 1y = -1.86794*x + 2.92875: fit to y = -2*x + 3.0 + normal noisedata point

Page 35

c! www.simula.no/˜hplMaking arrays>>> from numpy import*>>> n = 4>>> a = zeros(n) # one-dim. array of length n>>>

Page 36

c! www.simula.no/˜hplMaking float, int, compl ex arrays>>> a = zeros(3)>>> print a.dtype # a’s data typefloat64>>> a = zeros

Page 37 - More info about Easyviz

c! www.simula.no/˜hplArray with a sequence of numberslinspace(a, b, n) generates n uniformly spaced coordinates,starting with a and ending with b>&

Comments to this Manuals

No comments