3
$\begingroup$

I frequently work with tables of engineering equations in different units. I suppose physicists might do the same thing. For example, a table might look like this:

enter image description here

Some of the values are inputs and others are computed from the inputs. I put them in spreadsheets with each column representing a different set of units, as you can see. In some cases I copy a formula from one column to the other. Other times, I will convert the value in one column to the other column using a unit conversion.

How can I do something like this in Mathematica? Ideally I would like the input values to be easily editable.

The Dream

What would be really cool is if any of the values could be edited and the other values would compute correctly. When I use a spreadsheet, this is impossible. I either have to make the cell contain a number (an independent value), or a formula (a dependent value). Usually, I color the inputs blue. If I could make it so that any cell could be edited and all the others would solve and update correctly, that would be very cool.

$\endgroup$
  • $\begingroup$ Have you tried anything at all yourself? Isn't this question essentially a duplicate of your previous question from today? $\endgroup$ – MarcoB Jan 19 '17 at 14:44
  • $\begingroup$ @MarcoB No, the previous question has nothing to do with organization of information and inputs and units. It is just about working with equalities. $\endgroup$ – Tyler Durden Jan 19 '17 at 14:56
3
$\begingroup$

I think you want something like this. I use InputField expressions with Dynamic values, and the second parameter of Dynamic to update Symbols.

tF = tC = -40;

TableForm[{InputField[Dynamic[tC, (tC = #; tF = 9/5 # + 32) &]], 
  InputField[Dynamic[tF, (tF = #; tC = 5/9 (# - 32)) &]]}, 
 TableHeadings -> {{"Celsius", "Fahrenheit"}}]

enter image description here


Metaprogramming

From your comment below I take it that this behavior is what you want, but you seek an easy way to extend this to larger tables. Here is one approach to that. My function (toFields) takes a List of related equations, each with a single Symbol on the left-hand side which will become the field variable. These Symbols must be unassigned (cleared) at that time. The output is a list of InputFields as shown above, which may then be used in a Row, Grid, TableForm, etc. as desired.

Basic example

Clear[tC, tF, tK]

toFields[
  {tC == 5/9 (tF - 32), tF == 9/5 tC + 32, tK == tC + 273 + 3/20}
] // Column

enter image description here

A complete table

Clear[tC, tF, tK, ft, in, cm]

eq = {
   {tC == 5/9 (tF - 32), tF == 9/5 tC + 32, tK == tC + 273 + 3/20},
   {ft == in/12, in == cm 50/127, cm == 127/50 in}
   };

labels = {{"Celsius", "Fahrenheit", "Kelvin"},
          {"foot", "inch", "centimeter"}};

Grid[
  MapThread[Riffle, {toFields[FieldSize -> 5] /@ eq, labels}]
  , Alignment -> Left
]

enter image description here

Function code

toDynamic[s_][expr : _Equal | _And] := 
  Dynamic[s, (s = #; expr) & /. {Equal -> Set, And -> CompoundExpression}]

toFields[opts : OptionsPattern[InputField]][rel_] := toFields[rel, opts]

toFields[rel : {(_Symbol == _) ..}, opts___] := 
  With[{var = rel[[All, 1]]}, 
    InputField[#, opts] & @ toDynamic[#]@Reduce[rel, DeleteCases[var, #]] & /@ var
  ]
$\endgroup$
  • $\begingroup$ That definitely seems like the right idea, except I generally have 10 to 20 quantities along the vertical axis and then 2 to 4 different columns each with a different unit system (or scale) $\endgroup$ – Tyler Durden Jan 19 '17 at 20:13
  • $\begingroup$ @TylerDurden This method should work for that case I think. I guess the issue is programmatically generating this so you don't have to manually type it all in? $\endgroup$ – Mr.Wizard Jan 19 '17 at 20:16
  • $\begingroup$ The main thing I am trying to accomplish is the spreadsheet functionality shown in the question. I tried your complete table example in Mathematica 9 but it did not evaluate. Does it have a 10/11 function? $\endgroup$ – Tyler Durden Jan 19 '17 at 21:29
  • $\begingroup$ @Tyler You first loaded the Function code shown at the bottom of my answer? I made a change to it; please be sure you are using the current version. $\endgroup$ – Mr.Wizard Jan 19 '17 at 21:32
  • $\begingroup$ It works, that is really cool. It need to play around with the formatting and layout, but I think this is a good solution. $\endgroup$ – Tyler Durden Jan 19 '17 at 22:08

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.