CSCE Review—Fortran
CSCE 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)
CSCE Conditional Execution IF (…) THEN … ELSE … END IF Keep it simple Follow rules of boolean logic Use Logical variables if necessary
CSCE Loops DO n = begin,end,step END DO ………………………………………………….. DO IF(we’re done) THEN EXIT END IF … END DO
CSCE 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
CSCE Loops DO IF(we’re done) THEN EXIT END IF … END DO Works for searching, indeterminate termination conditions
CSCE Data Types and Variables Intrinsic logical, character, integer, real, complex Structures Static or dynamic allocation Call by reference, call by value INTENT keyword
CSCE 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
CSCE Algorithms
CSCE 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
CSCE Divide and Conquer Bisection is divide and conquer Split the problem in half Determine which half is relevant Inherently fast
CSCE 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 …
CSCE Numerical Algorithms
CSCE Root Finding Bisection method more or less guaranteed to work but slow to converge Newton’s method generally fast in convergence but less predictable
CSCE Approximation of Functions Forward (backward) difference approximations: Approximate derivatives with slopes from data points forward (backward) from an initial base point Central difference approximations: Approx with average forward and backward Both 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.
CSCE 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.
CSCE 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
CSCE Random Numbers Monte Carlo computations Numerical integration Simulation of statistical events (traffic flow, particle interactions, etc.)
CSCE 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
CSCE 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?
CSCE Matrix Operations Matrix multiplication Loops
CSCE 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
CSCE 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)
CSCE The End