The Polynomial ADT By Karim Kaddoura For CS146-6.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Chapter 25 Lists, Stacks, Queues, and Priority Queues
A f r i d i i m r a n S O L V IN G S Y ST E M S O F E Q U A T I O N S A f r i d i i m r a n
TK1924 Program Design & Problem Solving Session 2011/2012
There is a pattern for factoring trinomials of this form, when c
Chapter 17 vector and Free Store Bjarne Stroustrup
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Chapter 3: Linked List Tutor: Angie Hui
How to Factor Quadratics of the Form
Chapter R: Reference: Basic Algebraic Concepts
Pointers.
Solve Multi-step Equations
Factoring Trinomials.
Adding and Subtracting Polynomials Whats a polynomial?
Factoring Quadratics — ax² + bx + c Topic
Lists CS 3358.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Lists Chapter 6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Data Structures Using C++
LIST PROCESSING.
Modern Programming Languages, 2nd ed.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
ABC Technology Project
Digital Lessons on Factoring
Hash Tables.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
Introduction to Complex Numbers
Copyright © 2013, 2009, 2005 Pearson Education, Inc.
LIAL HORNSBY SCHNEIDER
Review Pseudo Code Basic elements of Pseudo code
Copyright © 2013, 2009, 2006 Pearson Education, Inc.
Copyright © 2013, 2009, 2006 Pearson Education, Inc. 1 Section 5.5 Dividing Polynomials Copyright © 2013, 2009, 2006 Pearson Education, Inc. 1.
Constant, Linear and Non-Linear Constant, Linear and Non-Linear
Warm-Up DEGREE is Even/Odd, LEADING COEFFICIENT is Positive/Negative,
Copyright © 2013, 2009, 2006 Pearson Education, Inc. 1 Section 5.4 Polynomials in Several Variables Copyright © 2013, 2009, 2006 Pearson Education, Inc.
By Dr. Julia Arnold Tidewater Community College
Do Now 10/22/ = 10 = ? Copy HW in your planner.
Warm up Factor: 1. p2 + 13p – a2 – 12a – x2 – 9x – 8.
Multiply Binomials (ax + b)(cx +d) (ax + by)(cx +dy)
Virginia Birch MFNERC Numeracy Specialist
U1A L1 Examples FACTORING REVIEW EXAMPLES.
Exponents and Radicals
a*(variable)2 + b*(variable) + c
Data Structures Using C++ 2E
Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
Section P4 Polynomials. How We Describe Polynomials.
Linked list Applications: Polynomial handling. Representing a polynomial using a linked list Store the coefficient and exponent of each term in nodes.
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Polynomial Addition using Linked lists
© Copyright by Houghton Mifflin Company. All rights reserved.1 Monomials & Polynomials  Define monomials and polynomials.  Determine the degree of a.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
Algebra II Honors POD Multiply the following: Homework: p odds.
Aims: To understand what a polynomial is To be able to add and subtract polynomials To be able to multiply polynomials Polynomials/Factorisation Lesson.
UNIT – I Linked Lists.
What is a monomial In one variable; is the product of a constant and a variable raised to a non negative integer power. The form is axk a is the constant.
Further Data Structures
Adding and Subtracting Polynomials
Section 5.3 Polynomials and Polynomial Functions
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Presentation transcript:

The Polynomial ADT By Karim Kaddoura For CS146-6

Polynomial ADT What is it? ( Recall it from mathematics ) An example of a single variable polynomial: 4x x 4 - 5x + 3 Remark: the order of this polynomial is 6 (look for highest exponent)

Polynomial ADT (continued) Why call it an Abstract Data Type (ADT)? A single variable polynomial can be generalized as:

Polynomial ADT (continued) …This sum can be expanded to: a n x n + a (n-1) x (n-1) + … + a 1 x 1 + a 0 Notice the two visible data sets namely: (C and E), where C is the coefficient object [Real #]. and E is the exponent object [Integer #].

Polynomial ADT (continued) Now what? By definition of a data types: A set of values and a set of allowable operations on those values. We can now operate on this polynomial the way we like…

What kinds of operations? Here are the most common operations on a polynomial: Add & Subtract Multiply Differentiate Integrate etc… Polynomial ADT (continued)

Why implement this? Calculating polynomial operations by hand can be very cumbersome. Take differentiation as an example: d(23x x x x 4 + 5x + 3)/dx = (23*9)x (9-1) + (18*7)x (7-1) + (41*6)x (6-1) + …

Polynomial ADT (continued) How to implement this? There are different ways of implementing the polynomial ADT: Array (not recommended) Double Array (inefficient) Linked List (preferred and recommended)

Polynomial ADT (continued) Array Implementation: p1(x) = 8x 3 + 3x 2 + 2x + 6 p2(x) = 23x x Index represents exponents p1(x)p2(x)

This is why arrays arent good to represent polynomials: p3(x) = 16x x 5 + 2x + 6 Polynomial ADT (continued) ………… WASTE OF SPACE!

Polynomial ADT (continued) Advantages of using an Array: only good for non-sparse polynomials. ease of storage and retrieval. Disadvantages of using an Array: have to allocate array size ahead of time. huge array size required for sparse polynomials. Waste of space and runtime.

Polynomial ADT (continued) Double Array Implementation: Say you want to represent the following two polynomials: p1(x) = 23x x x x 4 - 5x + 3 p2(x) = 4x x x + 8

p1(x) = 23x x x x 4 - 5x + 3 p2(x) = 4x x x + 8 Polynomial ADT (continued) Coefficient Exponent Start p1(x)Start p2(x) End p1(x)End p2(x) 0 28

Polynomial ADT (continued) Advantages of using a double Array: save space (compact) Disadvantages of using a double Array: difficult to maintain have to allocate array size ahead of time more code required for misc. operations.

Polynomial ADT (continued) Linked list Implementation: p1(x) = 23x x x x p2(x) = 4x x x P1 P2 NODE (contains coefficient & exponent) TAIL (contains pointer)

Polynomial ADT (continued) Advantages of using a Linked list: save space (dont have to worry about sparse polynomials) and easy to maintain dont need to allocate list size and can declare nodes (terms) only as needed Disadvantages of using a Linked list : cant go backwards through the list cant jump to the beginning of the list from the end.

Polynomial ADT (continued) Adding polynomials using a Linked list representation: (storing the result in p3) To do this, we have to break the process down to cases: Case 1: exponent of p1 > exponent of p2 Copy node of p1 to end of p3. [go to next node] Case 2: exponent of p1 < exponent of p2 Copy node of p2 to end of p3. [go to next node]

Polynomial ADT (continued) Case 3: exponent of p1 = exponent of p2 Create a new node in p3 with the same exponent and with the sum of the coefficients of p1 and p2.

Polynomial ADT (continued) Introducing Horners rule: -Suppose for simplicity we use an array to represent the following non-sparse polynomial: 4x x 2 + 5x Place it in an array, call it a[i], and compute it…

Polynomial ADT (continued) 4x x 2 + 5x + 3 A general (and inefficient) algorithm: int Poly = 0; int Multiply; for (int i=0; i < a.Size; i++) { Multiply =1; for (int j=0; j<i; j++) { Multiply *= x; } Poly += m*a[i]; } ijPoly x x + 5x x 2 + 5x x + 10x 2 + 5x x x 2 + 5x x x 2 + 5x + 3 Time Complexity O(n 2 )

Polynomial ADT (continued) 4x x 2 + 5x + 3 Now using Horners rule algorithm: int Poly = 0; for (int i = (a.Size-1); i >= 0 ; i++) { Poly = x * Poly + a[i]; } iPoly 34 24x x2 + 10x x3 +10x2 + 5x +3 Time Complexity O(n) MUCH BETTER!

Polynomial ADT (continued) So what is Horners rule doing to our polynomial? instead of: ax 2 + bx + c Horners simplification: x(x(a)+b)+c

Polynomial ADT (continued) So in general a n x n + a (n-1) x (n-1) + … + a 1 x 1 + a 0 EQUALS: ( ( (a n + a (n-1) )x + a (n-2) ) x + … + a 1 ) x + a 0