# How to define polynomials and manipulate them?

(Davi Giugno) #1

I’m pretty new to Math.Net and I must by now do polynomial multiplication efficiently. What I want to know, to be more precise, is

1. How to define a polynomial in terms of its coefficients? That is, if A(x) = a0 + a1x + … + aNx^N, how do I tell Math.NET to define/instantiate this?

2. Having defined two given polynomials A(x) and B(x), how can I get the coefficients of their product C(x) = A(x)*B(x), for use in later computations?

Sorry if this question sounds too naive, but I’ve Googled it befoore and found no simple/satisfactory answer, and I need an answer quickly. Regards.

Math.Net Polynomial Class Proposal
#2

I think you need to use Math.NET Symbolics:
http://symbolics.mathdotnet.com/

A simple example:

``````open MathNet.Symbolics

//some function
let astr = "a*x + b*x*x + c"
let bstr = "b*x + d*x*x + f*x*x*x"

let a = Infix.parseOrThrow(astr)
let b = Infix.parseOrThrow(bstr)

//new function
let c = a * b
//print
c |> Infix.print |> printfn "%s"

//set random values to symbol
let setRandom s =
let rand = System.Random()
s |> List.map(fun x -> x |> Infix.print, rand.Next(3) |> float |> FloatingPoint.Real)
|> Map.ofList

//get random values
let values = c |> Structure.collectIdentifiers |> setRandom

//print values
values |> printfn "%A"

//get result
c |> Evaluate.evaluate values |> printfn "%A"
``````

Print:

``````(c + a*x + b*x^2)*(b*x + d*x^2 + f*x^3)
map
[("a", Real 0.0); ("b", Real 1.0); ("c", Real 1.0); ("d", Real 0.0);
("f", Real 0.0); ("x", Real 2.0)]
Real 10.0
``````

If this is not what you need, describe the problem in more detail.

(Christoph Rüegg) #3

Math.NET Numerics does not yet have any routines for fast (large) polynomial products. This is something we’d like to add and would certainly accept pull requests. Ah, and yes, this functionality would also be useful within some algorithms over at Math.NET Symbolics.