Solving calculator problems

Harold Thimbleby - http://www.uclic.ucl.ac.uk/harold

This file is the background for establishing some matrix identities for the calculator examples used in the papers.

Utility functions...

NameOf[matrix_, name_List] := NameOf[matrix] = 
          RowBox[Map[NameOf[matrix, #] &, name]];
NameOf[matrix_, name_String] := NameOf[matrix] = 
          FrameBox[GridBox[{{name}}]];

    
problemNameOf[matrix_, name_String] := problemNameOf[matrix] = 
          FrameBox[GridBox[{{name}}]];
\(\(\(problemNameOf[matrix_] := NameOf[matrix];\)\(
\)
    \)\)
\(\(\(explain[mat_] := 
        Module[{v = {StyleForm["d", FontSlant -> Italic, 
                  FontFamily -> "Times"], 
                StyleForm["m", FontSlant -> Italic, 
                  FontFamily -> "Times"]}}, 
{problemNameOf[mat], "=", 
            MatrixForm[mat], "    ", {v} // 
              TraditionalForm, " -> ", {v . mat} // 
              TraditionalForm}
];\)\(
\)
    \)\)
explainList[matlist_] := 
      StylePrint[
        GridBox[Map[(explain[#]) &, matlist], 
            ColumnAlignments -> Left] // DisplayForm, 
        Background -> RGBColor[1, 1, 0.6]]

Define basic keys

NameOf[ac=(({{0, 0}, {0, 1}})),"AC"];
NameOf[mp=(({{1, 1}, {0, 1}})),"M+"];
NameOf[mm=(({{1, -1}, {0, 1}})),"M-"];
NameOf[cs=(({{-1, 0}, {0, 1}})),"±"];
NameOf[mrc=(({{0, 0}, {1, 1}})),"MRC"];
NameOf[mmrce=(({{1, 0}, {-1, 1}})),{"-", "MRC", "="}];
NameOf[mrcmrc=(({{0, 0}, {1, 0}})),{"MRC", "MRC"}];
mats  =  { ac,  mp,  mm,  mrc, 
          cs, mmrce,  mrcmrc};

Summarise key definitions

StylePrint[RowBox@Map[NameOf, {ac, mp, mm, cs, mmrce}] // DisplayForm, 
      Background -> RGBColor[1, 1, 0.6]]

explainList[mats]

Define the goals

problemNameOf[swap = (({{0, 1}, {1, 0}})),"SWAP DISPAY & MEMORY"];
problemNameOf[savedisplay=(({{1, 1}, {0, 0}})),"SAVE DISPLAY"];
problemNameOf[zerodisplay=(({{0, 0}, {0, 1}})),"ZERO DISPLAY"];
problemNameOf[zeroeverything=(({{0, 0}, {0, 0}})),"ZERO DISPAY & MEMORY"];
problemNameOf[zeromemory=(({{1, 0}, {0, 0}})),"ZERO MEMORY"];

explainList[
      newmats = {swap, savedisplay, zeromemory, zeroeverything, 
          zerodisplay}]

Solve goals using the keys

The function howto finds the shortest products (with no more terms than maxTerms) that get to the goal. You can change limit to make it search further. Note that maxTerms refers to matrices, rather than button presses (e.g., mrcmrc is the product of two button presses).

\(howto[goal_, 
        maxTerms_:  8] := 
Module[{rec, tried, 
          i}, 
rec[m_, depth_, maxDepth_, 
            soln_] := 
Module[{n, product, s, 
              i}, 
If[MemberQ[tried, μ[m]], 
              Return[False]]; 
AppendTo[
              tried, μ[m]]; 
For[n = 1, 
              n ≤ 
                Length[mats], \(n++\), 
product = 
                m . mats\_\(\([[\)\(n\)\(]]\)\); 
s = 
                Append[soln, 
                  NameOf[mats\_\(\([[\)\(n\)\(]]\)\)]]; 
If[
                goal == product, 
StylePrint[
                  RowBox[{"To implement ", 
                        problemNameOf[goal], "press", RowBox[s]}] // 
                    DisplayForm, 
                  Background -> 
                    RGBColor[1, 1, 0.5]]; 
Return[
                  True], 
If[
                  depth < maxDepth && 
                    rec[product, depth + 1, maxDepth, 
                      s], 
Return[
                    True]
]
];
]; 
Return[
              False];
]; 
For[i = 1, 
          i ≤ 
            maxTerms, \(i++\), 
tried = {}; 
If[rec[IdentityMatrix[2], 1, i, {}], 
            Return[]]
]; 
StylePrint[
          RowBox[{problemNameOf[goal], " not possible in no more than ", 
                ToString@maxTerms, \ " terms."}] // DisplayForm, 
          Background -> 
            RGBColor[1, 1, 0.5]]
]\)

Scan[howto, newmats]

howto[swap, 3]