Programming math - polynomial class



How to write templated polynomial class

I would like to have overloaded operators like
+
-(unary,binary)
* Here we can have school multiplication or Karatsuba multiplication
/
%
== , != (relational , comparing two polynomials)
>>,<< (stream , reading and writing)

It will be good to have
parseString(string s) and toString() methods

Polynomial evaluation - Horner's rule

How should I store coefficients ?
In vector or in array

What constructors and destructor should look like


I found C struct with operations but how to rewrite it to C++

Here is link to this struct

https://pastebin.com/75byrXHW





If you search the internet you will find several examples of templated class C++ code for polynomials.
I love peoples who have nothing to write about given topic but answering it anyway
hej kujon, you're just asking people to do your assignment without showing a shred of evidence that you've tried to write any code yourself.

If you don't know how many coefficients you will need, you either need to have an array with a maximum supported size, or you use a dynamic array/vector. The default option in C++ normally is "use vector".

Maybe tackle Polynomial evaluation / Horner's rule last, since that's probably the hardest to calculate.

Start simple, say with == and !=. Then move onto +.

You can represent your polynomial as a sequence of coefficients, e.g.
6 x^3 - 5 x + 3 could be represented as [6, 0, -5, 3].

So checking for equality would just be first checking the that the sizes of two polynomial's are the same (assuming no leading zero coefficients), and then testing for equality element by element. Try it.

Your constructor's argument could be a vector of coefficients. If you do it correctly, you probably won't even need to define a destructor. You might need to simplify your polynomial if the user enters a polynomial where the highest power(s) have zeroes as their coefficients.

e.g.
1
2
3
std::vector<int> simplify(const std::vector<int>& coeffs) {

}


but I'm getting ahead of myself, I wouldn't even worry about that yet.

Write test cases as you build functionality with expected results.

e.g.
1
2
3
4
5
6
7
8
// Test ==
Assert(Polynomial({1, 2, 3}) == Polynomial({1, 2, 3}));
Assert(Polynomial({1, 2, 3}) != Polynomial({1}));

// Test +
Assert(Polynomial({1}) + Polynomial({2}) == Polynomial({3}));
Assert(Polynomial({1, 2}) + Polynomial({1}) == Polynomial({1, 3}));
...


It's not clear what you'd want the "/" operator to do. I mean, if you are just dealing with scalars, then that's trivial, but are you trying to support Polynominal / Polynomial? The answer isn't necessarily a polynomial. If you want "/" to only give the "whole polynomial" and % to only give the remainder, then it could work.
Last edited on
I love peoples who have nothing to write about given topic but answering it anyway


Well you provided a link to some c code and asked for C++ code to be provided without seemingly any effort on your part. NO. We won't do that. If you want Polynomial C++ code then first search for something that (nearly)fits your requirements (hint - there's several), try it out and then ask for any code assistance you may require. Post some code for which you have questions - don't just say I need this please give me without any effort on my part. Converting the c code to C++ is a waste of time as there are already existing C++ codes for this task.

Seeplus so why you are wasting your precious time writing posts in such threads

Ganado ,

just asking people to do your assignment without showing a shred of evidence that you've tried to write any code yourself


If I knew how to do this I would have done it myself and I would not have written it

I had some C++ lessons at school but quarter century ago
so I dont remember much , especially from object programming


Yes operator / should return the quotient and
operator % should return remainder

But let's start with constructors, prototypes and stream operators

But lets start with existing C++ code otherwise you are wasting your time and mine. if you'd started with some existing C++ code by now you'd have working code that could be modified if required. Instead all you're doing is to complain that you're not been provided with the code and are no further forward.

If you need to learn c++ then
https://www.learncpp.com/
unless you need to do so for a specific reason (such as, compatibility with code that uses the C version), then converting the C code into C++ will be more difficult and less clean than either starting over or grabbing a C++ source code that does the job.

this is a good exercise to just do yourself. If you think about it, a polynomial is just an array of floats for the variable terms, eg 3x^2+11 is zero x cubed, 3 x squared, zero x to the 1, and 11 x to the zero (one!). doing that in reverse, though, you can leave off the higher powers... no one cares that you have zero x to the 57th power here, so if vector[0] is 11, vector[1] is 0, ... it will be sized correctly to support the highest level power. This is inefficient in that you may end up storing a lot of zero terms for weird equations like x^87 +32, but as for any tool, you have to ask what the data will look like and how it will be used. If the use is for typical textbook polynomials of 10th and less powers, that is a non issue.

so with nothing but a vector and some rather simple overloaded operators with a pretty print routine, you can have it up and running in very short order using the above straightforward approach. Multiplication is probably the only 'difficult' operation and the problem is well understood (assuming you got out of high school), just the code might have some complexity (meaning, its not just 10-15 lines for that one). Evaluation (meaning, provide a value for x and get a number?) doesn't require anything special; its just a sum of terms of something like for(... the vector) {current_xpower *= x; result += vec[i]*current_xpower;}. I don't even remember horner's rule but that is pretty much it (and maybe an if statement for when xpower == 0 depending on how you handled that part of the logic).

get it started... get your stream out operator going so you can print what you have, and basic storage vector in play. Then do something simple like unary - which is just for(vec) vec[i] = -vec[i]; Test that one,... work your way up to addition and finish up with multiplication last.

parse string and to string vs << and >> .. In my opinion the stream operators should reuse the string functions as a part of their logic. I have always preferred that my objects simply produce strings (whether they accept them or not) so that a text program can use << and >> on the string itself but a UI program can just use the strings directly (setwindowtext class.tostring(..) type stuff). I rarely want or use the << and >> in a real program, though you may need them if you use text files (here again, the tostring should be sufficient and the stream operation isnt enabled for the class is just fine).
Last edited on
Registered users can post here. Sign in or register to post.