Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.

Slides:



Advertisements
Similar presentations
Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
Advertisements

1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
Introduction to Programming (in C++) Advanced examples Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
CDA6530: Performance Models of Computers and Networks Chapter 5: Generating Random Number and Random Variables TexPoint fonts used in EMF. Read the TexPoint.
Introduction to Programming (in C++) Vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
2.1 Graphs of Quadratic Functions
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
CS 450: Computer Graphics REVIEW: OVERVIEW OF POLYGONS
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
Mathematics for Computer Graphics (Appendix A) Won-Ki Jeong.
CSCE 2100: Computing Foundations 1 Probability Theory Tamara Schneider Summer 2013.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Data types and their representation Jordi Cortadella Department of Computer Science.
Chars and strings Jordi Cortadella Department of Computer Science.
Matrices Jordi Cortadella Department of Computer Science.
Machine Learning Lecture 23: Statistical Estimation with Sampling Iain Murray’s MLSS lecture on videolectures.net:
Prime numbers Jordi Cortadella Department of Computer Science.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
A little bit of geometry Jordi Cortadella Department of Computer Science.
Gaussian elimination Jordi Cortadella Department of Computer Science.
CS 101: “If” and Recursion Abhiram Ranade. This week’ lab assignment: Write a program that takes as input an integer n and a sequence of numbers w1,w2,...,wn.
CSCI 125 & 161 Lecture 13 Martin van Bommel. Floating Point Data Floating point numbers are not exact Value 0.1 in binary is very close to 1/10, but not.
Recursion Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Module 1: Statistical Issues in Micro simulation Paul Sousa.
Approximate computations Jordi Cortadella Department of Computer Science.
Sequences Jordi Cortadella Department of Computer Science.
Jordi Cortadella Dept. of Computer Science, UPC
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Data structure design Jordi Cortadella Department of Computer Science.
Polynomials Jordi Cortadella Department of Computer Science.
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
Structures Jordi Cortadella Department of Computer Science.
Sudoku Jordi Cortadella Department of Computer Science.
First steps Jordi Cortadella Department of Computer Science.
Reusing computations Jordi Cortadella Department of Computer Science.
Lecture 4: Statistics Review II Date: 9/5/02  Hypothesis tests: power  Estimation: likelihood, moment estimation, least square  Statistical properties.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Sorting Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Numerical algorithms Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Computer simulation Sep. 9, QUIZ 2 Determine whether the following experiments have discrete or continuous out comes A fair die is tossed and the.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Vectors Jordi Cortadella Department of Computer Science.
Midterm: Question 1 (35%) (30 minutes) Write an assembly program to draw a rectangle. – (5%) Show a message to ask for information of the rectangle – (5%)
Graphs and Shortest Paths Using ADTs and generic programming.
Functions Jordi Cortadella Department of Computer Science.
Combinatorial problems Jordi Cortadella Department of Computer Science.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Monte Carlo Methods Some example applications in C++
While loop statement condition list
Approximate computations
Reasoning with invariants
For loops, nested loops and scopes
Combinatorial problems
Some problems for your consideration
Approximate computations
Geometry.
Jordi Cortadella Department of Computer Science
Jordi Cortadella Department of Computer Science
Containers: Queue and List
Jordi Cortadella Department of Computer Science
A little bit of geometry
Jordi Cortadella and Jordi Petit Department of Computer Science
CS100A Lecture 21 Previous Lecture This Lecture
Geometry.
Introduction to Programming (in C++) Advanced examples
Presentation transcript:

Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC

Living with floating-point numbers Standard normalized representation (sign + fraction + exponent): Ranges of values: Representations for: – , + , +0,  0, NaN (not a number) Be careful when operating with real numbers: double x, y; cin >> x >> y; // cout.precision(20); cout << x + y << endl; // Introduction to Programming© Dept. CS, UPC2

Comparing floating-point numbers Comparisons: a = b + c; if (a – b == c) … // may be false Allow certain tolerance for equality comparisons: if (expr1 == expr2) … // Wrong ! if (abs(expr1 – expr2) < ) … // Ok ! Introduction to Programming© Dept. CS, UPC3

Monte Carlo methods Algorithms that use repeated generation of random numbers to perform numerical computations. The methods often rely on the existence of an algorithm that generates random numbers uniformly distributed over an interval. In C++ we can use rand(), that generates numbers in the interval [0, RAND_MAX) Introduction to Programming© Dept. CS, UPC4

Approximating  Introduction to Programming© Dept. CS, UPC5 Let us pick a random point within the unit square. Q: What is the probability for the point to be inside the circle? A: The probability is  /4 Algorithm: Generate n random points in the unit square Count the number of points inside the circle (n in ) Approximate  /4  n in /n

Approximating  Pre: Post: #include // Pre: n is the number of generated points // Post: returns an approximation of  using // n random points double approx_pi(int n) { int nin = 0; double randmax = double(RAND_MAX); for (int i = 0; i < n; ++i) { double x = rand()/randmax; double y = rand()/randmax; if (xx + yy < 1.0) nin = nin + 1; } return 4.0nin/n; } Introduction to Programming© Dept. CS, UPC6

Approximating  n  , , , ,000, ,000, ,000, ,000,000, Introduction to Programming© Dept. CS, UPC7

The Newton-Raphson method Introduction to Programming© Dept. CS, UPC8 A method for finding successively approximations to the roots of a real-valued function. The function must be differentiable.

The Newton-Raphson method Introduction to Programming© Dept. CS, UPC9

The Newton-Raphson method Introduction to Programming© Dept. CS, UPC10 source:

Square root (using Newton-Raphson) Calculate Find the zero of the following function: where Recurrence: Introduction to Programming© Dept. CS, UPC11

Square root (using Newton-Raphson) Pre: Post: // Pre: a >= 0 // Post: returns x such that |x 2 -a| <  double square_root(double a) { double x = 1.0; // Makes an initial guess // Iterates using the Newton-Raphson recurrence while (abs(xx – a) >= epsilon) x = 0.5(x + a/x); return x; } Introduction to Programming© Dept. CS, UPC12

Square root (using Newton-Raphson) Example: square_root(1024.0) Introduction to Programming© Dept. CS, UPC13 x

Approximating definite integrals There are various methods to approximate a definite integral: The trapezoidal method approximates the area with a trapezoid: Introduction to Programming© Dept. CS, UPC14

Approximating definite integrals The approximation is better if several intervals are used: Introduction to Programming© Dept. CS, UPC15 a b

Approximating definite integrals Introduction to Programming© Dept. CS, UPC16 h

Approximating definite integrals Pre: Post: // Pre: b >= a, n > 0 // Post: returns an approximation of the definite // integral of f between a and b using n intervals. double integral(double a, double b, int n) { double h = (b – a)/n; double s = 0; for (int i = 1; i < n; ++i) s = s + f(a + ih); return (f(a) + f(b) + 2s)h/2; } Introduction to Programming© Dept. CS, UPC17

Representation of polygons Introduction to Programming© Dept. CS, UPC18 (1,3) (4,1) (7,3) (5,4) (6,7) (2,6) A polygon can be represented by a sequence of vertices. Two consecutive vertices represent an edge of the polygon. The last edge is represented by the first and last vertices of the sequence. Vertices: (1,3) (4,1) (7,3) (5,4) (6,7) (2,6) Edges: (1,3)-(4,1)-(7,3)-(5,4)-(6,7)-(2,6)-(1,3)

Point in polygon Introduction to Programming© Dept. CS, UPC19 Is a point inside a polygon? Use the crossing number algorithm:  Draw a ray from the point  Count the number of crossing edges:  even  outside, odd  inside

Point in polygon // A data structure to represent a point struct Point { double x; double y; }; // A data structure to represent a polygon // (an ordered set of vertices) typedef vector Polygon; Introduction to Programming© Dept. CS, UPC20

Point in polygon Introduction to Programming© Dept. CS, UPC21 Use always the horizontal ray increasing x (y is constant) Assume that the probability of “touching” a vertex is zero The ray crosses the segment if: y is between y 1 and y 2 and x c > x

Point in polygon Post: // Post: indicates whether the point q // is inside the polygon P bool in_polygon(const Polygon& P, const Point& q) { int nvert = P.size(); int src = nvert – 1; int ncross = 0; // Visit all edges of the polygon for (int dst = 0; dst < nvert; ++dst) { if (cross(P[src], P[dst], q) ++ncross; src = dst; } return ncross%2 == 1; } Introduction to Programming© Dept. CS, UPC22

Point in polygon Post: // Post: Indicates whether the horizontal ray // generated from q by increasing x crosses // the segment defined by p1 and p2. bool cross(const Point& p1, const Point& p2, const Point& q) { // Check whether q.y is between p1.y and p2.y if ((p1.y > q.y) == (p2.y > q.y)) return false; // Calculate the x coordinate of the crossing point double xc = p1.x + (q.y – p1.y)(p2.x – p1.x)/(p2.y – p1.y); return xc > q.x; } Introduction to Programming© Dept. CS, UPC23

Cycles in permutations Introduction to Programming© Dept. CS, UPC i P[i] Cycles: ( ) (2) ( ) Let P be a vector of n elements containing a permutation of the numbers 0…n-1. The permutation contains cycles and all elements are in some cycle. Design a program that writes all cycles of a permutation.

Cycles in permutations Introduction to Programming© Dept. CS, UPC i P[i] visited[i] Use an auxiliary vector (visited) to indicate the elements already written. After writing one permutation, the index returns to the first element. After writing one permutation, find the next non-visited element.

Cycles in permutations Pre: Post: // Pre: P is a vector with a permutation of 0..n-1 // Post: The cycles of the permutation have been written in cout void print_cycles(const vector & P) { int n = P.size(); vector visited(n, false); int i = 0; while (i < n) { // All the cycles containing 0..i-1 have been written bool cycle = false; while (not visited[i]) { if (not cycle) cout << (; else cout << ; // Not the first element cout << i; cycle = true; visited[i] = true; i = P[i]; } if (cycle) cout << ) << endl; // We have returned to the beginning of the cycle i = i + 1; } } Introduction to Programming© Dept. CS, UPC26

Taylor and McLaurin series Many functions can be approximated by using Taylor or McLaurin series, e.g.: Example: sin x Introduction to Programming© Dept. CS, UPC27

Calculating sin x McLaurin series: It is a periodic function (period is 2  ) Convergence improves as x gets closer to zero Introduction to Programming© Dept. CS, UPC28

Calculating sin x Reducing the computation to the (-2 ,2  ) interval: Incremental computation of terms: Introduction to Programming© Dept. CS, UPC29

Calculating sin x Post: #include // Post: returns an approximation of sin x. double sin_approx(double x) { int k = int(x/(2M_PI)); x = x – 2kM_PI; // reduce to the (-2,2) interval double term = x; double x2 = xx; int d = 1; double sum = term; while (abs(term) >= 1e-8) { term = -termx2/((d+1)(d+2)); sum = sum + term; d = d + 2; } return sum; } Introduction to Programming© Dept. CS, UPC30

Lattice paths We have an n  m grid. How many different routes are there from the bottom left corner to the upper right corner only using right and up moves? Introduction to Programming© Dept. CS, UPC31

Lattice paths Some properties:  paths(n, 0) = paths(0, m) = 1  paths(n, m) = paths(m, n)  If n > 0 and m > 0: paths(n, m) = paths(n-1, m) + paths(n, m-1) Introduction to Programming© Dept. CS, UPC32

Lattice paths Pre: Post: // Pre: n and m are the dimensions of a grid // (n  0 and m  0) // Post: returns the number of lattice paths // in the grid int paths(int n, int m) { if (n == 0 or m == 0) return 1; return paths(n – 1, m) + paths(n, m – 1); } Introduction to Programming© Dept. CS, UPC33

Lattice paths Introduction to Programming© Dept. CS, UPC How large is the tree (cost of the computation)? Observation: many computations are repeated

Lattice paths Introduction to Programming© Dept. CS, UPC

Lattice paths Introduction to Programming© Dept. CS, UPC36 Pre: Post: // Pre: n and m are the dimensions of a grid // (n  0 and m  0) // Post: returns the number of lattice paths in the grid int paths(int n, int m) { vector > M(n + 1, vector (m + 1)); // Initialize row 0 for (int j = 0; j <= m; ++j) M[0][j] = 1; // Fill the matrix from row 1 for (int i = 1; i <= n; ++i) { M[i][0] = 1; for (int j = 1; j <= m; ++j) { M[i][j] = M[i – 1][j] + M[i][j – 1]; } } return M[n][m]; }

Lattice paths Introduction to Programming© Dept. CS, UPC

Lattice paths In a path with n+m segments, select n segments to move right (or m segments to move up) Subsets of n elements out of n+m Introduction to Programming© Dept. CS, UPC38

Lattice paths Introduction to Programming© Dept. CS, UPC39

Lattice paths Pre: Post: // Pre: n and m are the dimensions of a grid // (n  0 and m  0) // Post: returns the number of lattice paths in the grid int paths(int n, int m) { return combinations(n + m, n); } Pre: Post: // Pre: n  k  0 // Post: returns the number of k-combinations // of a set of n elements int combinations(int n, int k) { if (k == 0) return 1; return n*combinations(n – 1, k – 1)/k; } Introduction to Programming© Dept. CS, UPC40

Lattice paths Introduction to Programming© Dept. CS, UPC41

Lattice paths How about counting paths in a 3D grid? And in a k-D grid? Introduction to Programming© Dept. CS, UPC42