Yes you may use it for commercial use and are free to modify the code as you wish.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
First off I commend you for investigating what I think is one of the most difficult classes to write. As always with a polynomial class, it is a case of compromises, and further improvements as scope and efficiency are improved.
There are a few aspects that I think the code would be better for.
First is that obviously polynomials are rarely integer, double or complex are common, and more interesting polynomials exist.
Second: Please don't do copy by value, use a copy by reference:
Polynomial minus ( Polynomial b ) // this copies a 100 integer array!!
{ }
it is better to write:
Polynomial minus(const Polynomial& B) const
{ }
I have added (a) constant and (b) B is copied by reference.
Finally, care needs to be taken with the degree. Often a polynomial system is being simplified. You would expect the degree to reduce in addition/subtraction. e.g
x^2+6x +3= 0 ; -x^2-4x+2=0 in addition make 2x+5=0 and the degree has reduced by one.
Basically, a polynomial class seems easy on the surface, but is horribly complex. There are significant problems along the way. Numerical issues abound, what do do about division, efficiency etc. Solutions, etc, special cases [quadratic->quintic],
and also do you want to handle 1/x etc.
What always seems to happen, is the the polynomial class is converted into a specialized polynomial class for the local use, and a different polynomial class ends up begin written for each project.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
>First off I commend you for investigating what I think is one of the most difficult classes to write.
Thank you... But the hard work was taken from a java snippet. Really all I did was try to port it to c++ and probably did a p.iss poor job of it too.
>As always with a polynomial class, it is a case of compromises, and further improvements as scope and efficiency are improved.
Indeed.
>There are a few aspects that I think the code would be better for.
First is that obviously polynomials are rarely integer, double or complex are common, and more interesting polynomials exist.
True. I'd love to extend this to handle complex number or even multi-variate polynomials but I'm lazy.
>Second: Please don't do copy by value, use a copy by reference:
Noted.
>Finally, care needs to be taken with the degree. Often a polynomial system is being simplified. You would expect the degree to reduce in addition/subtraction. e.g
x^2+6x +3= 0 ; -x^2-4x+2=0 in addition make 2x+5=0 and the degree has reduced by one.
I believe the class as it stands does this.
>what do do about division
Yes division sure is messy.
I guess if I had all the time and patience in the world I would spend my time writing something like mathematica's engine.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Re: degree of polynomial.
I am sorry I mis-read/failed to understand the code. You are correct, in that the code does indeed find the correct degree.
Obviously, that is work in progress, since you seem to be implementing degree, as a way to improve efficiency. [e.g. limiting loops etc], and to avoid array overrun, in the case of multiplication [e.g. x^51 * x^50 is a problem].
I hope it works out. Everything associated with polynomials seems to be very very expensive. [cpu/memory etc] For example : the representation of quadratic/cubic surfaces in 3D. The solution of a three surface intersect, reducing three f(x,y,z)s, even at only order 2, [e.g. x^2, xy and below] gives a 16th power polynomial and 16^3 (4096) coefficient operations to reduce it from three equations in three variables to one equation in 1 variable. That is extremely expensive!!
I do think that you can just change your array to double or complex and with a few minor tweaks continue. e.g. instead of coeff[i]==0 you can write fabs(coeff[i])<tolerance . You might want to consider implementing a maxAbsCoeff and set the test to be fabs(coeff[i])<tolerance*maxAbsCoeff but depends on the problem set.
As to recreating mathematica, I would just settle for understanding about half of the maths in it.
Anyway, many thanks for submitting this code.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138