在 Mathematica 中获取矩阵输入

Getting Matrix Input in Mathematica

我想让用户输入一个矩阵。我有以下代码。

rmatrix = {{Input["r11"], Input["r12"]}, {Input["r21"], Input["r22"]}}

但它为每个项目显示一个单独的对话框。 我想在一个对话框中获得完整的矩阵。

我也试过另一种方法。检查下面的代码。

form = FormFunction[{{"r11" -> "Number", 
     "r12" -> "Number"}, {"r21" -> "Number", "r22" -> "Number"}}, 
   MatrixForm];

但我不知道如何将这些值分配给矩阵。

rmatrix = DialogInput[{r11, r12, r21, r22}, Grid[{
     {"r11", InputField[Dynamic[r11], Number], "r12", 
      InputField[Dynamic[r12], Number]},
     {"r21", InputField[Dynamic[r21], Number], "r22", 
      InputField[Dynamic[r22], Number]},
     {
      Button["done", DialogReturn[{{r11, r12}, {r21, r22}}], 
       ImageSize -> Automatic]}}]];

如果您这样做,它不会让您在不输入所有值的情况下退出:

rmatrix = DialogInput[{r11, r12, r21, r22}, Grid[{
     {"r11", InputField[Dynamic[r11], Number], "r12", 
      InputField[Dynamic[r12], Number]},
     {"r21", InputField[Dynamic[r21], Number], "r22", 
      InputField[Dynamic[r22], Number]},
     { Button["done", DialogReturn[{{r11, r12}, {r21, r22}}], 
       ImageSize -> Automatic, 
       Enabled -> Dynamic[AllTrue[{r11, r12, r21, r22}, NumericQ]]]}}]];
Interpretation[{
  r11 = MakeBoxes[0],
  r12 = MakeBoxes[0],
  r21 = MakeBoxes[0],
  r22 = MakeBoxes[0]}, Panel[Grid[{
    {InputField[Dynamic[r11], Boxes, FieldSize -> Tiny], 
     InputField[Dynamic[r12], Boxes, FieldSize -> Tiny]},
    {InputField[Dynamic[r21], Boxes, FieldSize -> Tiny], 
     InputField[Dynamic[r22], Boxes, FieldSize -> Tiny]}
    }]],
 With[{r11 = ToExpression[r11], r12 = ToExpression[r12], 
   r21 = ToExpression[r21], 
   r22 = ToExpression[r22]}, {{r11, r12}, {r21, r22}}]]