Computation for Physics 計算物理概論 Introduction to NumPy and SciPy.

Slides:



Advertisements
Similar presentations
The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs.
Advertisements

Introduction to Matlab
Coordinatate systems are used to assign numeric values to locations with respect to a particular frame of reference commonly referred to as the origin.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
SPIM and MIPS programming
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming.
Maths for Computer Graphics
Computer Science 1620 Loops.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
1 ICS102: Introduction To Computing King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Matlab Intro. Outline Matlab introduction Matlab elements Types Variables Matrices.
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
4.2 Adding and Subtracting Matrices 4.3 Matrix Multiplication
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Computation for Physics 計算物理概論
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Recap Script M-file Editor/Debugger Window Cell Mode Chapter 3 “Built in MATLAB Function” Using Built-in Functions Using the HELP Feature Window HELP.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Class Opener:. Identifying Matrices Student Check:
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Array Operations ENGR 1181 MATLAB 4.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
1 CSC103: Introduction to Computer and Programming Lecture No 19.
September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB
CMPS 1371 Introduction to Computing for Engineers VECTORS.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
int [] scores = new int [10];
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
(4-2) Adding and Subtracting Matrices Objectives: To Add and subtract Matrices To solve certain Matrix equations.
Arrays. Arrays are objects that help us organize large amounts of information.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Coordinatate systems are used to assign numeric values to locations with respect to a particular frame of reference commonly referred to as the origin.
Introduction to programming in java Lecture 21 Arrays – Part 1.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
PH2150 Scientific Computing Skills
Lecture: MATLAB Chapter 1 Introduction
Arrays Declarations CSCI N305
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
PH2150 Scientific Computing Skills
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Unit 5 – Series, Sequences, and Limits Section 5
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Unit 5 – Series, Sequences, and Limits Section 5
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

Computation for Physics 計算物理概論 Introduction to NumPy and SciPy

List v.s. Array (numpy package) List – Variable length, you can add or remove elements. – Elements can be of different types. – Flexible. Array – The number of elements is fixed. – The elements must be of the same type. – Behave roughly like vectors and matrices. – Faster.

1D Array >>>from numpy import zeros >>>a = zeros(4, float) >>>print(a) [ ] >>>a = zeros(5, float) >>>print(a) [ ] >>>a = zeros(3,complex) >>>print(a)

2D Array >>>a = zeros([3,4],float) >>>print(a) [[ ] [ ] [ ]]

Array Initialization in NumPy zeros – Create an array with all elements equal to zero. ones – Create an array with all elements equal to one. empty – Create an empty array. (Faster.) – In practice the aren't empty. (Random stuff in the memory).

List  Array  List >>>r = [1.0, 1.5, -2.2] >>>a = array(r,float) >>>a = array([1.0, 1.5, -2.2], float) >>>print(a[0]) 1.0 >>>a = array([[1,2,3],[4,5,6]], int) >>>print(a) [[ 1 2 3] [ 4 5 6]] >>>print(a[ >>>r = list(a)

Accessing and Modifying Elements >>>a = array([1.0, 1.5, -2.2], float) >>>a[0] 1.0 >>>a[2]=4 (4 will be converted into float) >>>a = array([[1,2,3],[4,5,6]], int) >>>a[0][1] 2 >>>a[0][1]=0 >>>print(a) [[ 1 0 3] [ 4 5 6]]

Reading a 1D Array from a File values.txt >>>from numpy import loadtxt >>>a = loadtxt("values.txt", float) >>>print(a) [ ]

Reading a 2D Array from a File values.txt >>>from numpy import loadtxt >>>a = loadtxt("values.txt", float) >>>print(a) [ ]

Arithmetic with Arrays >>>a[0] = a[1] + 1 >>>x = a[2]**2 -2*a[3]/y >>>r = [1,2,3,4] >>>a = array(r, int) >>>s = 2*r >>>b = 2*a >>>print(s) [1,2,3,4,1,2,3,4] >>>print(b) [ ]

Add or Subtract Two Arrays >>>a = array([1,2,3,4],int) >>>b = array([2,4,6,8],int) >>>print(a+b) [ ] >>>print(b-a) [ ] (Two arrays must have the same size) >>>>print(a+1)

Multiply Two Arrays ≠ Dot Product >>>a = array([1,2,3,4],int) >>>b = array([2,4,6,8],int) >>>print(a*b) [ ] >>>print(b/a) [ ]

"Dot Product" of Two 1D Arrays="Inner Product" >>>from numpy import array, dot >>>a = array([1,2,3,4],int) >>>b = array([2,4,5,8],int) >>>print(dot(a,b)) 60

"Dot Product" of Two 2D Arrays = "Matrix Product" >>>a = array([[1,3],[2,4]],int) >>>b = array([[4,-2],[-3,1]],int) >>>c = array([[1,2],[2,1]],int) >>>print(dot(a,b)+2*c) [[-3 5] [ 0 2]]

Array Functions Most list functions can be applied to 1D array – sum – max – min – len You can also apply math functions >>>a = array([1,2,3,4],float) >>>print(sin(a)) [ ]

Array Methods >>>a = array([1,2,3,4],float) >>>a.size 4 >>>a.shape (4,) >>>a = array([[1,2,3],[4,5,6],int) >>>a.size 6 >>>a.shape (2,3)

Try: Average of a Set of Values in a File A set of numbers stored in a file values.txt. We don't know how many numbers there are. Want to calculate their mean.

Mean and Mean-Square from numpy import loadtxt values = loadtxt("values.txt",float) mean = sum(values)/len(values) mean_sqr = sum(values*values)/len(values) print(mean) print(mean_sqr)

Geometric Mean from numpy import loadtxt,log,exp from math import log,exp values = loadtxt("values.txt",float) geometric=exp(log(values)/len(values)) print(geometric)

Linspace Return evenly spaced numbers over a specified interval. >>>linspace(2.0, 3.0, num=5) array([ 2., 2.25, 2.5, 2.75, 3. ]) >>>linspace(2.0, 3.0, num=5, endpoint=False) array([ 2., 2.2, 2.4, 2.6, 2.8]) >>>linspace(2.0, 3.0, num=5, retstep=True) (array([ 2., 2.25, 2.5, 2.75, 3. ]), 0.25)

Numpy: random=random_sample() >>>random_sample() >>>random_sample(100)

Try: Random Number Input N. Generate N random numbers. Output the mean and the standard deviation.