#! /usr/bin/env # def lesson00 ( ): #*****************************************************************************80 # ## lesson00 is a brief introduction to python. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 November 2015 # # Author: # # Initial version by Lorena Barba # This version by John Burkardt # #------------------------------------------------------------------------------- # IMPORTING #------------------------------------------------------------------------------- # # Import numpy, which is useful for working with arrays. # import numpy # # Import the pyplot plotting library. # from matplotlib import pyplot # #------------------------------------------------------------------------------- # USING A FUNCTION FROM AN IMPORTED LIBRARY #------------------------------------------------------------------------------- # # The numpy "linspace(a,b,n)" function creates an array of n # equally spaced values between a and b. We can use linspace if # we import numpy (which we did) and indicate that linspace is # a member of the numpy library (which we do by writing the name # of the library, followed by a dot, followed by "linspace". # myarray = numpy.linspace ( 0, 5, 10 ) # # Typing the name of the array causes it to be printed out. # myarray # # If we had forgotten the "numpy" preface, the command would not work, # and python would issue an error report. # myarray = linspace ( 0, 5, 10 ) # #------------------------------------------------------------------------------- # VARIABLES ARE AUTOMATICALLY TYPED #------------------------------------------------------------------------------- # # Variables have types (real, integer, string) but python allows you to # define variables without declaring a type, and it will usually guess # what you meant. # a = 5 b = 'five' c = 5.0 # # The type() function will the type of a variable. # type ( a ) type ( b ) type ( c ) # # Arithmetic operators are available like + - * and /. # Exponentiation uses the ** symbol. # One peculiarity arises if you divide an integer by an integer. # In python 2, the result is an integer, but in python 3 it is a float. # d = 13 / 5 # #------------------------------------------------------------------------------- # WHITESPACE #------------------------------------------------------------------------------- # # Python uses indenting to group statements together. A python "for" loop # will repeat each statement in the indented block that follows. # for i in range ( 5 ) print 'Hi!' # <-- This statement is in the loop print 'Hello!' # <-- So is this one. print 'Howdy!' # <-- Not part of the loop. # # Loops can be nested. # for i in range ( 3 ) for j in range ( 4 ) print ( i, j ) print 'This statement is part of the i loop, but not the j loop.' print 'This statement is not part of either loop.' #------------------------------------------------------------------------------- # VECTORS ARE NUMPY ARRAYS #------------------------------------------------------------------------------- # # Numpy includes functions zeros() and ones() that allow you to create a # vector of zeros or ones of a given size. You can also create a vector # by giving its elements in a list enclosed in square brackets. # u = numpy.zeros ( 5 ) u v = numpy.ones ( 3 ) v w = numpy.array ( [ 1, 2, 3, 4, 5 ] ) w # # You can access a particular entry of an array using the array name # followed by the index in square brackets. Python uses 0-based indexing. # w[0] w[1] = 99 w[2] = 2 * w[3] + w[4] w #------------------------------------------------------------------------------- # VECTOR OPERATIONS #------------------------------------------------------------------------------- # # If V is a vector, then V[2:4] is the vector created from entries 2, 3, and 4. # v = linspace ( 0.0, 1.0, 11 ) v[2:4] # # numpy includes a sum function for vectors. # numpy.sum ( v ) numpy.max ( v ) numpy.min ( v ) numpy.norm ( v ) # # numpy dot() can be used to compute the dot product. # w = ones ( 11 ) d = numpy.dot ( v, w ) # # The norm of a vector v is the square root of v' * v. # We can get a sqrt() function from numpy. # d = numpy.sqrt ( numpy.dot ( v, v ) ) # #------------------------------------------------------------------------------- # VECTOR ERRORS #------------------------------------------------------------------------------- # # If W contains 5 elements, indexed 0 through 4, and we request W[5], # we generate an "out of bounds" error. # w = linspace ( 11, 15, 5 ) w[5] # # If A is a vector, the command A = 7 does NOT set every element of A to 7. # If changes A to a scalar! # a = linspace ( 1, 5, 5 ) a a = 7 a # # If A and B are vectors, the command A = B does not copy the elements of # B into A. Instead, it makes the names A and B point to the same values. # This is almost never what you want! # a = linspace ( 1, 5, 5 ) a b = linspace ( 6, 10, 5 ) b a = b a # # Everything seems fine. But now, if we change A[0], we also change B[0], # because they have become two names for the same thing. # a[0] = 2001 b # # The right way to do this is to use the copy() function. # Now we can copy values from B into A, then change an entry of A # without affecting B. # a = linspace ( 1, 5, 5 ) a b = linspace ( 6, 10, 5 ) b a = b.copy ( ) a a[0] = 2001 b # #------------------------------------------------------------------------------- # MATRICES ARE NUMPY ARRAYS #------------------------------------------------------------------------------- # # You can create a matrix by using a pair of dimensions in square brackets. # u = numpy.zeros ( [ 2, 3 ] ) u v = numpy.ones ( [ 2, 4 ] ) v w = numpy.array ( [ 11, 12 ], [ 21, 22 ], [ 31, 32 ] ) w # # Any entry can be accessed using [row,col] indexing. # w[2,0] #------------------------------------------------------------------------------- # MATRIX OPERATIONS #------------------------------------------------------------------------------- # a = numpy.array ( [ 1, 2], [ 3, 4 ] ) x = numpy.array ( [ 5, 6 ] ) b = numpy.dot ( a, x ) #------------------------------------------------------------------------------- # RANDOMIZATION #------------------------------------------------------------------------------- # for i in range ( 0, 5 ): a = numpy.random ( ) a v = random ( 5 ) v a = random ( [ 3, 5 ] ) a return