csce 206 1 review—fortran. csce 206 2 review—i/o patterns: read until a sentinel value is found...

24
CSCE 206 1 Review—Fortran

Post on 19-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

1

Review—Fortran

Page 2: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

2

Review—I/O

Patterns:

Read until a sentinel value is found

Read n, then read n things

Read until EOF encountered

Techniques:

Unformatted reads (preferred)

Formatted reads (deprecated)

Unformatted writes (deprecated)

Formatted writes (preferred)

Page 3: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

3

Conditional Execution

IF (…) THEN

ELSE

END IF

Keep it simple

Follow rules of boolean logic

Use Logical variables if necessary

Page 4: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

4

Loops

DO n = begin,end,step

END DO

…………………………………………………..

DO

IF(we’re done) THEN

EXIT

END IF

END DO

Page 5: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

5

Loops

X = initial value

DO n = begin,end,step

update X

END DO

Works for finding min, max,

Works for sum of values, product of values

Page 6: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

6

Loops

DO

IF(we’re done) THEN

EXIT

END IF

END DO

Works for searching, indeterminate termination conditions

Page 7: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

7

Data Types and Variables

Intrinsic

logical, character, integer, real, complex

Structures

Static or dynamic allocation

Call by reference, call by value

INTENT keyword

Page 8: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

8

Subprograms and Scope of Variables

Functions and subroutines

Internal and external

Variables are available to internal subprograms unless otherwise redefined

Variables are not available to external subprograms

Page 9: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

9

Algorithms

Page 10: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

10

Pointers to Data

Sorting with an index array

Linked lists, queues, stacks

Old fashioned way used arrays• bad—more cumbersome?• good—all the data can be read

More modern way uses dynamic allocation• bad—can’t necessarily find the data• good—perhaps simpler

Page 11: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

11

Divide and Conquer

Bisection is divide and conquer

Split the problem in half

Determine which half is relevant

Inherently fast

Page 12: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

12

Algorithm Analysis

f(n) = O(g(x)) means f(x) < C g(x) for some fixed constant C and “all large x”

If f(x) is a polynomial, then it’s big O of the leading term

Hierarchy

log n

n

n log n

n^2

Page 13: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

13

Numerical Algorithms

Page 14: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

14

Root Finding

Bisection method

more or less guaranteed to work

but slow to converge

Newton’s method

generally fast in convergence

but less predictable

Page 15: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

15

Approximation of Functions

Forward (backward) difference approximations:• Approximate derivatives with slopes from data

points forward (backward) from an initial base pointCentral difference approximations:• Approx with average forward and backwardBoth methods approx a function with a polynomial by

approximating the derivatives using data points and then approximating the function with a Taylor series

Lagrange interpolation:Create the exact polynomial passing through the data

points.

Page 16: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

16

Approximation of Functions

When the data is felt to be accurate, difference methods are appropriate.

When the data is known to be inaccurate, a least squares approximation is used.

Usually: Minimize the sums of the errors in the vertical (y) direction if we were to approx the function with a straight line

If we have nonlinear data, do a transformation, then approximate with a straight line.

Page 17: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

17

Means, Variances, etc.

Mean can be computed “online”

Variance can be computed online, but is usually expressed as a computation that requires first computing the mean

Page 18: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

18

Random Numbers

Monte Carlo computations

Numerical integration

Simulation of statistical events (traffic flow, particle interactions, etc.)

Page 19: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

19

Differential Equations

Start from an initial condition (time zero, for example)

Estimate rate of change numerically

Proceed in the direction of that change for one time step

Recalculate rates of changes

repeat

Page 20: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

20

Sorting

Bubble sort/insertion sort, O(n^2) time, but there are faster ways

Sort data?

Sort keys?

Create an index array but don’t move the data itself?

Page 21: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

21

Matrix Operations

Matrix multiplication

Loops

Page 22: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

22

Gaussian Elimination

The most basic of all direct linear system solvers

Also not very stable or successful, and not very efficient

Best done as vector-matrix multiplication either explicitly or implicitly

Page 23: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

23

Gauss-Seidel Iteration

The most basic of all iterative linear system solvers

Reasonably stable and successful, and reasonably efficient

Can be done either sequentially (G-S) or in parallel (Jacobi iteration)

Page 24: CSCE 206 1 Review—Fortran. CSCE 206 2 Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered

CSCE 206

24

The End