4
$\begingroup$

I have a list of equations:

dene = {x1== 0.2, x2== 0.4, x3==0.5};

I would like to convert this list into a rule:

iki = {ToRules[x1==0.2 || x2==0.4 || x3==0.5]} // Flatten

which works. Since my list of equations is a very long list, I'd like to use some Mathematica function.

I have tried two options to make "dene" a rule: either eeplace "==" with "->" or substitute "," with "||". I could not do any of these options work, although I've spent quite some time on the problem. I need your help.

$\endgroup$
  • 8
    $\begingroup$ {ToRules[Or @@ {x1 == 0.2, x2 == 0.4, x3 == 0.5}]}? $\endgroup$ – J. M. is away Sep 28 '18 at 13:20
  • 5
    $\begingroup$ Rule @@@ {x1 == 0.2, x2 == 0.4, x3 == 0.5} ? $\endgroup$ – AccidentalFourierTransform Sep 28 '18 at 13:21
  • $\begingroup$ Both answers work for me, though @Accidenta's answer is more compact. $\endgroup$ – Tugrul Temel Sep 28 '18 at 15:27
  • 2
    $\begingroup$ I think it's good to point out here that replacing comma's in expressions is not possible in Mathematica. The comma is a delimiter in expressions and does not represent an infix notation for a system symbol (unlike, e.g., ; which is short for CompoundExpression). You can play games with pretty much everything else, but comma's are basically untouchable unless you start converting expressions back and forth between strings (which is generally not how you want to program). Instead, you always want to find out what heads you need to transform. And if you want to splice things, use Sequence. $\endgroup$ – Sjoerd Smit Sep 28 '18 at 16:22
  • 1
    $\begingroup$ This is the second line in an MMA notebook which contains the buttons File, Edit, Insert, Format, Cell, Graphics, Evaluation, Palettes, and Help. Don't hesitate to ask for further explanationin need. $\endgroup$ – user64494 Sep 29 '18 at 17:51
4
$\begingroup$

Much like @AccidentalFourierTransform's comment and @Pillsy's answer, you can also replace the heads but on all levels.

{x1 == 0.2, x2 == 0.4, x3 == 0.5} /. Equal -> Rule

{x1 -> 0.2, x2 -> 0.4, x3 -> 0.5}

A multi-level example

Array[x[##] == {##} &, {2, 2}]
% /. Equal -> Rule

{{x[1, 1] == {1, 1}, x[1, 2] == {1, 2}}, {x[2, 1] == {2, 1}, x[2, 2] == {2, 2}}}

{{x[1, 1] -> {1, 1}, x[1, 2] -> {1, 2}}, {x[2, 1] -> {2, 1}, x[2, 2] -> {2, 2}}}

$\endgroup$
6
$\begingroup$

Here is a simple function that does what I think you want.

makeRules[p : {_Equal ..}] := Rule @@@ p

Then

dene = {x1 == 0.2, x2 == 0.4, x3 == 0.5};
makeRules[dene]
{x1 -> 0.2, x2 -> 0.4, x3 -> 0.5}
$\endgroup$
  • $\begingroup$ All the answers work for me, your as well. For my purpose, a simple replacement rule was needed. Your answer is much more advanced than I need but a nice structural solution. $\endgroup$ – Tugrul Temel Sep 28 '18 at 15:48
  • $\begingroup$ @Tebernus. In practice, it will often be simpler just to write Rule @@@ ... but you said in your question that you would like a function. $\endgroup$ – m_goldberg Sep 28 '18 at 15:55
  • $\begingroup$ Yes, in my text I used a wrong term "function", with which I meant any solution in layman's language. Since I am not a programmer, I possibly misinformed experts in this area. Sorry for that. Regards, Tebernus $\endgroup$ – Tugrul Temel Sep 28 '18 at 17:19
5
$\begingroup$

This is a good use for Cases with a level specification, which allows you to use rules and pattern matching to create rules! The level spec helps if you have deeper nesting of Or expressions:

expr = x1 == 0.2 || x2 == 0.4 || x3 == 0.5;

Cases[expr, lhs_ == rhs_ :> {lhs -> rhs}, Infinity]
(* {{x1 -> 0.2}, {x2 -> 0.4}, {x3 -> 0.5}} *)
$\endgroup$
  • $\begingroup$ This is a good answer for pattern recognition. $\endgroup$ – Tugrul Temel Sep 28 '18 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.