Mathematica Asked by Tyilo on February 25, 2021
If I have assigned values to a
, b
and c
:
a=7;
b=5;
c=6;
and I select the expression:
(a+b)/c
and I press cmd + enter
in the Mathematica frontend, the expression will be replaced by 2
.
I would like to do something similar which would fill in the values of the symbols without evaluating the result. So (a+b)/c
would be replaced by (7+5)/6
.
Is there a way of doing this?
If so, can you also assign this to a keyboard shortcut that would replace the selected expression?
Note: Assume you don’t know the values or the names of the variables. Inputting (a+b)/c
to a function, should be able to produce something like (7+5)/6
without having to input a
, b
and c
or 7
, 5
and 6
.
Using OwnValues
and HoldForm
:
{a = 7, b = 5, c = 6};
HoldForm[(a + b)/c] /. OwnValues@a /. OwnValues@b /. OwnValues@c
(7 + 5)/6
With[{a = a, b = b, c = c}, HoldForm[(a + b)/c]]
(7 + 5)/6
Assuming that variables are already defined and you don't want to bother with listing the symbols, and you want to have a function that does it in one go:
Attributes[hold] = {HoldAll};
hold[x_] := HoldForm@x /. Cases[Hold@x, s_Symbol :> (HoldPattern@s -> s), Infinity];
hold[(a + b)/c]
(7 + 5)/6
Correct answer by István Zachar on February 25, 2021
Hold[(a + b)/c] /. {a -> 1, b -> 3, c -> 4} /. Hold -> Defer
(* (1 + 3)/4 *)
Hold[(a + b)/c] /. {a -> 1, b -> -3, c -> 4} /. Hold -> Defer
(* (1 - 3)/4 *)
Hold[(a + b)/c] /. {a -> 1, b -> -3, c -> -4} /. Hold -> Defer
(* -(1/4) (1 - 3) *)
Answered by Alexei Boulbitch on February 25, 2021
Edit: answer rewrite
This question in its base form is a duplicate, but since I cannot find the original, and since you extended the question to something more unique than what I recall, I shall provide a short answer.
You asked:
I know this a probably even harder, but would it be possible to output
(7+5)/(-6)
instead of-(1/6)*(7+5)
whenc=-6
?
For that kind of control see for example: Returning an unevaluated expression with values substituted in
Combining that with RuleCondition
, described in Replacement inside held expression, we can use:
SetAttributes[{defer, fill}, HoldAll]
MakeBoxes[defer[args__], fmt_] := Block[{Times}, MakeBoxes[Defer[args], fmt]]
fill[expr_] := defer[expr] /. x_Symbol :> RuleCondition[x]
Now:
{a, b, c} = {7, 5, -6};
fill[(a + b)/c]
(7+5)*-(1/6)
Because this uses Defer
you can use the output of fill
as input and it will fully evaluate.
Answered by Mr.Wizard on February 25, 2021
Mathematica has the function Hold which prevents evaluation, until you Release that Hold.
Hold[(a+b)/c] /.{a->1,b->2,c->3}
gives
Hold[(1 + 2)/3]
Then
Release[Hold[(1 + 2)/3]]
will give
1
Answered by Ymareth on February 25, 2021
In the same search that found your post, I also found: https://mathematica.stackexchange.com/a/100152/76328
I tailored Edmund's approach just slightly to fit my needs. I hadn't heard of Inactivate before, but it performs substitutions while not evaluating the main expression. It sounds like a simple solution to this post?
That said, I do like István Zachar's general-case approach as well.
Tailored version noted above:
InactiveAndActive = Function[expr, Column@Through@{Inactivate, Identity}@Unevaluated@expr, HoldAll];
EDIT: I just noticed that the above doesn't format things like 1/x in StandardForm. A better solution is:
HoldAndEvaluate = Function[expr, Column@Through@{Inactivate, Identity}@Unevaluated@expr /. Inactive[x_] -> HoldForm@*x, HoldAll];
Answered by Sean on February 25, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP