Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: Is there an "inverse" of ToRules?

90 views
Skip to first unread message

Mark S. Coleman

unread,
Oct 5, 2001, 1:30:44 AM10/5/01
to

Greetings,

Is there an "inverse" to the ToRules function? That is, a function
that converts rules to equations (expressed in == form), which could
then be used in the Solve function?

Thanks,

Mark

Allan Hayes

unread,
Oct 6, 2001, 3:50:06 AM10/6/01
to
Mark,

Two ideas:

{a->b,{c:>d}}/.r:Rule|RuleDelayed -> Equal

{a==b,{c==d}}

ToEquations[(Rule|RuleDelayed)[x_,y_]]:= x==y;
ToEquations[z_]:= z;

ToEquations//@{a->b,{c:>d}}

{a==b,{c==d}}

You might want to flatten the outputs.

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
h...@haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Mark S. Coleman" <mcol...@bondspace.com> wrote in message
news:9pjgi4$3fr$1...@smc.vnet.net...

BobH...@aol.com

unread,
Oct 6, 2001, 3:57:21 AM10/6/01
to

In a message dated 2001/10/5 1:48:07 AM, mcol...@bondspace.com writes:

>Is there an "inverse" to the ToRules function? That is, a function
>that converts rules to equations (expressed in == form), which could
>then be used in the Solve function?
>

{a -> b, c -> d, e -> f} /.
(x_ -> y_) :> (x == y)

{a == b, c == d, e == f}


Bob Hanlon
Chantilly, VA USA

John Jowett

unread,
Oct 6, 2001, 4:00:36 AM10/6/01
to
Mark,

This is easy if you understand the structure of Mathematica expressions.

Try evaluating

myrule=(a->b)

myeqn = Equal@@myrule (or Apply[Equal,myrule])

and you will have turned a rule into an equation. To understand it better,
look at

FullForm[myrule]

FullForm[myeqn]

to see that you just had to change the Head of the expression.

I hope this helps,

John Jowett

My home page: http://home.cern.ch/jowett/

Postal address: SL Division, CERN, CH-1211 Geneva 23, Switzerland

"Mark S. Coleman" <mcol...@bondspace.com> wrote in message
news:9pjgi4$3fr$1...@smc.vnet.net...
>

Tomas Garza

unread,
Oct 6, 2001, 4:12:02 AM10/6/01
to
You may define your own. Example:

In[10]:=
b = Solve[x^2 - 2 == 0, x]
Out[10]=
{{x -> -Sqrt[2]}, {x -> Sqrt[2]}}
In[11]:=
ToExpression[StringReplace[ToString[b], "->" -> "=="]]
Out[11]=
{{x == -Sqrt[2]}, {x == Sqrt[2]}}

But I think it is better to stick to the solution expressed in terms of
rules. It takes a while to get used to it, but it pays. The rule has a
definite meaning which can be used in a ReplaceAll operation, whereas
something like the last output, with two "Equal" functions for the same x,
can lead to confusion.

Tomas Garza
Mexico City

0 new messages