By. What advantages has it? The Reasons for Choosing Python  Python is free  It is object-oriented  It is interpreted  It is operating-system independent.

Slides:



Advertisements
Similar presentations
Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party.
Advertisements

MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Intro to Python Welcome to the Wonderful world of GIS programing!
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
By Hrishikesh Gadre Session II Department of Mechanical Engineering Louisiana State University Engineering Equation Solver Tutorials.
MathCAD Data. Data in tables Tables are analogous to matrix The numbers of columns and rows can be dynamically changed (in contrast to matrix) To enter.
Introduction to Python
Guide To UNIX Using Linux Third Edition
Python plotting for lab folk Only the stuff you need to know to make publishable figures of your data. For all else: ask Sourish.
Data processing in MathCAD. Data in tables Tables are analogous to matrices Tables are analogous to matrices The numbers of columns and rows can be dynamically.
A First Program Using C#
Python Programming Fundamentals
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Munster Programming Training
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Computer Science 111 Fundamentals of Programming I Overview of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Scientific Computing with NumPy & SciPy NumPy Installation and Documentation  Not much on the home page—don’t buy the guide, it’s.
Eng Ship Structures 1 Introduction to Matlab.
Khoros Yongqun He Dept. of Computer Science, Virginia Tech.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Computer Science 101 Introduction to Programming.
Chapter 2: First Steps in MuPAD MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Derivatives In modern structural analysis we calculate response using fairly complex equations. We often need to solve many thousands of simultaneous equations.
Term 2, 2011 Week 1. CONTENTS Problem-solving methodology Programming and scripting languages – Programming languages Programming languages – Scripting.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
CIS 601 Fall 2003 Introduction to MATLAB Longin Jan Latecki Based on the lectures of Rolf Lakaemper and David Young.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
M ATLAB – What Is It ? Name is from matrix laboratory Powerful tool for – Computation and visualization of engineering and science mathematics – Communication.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Physics 114: Lecture 1 Overview of Class Intro to MATLAB
Fundamentals of Programming I Overview of Programming
PH2150 Scientific Computing Skills
Programming For Big Data
Development Environment
CST 1101 Problem Solving Using Computers
Introduction to Python
Introduction to Matlab
Lecture: MATLAB Chapter 1 Introduction
Presented By S.Yamuna AP/IT
Data Analysis using Python-I
Matlab Workshop 9/22/2018.
PH2150 Scientific Computing Skills
Introduction to MATLAB
What Is a Program? A program is like an algorithm, but describes a process that is ready or can be made ready to run on a real computer Retrieved from:
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
FEniCS = Finite Element - ni - Computational Software
Experiment No. (1) - an introduction to MATLAB
Simulation And Modeling
General Computer Science for Engineers CISC 106 Lecture 03
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Presentation transcript:

by

What advantages has it?

The Reasons for Choosing Python  Python is free  It is object-oriented  It is interpreted  It is operating-system independent  It has an excellent optimization module  It offers modern COM modules for interfacing with Solids Works  Python is free  It is object-oriented  It is interpreted  It is operating-system independent  It has an excellent optimization module  It offers modern COM modules for interfacing with Solids Works

Getting Started with Python  Python(x,y) package from The Python(x,y) package comes with all numerical and scientific Python modules. Python(x,y) is a free scientific and engineering development software for numerical computations, data analysis and data visualization based on Python programming language. Spyder is excellent integrated development environment (IDE). Index for some packages related to python  SfePy is a software for solving systems of coupled partial differential equations (PDEs) by the finite element method in 2D and 3D  Python(x,y) package from The Python(x,y) package comes with all numerical and scientific Python modules. Python(x,y) is a free scientific and engineering development software for numerical computations, data analysis and data visualization based on Python programming language. Spyder is excellent integrated development environment (IDE). Index for some packages related to python  SfePy is a software for solving systems of coupled partial differential equations (PDEs) by the finite element method in 2D and 3D

Since Python is an object-oriented language, everything one creates in Python is an object, including integers, float, strings, arrays, etc. >>> i=4 >>> x=3.56 >>> a=“hello” Examples Associated with objects are methods that act on these objects. By Typing a ‘dot’ after the object variable name, we can access a list of methods associated with it. Examples >>> a=“hello” >>> a.capitalize() ‘Hello’ Basic Objects

For integers and floats, it is interpreted as the usual addition; for strings it is interpreted in Python as a concatenation. We can reassign the variables. >>> i=1+2 >>> i 3 >>>a=“hello”+“world!” >>>a “hello world!” >>>a=“hello” >>>b=a >>>print a,b hello >>>b=“world!” >>>print a,b hello world! Examples Basic Objects

A list is a collection of other Python objects. Lists can contain a variety of objects (integers, strings, etc). They can contain other list objects as in b= [3,a]. Addition of lists leads to a concatenation as in c=a+a. There is an access to individual elements of a list is through the [] operator (as In a[2]). The indexing of individual elements f a list starts from 0. >>> a=[1, 2, “srt”] >>> b=[3,a] >>> c=a+a >>> print a,b,c [1, 2, “str”][3, [1, 2, “str”]][1, 2, “str”,1, 2, “str”] >>> b=a >>>b[2]=3 >>>print a [1, 2, 3] >>> range(5) [0, 1, 2, 3, 4] Examples Lists

Simple Python program in the Editor (e.g. within Spyder). You can give a name, e.g. PythonObjects.py, ‘py’ extension refers to a Python file. # Floats and integers print 2**10 #2 to the power 10 x=0.5 print 2.5*x/3 # Strings s=“Hello World!” print 3*s # implies concatenation # Lists a=[0,1,2,3] # list, not an array or vector b=range(4) # list, with the same contents as a print a,b print 3*a # implies concatenation Python Scripts File Edit Format Run Options Windows Help PythonObjects.py-…

The following output appears in the Console window after running the code PythonObjects.py Hello World!Hello World!Hello World! [0, 1, 2, 3] [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3] Output

The following example illustrates the use of ‘for’, ‘if’ and ‘while’ commands in Python. # Flow control in Python for i in range(10): # does not include 10 if i<=4: print i, i**2 elif i<=7: print i,i**2+1 else: print i,i**2+2 s='-' while len(s)<25: s+='-' print s Flow Control File Edit Format Run Options Windows Help FControl.py-…

The following output appears in the Console window after running the code FControl.py Output

Python provides two commands, namely ‘raw_input’ and ‘input’ for user. The first command returns the user input as a string, while the second Command will interpret and evaluate the input, and return the interpreted value if the evaluation is meaningful. >>> a=raw_input(“Enter data:”) Enter data: 3*4-5 >>> a '3*4-5' >>> a=input(“Enter data:”) Enter data: 3*4-5 >>> a 7 User Input

There are numerical objects (arrays, dot product, etc) and methods that are not part of the core Python language, but are part of the numpy and scipy libraries/modules. They are installed when we install Python. However, in order to access them in a script file we must import them. # Using Pylab import pylab as py #(or e.g. import numpy as py) x=py.array([0,1,2,3]) # creates an array from a list y=x+x # this is now an addition not concatenation print y a=py.pi # the number theta=py.arange(-a,a,0.1) # sample from -pi to +pi using arange z=py.sin(theta) # compute sin(theta) for all samples print sz.max() # find the maximum value Numerical Python File Edit Format Run Options Windows Help UsingPylab.py-…

The resulting output in the Console window is shown [ ] Output

>>> a=3+4j >>> a**2 ‘(-7+24j)' >>> sqrt(a) # it is needed to import Numerical Python before ‘(2+1j) Complex Numbers Examples

There are numerical objects (arrays, dot product, etc) and methods that are not part of the core Python language, but are part of the numpy and scipy libraries/modules. They are installed when we install Python. However, in order to access them in a script file we must import them. # Linear Algebra import pylab as py #(or e.g. import numpy as py) A=py.array([[2,-1],[-1,2]]) # creates an array from a list B=py.array([1,1]) x=py.solve(A,b) print “Solution for 2x2 problem is” +str(x) Linear Algebra File Edit Format Run Options Windows Help LinearAlgebra.py-…

# Linear Algebra (continuation) Lambda, V=py.eig(A) print “Eigenvalues of matrix are” +str(Lambda) Print “Eigenvectors of matrix are \n” +str(V) A=py.rand(50,50) xIn=py.rand(50,1) B=py.dot(A,xIn) xOut=py.solve(A,b) Err=py.norm(xIn-xOut) print “Error for a random matrix solve is “ +str(err) Linear Algebra (cont.) File Edit Format Run Options Windows Help LinearAlgebra.py-…

Pylab supports 2D and 3D plotting via matlibplot ( package that can behttp://matplot.souceforge.net Accessed through pylab. Plots MatLibPlot.py-… File Edit Format Run Options Windows Help # 2-D plots using Python/Pylab import pylab as py pi=py.pi x=py.arrange(0,2*pi,pi/50) y=py.sin(x) Z=py.cos(x) py.plot(x,y) py.plot(x,z) py.xlabel(“x”) py.ylabel(“sin(x)&cos(x)”) py.legend(“sin(x)’,’cos(x)”)) py.savefig(“Fig2.png”) py.show()

The resulting output in the Console window is shown Plots

One can include multiple functions within a single Python file, and Access each one of them individually (a distinct advantage over Matlab). Modules Example: a file containing multiple functions SampleFunctions.py-… File Edit Format Run Options Windows Help # Module consists of 1-D functions, and derivatives of some of these funcs. import pylab as py def f1(x): f=-x*py.exp(-x**2) # returns -x*exp(-x**2) return f def f1_gradient(x): g=-py.exp(x**2)+2*x*x*py.exp(-x**2) # returns the derivative of f return g def f2_hessian(x): h=6*x*py.exp(x**2)-4*x**3*py.exp(-x**2) # return the second derivative of f

The resulting output in the Console window is shown below >>> import SimpleFunctions >>> SimpleFunctions.f1(2) Modules

Python offers a rich set language features for passing arguments into Functions. We consider the function f1 (together with a testing script) Function Arguments FunctionsArguments.py-… File Edit Format Run Options Windows Help # Example to illustrate function arguments def f1(x, a=4, s=‘hello’): print x, a, s if __name__==“__main__”: f1(0.3) f1(x=0.4) f1(x=0.5,a=5) f1(0.5, a=5) f1(x=0.6,s=“world”) f1(0.6,s=“world”) f1(s=“world”,a=7,x=0.7)

The resulting output in the Console window is shown below hello hello hello world world Function Arguments

There are a few Python ‘quirks’ that one must keep in mind Python Quirks Examples

An important concept “class”, in object oriented languages such Python, Is a collection of objects and methods that are closely related. import pylab as py class Polynomial: def __init__ (self,aIn): self.a=py.array(aIn) Python Class def evaluate(x): #v=a[0]+a[1]*x+a[2]*x**2+... v,temp=0.0,1.0 for coeff in a: v+=coeff*temp temp*=x return v if __name__=="__main__": p=Polynomial([1,-1,2]) a=p.a print a st=__str__(); print st p1=evaluate(2.0) print p1 import PolynomialClass from PolynomialClass import Polynomial def __str__(): string=str(a[0]) for i, coeff in enumerate(a[1:]): if coeff == 0.0: continue elif (coeff<0): sign=' - ' else: sign=' + ' string+=sign+str(abs(coeff))+’*x^’+str(i+1) return string PolynomialClass.py

SfePy - software for solving PDEs in Python  SfePy is a software for solving systems of coupled partial differential equations (PDEs) by the finite element method in 2D and 3D  SfePy can use many terms to build systems of partial differential equations (PDEs) to be solved  SfePy comes with a number of examples that can get you started  Sources : m2p.ensam.eu/SphinxDoc/cnem/index.html  SfePy is a software for solving systems of coupled partial differential equations (PDEs) by the finite element method in 2D and 3D  SfePy can use many terms to build systems of partial differential equations (PDEs) to be solved  SfePy comes with a number of examples that can get you started  Sources : m2p.ensam.eu/SphinxDoc/cnem/index.html

Biot problem - deformable porous mediumm With using modules/lib.: numpy, sfepy

Biot problem - deformable porous medium with the no- penetration boundary condition on boundary region With using modules/libraries: sfepy.linalg, sfepy.mechanics.matcoefs

Linear viscoelasticity with pressure traction load on surface and constrained to one-dimensional motion. The fading memory terms require an unloaded initial configuration, so the load starts in the second time step. With using modules/libraries sfepy.base.base sfepy.mechanics.matcoefs sfepy.homogenization.utils

References  Mark Lutz & David Ascher, Learning Python, O’Reilly, 1999 (Help for Programmers)  Mark Lutz, Programming Python, O’Reilly, 2001 (Solutions for Python Programmers)  Documentations from internet sources  Mark Lutz & David Ascher, Learning Python, O’Reilly, 1999 (Help for Programmers)  Mark Lutz, Programming Python, O’Reilly, 2001 (Solutions for Python Programmers)  Documentations from internet sources