# How to simplify expression like sqrt((a+b)^2)?

Hi! I am interested in Math.net.symbolics, and trying to find roots of eq like y=ax2+bx+c using visual basic throught formula (refer to the previous post). It works except that the result can not be simplified. For example:
for "x^2-1
x-6" , it gives x1=1/2 + sqrt(25)/2, x2=1/2 - sqrt(25)/2, instead of 3 and -2, respectively.
Another example:
for x^2-(a+b)x+ab, it gives two roots:
a/2 + b/2 + sqrt(-4ab + (-a - b)^2)/2==>should be a
a/2 + b/2 - sqrt(-4ab + (-a - b)^2)/2==>should be b
is there any suggestion?
I tried to read the structure of the “expression” and simplify by myself. However, I can only see the items of the expression in VB IDE, but can not access those items. I also tried reading recursively the collections from the expression, however, it is not formulated as I expected.
Thanks!

Below is my code:

``````Imports Expr = MathNet.Symbolics.SymbolicExpression
Imports MathNet.Numerics
Imports MathNet
Imports MathNet.Symbolics
Function FindRootsbyFormula(variable As Expression, ex As Expression) As List(Of
Expression)
Dim ExpSimp = Rational.Simplify(variable, ex)
Dim NumeratorExp = Algebraic.Expand(Rational.Numerator(ExpSimp))
Dim DenominatorExp = Algebraic.Expand(Rational.Denominator(ExpSimp))
Dim coeff() As Expression = Symbolics.Polynomial.Coefficients(variable, NumeratorExp)
Dim ls As New List(Of Expression)
Select Case coeff.Length
Case 1
Select Case True
Case Expression.Zero.Equals(coeff(0)) = True 'root can be any number
Return ls
Case Else
Dim coeffx() As Expression = Symbolics.Polynomial.Coefficients(variable, DenominatorExp)
If coeffx.Count > 1 Then
Else
End If
Return ls
End Select
Case 2
Dim coeffDenom() As Expression = Symbolics.Polynomial.Coefficients(variable, DenominatorExp)
Dim coeffNumerator() As Expression = Symbolics.Polynomial.Coefficients(variable, NumeratorExp)
If coeffDenom.Count > coeffNumerator.Count Then
End If
Return ls
Case 3 'ax^2+bx+c=0
Dim a = coeff(2)
Dim b = coeff(1)
Dim c = coeff(0)
Dim s1 = (-b + Expression.Sqrt(b * b - 4 * a * c)) / (2 * a)
Dim s2 = (-b - Expression.Sqrt(b * b - 4 * a * c)) / (2 * a)
Dim coeffDenom() As Expression = Symbolics.Polynomial.Coefficients(variable, DenominatorExp)
Dim coeffNumerator() As Expression = Symbolics.Polynomial.Coefficients(variable, NumeratorExp)
If coeffDenom.Count > coeffNumerator.Count Then
End If
Return ls
Case 4 'ax^3+bx^2+cx+d=0
Dim a = coeff(3)
Dim b = coeff(2)
Dim c = coeff(1)
Dim d = coeff(0)
Dim p = b * c / (6 * a * a) - b * b * b / (27 * a * a * a) - d / (2 * a)
Dim p2 = p * p
Dim q = (c / 3 / a - b * b / 9 / a / a)
Dim q3 = q * q * q
Dim pp = Expression.Sqrt(p2 + q3)
Dim x1 = -b / (3 * a) + Expression.NewPower(p + pp, 1 / 3) + Expression.NewPower(p - pp, 1 / 3)
Dim x2 = -b / (3 * a) + (-1 + Expression.Sqrt(3) * Expression.I) / 2 * Expression.NewPower(p + pp, 1 / 3) + (-1 - Expression.Sqrt(3) * Expression.I) / 2 * Expression.NewPower(p - pp, 1 / 3)
Dim x3 = -b / (3 * a) + (-1 - Expression.Sqrt(3) * Expression.I) / 2 * Expression.NewPower(p + pp, 1 / 3) + (-1 + Expression.Sqrt(3) * Expression.I) / 2 * Expression.NewPower(p - pp, 1 / 3)