physics 2660: fundamentals of scientific...

189
Physics 2660: Fundamentals of Scientific Computing Lecture 14 Instructor: Prof. Chris Neu ([email protected])

Upload: others

Post on 14-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Physics 2660: Fundamentals of Scientific Computing

Lecture 14 Instructor: Prof. Chris Neu ([email protected])

Page 2: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Notes•  Last lecture!•  Upcoming homeworks:

–  HW13 due Wednesday 2 May at 11:59pm–  HW14 due Wednesday 2 May at 11:59pm – a course evaluation

•  alt3zq Tuma, Andrew•  ces2fa Stephens, Catherine•  ecj4wb Jones, Ethan•  jls6gw Selby, Jonathan•  raj9qv Jennings, Robert•  rm3xw Mora, Raymundo•  sam3bf Murri, Samuel•  vh2[ Huang, Vicki

•  Solutions to all labs and hw’s are in the process of being posted …sorry for recent delays

2

Why have you not completed

this?

Page 3: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Reminder•  Grading of HWs almost caught up!

•  Register for piazza! –  Only 50 out of 60 so far–  All questions about final exam will only be answered via piazza

•  Office hours: all held in our computer lab, room 022-C of this bldg

–  Me: After lecture 3:30-4:30 every Tuesday –  TAs:

•  Mondays: 3-5pm and 6-8pm•  Wednesdays: 5-9pm

3

Page 4: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Notes

•  Final exam is coming:

–  Take-home projects•  3 problems

–  Like a more involved, longer multipart homework assignment–  Assigned last week of semester on Tuesday 2 May

•  will be posted by 3:30pm today

–  Due Thursday May 10:•  electronic copies by 9:00am •  hard-copies must be submiied Thursday 10 May between 08:00-10:00

in room 022-C, our computer lab….or earlier by personal arrangement with me.

4

Page 5: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Notes•  Final exam is coming:

–  Take-home projects•  3 problems

–  Like a more involved, longer multipart homework assignment–  Assigned last week of semester on Tuesday 1 May–  Due Thursday May 10:

•  electronic copies by 9:00am •  hard-copies must be submiied Thursday 10 May between 08:00-10:00

in room 022-C, our computer lab….or earlier by personal arrangement with me.

5

Due Thursday May 10, electronically by 9:00am and hard copies between 08:00 and 10:00 in 022-C

Page 6: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Review and Outline•  Last time:

–  Comparing data to a prediction

•  Today:–  Tuning a model to best represent the data–  More tests of compatibility

6

Page 7: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Summary so far…•  Compare some data to a model, account

for uncertainties•  Calculate reduced χ2

•  If good agreement–  should see different points sometimes

high/low–  if k large, reduced χ2 ~ 1.0

7

Page 8: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Tuning a Model to Best Match Some Data

8

Page 9: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Tuning a Model

9

Page 10: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Tuning a Model

10

Can we figure out which model – which values of a and b – the data most

favors?

Page 11: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Probability of Some Observation

11

Page 12: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Probability of Multiple Observations

12

The probability of the collection of data – 3 observations – is just the product of the three individual probabilities

Page 13: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Probability of Multiple Observations

13

The probability of the collection of data – k observations – is just the product of the k individual probabilities

Page 14: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

P: The χ2 Likelihood Function

14

Page 15: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Minimizing the χ2

15

Page 16: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Minimizing the χ2

16

Page 17: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Minimizing the χ2

17

Page 18: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Minimizing the χ2

18

Page 19: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

More Powerful Application: An Arbitrary Theory

19

Page 20: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

FiPing with Gnuplot

20

P(x;a,b,c) = 12πc2

e−(ax−b)2

2c2

Page 21: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Assessing the Quality of a Fit

21

Page 22: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Assessing the Quality of a Fit

22

Consequence: If the number of fit parameters is greater than or equal to the number of data points the χ2 is undefined.

Trivial case

Page 23: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Assessing the Quality of a Fit

23

P(x;a,b,c) = 12πc2

e−(ax−b)2

2c2

Page 24: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Assessing the Quality of a Fit

24

So there is a 90% probability that, if the data were consistent with the model (here a Gaussian-like thing with 3 params), the data would have a higher chi2 value. Too good to be true? Why are the points so close to the model? Did the fit procedure cheat in some way? Are the uncertainties over-estimated?

Page 25: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

FiPing is Done EVERYWHERE

25

Page 26: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Aside: Gravitational Waves

hips://www.nytimes.com/video/science/100000004200661/what-are-gravitational-waves-ligo-black-holes.html

26

Page 27: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Curve FiPing

27

Page 28: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Deviations from the Model

28

Page 29: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The Pull Distribution

29

Page 30: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Bias – Is the Prediction In Accord with the Data?

30

Page 31: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Clusters of Data Above/Below

31

Page 32: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Clusters of Data Above/Below

32

Page 33: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

What Have We Learned

33

Page 34: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

What Have We Learned

•  We learned a whole lot this semester!

•  Computers•  Programming Language•  C

–  For many of you this was your first entrypoint into the use of a computer as a tool for data analysis

–  Many of you started from very liile experience–  Generated a set of tools you can rely on in the future for a whole

host of problems in your chosen field, science or otherwise

•  Data Presentation•  Data Analysis and Assessment•  Some Statistics – some very important statistical concepts

34

Page 35: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

C vs. C++

35

Page 36: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

C vs. C++

0 C is much simpler for beginners than C++ but far less powerful1  C++ can run most C code but not the other way round!2  Functions are the building blocks of C whereas objects are the building

blocks of C++3  In C, the program is formulated step-by-step but in C++ the base

elements are formulated first and then linked together in larger systems4  In C++, functions can be overloaded, but not in C5  C++ has a much larger library than C6  C++ allows user to create classes (similar to structures) to which methods

and functions can be assigned and within which they can operate7  In C++ you can add your own types and objects inheriting properties and

routines from other classes. This allows you to add more features to your own classes and the standard library as well

8  C++ uses “Namespace”, which is very useful in avoiding multiple declarations

9  C++ has built in exception support

36

Page 37: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

What is next? What more could you learn?

37

Page 38: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

What Could You Try Next?

•  You now have a firm foundation in C. What could be next?–  Another powerful language used in scientific seiings, such as python

–  Some scripting language: Bash scripts, perl, etc., to automate many types of text and data manipulation, etc.

–  Object Oriented features of C++, Java, C#, etc: Powerful (and sometimes dangerous) design tools!

–  A physics/math library, like ROOT, R, CERNLIB, CLHEP, LINPACK, GSL, etc…

–  Overview of analytical techniques in classes like PHYS 5630/40(Computational Physics 1/2)

–  Get a job w/ a research group, apply your skills, and learn more!38

Page 39: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

What Could You Try Next?•  Worldwide distributed computing grids

–  hip://www.opensciencegrid.org

•  UVa’s High-Performance Computing cluster:–  hip://arcs.virginia.edu/rivanna

•  Massively Parallel Processing

•  Real time controls–  automation–  robotics–  eg. autonomous vehicles, ...

•  Quantum Computing

–  hip://www.sciencedaily.com/news/computers_math/quantum_computers/

39

Page 40: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Grid Computing

•  We have operated this semester in the following fashion:–  sit down at some personal or lab computer–  use this as a portal to a cluster of computers, galileo, featuring

•  large amounts of storage•  many nodes allowing multiple users to exploit the same resource

simultaneously

•  Some drawbacks:–  galileo can get slow – bogged down by other users. What do we do in

these cases? Other clusters available to us – and where?–  /home is a shared resource and has goien full a few times,

preventing work – not an infinite space resource–  interactive jobs are tempermental: what if you needed to run 1000s of

jobs – need 1000s of terminal sessions?–  what if I needed lots of data to be stored but only needed it very

occasionally?

40

Page 41: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

TIER-0 CERN

•  AT LHC, need to store, analyze and distribute 15 Petabytes of data per year (1% of digital information produced in the world)•  All data is processed at CERN and backed up on tape•  But CERN provides only ~20% total compute capacity

Page 42: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Tier-1 sites

•  Sufficient storage capacity for large fraction of the data

•  Large scale reprocessing

•  Distribution to Tier-2 sites

•  11 sites for all LHC experiments

Distributes data to ~30 Tier-3 sites

Only store data specific to given task

Distributed Computing

Page 43: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Worldwide LHC Computing Grid

•  LHC Grid gives access to more than 7000 scientists in more than 30 countries•  World’s largest computer grid connecting more than 200,000 CPUs at universities and research institutes •  Runs over 1,000,000 tasks every day

Page 44: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

TIER-0/1s

TIER-3sTIER-2s

•  CMS observes more than 400 unique users submitting jobs per week

•  Capacity run up to 200,000 jobs per day on the Grid

CMS Data Processing on the Grid

Page 45: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

CERN Computing

45

hips://www.youtube.com/watch?v=jDC3-QSiLB4

Page 46: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The Final Exam

46

Page 47: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Notes

•  Final exam:–  Take-home projects–  Like a more involved, longer multipart homework assignment–  Due Thursday May 10:

•  electronic copies by 9:00am •  hard-copies must be submiied Thursday 10 May between 08:00-10:00

in room 022-C, our computer lab…or earlier by personal arrangement with me

47

Due Thursday May 10, electronically by 9:00am and hard copies between 08:00 and 10:00 in 022-C

Page 48: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Some Guidelines for Final•  All students must work individually, no collaboration.

•  You can ask questions via email or chat or on Piazza but the insight will be limited to conceptual assistance or clarifications.

•  TA hours will be held on Thursday 3 May xxxxx and Friday 4 May yyyyyyy. •  These will not be standard lab-TA hours: students can come individually to ask questions to

the TA but then you will go and work on the questions on your own. Conceptual and clarifications on content only, no assistance in determining the solutions. No examination of code will be allowed.

•  Pose all questions before Monday 8 May 8pm.

•  Allowed resources: class texts, class notes, class web page (including solutions), and web links provided directly from the class web pages. You can use functions available in the standard C libraries we have used in class/lab/homeworks.

•  Email/chat/Piazza any requests for problem clarifications by Monday, May 7, 8pm. This may be followed by a single FAQ email covering appropriate questions. Watch for this email. After this, there will be no more discussion during the exam.

48

Page 49: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Some Guidelines for Final•  All students must work individually, no collaboration.

•  You can ask questions via email or chat or on Piazza but the insight will be limited to conceptual assistance or clarifications.

•  TA hours will be held on Thursday 3 May xxxxx and Friday 4 May yyyyyyy. •  These will not be standard lab-TA hours: students can come individually to ask questions to

the TA but then you will go and work on the questions on your own. Conceptual and clarifications on content only, no assistance in determining the solutions. No examination of code will be allowed.

•  Pose all questions before Monday 8 May 8pm.

•  Allowed resources: class texts, class notes, class web page (including solutions), and web links provided directly from the class web pages. You can use functions available in the standard C libraries we have used in class/lab/homeworks.

•  Email/chat/Piazza any requests for problem clarifications by Monday, May 7, 8pm. This may be followed by a single FAQ email covering appropriate questions. Watch for this email. After this, there will be no more discussion during the exam.

49

Start your work early!

There is an early deadline on questions to encourage you to start working on it – at least

thinking about it – right away.

Page 50: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The End!

50

Page 51: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

More Testing of Compatibility

51

Page 52: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Cumulative Distribution Function

52

Page 53: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Cumulative Distribution Function

53

Page 54: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Example: PDF and CDF

54

Page 55: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Empirical Distribution Function

55

Page 56: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Empirical Distribution Function•  The ECDF is made from

“unbinned” data–  not from a binned

histogram–  use raw measured values

•  Do this by:1.  say you have N values, xi2.  sort the N values in order

of increasing value3.  plot each of the N values

with xi on the x-axis and i/N on the y-axis

•  Now, compare model’s CDF and the data’s ECDF…

56

Page 57: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Testing Compatibility

57

Page 58: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Testing Compatibility

58

Page 59: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Manipulating Binary Data

59

Page 60: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Bits and Bytes

60

Page 61: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Flipping Bits

61

Page 62: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Bitwise Operators

62

Page 63: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Bitwise AND

63

Page 64: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Bitwise OR

64

Page 65: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Bitwise Left Shift

65

Page 66: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Bitwise Right Shift

66

Page 67: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Bitwise Inverse

67

Page 68: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Bitwise Shift on Constants

68

Page 69: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Testing and SePing Bits

69

( )

Page 70: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Testing and SePing Bits: Masks

70

( )“Mask”

Page 71: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Testing and SePing Bits: Masks

71

( )

Page 72: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Clearing a Bit

72

Page 73: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Manipulating Binary Data: Example

73

Page 74: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Hexadecimal Representation

74

Page 75: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Hexadecimal Representation

75

Page 76: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Hexadecimal Representation

76

Page 77: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Data Storage

77

Page 78: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Example

78

Page 79: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Example

79

Page 80: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Example

80

Page 81: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The Exclusive Or (XOR) Operation

81

Page 82: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The Exclusive Or (XOR) Operation

82

Page 83: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

XOR Application

83

Page 84: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

XOR Application

84

1 01 1 0 0 0 0

1 010 1 010

010 1 0100

Say you want to send this plain-text “message”:

You and the recipient have a copy of some pre-determined “key”

key ^ message = encrypted message:

And you send this encrypted message to your intended recipient….

^ ^ ^ ^ ^ ^ ^ ^

Page 85: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

XOR Application

85

1 01 1 0 0 0 0

1 010 1 010

010 1 0100Your recipient receives your encrypted message:

and applies the key:

^ ^ ^ ^ ^ ^ ^ ^

and gets a copy of the plain-text message. Successful secret communication!

Page 86: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

XOR Application

86

1 01 1 0 0 0 0

1 010 1 010

010 1 0100

One flaw: if someone intercepts both the coded and plain message:

^ ^ ^ ^ ^ ^ ^ ^

They can derive the key!This a problem if the sender/recipient ever re-use a key.

Page 87: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Review: Bitwise Operators

87

Page 88: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Review: Testing and SePing Bits

88

Page 89: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Storage of Binary Data

89

Page 90: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The fputc Function

90

Page 91: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The fputc Function

91

1 byte

1 byte4 bytes

4 bytes = 4 chars

Page 92: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Comparison of ASCII and Binary Storage

92

4 bytes = 4 chars

10 bytes = 10 chars

Page 93: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

More on Characters

93

Page 94: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Signed v. Unsigned Vars

94

Page 95: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Signed vs. Unsigned Vars

•  Example:– Say

a = 1d = 00000001b and b = -1d = 10000001b

95

Page 96: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Signed vs. Unsigned Vars

•  Example:– Say

a = 1d = 00000001b and b = -1d = 10000001b

– Thena+b = 1d + (-1d) = ?

96

Page 97: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Signed vs. Unsigned Vars

•  Example:– Say

a = 1d = 00000001b and b = -1d = 10000001b

– Thena+b = 1d + (-1d) = 00000001b + 10000001b

97

Page 98: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Signed vs. Unsigned Vars

•  Example:– Say

a = 1d = 00000001b and b = -1d = 10000001b

– Thena+b = 1d + (-1d) = 00000001b + 10000001b

= 10000010b

98

Page 99: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Signed vs. Unsigned Vars

•  Example:– Say

a = 1d = 00000001b and b = -1d = 10000001b

– Thena+b = 1d + (-1d) = 00000001b + 10000001b

= 10000010b = -2d

99

Page 100: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Signed vs. Unsigned Vars

•  Example:– Say

a = 1d = 00000001b and b = -1d = 10000001b

– Thena+b = 1d + (-1d) = 00000001b + 10000001b

= 10000010b = -2d

– That looks wrong to me….

100

Page 101: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Trick: Two’s Complement Notation

101

Page 102: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Trick: Two’s Complement Notation

•  To form the two’s complement of any negative number:1.  Convert |number| to N-bit representation2.  Subtract |number| from 2N

3.  Add one to the number

•  Examples, 8-bit negative numbers:–  If a = -1 then |-1| in 8-bit representation is 00000001

a2c = (11111111 – 00000001) + 00000001 = 11111111

–  If a = -5 thena2c = 11111111 – 00000101 + 00000001 = 11111011

102

Page 103: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Signed vs. Unsigned Vars

•  Example:– Say

a = 1d = 00000001b and b = -1d = 11111111b (using 2’s complement)

– Thena+b = 1d + (-1d) = 00000001b + 11111111b

= 00000000b = 0d

– That’s beier!

103

Page 104: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

More On Binary Data Output

104

Page 105: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

More On Binary Data Output

105

Page 106: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

More On Binary Data Output

106

Page 107: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

More On Binary Data Input

107

Page 108: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

More On Binary Data Output

108

Page 109: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

More On Binary Data Output

109

Page 110: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

More On Binary Data Input

110

Page 111: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

More On Binary Data Input

111

Page 112: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

fopen: File I/O Modes

112

Page 113: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

fopen, fwrite and fread

113

Page 114: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

rewind

114

Page 115: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

fseek

115

Page 116: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

fseek

116

Page 117: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

ftell

117

Page 118: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

feof

118

Page 119: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Writing Binary for Arbitrary Structs

119

Page 120: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Filesize Comparison of Binary v. ASCII Formats

120

Page 121: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Solving Differential Equations

121

Page 122: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

A Trajectory

122

Page 123: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

A Trajectory

123

We don’t know v(t) but we know v(t0) and the time derivative.We seek v(t).

Page 124: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

A Trajectory

124one step:

result:

not very accurate!!!All we know is the derivative of v! Here v=y and t=x.

Page 125: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

A BePer Method

125

Page 126: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

A BePer Method

126

All we know is the derivative!

Page 127: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

A BePer Method

127

All we know is the derivative!

Page 128: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

A BePer Method

128

Page 129: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

A BePer Method

129

Page 130: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Passing Pointers to Structures

130

Page 131: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Unintended Consequences

131

Page 132: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Unintended Consequences

132

Warning tells you to rewrite this!

Page 133: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Optional Function Arguments

133

Page 134: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Default Argument Values in Functions: C++

134

Page 135: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Default Argument Values in Functions: C++

135

Page 136: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Default Argument Values in Functions: C++

136

Example:

Page 137: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Searching in Arrays

137

Page 138: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Searching for a Particular Item

138

Page 139: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Linear Search

139

Page 140: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Binary Search

140

Page 141: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Binary Search

141

Page 142: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Binary Search

142

Page 143: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Binary Search

143

Page 144: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Binary Search

144

Page 145: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Search Speed: Linear Method

145

Page 146: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Search Speed: Binary Method

146

Page 147: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Sorting

147

Page 148: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Sorting Methods

148

Page 149: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Selection Sort

149

Page 150: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Selection Sort

150

Page 151: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Selection Sort

151

Page 152: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Quicksort

152

Page 153: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Quicksort

153

Page 154: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Quicksort

154

Page 155: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Quicksort

155

Page 156: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Quicksort

156

Page 157: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Method: Quicksort

157

Page 158: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists

158

Page 159: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Reminder About Arrays

159

Page 160: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: A Special Data Structure

160

Page 161: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: A Special Data Structure

161

mallccallocrealloc

Page 162: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: A Special Data Structure

162

Page 163: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: A Special Data Structure

163

Page 164: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: A Special Data Structure

164

If these were stored in an array, this could be a huge job.

Page 165: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: A Special Data Structure

165

If these were stored in an array, this could be a huge job.

Page 166: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: A Special Data Structure

166

Page 167: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: A Special Data Structure

167

Page 168: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: A Special Data Structure

168

Page 169: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: A Special Data Structure

169

Page 170: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: A Special Data Structure

170

Page 171: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

171

So What?

These things can actually be very useful!

Page 172: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists Example: FIFOs

172

Page 173: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Linked Lists: Summary

173

Page 174: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Dynamic Memory Allocation:

Memory is typically allocated by the compiler…but what if we need to change the requirements on the fly while executing a

program?

(We did not go through this unit in lecture.)

174

Page 175: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The Stack

175

Page 176: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The Stack

•  Each running program has its own stack

•  As functions are called, the variables local to that function are loaded to the top of the stack. When the function is complete that memory is freed for other uses

•  One can use up all the available memory in a stack:“stack overflow” error

176

Page 177: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The Heap

177

Page 178: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The malloc function

178

Page 179: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The calloc function

179

Page 180: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The free function

180

Page 181: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

The realloc function

181

Page 182: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Memory Allocation Failure

182

Page 183: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Dynamic Memory Allocation: Example I

183

Page 184: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Dynamic Memory Allocation: Example II

184

Page 185: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Dynamic Memory Allocation: Example II

185

Page 186: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Optional Function Arguments

186

Page 187: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Default Argument Values in Functions: C++

187

Page 188: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Default Argument Values in Functions: C++

188

Page 189: Physics 2660: Fundamentals of Scientific Computingfaculty.virginia.edu/comp-phys/phys2660/html/notes/...– Like a more involved, longer multipart homework assignment – Assigned

Default Argument Values in Functions: C++

189

Example: