1
$\begingroup$

I would like to define two rules: one for the material and thickness the second one for volume and mass

rule1={t->1/2,row->1000};
rule2={v->t^3,m->v*row};

now if I used the following:

rule2/.rule1={v->1/8,m->v*1000}

how to replace v in rule2 to get the following:

{v->1/8,m->125}

$\endgroup$
6
  • $\begingroup$ Why not just use Set and allow MMA to do the substitutions for you: t = 1/2; row = 1000; vv = t^3; mm = vv row; Thread[{v, m} -> {vv, mm}] $\endgroup$
    – MelaGo
    Commented May 19, 2021 at 2:48
  • $\begingroup$ Somewhat convoluted: ReplacePart[rule2, 2 -> (rule2[[2]] /. rule2[[1]])] /. rule1 $\endgroup$
    – Bob Hanlon
    Commented May 19, 2021 at 2:49
  • $\begingroup$ I dont want to set the parameters to values because I am not sure what is the material that I'll be using (design problem). But after conducting the full study I will replace all parameters by the appropriate values. @MelaGo $\endgroup$
    – Love Eva
    Commented May 19, 2021 at 2:54
  • 1
    $\begingroup$ (rule2 /. rule1) /. (lhs_ -> rhs_) :> (lhs -> (rhs /. (rule2 /. rule1))) $\endgroup$
    – Bob Hanlon
    Commented May 19, 2021 at 3:24
  • 2
    $\begingroup$ I would turn your rules into equations (e.g. Equal@@@rule1) then use Solve. $\endgroup$
    – mikado
    Commented May 19, 2021 at 9:33

1 Answer 1

3
$\begingroup$
ReplaceAll[rule1] @ MapAt[ReplaceAll[rule2], rule2, {All, 2}]
{v -> 1/8, m -> 125}
$\endgroup$
2
  • $\begingroup$ What changes do I need yo do to the above code to work with Mathematica 7 ? $\endgroup$
    – Love Eva
    Commented May 19, 2021 at 16:21
  • $\begingroup$ @LoveEva, try ReplaceAll[MapAt[ReplaceAll[#, rule2] &, rule2, {All, 2}], rule1] or MapAt[# /. rule2 &, rule2, {All, 2}] /. rule1. $\endgroup$
    – kglr
    Commented May 19, 2021 at 18:55

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.