Click here to Skip to main content
11,583,859 members (46,090 online)
Click here to Skip to main content

A C# Class for Complex Polynomials

, 3 Jul 2007 CPOL 46.2K 1.6K 36
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

hanzzoid
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionWrong Result for cubic polynomial with real roots, adding additional 0 root gives the correct results. Pin
j1s1e120-Oct-12 17:22
memberj1s1e120-Oct-12 17:22 
QuestionReally Good Pin
Achachan20-Feb-12 23:13
memberAchachan20-Feb-12 23:13 
QuestionGetHashCode Pin
Philippe Mori15-Dec-11 3:21
memberPhilippe Mori15-Dec-11 3:21 
QuestionDuplicate code Pin
Philippe Mori15-Dec-11 3:17
memberPhilippe Mori15-Dec-11 3:17 
Question.NET 4.0 Complex type Pin
Philippe Mori15-Dec-11 3:05
memberPhilippe Mori15-Dec-11 3:05 
AnswerRe: .NET 4.0 Complex type Pin
DEGT11-Feb-15 6:48
memberDEGT11-Feb-15 6:48 
GeneralModifying the ToString Method Pin
Ryano12113-Feb-11 4:56
memberRyano12113-Feb-11 4:56 
QuestionMultiplying Polynoms Pin
vdsanchez24-Nov-10 9:55
membervdsanchez24-Nov-10 9:55 
GeneralMultivariable polynomial Pin
Lukasz Swiatkowski10-Jul-07 5:17
memberLukasz Swiatkowski10-Jul-07 5:17 
GeneralRe: Multivariable polynomial [modified] Pin
hanzzoid14-Jul-07 6:48
memberhanzzoid14-Jul-07 6:48 
GeneralRe: Multivariable polynomial Pin
Lukasz Swiatkowski16-Jul-07 6:48
memberLukasz Swiatkowski16-Jul-07 6:48 
GeneralRe: Multivariable polynomial Pin
hanzzoid17-Jul-07 2:18
memberhanzzoid17-Jul-07 2:18 
GeneralRe: Multivariable polynomial Pin
Lukasz Swiatkowski17-Jul-07 7:29
memberLukasz Swiatkowski17-Jul-07 7:29 
GeneralRe: Multivariable polynomial Pin
hanzzoid17-Jul-07 5:54
memberhanzzoid17-Jul-07 5:54 
GeneralRe: Multivariable polynomial Pin
Lukasz Swiatkowski17-Jul-07 7:36
memberLukasz Swiatkowski17-Jul-07 7:36 
GeneralSuggestion Pin
leppie3-Jul-07 6:59
memberleppie3-Jul-07 6:59 
GeneralRe: Suggestion Pin
hanzzoid3-Jul-07 8:17
memberhanzzoid3-Jul-07 8:17 
GeneralRe: Suggestion Pin
Steve Hansen3-Jul-07 21:58
memberSteve Hansen3-Jul-07 21:58 
GeneralYes, yes... Pin
hanzzoid3-Jul-07 23:02
memberhanzzoid3-Jul-07 23:02 
GeneralRe: Yes, yes... Pin
Steve Hansen3-Jul-07 23:07
memberSteve Hansen3-Jul-07 23: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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 3 Jul 2007
Article Copyright 2007 by hanzzoid
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid