12,634,101 members (22,896 online)
alternative version

53.1K views
36 bookmarked
Posted

# A C# Class for Complex Polynomials

, 3 Jul 2007 CPOL
 Rate this:
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()`

• Uploaded `PolyLib `with basic functionality; no complaints so far

## Share

 Germany
No Biography provided

## You may also be interested in...

 First PrevNext
 it gives a wrong result of polynomial equations. I AM SORRY, I WAS WRONG! Member 1206231115-Oct-15 17:27 Member 12062311 15-Oct-15 17:27
 Wrong Result for cubic polynomial with real roots, adding additional 0 root gives the correct results. j1s1e120-Oct-12 18:22 j1s1e1 20-Oct-12 18:22
 Really Good Achachan21-Feb-12 0:13 Achachan 21-Feb-12 0:13
 GetHashCode Philippe Mori15-Dec-11 4:21 Philippe Mori 15-Dec-11 4:21
 Duplicate code Philippe Mori15-Dec-11 4:17 Philippe Mori 15-Dec-11 4:17
 .NET 4.0 Complex type Philippe Mori15-Dec-11 4:05 Philippe Mori 15-Dec-11 4:05
 Re: .NET 4.0 Complex type DEGT11-Feb-15 7:48 DEGT 11-Feb-15 7:48
 Modifying the ToString Method Ryano12113-Feb-11 5:56 Ryano121 13-Feb-11 5:56
 Multiplying Polynoms vdsanchez24-Nov-10 10:55 vdsanchez 24-Nov-10 10:55
 Multivariable polynomial Lukasz Swiatkowski10-Jul-07 6:17 Lukasz Swiatkowski 10-Jul-07 6:17
 Last Visit: 31-Dec-99 19:00     Last Update: 9-Dec-16 12:23 Refresh 123 Next »