MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: solve for a squared variable

  • To: mathgroup at smc.vnet.net
  • Subject: [mg60462] Re: solve for a squared variable
  • From: Peter Pein <petsie at dordos.net>
  • Date: Sat, 17 Sep 2005 02:31:44 -0400 (EDT)
  • References: <200509150916.FAA15860@smc.vnet.net> <200509160749.DAA00670@smc.vnet.net> <a6e65e8d05091601107c5e6f8a@mail.gmail.com> <dge3qf$52t$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Ruth Lazkoz schrieb:
> Thank you. I was aware of that possibility. I was only wondering if there is 
> a more elegant procedure, perhaps using patters or whatever.
> ----- Original Message ----- 
> From: "Zhengji Li" <zhengji.li at gmail.com>
To: mathgroup at smc.vnet.net
> Subject: [mg60462] [work] Re:  solve for a squared variable
> 
> 
> 
>>That's because x^2 is not recognized as a symbol.
>>
>>let t = x ^ 2, then Solve[t + y == 1, t], you will get the result 
>>immediately.
>>
>>-- 
>>Li Zhengji
>>-------------------------------------------------------------
>>If all you have is a hammer, everything is a nail.
>>-------------------------------------------------------------
>>
> 
> 

Hi Ruth,

I guess you have a function in mind, which does this:

In[9]:=
SolveExpression[x^2 + y == 1, x^2]

Out[9]=
{{x^2 -> 1 - y}}

or even this:

In[10]:=
SolveExpression[{Tan[t] == b/a, c Cos[t] == a}, a^2 + b^2]

Out[10]=
{{a^2 + b^2 -> c^2}}

?

Well, far from being elegant, the function in this q&d hack does it
(almost surely with as many bugs as possible):

Needs["Utilities`FilterOptions`"];

Clear[SolveExpression];
Options[SolveExpression] ^= UseReduce -> False;

(* when to solve for symbols only, use built - in functions *)
SolveExpression[eqns_List,
  vars_List /; Union[Head /@ vars] === {Symbol}, opts___] :=
  Module[{f = If[TrueQ[UseReduce /. {opts}], Reduce, Solve]},
    f[eqns, vars, FilterOptions[f, opts]]
  ];

SolveExpression[eqns_List, vars_List, opt___] :=
  Module[{eq2, va2, t, nosyms, syms, sol, nsvars,
   usered = TrueQ[UseReduce /. {opt}],
   varnames = Cases[#1, x_Symbol /; !NumericQ[x], Infinity] & },
     nosyms = Complement[vars, syms = varnames[vars]];
     t = Table[Unique[], {Length[nosyms]}];
     eq2 = Join[eqns, Thread[t == nosyms]];
     va2 = Join[syms, t];
     nsvars = varnames[nosyms];
     sol = If[usered,
       Reduce[eq2, va2, Backsubstitution -> True,
         FilterOptions[Reduce, opt]],
       Solve[eq2, va2, nsvars, FilterOptions[Solve, opt]]
     ] /. Thread[t -> nosyms];
     sol = sol /. (Rule | Equal)[x_, _] | _ == (x_) /; MemberQ[nsvars,
x] :> Sequence[];
     If[usered,
       LogicalExpand[sol] /. (a_) == (b_) /; MemberQ[vars, b] :> b == a,
       Union[sol]
     ]
  ];

SolveExpression[e_List, v_, o___] := SolveExpression[e, {v}, o];
SolveExpression[e_, v_List, o___] := SolveExpression[{e}, v, o];
SolveExpression[e_, v_, o___] := SolveExpression[{e}, {v}, o];


-- 
Peter Pein, Berlin
GnuPG Key ID: 0xA34C5A82
http://people.freenet.de/Peter_Berlin/


  • Prev by Date: Re: NonlinearFit - Logistic Function-CalcCenter3
  • Next by Date: Re: Why this function does not return a single value
  • Previous by thread: Re: Re: solve for a squared variable
  • Next by thread: Re: solve for a squared variable