DEV Community

maxwizard01
maxwizard01

Posted on • Updated on

How to use sage to solve the discriminant of polynomial

Introduction to Sage

Sage is an open source software system that bundles many standard open source mathematical software
libraries into a common Python-based interface. Cloud computing is well supported: you do not have to
install Sage on your computer to compute.
1.1 The Sage environment
We will use the online version of Sage, so the GUI is your web browser.
Three steps to getting started:

  1. Go to http://www.sagemath.org
  2. click on download!
  3. download the one suitable for your laptop this will consume so much data so you can click on this below link to try to do your work online. https://cocalc.com

Polynomial Problem

so how can we use this sage to solve the discriminant of a polynomial? it is very simple logic
just know the equation of your function. you need to compute all your co-efficient and let it equal to a variable, then write a code to calculate the discriminant
An Intersection Problem discriminant.

Example

let say we need to calculate the discriminant of a quadratic with sageMath.

solution

we know quadratic can be written as ax^2+bx+c.
and we can see that all the co-efficient are a,b and c.
now these are the steps to write the codes

step1:

type 'R.' follow by the variable (x) and all the co-efficient equal to QQ[] like the following.

R.<x,a,b,c> = QQ[] 
Enter fullscreen mode Exit fullscreen mode

step2

let x=polygen(R). it might not be x it can be any variable you use for your equation for examples: if your equation is in form of r i.e(ar^2+br+c) then you use r
I mean your step2 will look like r=polygen(R)

step3

let any letter be the equation it self (I will advise you use y) so step3 look like

y=ax^2+bx+c
Enter fullscreen mode Exit fullscreen mode

Step4

write a code to calculate the discriminant by writing the letter that denote the equation followed by discriminant function like the following.

y.discriminant()
Enter fullscreen mode Exit fullscreen mode

now click run on the screen you will get your answer below the codes.
the whole codes look like the following.

R.<x,a,b,c> = QQ[] 
x=polygen(R).
y=a*x^2+b*x+c
y.discriminant()
Enter fullscreen mode Exit fullscreen mode

Result

let us try another example

Example2

let's try to find the discriminant of a polynomial with degree 3
solutions
let the equation be ax^3+bx^2+cx+d
Now our code is
Let y= a*x^3 + b*x^2 + c*x + d
Codes

R.<x,a,b,c,d> = QQ[]
x = polygen(R)
y = a*x^3 + b*x^2 + c*x + d
y.discriminant()
Enter fullscreen mode Exit fullscreen mode

Result

b^2*c^2 - 4*a*c^3 - 4*b^3*d + 18*a*b*c*d - 27*a^2*d^2
Enter fullscreen mode Exit fullscreen mode

Example3
let's try it for polynomial of degree 4.
Let y = a*x^4+ b*x^3+ c*x^2 + d*x + e
Codes

R.<x,a,b,c,d,e> = QQ[]
x = polygen(R)
y = a*x^4+ b*x^3+ c*x^2 + d*x + e
y.discriminant()
Enter fullscreen mode Exit fullscreen mode

Result

b^2*c^2*d^2 - 4*a*c^3*d^2 - 4*b^3*d^3 + 18*a*b*c*d^3 - 27*a^2*d^4 - 4*b^2*c^3*e + 16*a*c^4*e + 18*b^3*c*d*e - 80*a*b*c^2*d*e - 6*a*b^2*d^2*e + 144*a^2*c*d^2*e - 27*b^4*e^2 + 144*a*b^2*c*e^2 - 128*a^2*c^2*e^2 - 192*a^2*b*d*e^2 + 256*a^3*e^3
Enter fullscreen mode Exit fullscreen mode

How to find the number of terms of any polynomial.

To find the number of terms of any polynomial use number_of_terms() function. example
let say y=3x^2+4x^5-3x+5. if we need to find the number of terms of y our code will be the following.
codes

 y=3x^2+4x^5-3x+5
 y.number_of_terms()
Enter fullscreen mode Exit fullscreen mode

Result
4
Now let's use this to solve a polynomial that we solve in previous example. let's solve for the number of terms in the discriminant of polynomial of 4th degree. our codes will look like the following:
Codes

R.<x,a,b,c,d,e> = QQ[]
x = polygen(R)
y = a*x^4+ b*x^3+ c*x^2 + d*x + e
y.discriminant()
k=y.discriminant()
k.number_of_terms()
Enter fullscreen mode Exit fullscreen mode

Result

b^2*c^2*d^2 - 4*a*c^3*d^2 - 4*b^3*d^3 + 18*a*b*c*d^3 - 27*a^2*d^4 - 4*b^2*c^3*e + 16*a*c^4*e + 18*b^3*c*d*e - 80*a*b*c^2*d*e - 6*a*b^2*d^2*e + 144*a^2*c*d^2*e - 27*b^4*e^2 + 144*a*b^2*c*e^2 - 128*a^2*c^2*e^2 - 192*a^2*b*d*e^2 + 256*a^3*e^3

16
Enter fullscreen mode Exit fullscreen mode

How to find the degree of polynomial

To find the degree of polynomial is very similar to others all we just need is to apply degree() function.

Example

let say y=3x^2+4x^5-3x+5. if we need to find the degree of y our code will be the following.
codes

 y=3x^2+4x^5-3x+5
 y.degree()
Enter fullscreen mode Exit fullscreen mode

Result
5
Now let's use this to solve a polynomial that we solve in previous example. let's solve for the degree in the discriminant of polynomial of 4th degree. our codes will look like the following:
Codes

R.<x,a,b,c,d,e> = QQ[]
x = polygen(R)
y = a*x^4+ b*x^3+ c*x^2 + d*x + e
y.discriminant()
k=y.discriminant()
k.degree()
Enter fullscreen mode Exit fullscreen mode

Result

b^2*c^2*d^2 - 4*a*c^3*d^2 - 4*b^3*d^3 + 18*a*b*c*d^3 - 27*a^2*d^4 - 4*b^2*c^3*e + 16*a*c^4*e + 18*b^3*c*d*e - 80*a*b*c^2*d*e - 6*a*b^2*d^2*e + 144*a^2*c*d^2*e - 27*b^4*e^2 + 144*a*b^2*c*e^2 - 128*a^2*c^2*e^2 - 192*a^2*b*d*e^2 + 256*a^3*e^3

6
Enter fullscreen mode Exit fullscreen mode

Now I believe you now understand how it works. now try to do for degree 5,6,7,8 and 9. Yeah just follow the same step.
Enjoy coding! it's the guy maxwizard meet you in the next lesson.

Top comments (0)