Product of polynomils Introduction to Progrmming (in C++) Numericl lgorithms Jordi Cortdell, Ricrd Gvldà, Fernndo Orejs Dept. of Computer Science, UPC Given two polynomils on one vrile nd rel coefficients, compute their product (we will decide lter how we represent polynomils) Exmple: given x 2 + 3x - 1 nd 2x - 5, otin 2x 3-5x 2 + 6x 2-15x - 2x + 5 = 2x 3 + x 2-17x + 5 Key point: Product of polynomils Given p(x) = n x n + n-1 x n-1 + + 1 x + 0 nd q(x) = m x m + m-1 x m-1 + + 1 x + 0, wht is the coefficient c i of x i in (p*q)(x)? Introduction to Progrmming Dept. CS, UPC 2 Product of polynomils Suppose we represent polynomil of degree n y vector of size n+1. Tht is, v[0..n] represents the polynomil v[n] x n + v[n-1] x n-1 + + v[1] x + v[0] We otin x i+j whenever we multiply i x i j x j Ide: for every i nd j, dd i j to the (i+j)-th coefficient of the product polynomil. We wnt to mke sure tht v[v.size() - 1] 0 so tht degree(v) = v.size() - 1 The only exception is the constnt-0 polynomil. We ll represent it y vector of size 0. Introduction to Progrmming Dept. CS, UPC 3 Introduction to Progrmming Dept. CS, UPC 4
Product of polynomils Product of polynomils Polynomil product(const Polynomil& p, const Polynomil& q) { typedef vector<doule> Polynomil; // Pre: -- // Returns p q Polynomil product(const Polynomil& p, const Polynomil& q); // Specil cse for polynomil of size 0 if (p.size() == 0 or q.size() == 0) return Polynomil(0); else { int deg = p.size() 1 + q.size() - 1; // degree of p q Polynomil r(deg + 1, 0); for (int i = 0; i < p.size(); ++i) { for (int j = 0; j < q.size(); ++j) { r[i + j] = r[i + j] + p[i] q[j]; return r; // Invrint (of the outer loop): r = product p[0..i-1] q // (we hve used the coefficients p[0] p[i-1]) Introduction to Progrmming Dept. CS, UPC 5 Sum of polynomils Note tht over the rel numers, degree(p q) = degree(p) + degree(q) (except if p = 0 or q = 0). So we know the size of the result vector from the strt. This is not true for the polynomil sum, e.g. Introduction to Progrmming Dept. CS, UPC 6 Sum of polynomils // Pre: -- // Returns p+q Polynomil sum(const Polynomil& p, const Polynomil& q); int mxdeg = mx(p.size(), q.size()) - 1; int deg = -1; Polynomil r(mxdeg + 1, 0); // Inv r[0..i-1] = (p+q)[0..i-1] nd // deg = lrgest j s.t. r[j]!= 0 (or -1 if none exists) for (int i = 0; i <= mxdeg; ++i) { if (i >= p.size()) r[i] = q[i]; else if (i >= q.size()) r[i] = p[i]; else r[i] = p[i] + q[i]; if (r[i]!= 0) deg = i; degree((x + 5) + (-x - 1)) = 0 Polynomil rr(deg + 1); for (int i = 0; i <= deg; ++i) rr[i] = r[i]; return rr; Introduction to Progrmming Dept. CS, UPC 7 Introduction to Progrmming Dept. CS, UPC 8
Sum of sprse vectors Sum of sprse vectors In some cses, prolems must del with sprse vectors or mtrices (most of the elements re zero). Sprse vectors nd mtrices cn e represented more efficiently y only storing the non-zero elements. For exmple, vector cn e represented s vector of pirs (index, vlue), sorted in scending order of the indices. Exmple: [0,0,1,0,-3,0,0,0,2,0,0,4,0,0,0] cn e represented s Design function tht clcultes the sum of two sprse vectors, where ech non-zero vlue is represented y pir (index, vlue): struct Pir { int index; int vlue; typedef vector<pir> SprseVector; [(2,1),(4,-3),(8,2),(11,4)] Introduction to Progrmming Dept. CS, UPC 9 Sum of sprse vectors Introduction to Progrmming Dept. CS, UPC 10 Sum of sprse vectors // Pre: -- // Returns v1+v2 SprseVector sprse_sum(const SprseVector& v1, const SprseVector& v2); // Inv: p1 nd p2 will point to the first // non-treted elements of v1 nd v2. // vsum contins the elements of v1+v2 treted so fr. // psum points to the first free loction in vsum. Strtegy: Clculte the sum on sufficiently lrge vector. Copy the result on nother vector of pproprite size. SprseVector sprse_sum(const SprseVector& v1, const SprseVector& v2) { SprseVector vsum; int p1 = 0, p2 = 0; while (p1 < v1.size() nd p2 < v2.size()) { if (v1[p1].index < v2[p2].index) { // Element only in v1 vsum.push_ck(v1[p1]); ++p1; else if (v1[p1].index > v2[p2].index) { // Element only in v2 vsum.push_ck(v2[p2]); ++p2; else { // Element in oth Pir p; p.index = v1[p1].index; p.vlue = v1[p1].vlue + v2[p2].vlue; if (p.vlue!= 0) vsum.push_ck(p); ++p1; ++p2; Introduction to Progrmming Dept. CS, UPC 11 Introduction to Progrmming Dept. CS, UPC 12
Sum of sprse vectors // Copy the remining elements of v1 while (p1 < v1.size()) { vsum.push_ck(v1[p1]); ++p1; Bolzno s theorem: Let f e rel-vlued continuous function. Let nd e two vlues such tht < nd f() f() < 0. Then, there is vlue c [,] such tht f(c)=0. // Copy the remining elements of v2 while (p2 < v2.size()) { vsum.push_ck(v2[p2]); ++p2; return vsum; c Introduction to Progrmming Dept. CS, UPC 13 Design function tht finds root of continuous function f in the intervl [, ] ssuming the conditions of Bolzno s theorem re fulfilled. Given precision ( ), the function must return vlue c such tht the root of f is in the intervl [c, c+ ]. Introduction to Progrmming Dept. CS, UPC 14 Strtegy: nrrow the intervl [, ] y hlf, checking whether the vlue of f in the middle of the intervl is positive or negtive. Iterte until the width of the intervl is smller. c Introduction to Progrmming Dept. CS, UPC 15 Introduction to Progrmming Dept. CS, UPC 16
// Pre: f is continuous, < nd f() f() < 0. // Returns c [,] such tht root exists in the // intervl [c,c+ ]. // Inv: root of f exists in the intervl [,] doule root(doule, doule, doule epsilon) { while ( > epsilon) { doule c = ( + )/2; if (f() f(c) <= 0) = c; else = c; return ; Introduction to Progrmming Dept. CS, UPC 17 // A recursive version doule root(doule, doule, doule epsilon) { if ( <= epsilon) return ; doule c = ( + )/2; if (f() f(c) <= 0) return root(,c,epsilon); else return root(c,,epsilon); Introduction to Progrmming Dept. CS, UPC 18 Brcode A rcode is n opticl mchine-redle representtion of dt. One of the most populr encoding systems is the UPC (Universl Product Code). A UPC code hs 12 digits. Optionlly, check digit cn e dded. Introduction to Progrmming Dept. CS, UPC 19 Introduction to Progrmming Dept. CS, UPC 20
Brcode Brcode The check digit is clculted s follows: 1. Add the digits in odd-numered positions (first, third, fifth, etc.) nd multiply y 3. 2. Add the digits in the even-numered positions (second, fourth, sixth, etc.) to the result. 3. Clculte the result modulo 10. 4. If the result is not zero, sutrct the result from 10. Exmple: 380006571113 (3+0+0+5+1+1) 3 = 30 8+0+6+7+1+3 = 25 (30+25) mod 10 = 5 10 5 = 5 Design progrm tht reds sequence of 12-digit numers tht represent UPCs without check digits nd writes the sme UPCs with the check digit. Question: do we need dt structure to store the UPCs? Answer: no, we only need few uxiliry vriles. Introduction to Progrmming Dept. CS, UPC 21 Brcode The progrm might hve loop treting UPC t ech itertion. The invrint could e s follows: // Inv: ll the UPCs of the treted codes // hve een written. At ech itertion, the progrm could red the UPC digits nd, t the sme time, write the UPC nd clculte the check digit. The invrint could e: // Inv: ll the treted digits hve een // written. The prtil clcultion of // the check digit hs een performed // sed on the treted digits. Introduction to Progrmming Dept. CS, UPC 22 Brcode // Pre: the input contins sequence of UPCs without check digits. // Post: the UPCs t the input hve een written with their check digits. int min() { chr c; while (cin >> c) { cout << c; int d = 3 (int(c) - int('0')); // first digit in n odd loction for (int i = 2; i <= 12; ++i) { cin >> c; cout << c; if (i%2 == 0) d = d + int(c) - int('0'); else d = d + 3 (int(c) - int('0')); d = d%10; if (d > 0) d = 10 d; cout << d << endl; Introduction to Progrmming Dept. CS, UPC 23 Introduction to Progrmming Dept. CS, UPC 24