473,396 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

polynomial operations

3 2Bits
i've got this assignment where the task is to add and multiply polynomials. my question is could it be maintained in the array data structure to make the operations easier? does the algorithm choice have an effect on the data structure?
Oct 15 '20 #1
5 2531
dev7060
636 Expert 512MB
I remember working with polynomial arithmetic. I didn't use a particular named algo to achieve the results, just the common sense math. Linked lists serve the purpose and would represent the polynomial better than the array. Prepare a separate list for every expression. Assign three fields to every node; coefficient part, exponent part, and the pointer to the next expression. You need to know how the traversal in the linked list works to implement the operations.
Oct 15 '20 #2
kristineg
3 2Bits
could you please explain how to get into the implementation part?
thanks a lot for the previous explanation but ugh this is bugging me out for long now LOL. i tried reading online and am stuck in one way or the other. there's only code but no clear explanation to me. i understand the list preparation part but everything after that seems just out of the context.



Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Node *last = *head_ref;
  4. new_node->c = c;
  5. new_node->e = e; 
  6. new_node->next = NULL;   
  7. if (*head_ref == NULL)   
  8. {   
  9. *head_ref = new_node;
  10. return;
  11. }  
  12. while (last->next != NULL)   
  13. last = last->next;   
  14. last->next = new_node;
  15. return;
  16.  
  17.  
  18.  
i'd really appreciate an example or snippet of your approach.


kristine
Oct 16 '20 #3
Banfa
9,065 Expert Mod 8TB
What language are you using? If you are using C++ I would say a std::map lends itself well to working with polynomials in a single variable with an unstated maximum degree.

Alternatively in C a variable sized array is probably better than a linked list because order is important but there isn't that much data to carry and being able to index straight into the array for constant for a specific degree seems to be to have some value in efficiency. (You could apply this argument to C++ too actually)

The code snippet you have posted appears to be part of a function to add a new node to a linked list although it appears to be missing the actual allocation of the node.
Oct 16 '20 #4
dev7060
636 Expert 512MB
i tried reading online and am stuck in one way or the other. there's only code but no clear explanation to me. i understand the list preparation part but everything after that seems just out of the context.
You didn't specify what exact part you are having difficulty understanding from what you're reading.

could you please explain how to get into the implementation part?
i'd really appreciate an example or snippet of your approach.
I was building a scientific calculator back in the days as a hobby project. I've pulled up some code from it related to polynomial operations. Keep in mind that this involves a few bad practices since I was new to algorithms and programming at that time. More like I was trying to be the jack of all trades and ended up mixing c & cpp concepts for some reason. But everything does work as one lang is the superset of the other. Hopefully, it would give you the idea of the algo and implementation and prove helpful. Here are few things you may need to consider to change (for c++); use new instead of malloc, use class data type for the list, assign proper names to the variables, free the memory wherever required, plus other patches that you feel like returning and accepting relevant pointers from/to the functions, etc. Also, the code isn't prepared for the boundary and invalid inputs. For the addition part; if any of the two lists is empty, the other will be the result. If both are non-empty; the coefficients of the nodes having the same exponents are added. The next step is to check what list is completely traversed, the rest nodes of the other are then added to the final expression as it is. In the multiplication one, nested loops are used to multiply the fields of one expression with another and to cover every node. Add the coefficients of the terms having the same exponents to produce the final expression. I've used a separate list to keep track of the unique coefficients. Other checks can also be included that were there in the addition part.

Expand|Select|Wrap|Line Numbers
  1. /*
  2. *
  3. *
  4. * Author: deV
  5. * Last modified: Jan 28, 2018
  6. * License: GPL-3.0
  7. ** Master branch src **
  8. */
  9. #include <iostream>
  10. #include <stdlib.h>
  11. #include "poly.h"
  12. using namespace std;
  13. Poly::Poly()
  14. {
  15.     start = NULL;
  16.     begin1 = NULL;
  17.     add = NULL;
  18.     global_ch = 0;    //global_ch=1 -> subtraction
  19. }
  20.  
  21. Poly::~Poly() {}
  22.  
  23. void Poly::first()
  24. {
  25.     int cont;
  26.     struct POLYS *node, *temp1;
  27.     cout << "Insertion of first expression:\n";
  28.     do {
  29.         node = (struct POLYS *) malloc(sizeof(struct POLYS));
  30.         cout << "Enter coef: ";
  31.         cin >> node->coef;
  32.         cout << "Enter exponent:";
  33.         cin >> node->exp;
  34.         node->next = NULL;
  35.         temp1 = start;
  36.         if (start == NULL)
  37.         {
  38.             start = node;
  39.         }
  40.         else
  41.         {
  42.             while (temp1->next != NULL)
  43.             {
  44.                 temp1 = temp1->next;
  45.             }
  46.  
  47.             temp1->next = node;
  48.         }
  49.  
  50.         cout << "First expression is: ";
  51.         struct POLYS *view1 = start;
  52.         int a = 0, i = 0;
  53.         while (view1 != NULL)
  54.         {
  55.             a++;
  56.             view1 = view1->next;
  57.         }
  58.  
  59.         view1 = start;
  60.         while (view1 != NULL)
  61.         {
  62.             if (view1->coef != 0)
  63.                 cout << "[" << view1->coef << "x^" << view1->exp << "]";
  64.             if (i < (a - 1))
  65.                 cout << "+";
  66.             i++;
  67.             view1 = view1->next;
  68.         }
  69.  
  70.         cout << "\nPress 5 to continue the first expression insertion: ";
  71.         cin >> cont;
  72.     } while (cont == 5);
  73. }
  74.  
  75. void Poly::second()
  76. {
  77.     int cont;
  78.     struct POLYS *node, *temp2;
  79.     cout << "Insertion of second expression:\n";
  80.     do {
  81.         node = (struct POLYS *) malloc(sizeof(struct POLYS));
  82.         cout << "Enter coef: ";
  83.         cin >> node->coef;
  84.         cout << "Enter exponent:";
  85.         cin >> node->exp;
  86.         node->next = NULL;
  87.         temp2 = begin1;
  88.         if (begin1 == NULL)
  89.         {
  90.             begin1 = node;
  91.         }
  92.         else
  93.         {
  94.             while (temp2->next != NULL)
  95.             {
  96.                 temp2 = temp2->next;
  97.             }
  98.  
  99.             temp2->next = node;
  100.         }
  101.  
  102.         cout << "Second expression is: ";
  103.         struct POLYS *view2 = begin1;
  104.         int a = 0, i = 0;
  105.         while (view2 != NULL)
  106.         {
  107.             a++;
  108.             view2 = view2->next;
  109.         }
  110.  
  111.         view2 = begin1;
  112.         while (view2 != NULL)
  113.         {
  114.             if (view2->coef != 0)
  115.                 cout << "[" << view2->coef << "x^" << view2->exp << "]";
  116.             if (i < (a - 1))
  117.                 cout << "+";
  118.             i++;
  119.             view2 = view2->next;
  120.         }
  121.  
  122.         cout << "\nPress 5 to continue the second expression insertion: ";
  123.         cin >> cont;
  124.     } while (cont == 5);
  125. }
  126.  
  127. void Poly::addpoly()
  128. {
  129.     struct POLYS *temp1, *temp2, *temp3, *node;
  130.     if (start == NULL && begin1 == NULL)
  131.     {
  132.         cout << "No expression!";
  133.     }
  134.     else if (start != NULL && begin1 == NULL)
  135.     {
  136.         temp1 = start;
  137.         while (temp1 != NULL)
  138.         {
  139.             cout << temp1->coef << "x^" << temp1->exp;
  140.             temp1 = temp1->next;
  141.         }
  142.     }
  143.     else if (start == NULL && begin1 != NULL)
  144.     {
  145.         temp2 = begin1;
  146.         while (temp2 != NULL)
  147.         {
  148.             cout << temp2->coef << "x^" << temp2->exp;
  149.             temp2 = temp2->next;
  150.         }
  151.     }
  152.     else if (start != NULL && begin1 != NULL)
  153.     {
  154.         temp1 = start;
  155.         temp2 = begin1;
  156.         while (temp1 != NULL && temp2 != NULL)
  157.         {
  158.             node = (struct POLYS *) malloc(sizeof(struct POLYS));
  159.             if (temp1->exp > temp2->exp)
  160.             {
  161.                 node->coef = temp1->coef;
  162.                 node->exp = temp1->exp;
  163.                 node->next = NULL;
  164.                 temp1 = temp1->next;
  165.             }
  166.             else if (temp2->exp > temp1->exp)
  167.             {
  168.                 node->coef = temp2->coef;
  169.                 node->exp = temp2->exp;
  170.                 node->next = NULL;
  171.                 temp2 = temp2->next;
  172.             }
  173.             else if (temp1->exp == temp2->exp)
  174.             {
  175.                 node->coef = temp1->coef + temp2->coef;
  176.                 node->exp = temp1->exp;
  177.                 node->next = NULL;
  178.                 temp1 = temp1->next;
  179.                 temp2 = temp2->next;
  180.             }
  181.  
  182.             temp3 = add;
  183.             if (add == NULL)
  184.             {
  185.                 add = node;
  186.             }
  187.             else
  188.             {
  189.                 while (temp3->next != NULL)
  190.                 {
  191.                     temp3 = temp3->next;
  192.                 }
  193.  
  194.                 temp3->next = node;
  195.             }
  196.         }    //end of while
  197.         if (temp1 != NULL && temp2 == NULL)
  198.         {
  199.             while (temp1 != NULL)
  200.             {
  201.                 node = (struct POLYS *) malloc(sizeof(struct POLYS));
  202.                 node->coef = temp1->coef;
  203.                 node->exp = temp1->exp;
  204.                 node->next = NULL;
  205.                 temp3 = add;
  206.                 while (temp3->next != NULL)
  207.                     temp3 = temp3->next;
  208.                 temp3->next = node;
  209.                 temp1 = temp1->next;
  210.             }
  211.         }
  212.         else if (temp2 != NULL && temp1 == NULL)
  213.         {
  214.             while (temp2 != NULL)
  215.             {
  216.                 node = (struct POLYS *) malloc(sizeof(struct POLYS));
  217.                 node->coef = temp2->coef;
  218.                 node->exp = temp2->exp;
  219.                 node->next = NULL;
  220.                 temp3 = add;
  221.                 while (temp3->next != NULL)
  222.                     temp3 = temp3->next;
  223.                 temp3->next = node;
  224.                 temp2 = temp2->next;
  225.             }
  226.         }
  227.  
  228.         if (global_ch == 0)
  229.             cout << "Added expression is: ";
  230.         else if (global_ch == 1)
  231.             cout << "Subtracted expression is: ";
  232.         temp3 = add;
  233.         int a = 0, i = 0;
  234.         while (temp3 != NULL)
  235.         {
  236.             a++;
  237.             temp3 = temp3->next;
  238.         }
  239.  
  240.         temp3 = add;
  241.         while (temp3 != NULL)
  242.         {
  243.             if (temp3->coef != 0)
  244.                 cout << "[" << temp3->coef << "x^" << temp3->exp << "]";
  245.             if (i < (a - 1))
  246.             {
  247.                 cout << "+";
  248.             }
  249.  
  250.             i++;
  251.             temp3 = temp3->next;
  252.         }
  253.     }    //end of outer if else
  254. }
  255.  
  256. void Poly::subpoly()
  257. {
  258.     struct POLYS *temp = begin1;
  259.     while (temp != NULL)
  260.     {
  261.         temp->coef = -temp->coef;
  262.         temp = temp->next;
  263.     }
  264.  
  265.     global_ch = 1;
  266.     addpoly();
  267.     global_ch = 0;
  268. }
  269.  
  270. void Poly::mulpoly()
  271. {
  272.     struct POLYS *temp1 = start;
  273.     struct POLYS *temp2 = begin1;
  274.     struct POLYS * mul_beg;
  275.     mul_beg = NULL;
  276.     struct UN_COE_LIST * unique_coef_list;
  277.     unique_coef_list = NULL;
  278.     struct POLYS * temp4;
  279.     while (temp1 != NULL)
  280.     {
  281.         temp2 = begin1;
  282.         while (temp2 != NULL)
  283.         {
  284.             struct POLYS * node;
  285.             node = (struct POLYS *) malloc(sizeof(struct POLYS));
  286.             node->coef = (temp1->coef) *(temp2->coef);
  287.             node->exp = (temp1->exp) + (temp2->exp);
  288.             node->next = NULL;
  289.             if (mul_beg == NULL)
  290.             {
  291.                 mul_beg = node;
  292.             }
  293.             else
  294.             {
  295.                 temp4 = mul_beg;
  296.                 while (temp4->next != NULL)
  297.                 {
  298.                     temp4 = temp4->next;
  299.                 }
  300.  
  301.                 temp4->next = node;
  302.             }
  303.  
  304.             temp2 = temp2->next;
  305.         }
  306.  
  307.         temp1 = temp1->next;
  308.     }
  309.  
  310.     temp4 = mul_beg;
  311.     int indicator;
  312.     //loop to add the unique exp values to the unique_coef_list
  313.     while (temp4 != NULL)
  314.     {
  315.         struct UN_COE_LIST * node;
  316.         node = (struct UN_COE_LIST *) malloc(sizeof(struct POLYS));
  317.         node->exp = temp4->exp;
  318.         node->next = NULL;
  319.         indicator = 10;
  320.         //10=coef not present
  321.         //11=coef already present
  322.         struct UN_COE_LIST * temp5;
  323.         temp5 = unique_coef_list;
  324.         while (temp5 != NULL)
  325.         {
  326.             if (temp5->exp == node->exp)
  327.             {
  328.                 indicator = 11;
  329.             }
  330.             else
  331.             {
  332.                 indicator = 10;
  333.             }
  334.  
  335.             temp5 = temp5->next;
  336.         }
  337.  
  338.         if (indicator == 10)
  339.         {
  340.             if (unique_coef_list == NULL)
  341.             {
  342.                 unique_coef_list = node;
  343.             }
  344.             else
  345.             {
  346.                 struct UN_COE_LIST * temp6;
  347.                 temp6 = unique_coef_list;
  348.                 while (temp6->next != NULL)
  349.                 {
  350.                     temp6 = temp6->next;
  351.                 }
  352.  
  353.                 temp6->next = node;
  354.             }
  355.         }
  356.  
  357.         temp4 = temp4->next;
  358.     }
  359.  
  360.     struct UN_COE_LIST * temp6;
  361.     temp6 = unique_coef_list;
  362.     int a = 0, i = 0;
  363.     while (temp6 != NULL)
  364.     {
  365.         a++;
  366.         temp6 = temp6->next;
  367.     }
  368.  
  369.     temp6 = unique_coef_list;
  370.     /*testing unique_coef_list
  371.     while(temp6!=NULL){
  372.             cout<<"["<<temp6->exp<<"]";
  373.             temp6=temp6->next;
  374.         }
  375.  
  376.     */
  377.     /*testing mul_beg list
  378.     temp6=mul_beg;
  379.     while(temp6!=NULL){
  380.             if(temp6->coef!=0)
  381.                 cout<<"["<<temp6->coef<<"x^"<<temp6->exp<<"]";
  382.             if(i < (a-1)){
  383.                 cout<<"+";
  384.             }
  385.  
  386.             i++;
  387.             temp6=temp6->next;
  388.         }
  389.  
  390.         */
  391.     struct UN_COE_LIST * ntemp1;
  392.     struct POLYS * ntemp2;
  393.     struct POLYS *final_list = NULL;
  394.     ntemp1 = unique_coef_list;
  395.     ntemp2 = mul_beg;
  396.     int
  397.     var = 0;
  398.     while (ntemp1 != NULL)
  399.     {
  400.         ntemp2 = mul_beg;
  401.         while (ntemp2 != NULL)
  402.         {
  403.             if (ntemp2->exp == ntemp1->exp)
  404.             {
  405.                 var =
  406.                 var +(ntemp2->coef);
  407.             }
  408.  
  409.             ntemp2 = ntemp2->next;
  410.         }
  411.  
  412.         struct POLYS * node;
  413.         node = (struct POLYS *) malloc(sizeof(struct POLYS));
  414.         node->coef =
  415.             var;
  416.         node->exp = ntemp1->exp;
  417.         node->next = NULL;
  418.         struct POLYS * ptr;
  419.         ptr = final_list;
  420.         //to skip the same exp term
  421.         int p = 0;
  422.         //p=0 if not present
  423.         //p=1 if present
  424.         while (ptr != NULL)
  425.         {
  426.             if (ptr->exp == node->exp)
  427.                 p = 1;
  428.             ptr = ptr->next;
  429.         }
  430.  
  431.         if (p == 0)
  432.         {
  433.             if (final_list == NULL)
  434.             {
  435.                 final_list = node;
  436.             }
  437.             else
  438.             {
  439.                 struct POLYS * temp7;
  440.                 temp7 = final_list;
  441.                 while (temp7->next != NULL)
  442.                 {
  443.                     temp7 = temp7->next;
  444.                 }
  445.  
  446.                 temp7->next = node;
  447.             }
  448.         }
  449.  
  450.         p = 0;
  451.         var = 0;
  452.         ntemp1 = ntemp1->next;
  453.     }
  454.  
  455.     struct POLYS * temp8;
  456.     temp8 = final_list;
  457.     i = 0;
  458.     a = 0;
  459.     while (temp8 != 0)
  460.     {
  461.         a++;
  462.         temp8 = temp8->next;
  463.     }
  464.  
  465.     temp8 = final_list;
  466.     cout << "Multiplied expression: ";
  467.     while (temp8 != NULL)
  468.     {
  469.         if (temp8->coef != 0)
  470.             cout << "[" << temp8->coef << "x^" << temp8->exp << "]";
  471.         if (i < (a - 1))
  472.         {
  473.             cout << "+";
  474.         }
  475.  
  476.         i++;
  477.         temp8 = temp8->next;
  478.     }
  479. }
  480.  
Structure blueprints:
Expand|Select|Wrap|Line Numbers
  1. struct POLYS{
  2.     int coef;
  3.     int exp;
  4.     struct POLYS *next;
  5. };
  6. struct UN_COE_LIST{
  7.     int exp;
  8.     struct UN_COE_LIST *next;
  9. };
Oct 16 '20 #5
kristineg
3 2Bits
I am using C++. thank you both Banfa and dev7060 for the advice. it starts making sense now when i have a little theory. dev7060, i will have a deep study of your code and reach out for the doubts. thank you so much. your post was very helpful.
Oct 17 '20 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Just | last post by:
While googling for a non-linear equation solver, I found Math::Polynomial::Solve in CPAN. It seems a great little module, except it's not Python... I'm especially looking for its poly_root()...
9
by: strotee76 | last post by:
What do I need to do to setTerm() for it to work properly? If you notice any other glaring issues, then please let me know. With this header file: ======================= #ifndef Polynomial_h...
1
by: Rubén Campos | last post by:
I've trying to implement polynomials of arbitrary order as a C++ template, as shown here: template <unsigned long int N> class Polynomial { public: Polynomial (); ~Polynomial ();
2
by: Chen L. | last post by:
Hi all, If I have a 32-bit data M, and the CRC genrator polynomial G(x),which power is 32, then I can get the CRC checkword R by the following algorithm: X^32*M(x) = Q(x)G(x) + R(x), But how...
14
by: Tiza Naziri | last post by:
Hi, Anybody have an idea on how to start writing a C code for generating the inverse of finite field GF(2^8) using extended Euclidean algorithm? What I mean is how to represent a polynomial,...
12
by: daniel.wolff | last post by:
I am looking for a quick C program that takes n+1 pairs of values (integers) (a_i, f(a_i)), i=0,...,n, generates the coefficients \alpha_i, i=0,...,n of the polynomial of degree n that fits these...
1
by: madman228 | last post by:
Hi guys I have run in to a littl bit of trouble. I am writing a class called polynomial in which i need a derivative method I have everything, just dont know how to start the derivative method. Any...
1
by: Cassandra Walke | last post by:
I am having trouble writing my c++ code for a polynomial I need help with istream and operator* Here is my code: #include "polynomial.h" #include <iostream> using namespace std; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.