Re: Quadratic form: symbolic transformation
- 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[2]:=
q1 = R*x^2 + S*x + T ;
In[3]:=
q2 = u*(x+v)^2 + w ;
Here are the procedure (*with comments*)
In[11]:=
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 /. %[[1]] (*verification*)
Expand /@ % (*no need for Simplify here*)
Out[11]=
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[12]=
-R + u == 0 && -S + 2*u*v == 0 && -T + u*v^2 + w == 0
Out[13]=
{{w -> (-S^2 + 4*R*T)/(4*R), u -> R, v -> S/(2*R)}}
Out[14]=
T + S*x + R*x^2 == (-S^2 + 4*R*T)/(4*R) + R*(S/(2*R) + x)^2
Out[15]=
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[16]:=
quadform[f_(*put here the expanded form*), g_(*put here the quadratic
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[17]:=
quadform[q1, q2, x]
Out[17]=
{{w -> (-S^2 + 4*R*T)/(4*R), u -> R, v -> S/(2*R)}}
Good!
In[27]:=
quadform[x^2 + 2*x + 4, q2, x]
Out[27]=
{{w -> 3, u -> 1, v -> 1}}
Better!
In[28]:=
quadform[x^3 + a*x^2 + b*x + c, q2, x]
>From In[28]:=
"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[112]:=
> q = R*x^2 + S*x + T == u*(x + v)^2 + w
> Out[112]=
> 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[113]:=
> eq1 = q /. {x -> 0}
> Out[113]=
> T == u*v^2 + w
> In[114]:=
> eq2 = D[q, x] /. {x -> 0}
> Out[114]=
> S == 2*u*v
> In[115]:=
> eq3 = D[q, {x, 2}] /. {x -> 0}
> Out[115]=
> 2*R == 2*u
> In[119]:=
> t = First[Solve[{eq1, eq2, eq3}, {u, v, w}]]
> Out[119]=
> {w -> (-S^2 + 4*R*T)/(4*R), u -> R, v -> S/(2*R)}
>
> (* writing down the result explicitly *)
> In[120]:=
> q /. t
> Out[120]=
> T + S*x + R*x^2 == (-S^2 + 4*R*T)/(4*R) + R*(S/(2*R) + x)^2
> In[122]:=
> Simplify[q /. t]
> Out[122]=
> True
>
> Thanks in advance for any hints.
> Regards,
> Wolfgang