user_guide:tutorials:polynomials_tutorial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revisionBoth sides next revision
tutorial:polynomials_tutorial [2017/02/20 13:44] – [Example: Toric Degeneration] wagneruser_guide:tutorials:polynomials_tutorial [2019/01/25 09:38] – ↷ Page moved from user_guide:polynomials_tutorial to user_guide:tutorials:polynomials_tutorial oroehrig
Line 1: Line 1:
-===== Basic Usage of Polynomials in Perl ===== +short note on variable naming up front: You can alter the settings for the names that are used for polynomial variables in parsing them from strings or for pretty-printing using the ''set_var_names'' or ''local_var_names'' functions. Refer to their [[https://polymake.org/release_docs/master/common.html#common__set_var_names__239|documentation]] for defaults and usage information. To restore the default settings to your ''customize.pl'', type this:
- +
-A polynomial always carries a reference to a polynomial ring.  Polynomial in different rings cannot be added, multiplied, or whatever.  The names of the indeterminates are relevant for the output functions only. +
 <code> <code>
-polytope$r=new Ring(qw(x y));+reset_custom %polynomial_var_names;
 </code> </code>
  
-It may be convenient to use special variable names for the indeterminates. +===== Usage of Polynomials in Perl ===== 
 +===Constructors=== 
 +The easiest way to create a simple [[https://polymake.org/release_docs/master/common.html#common__Polynomial__339 | Polynomial]] object is through a string:
 <code> <code>
-polytope> ($x,$y)=$r->variables;+$p = new Polynomial("4 + 3x_1 + x_2^5"); 
 +</code> 
 +Sometimes it's convenient to use the constructor that takes a vector of coefficients and a matrix of exponents: 
 +<code> 
 +$coeff = new Vector([9,-5]); 
 +$exp = new Matrix<Int>([[0,4],[8,3]])
 +> $p2 new Polynomial($coeff, $exp); 
 +> print $p2; 
 +-5*x_0^8*x_1^3 + 9*x_1^4 
 +</code> 
 +There is a seperate type for univariate polynomials, called [[https://polymake.org/release_docs/master/common.html#common__UniPolynomial__342 | UniPolynomial]]. 
 +<code> 
 +> $up = new UniPolynomial("3x + 2x^2 + 4");
 </code> </code>
  
-Notice that the variable names are chosen to match the print names; but this is "coincidental". +Polynomials (and UniPolynomials) are templated by their coefficient and exponent types, defaulting to Rational for coefficients and Int for exponents. You can even have polynomials of polynomials (of polynomials...).
 <code> <code>
-polytope > $p=1+($x^2)+$y+> $pp new UniPolynomial<UniPolynomial<Rational,Int>,Rational>("(4x^2+5)y3/2 - 5/3x4y2/3")
-polytope > $q=1+$x+2*($y^3)+$x*$y; +print $pp; 
-polytope > print($p*$q); +(4*x^+ 5)*y^3/2 (-5/3*x^4)*y^2/3 
-x^3*x^2*x^2*y^x^2 + x*y^2 + 2*x*2*y^+ 2*y^3 + y + 1+</code> 
 +===Computations=== 
 +The standard arithmetic functions "+", "-", "*", "^" are defined for polynomials of matching type. 
 +<code> 
 +> print $p + ($p^2); 
 +9*x_1^2 6*x_1*x_2^27*x_1 + x_2^10 + 9*x_2^20 
 +</code> 
 +However, note that due to the fact that their precedence is given in perl, it may be necessary to write more parentheses than expected at first sight. For example, as above, you always have to write "($p^2)" because of the lower precedence of the "^" operator... 
 +<code> 
 +> print $p $p^2; 
 +36*x_1^2 + 24*x_1*x_2^5 96*x_1 4*x_2^10 32*x_2^5 + 64 
 +</code> 
 +For UniPolynomials, we even have polynome division: 
 +<code> 
 +> print (($up^2)/$up); 
 +(2*x^2 + 3*x 4)/(1)
 </code> </code>
  
-Standard arithmetic function "+", "-", "*", "^" are defined.  However, note due to the fact that their precedence is given in perl it may be necessary to write more parentheses than expected at first sight. For example, as above, you always have to write "($x^2)" because of the lower precedence of the "^" operator. +===Example: Newton Polynomials===
 Here is one way to produce polytopes from polynomials (as the convex hull of the exponent vectors of all terms). Here is one way to produce polytopes from polynomials (as the convex hull of the exponent vectors of all terms).
- 
 <code> <code>
-polytope> $npq=newton($p*$q); +polytope > $np = newton($p*($p+$p)); 
-polytope> print $npq->VERTICES; +polytope > print $np->VERTICES; 
-1 0 0 +0 0 
-1 2 +2 0 
-4 +1 0 0 10 
-0 +polytope > print equal_polyhedra($np,minkowski_sum(newton($p),newton($p+$p)));
-1 3 1 +
-polytope> print equal_polyhedra($npq, minkowski_sum(newton($p),newton($q)));+
 1 1
 </code> </code>
Line 40: Line 60:
 The final "1" means "true": The Newton polytope of the product of two polynomials always equals the Minkowski sum of the Newton polytopes of the factors. The final "1" means "true": The Newton polytope of the product of two polynomials always equals the Minkowski sum of the Newton polytopes of the factors.
  
-===== Example: Toric Degeneration =====+=== Example: Toric Degeneration ===
  
 The following describes how to construct the polynomial which describes the toric deformation with respect to a point configuration and a height function.  This is the input data: The following describes how to construct the polynomial which describes the toric deformation with respect to a point configuration and a height function.  This is the input data:
Line 53: Line 73:
  
 <code> <code>
-polytope > @vars = ( "s", map { "x$_" } (1..$points->rows()) ); +polytope > $p = new Polynomial($coefficients,$height|$points);
-polytope > $R = new Ring(@vars); +
-polytope > $p = new Polynomial($height|$points,$coefficients,$R);+
 </code> </code>
  
  • user_guide/tutorials/polynomials_tutorial.txt
  • Last modified: 2019/02/04 22:55
  • by 127.0.0.1