Function Repository Resource:

RandomPolynomial

Source Notebook

Generate a polynomial with random terms

Contributed by: Jon McLoone

ResourceFunction["RandomPolynomial"][var]

generates a random polynomial in var with maximum degree 5.

ResourceFunction["RandomPolynomial"][var,maxdegree]

generates a random polynomial in var with maximum degree maxdegree.

ResourceFunction["RandomPolynomial"][varlist,maxdegree]

generates a random polynomial with maximum degree maxdegree in each of the elements of the varlist.

ResourceFunction["RandomPolynomial"][varlist,degreelist]

generates a random polynomial each of the elements of the varlist with degree chosen from degreelist.

ResourceFunction["RandomPolynomial"][var,degree,termcount]

generates a random polynomial with the number of terms given by termcount.

Details and Options

The following option is supported:
"TermMultiplierDistribution"the distribution to draw term multipliers fromDiscreteUniformDistribution[{-100,100}]

Examples

Basic Examples (2) 

Generate a random polynomial in x:

In[1]:=
ResourceFunction["RandomPolynomial"][x]
Out[1]=

Generate a random quadratic in x:

In[2]:=
ResourceFunction["RandomPolynomial"][x, 2]
Out[2]=

Generate a random polynomial in x of maximum degree 100:

In[3]:=
ResourceFunction["RandomPolynomial"][x, 100]
Out[3]=

Generate a random polynomial in x of maximum degree 100 with 3 terms:

In[4]:=
ResourceFunction["RandomPolynomial"][x, 100, 3]
Out[4]=

Scope (3) 

Restrict to only even powers of x:

In[5]:=
ResourceFunction["RandomPolynomial"][x, Range[0, 20, 2], 3]
Out[5]=

RandomPolynomial can generate a polynomial in multiple variables:

In[6]:=
ResourceFunction["RandomPolynomial"][{x, y, z}, 3]
Out[6]=

RandomPolynomial can generate polynomials in any kind of expression, not just symbols:

In[7]:=
ResourceFunction["RandomPolynomial"][Sin[x], 3]
Out[7]=

Generate a Laurent polynomial by including negative order terms:

In[8]:=
ResourceFunction["RandomPolynomial"][x, Range[-3, 3]]
Out[8]=

Options (2) 

TermMultiplierDistribution (2) 

Generate a polynomial whose coefficients are integers in the range [0,100]:

In[9]:=
ResourceFunction["RandomPolynomial"][x, 20, 10, "TermMultiplierDistribution" -> DiscreteUniformDistribution[{0, 100}]]
Out[9]=

Generate a polynomial with normally-distributed coefficients:

In[10]:=
ResourceFunction["RandomPolynomial"][x, 20, "TermMultiplierDistribution" -> NormalDistribution[]]
Out[10]=

Possible Issues (1) 

Requesting more terms than are possible causes a failure:

In[11]:=
ResourceFunction["RandomPolynomial"][x, 3, 1000]
Out[11]=

Publisher

Jon McLoone

Version History

  • 1.0.0 – 06 April 2022

License Information