5
$\begingroup$

I know that this seems to be unnecessary, but here I have a bunch of assignments, like around 50, and I want to create rules for all these to have the freedom to apply these rules only on certain expressions. However I can't find an efficient way to do this. Please help.

Here's an example:

I want to turn the following assignment expressions:

a = Cosh[ξ0];
b = Sinh[ξ0];
d = Coth[ξ0];
c = 1/((Sinh[ξ0])^2*Cosh[ξ0])^(1/3);
...

into a rule:

subabcd = {
   a -> Cosh[ξ0],
   b -> Sinh[ξ0],
   d -> Coth[ξ0],
   c -> 1/((Sinh[ξ0])^2*Cosh[ξ0])^(1/3)
   };

Problem is I got more then 50 of the definitions..

Clear is also not efficient because I have to put in all those names of the variables.

$\endgroup$
3
  • $\begingroup$ Consider using the text editing capabilities in the notebook front-end----search and replace. $\endgroup$ Commented Aug 5, 2020 at 2:14
  • $\begingroup$ Have you seen DownValues and OwnValues and so forth? $\endgroup$
    – Michael E2
    Commented Aug 5, 2020 at 3:00
  • $\begingroup$ @PaulCommentary hmm, it worked, I'll also have to replace the semicolon into comma. I guess that's the most efficient way for now. Thank you for reminding me of it. $\endgroup$
    – Ying Zhang
    Commented Aug 5, 2020 at 3:39

1 Answer 1

4
$\begingroup$

How about:

torulelist = 
  Function[a, Unevaluated[a] /. {Set -> Rule, CompoundExpression -> List}, HoldAll];

torulelist[
 a = Cosh[ξ0];
 b = Sinh[ξ0];
 d = Coth[ξ0];
 c = 1/((Sinh[ξ0])^2*Cosh[ξ0])^(1/3)]

(*
{a -> Cosh[ξ0], b -> Sinh[ξ0], d -> Coth[ξ0], 
 c -> 1/(Cosh[ξ0] Sinh[ξ0]^2)^(1/3)}
*)
$\endgroup$
7
  • $\begingroup$ Worked really well! Thank you! $\endgroup$
    – Ying Zhang
    Commented Aug 5, 2020 at 3:53
  • $\begingroup$ I have to ask, how to type in those greeks?? I can only copy it from other posting. $\endgroup$
    – Ying Zhang
    Commented Aug 5, 2020 at 3:56
  • 1
    $\begingroup$ @YingZhang Try this extension: mathematica.meta.stackexchange.com/a/1044/1871 or this site: steampiano.net/msc $\endgroup$
    – xzczd
    Commented Aug 5, 2020 at 4:01
  • $\begingroup$ Nice! Thank you very much! $\endgroup$
    – Ying Zhang
    Commented Aug 5, 2020 at 4:03
  • $\begingroup$ @YingZhang Hi, if the post indeed solves your problem, please consider accepting it. $\endgroup$ Commented Aug 5, 2020 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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