Mathematica function which formats Physics calculation

TraditionalForm[
 Column[{
   Defer[m = Quantity[1, "Kilograms"]],
   Defer[a = Quantity[9.81, "Meters" ("Seconds")^-2]],
   Defer[F == m a],
   m = Quantity[1, "Kilograms"]; 
   a = Quantity[9.81, "Meters" ("Seconds")^-2];
   HoldForm[F] == m a}]
 ]

enter image description here

For a function version:

display[x_Quantity, y_Quantity] := Block[{m, a, F},
  TraditionalForm[
   Column[{
     Defer[m = x],
     Defer[a = y],
     Defer[F == m a],
     HoldForm[F] == x*y}]
   ]
  ]

display[Quantity[1, "Kilograms"], 
 Quantity[9.81, "Meters" ("Seconds")^-2]]

enter image description here

Note that making a more general function is a little bit difficult given limited information in the OP.


Here is a function that given an equation and a list of replacement rules for the variables, inserts the values and formats the output:

insertEq[eqn_, var_] := TraditionalForm[Column[Flatten[{  
  var /. Rule -> Equal,  
  eqn,  
  eqn /. var  
 }]]]  

For your minimalist example you get:

insertEq[F == a m, {
  m -> Quantity[3, "Kilograms"],
  a -> Quantity[9.81, "Meters" ("Seconds")^-2]
 }]

enter image description here

Edit

To extend a bit: Here is a function that does unit simplification if needed. In addition, there is an optional third argument to specify the number of significant digits of the result.

Clear[insertEq]
insertEq[lhs_ == rhs_, var_, prec_: MachinePrecision] := Module[{rhsIn, rhsSim},
  rhsIn = SetPrecision[rhs /. var, prec];
  rhsSim = UnitSimplify[rhsIn];
  TraditionalForm[Column[Flatten[{
      var /. Rule -> Equal,
      Equal @@ {lhs, rhs, rhsIn, If[rhsIn === rhsSim, Nothing, rhsSim]}
     }]]]]

Example 1:

insertEq[F == a m, {
  m -> Quantity[3, "Kilograms"],
  a -> Quantity[9.81, "Meters" ("Seconds")^-2]
 }]

enter image description here

Example 2:

insertEq[A == Pi r^2, {r -> Quantity[0.5, "Meters"]}]

enter image description here

without too many nonsense digits:

insertEq[A == Pi r^2, {r -> Quantity[0.5, "Meters"]},3]

enter image description here


Via proper original record

I think it is much easier not via a "function" but via a "program" or basically the format you formulate it originally. There is almost no difference in amount of original information, but because of different format no function is necessary:

{mm=Quantity[1,"Kilograms"];
    Inactivate[m=mm,Set],
aa=Quantity[9.81,"Meters" ("Seconds")^-2];
    Inactivate[a=aa,Set],
    Inactivate[F == m a,Set|Times],
F == Activate[mm aa]}//Column//TraditionalForm

enter image description here

Via a function

Or if you insist on having a function then you probably should formulate original record a bit differently:

x=Inactivate[
{m=Quantity[1,"Kilograms"],
a=Quantity[9.81,"Meters" ("Seconds")^-2],
F==m a,
F=m a},Set]

Then defining a function as

someFunction[x_]:=Module[{aa,mm},
{Inactivate[m=x[[1,2]],Set],
Inactivate[a=x[[2,2]],Set],
x[[3]],
F==x[[1,2]]x[[2,2]]}//
Column//TraditionalForm]

you can get now same result:

someFunction[x]

but again, this seems redundant as formulating original record properly can give you what you need, no function needed.

Further thoughts on automation

For automation you might want to look into templating:

  • Generate a Notebook from a Template

  • Working with Templates