Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
0

Completing the square with Sage

asked 0 years ago

Malte gravatar image

updated 0 years ago

dan_fulea gravatar image

I am not an expert in the various ways of simplifying terms with Sage. Currently I do not even succeed in letting SAGE complete squares for me. If the square is already complete, as in x^2 + 2*x + 1, the command factor will do what I wish and come up with the desired output of (x + 1)^2. However, I would like Sage to simplify terms like x^2 + 2*x + 5 into (x + 1)^2 + 4, and I do not get that done.

Maybe I am asking too much from Sage, but I am even aiming at completing the square in several variables, up to three, that is. So, for two variables, I would like 4*x^2 + 4*x*y + 10*y^2 + 12*x - 18*y + 32 to be simplified into (2*x + y + 3)^2 + (3*y - 4)^2 + 7.

Can anyone help we with this?

Thank you! Malte

Preview: (hide)

Comments

1

Univariate case was discussed at https://ask.sagemath.org/question/10062/

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 0 years ago

Emmanuel Charpentier gravatar image

The single-variable case is indirect but easy. Let's use the symbolic equations machinery, and create the elements of your problem :

sage: var("a, b, c, f, g, h") # Variables
(a, b, c, f, g, h)
sage: Ex=a*x^2+b*x+c # Square to complete
sage: CS=((f*x+g)^2+h) # (Hypothetical) completed square

We want to solve x,ax2+bx+c=(fx+g)2+h, which is equivalent to

sage: (Ex-CS).expand()==0
-f^2*x^2 - 2*f*g*x + a*x^2 - g^2 + b*x + c - h == 0

A polynomial is zero if and only if all its coefficients are zero. Sage allows us to deduce an equivalent system :

sage: Sys=[u[0]==0 for u in (Ex-CS).coefficients(x)] ; Sys
[-g^2 + c - h == 0, -2*f*g + b == 0, -f^2 + a == 0]

whose solution is trivial :

sage: Sols=solve(Sys, (f, g, h), solution_dict=True) ; Sols
[{f: sqrt(a), g: 1/2*b/sqrt(a), h: -1/4*(b^2 - 4*a*c)/a},
 {f: -sqrt(a), g: -1/2*b/sqrt(a), h: -1/4*(b^2 - 4*a*c)/a}

We can check that both solutions check :

sage: [bool(CS.subs(s)==Ex) for s in Sols] 
[True, True]

One notes that this squaring problem has two solutions, because the equations are quadratic (or, if you prefer, x2=(x)2, roughly...)

The multivariate case can be solved in similar ways ; however, the solution is not identical since your expression has only one constant coefficient, whereas you have n squares to complete for n variables. Run :

reset()
NN=3 # Number of variables
var("c") # Constant coefficient set apart
varnames=("a", "b", "c", "f", "g", "h", "y") # Sheer laziness : create sets of "indexed" variables
for u in varnames: exec("%s=var('%s', n=NN)"%(u.upper(), u))
Exs=[A[u]*Y[u]^2+B[u]*Y[u]+C[u] for u in range(NN)] # Original expressions, with artificial constant coefficients !
CSs=[(F[u]*Y[u]+G[u])^2+H[u] for u in range(NN)] # Desired "completed squares".
Sys=flatten([(Exs[u]-CSs[u]).coefficients(Y[u], sparse=False) for u in range(NN)]) # Equations determining F, G, H
Sys+=[sum(C)-c] # ONE equation on the NN artificial constant coefficients and the (single) natural constant coefficient
Sols=solve(Sys, F+G+H+C, solution_dict=True)
Check=[bool(sum(Exs)==sum(CSs).subs(s)) for s in Sols]

The system to solve has 3n+1 (quadratic) equations with 4n unknowns ; there are 2n solutions involving n1 arbitrary constants (the rxx quantities in the solutions) ; one notes that this holds in the n=1 case...

There are better ways, using the polynomials machinery, to solve this problem ; there are lazily left to the reader as an exercise...

Another "interesting" problem : what happens if you introduce some xy terms ? (Hint : this is a special case of a very important class of problems...).

HTH,

Preview: (hide)
link

Comments

Thank you, Emmanuel, for your detailed answer. You are obviously operating on a much higher level of Sage mastery than I do. I was able to understand what you said about the case with a single variable. In the multivariate case, however, I cannot follow what you are doing there, especially in the last Sage cell of your answer. What would I have to add to that code if I had a specific term with specific coefficients? Could you give me an example?

Malte gravatar imageMalte ( 0 years ago )

I cannot follow what you are doing there, especially in the last Sage cell of your answer.

Where are you losing me ?

What would I have to add to that code if I had a specific term with specific coefficients?

Don't add : substitute the generic variables I used with your specific values.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 0 years ago )

The chosen sample case is very simple. What is the expected answer in the following cases: 2x2+2x+1 and 2x22x1 and 17xy and 7x2+17xy?

dan_fulea gravatar imagedan_fulea ( 0 years ago )

One way to give more structure to the question is to homogenize, then consider the corresponding quadratic form, and look at its invariants. An approach based on this sight would be easier from the structural point of view.

dan_fulea gravatar imagedan_fulea ( 0 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 0 years ago

Seen: 123 times

Last updated: Feb 13