Click here to Skip to main content
Licence CPOL
First Posted 3 Jul 2007
Views 28,986
Downloads 653
Bookmarked 34 times

A C# Class for Complex Polynomials

By | 3 Jul 2007 | Article
A C# class for complex polynomials providing polynomial arithmetics, differentiation and integration

Introduction

PolyLib is a C# class library providing basic polynomial arithmetics such as addition, multiplication and exponentiation, differentiation and integration, computation of the complex roots of a polynomial, factorization and evaluation at a complex point.

Background

Polynomials such as...

p(x) = 1 + x^2 + 2*x^3

... are a common thing, I hope. For an introduction to complex numbers, read the last chapter of the documentation of the C# Matrix Library (look for CSML on CodeProject).

Using the Code

There are, as for any open source library, two basic concepts of using PolyLib: Either you add PolyLib.dll to your project as reference and type...

using PolyLib; 

... at the beginning of the class in which you want to use polynomials; or you just add the files Polynomial.cs and Complex.cs as existing items to your project and adjust the namespace at the head of these classes.

Initializing a polynomial is easy:

// init p(x) = 1 + x^2 + 2+x^3
Polynomial p = new Polynomial(1, 0, 1, 2);

Now we compute the roots of p:

Complex[] roots = p.Roots(); // compute roots of p

Do we need the derivative?

Polynomial p1 = Polynomial.Derivative(p); // p1(x) = p'(x)

Output is intuitive as well (assuming we have a textbox txtOut):

txtOut.Text = p.ToString();

Points of Interest

I said it was easy, didn't I? The structure of polynomials is not complicated, and this is reflected by the simplicity of this class. Most of the work lies within the method Roots(), which forced the implementation of complex numbers.

You can also factorize a polynomial using Roots(); a structure for factorized polynomials is implemented. Of course you can again expand the factorized polynomial, but the inherent inaccuracy of floating point arithmetics becomes visible that way. Evaluation at a point is therefore possible for both (expanded) polynomials and factorized polynomials.

I issued the complex numbers class library on CodeProject as a single project as well (look for CompLib). Another library using Complex.cs is CSML, the C# Matrix Library, where I tried to port some of Matlabs functionality to C# .NET (also on CodeProject). This project is growing quickly, now consisting of almost a hundred methods for numerical linear algebra and matrix manipulation.

In future, I will try to merge PolyLib with CSML to an even more powerful class library, where matrix computation with matrices of polynomials will be possible.

History

Update July 4, 2007

  • Constructors adjusted (see Leppies comment below)

Update July 3, 2007

  • Major bug in Arg() fixed (thanks Petr Stanislav!); this affected Log(), Pow() and Sqrt()

Upload July 3, 2007

  • Uploaded PolyLib with basic functionality; no complaints so far

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

hanzzoid



Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionReally Good PinmemberAchachan23:13 20 Feb '12  
QuestionGetHashCode PinmemberPhilippe Mori3:21 15 Dec '11  
QuestionDuplicate code PinmemberPhilippe Mori3:17 15 Dec '11  
Question.NET 4.0 Complex type PinmemberPhilippe Mori3:05 15 Dec '11  
GeneralModifying the ToString Method PinmemberRyano1214:56 13 Feb '11  
QuestionMultiplying Polynoms Pinmembervdsanchez9:55 24 Nov '10  
GeneralMultivariable polynomial PinmemberLukasz Swiatkowski5:17 10 Jul '07  
GeneralRe: Multivariable polynomial [modified] Pinmemberhanzzoid6:48 14 Jul '07  
No, sorry, I don't.
 
After spending some seconds on the problem, it seems to
me it belongs to the area of symbolic computing instead
of numerical mathematics...
 
While factorization of complex (and real) polynomials is
done by finding the roots, e.i.
 
p(x) = A*(x-x1)*(x-x2)*...*(x-xn),
 
where x1, ..., xn are the n complex roots of p
(let A be the leading coefficient of p),
factorization of multivar polynomials is a matter of
algebraic transformation: Consider
 
p(x,y) = x^2 - yx - x + y.
 
Here, factorization is done by factoring out twice, leading to
 
p(x,y) = (x-1)*(x-y).
 
This is something thoroughly different than iterative root-finding.
 
Well, the main theorem of algebra guarantees us the existence of
a factorization of any complex polynomial as shown above.
 
But multivar polynomials do not need to have such decomposition,
e.g. consider
 
q(x,y) = x^2 + y^2.
 
[Edit:] q CAN be displayed as a product of linear polynomials
(see Lukasz's answer to this post). Verify q(x,y) = (x - iy)(x + iy)
by using Maple (or simple factoring out):
 
>factor(x^2 + y^2, I);
 
(See my second answer to Lukacz's initial post for a suggestion of a non-decomposable polynomial.)
 
hanzzoid
 

 

-- modified at 11:58 Tuesday 17th July, 2007
GeneralRe: Multivariable polynomial PinmemberLukasz Swiatkowski6:48 16 Jul '07  
GeneralRe: Multivariable polynomial Pinmemberhanzzoid2:18 17 Jul '07  
GeneralRe: Multivariable polynomial PinmemberLukasz Swiatkowski7:29 17 Jul '07  
GeneralRe: Multivariable polynomial Pinmemberhanzzoid5:54 17 Jul '07  
GeneralRe: Multivariable polynomial PinmemberLukasz Swiatkowski7:36 17 Jul '07  
GeneralSuggestion Pinmemberleppie6:59 3 Jul '07  
GeneralRe: Suggestion Pinmemberhanzzoid8:17 3 Jul '07  
GeneralRe: Suggestion PinmemberSteve Hansen21:58 3 Jul '07  
GeneralYes, yes... Pinmemberhanzzoid23:02 3 Jul '07  
GeneralRe: Yes, yes... PinmemberSteve Hansen23:07 3 Jul '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 3 Jul 2007
Article Copyright 2007 by hanzzoid
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid