Computation for Physics 計算物理概論

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

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.
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.
CS150 Introduction to Computer Science 1
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
LAB-10 1-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
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.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Lists in Python.
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 Number Systems.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
259 Lecture 13 Spring 2013 Advanced Excel Topics – Arrays.
MATH AND RANDOM CLASSES.  The need for random numbers occurs frequently when writing software.  The Random class, which is part of the java.util class,
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
Computer Science 210 Computer Organization Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
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;
IDL Tutorial Day 1 Goals: 1) Introduce IDL basics 2) Describe fundamental IDL structures Angela Des Jardins
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
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.
Course A201: Introduction to Programming 09/30/2010.
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) //
Computation for Physics 計算物理概論 Introduction to NumPy and SciPy.
Array Operations ENGR 1181 MATLAB 4.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
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.
September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
int [] scores = new int [10];
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Arrays. Arrays are objects that help us organize large amounts of information.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Introduction to programming in java Lecture 21 Arrays – Part 1.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
PH2150 Scientific Computing Skills
Data Types and Conversions, Input from the Keyboard
Lecture 6 C++ Programming
Python NumPy AILab Batselem Jagvaral 2016 March.
PH2150 Scientific Computing Skills
Suppose I want to add all the even integers from 1 to 100 (inclusive)
CHAPTER 3: String And Numeric Data In Python
Functions continued.
Presentation transcript:

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

List v.s. Array (numpy package) 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) [0. 0. 0. 0.] >>>a = zeros(5, float) [0 0 0 0 0] >>>a = zeros(3,complex)

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

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]] >>>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 1.0 1.5 -2.2 2.6 >>>from numpy import loadtxt >>>a = loadtxt("values.txt", float) >>>print(a) [ 1. 1.5 -2.2 2.6]

Reading a 2D Array from a File values.txt 1 2 3 4 3 4 5 6 5 6 7 8 >>>from numpy import loadtxt >>>a = loadtxt("values.txt", float) >>>print(a) [[ 1. 2. 3. 4.] [ 3. 4. 5. 6.] [ 5. 6. 7. 8.]]

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) [2 4 6 8]

Add or Subtract Two Arrays >>>a = array([1,2,3,4],int) >>>b = array([2,4,6,8],int) >>>print(a+b) [3 6 9 12] >>>print(b-a) [1 2 3 4] (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) [2 8 18 32] >>>print(b/a) [2.0 2.0 2.0 2.0]

"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]] 1 3 2 4 4 −2 −3 1 +2 1 2 2 1 = −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)) [0.84147098 0.90929743 0.14112001 -0.7568025]

Array Methods >>>a = array([1,2,3,4],float) >>>a.size 4 >>>a.shape (4,) >>>a = array([[1,2,3],[4,5,6],int) 6 (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(sum(log(values))/len(values)) print(geometric) 𝑥 = 𝑖=1 𝑛 𝑥 𝑖 1/𝑛 → 𝑥 =𝑒𝑥𝑝 1 𝑛 𝑖=1 𝑛 𝑙𝑛 𝑥 𝑖

numpy.arange arange([start],stop[,step],dtype=None) Return evenly spaced values within a given interval. Values are generated within the half-open interval [start, stop) (in other words, the interval including start but excluding stop). For integer arguments the function is equivalent to the Python built-in range function, but returns an ndarray rather than a list. When using a non-integer step, such as 0.1, the results will often not be consistent. It is better to use linspace for these cases. >>> np.arange(3) array([0, 1, 2]) >>> np.arange(3.0) array([ 0., 1., 2.]) >>> np.arange(3,7) array([3, 4, 5, 6]) >>> np.arange(3,7,2) array([3, 5])

numpy.linspace Return evenly spaced numbers over a specified interval. Returns num evenly spaced samples, calculated over the interval [start, stop ]. The endpoint of the interval can optionally be excluded. >>>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 import numpy.random from numpy.random import random numpy.random.random() from numpy.random import random random() from numpy.random import *

numpy.random seed([seed]) get_state() set_state(state) random(size)=random_sample(size)=ranf([size])=sample([size]) Return random floats in the half-open interval [0.0, 1.0). randint(low,high=None,size=None) Return random integers from low (inclusive) to high (exclusive). choice(a[,size,replace,p]) shuffle(X) permutation(X)

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