       • To: mathgroup at smc.vnet.net
• Subject: [mg76849] Re: Quadratic form: symbolic transformation
• From: dimitris <dimmechan at yahoo.com>
• Date: Mon, 28 May 2007 01:10:40 -0400 (EDT)
• References: <f3bj37\$40i\$1@smc.vnet.net>

```Hello Wolfgang.

It is very easy with the following code to make your procedure much
more compact.

If you are working in Mathematica 5.2 the following will take you in a
section of Mathematica Book that contains material to understand the
code.

FrontEndExecute[{HelpBrowserLookup["MainBook", "3.6.1"]}]

If you are working in 6 see here

http://reference.wolfram.com/mathematica/tutorial/SolvingEquationsInvolvingPowerSeries.html

Here are your polynomials in x.

In:=
q1 = R*x^2 + S*x + T ;

In:=
q2 = u*(x+v)^2 + w ;

Here are the procedure (*with comments*)

In:=
q1 == q2 + O[x]^3 (*this gives an equation involving the power
series*)
LogicalExpand[%] (*LogicalExpand generates a sequence of equations for
each power of x*)
Solve[%, {u, v, w}]  (*now the rest is obvious!*)
q1 == q2 /. %[]  (*verification*)
Expand /@ %        (*no need for Simplify here*)

Out=
T + S*x + R*x^2 == SeriesData[Global`x, 0, {Global`u*Global`v^2 +
Global`w, 2*Global`u*Global`v, Global`u}, 0, 3, 1]
Out=
-R + u == 0 && -S + 2*u*v == 0 && -T + u*v^2 + w == 0
Out=
{{w -> (-S^2 + 4*R*T)/(4*R), u -> R, v -> S/(2*R)}}
Out=
T + S*x + R*x^2 == (-S^2 + 4*R*T)/(4*R) + R*(S/(2*R) + x)^2
Out=
True

True? We are indeed right!

Let's make now an one-liner that will collect the steps (apart from
the verification!).
Note that this one liner is not something special. It is a quick
attempt to show
how you could work! Various modifications can be done to be better.
The code looks
big because I have add some coments to explain how it works. Please
feel free to make
me any questions.

In:=
form*), x_(*the variable please*)] := If[PolynomialQ[f, x] &&
PolynomialQ[g, x] && (Exponent[#1, x] & ) /@ {f, g} == {2, 2}(*check
if the expressions are polynomials in x and if their order is two*),
Solve[LogicalExpand[f == g + O[x]^3], Variables[CoefficientList[g,
x]](*this gives a list of the parameters which you want to express in
known parameters*) ], Print["sorry; try again! I am a quick attempt
not something special!"]]

Let's taste it!

In:=
Out=
{{w -> (-S^2 + 4*R*T)/(4*R), u -> R, v -> S/(2*R)}}

Good!

In:=
quadform[x^2 + 2*x + 4, q2, x]

Out=
{{w -> 3, u -> 1, v -> 1}}

Better!

In:=
quadform[x^3 + a*x^2 + b*x + c, q2, x]
>From In:=
"sorry; try again! I am a quick attempt not something special!"

The best! My code knows its level!

Regards
Dimitris Anagnostou

/  Dr. Wolfgang Hintze       :
> Hello,
>
> this is a simple question but perhaps I can get here some information
> towards a more apropriate way of using Mathematica.
>
> I take a very simple example: I would like to write the quadratic form
>
> q1 = R*x^2 + R*x + T
>
> in the form
>
> q2 = u*(x+v)^2 + w
>
> How can I find u, v, and w from R, S, and T?
>
> I'm sure there must be some symbolic way (using a sufficient amount of
> _'s) to answer this question.
>
> My (cumbersome) procedure compares coefficients and looks like this
>
> (* writing down lhs == rhs)
> In:=
> q = R*x^2 + S*x + T == u*(x + v)^2 + w
> Out=
> T + S*x + R*x^2 == w + u*(v + x)^2
>
> (* as q must be an identiy in x, i.e. must hold for all x, I compare
> coefficients at x=0 *)
> In:=
> eq1 = q /. {x -> 0}
> Out=
> T == u*v^2 + w
> In:=
> eq2 = D[q, x] /. {x -> 0}
> Out=
> S == 2*u*v
> In:=
> eq3 = D[q, {x, 2}] /. {x -> 0}
> Out=
> 2*R == 2*u
> In:=
> t = First[Solve[{eq1, eq2, eq3}, {u, v, w}]]
> Out=
> {w -> (-S^2 + 4*R*T)/(4*R), u -> R, v -> S/(2*R)}
>
> (* writing down the result explicitly *)
> In:=
> q /. t
> Out=
> T + S*x + R*x^2 == (-S^2 + 4*R*T)/(4*R) + R*(S/(2*R) + x)^2
> In:=
> Simplify[q /. t]
> Out=
> True
>
> Thanks in advance for any hints.
> Regards,
> Wolfgang

```

• Prev by Date: Re: ListIntegrate3D ?
• Next by Date: Re: Stopping Automatic Animation in v6