代码之家  ›  专栏  ›  技术社区  ›  Yaroslav Bulatov

在Mathematica中看到截断的消息

  •  8
  • Yaroslav Bulatov  · 技术社区  · 15 年前

    是否可以看到 Message 被截短了?我看到一些东西 0.105309,0.394682,<<20>>,<<20>>,<<20>>,0.394631 Messages 窗户。我猜 <<20>> 表示省略的部分,如何获取整个内容?

    在50个变量的问题上,函数叫做FindMaximum。

    更新: 西蒙的答案似乎适用于一般的消息,我也发现了一种特定的方法来捕获FindMaximum“不是真正的数字”消息。

    为了获得“最大值”消息失败的原因,您可以执行以下操作(重新定义) 消息 是我唯一能找到的方法,因为这一点没有被传递到 EvaluationMonitor StepMonitor )

    Unprotect[Message];
    Message[FindMaximum::"nrnum", args___] := (captured = {args}; 
       Print["Captured FindMaximum::nrnum at ", First[{args}]]);
    {badvals, badvars, badobj} = ReleaseHold[captured];
    
    4 回复  |  直到 15 年前
        1
  •  10
  •   Simon    15 年前

    我不确定您是否可以恢复已生成的长消息。作为 $MessageList Message[] 只存储消息名,不存储传递给它们的参数。

    停止 Short[] 从自动应用到邮件 Unset[$MessagePrePrint] . 它的默认值是 Automatic --不管需要什么。


    与其一直打印长消息,不如使用

    General::longmsg="A long message (`1`) was produced. The full message has been saved in `2`";
    $MessagePrePrint=With[{bc=ByteCount[#]},If[bc>65536,
      With[{fn=FileNameJoin[{$HomeDirectory,StringJoin["MmaMsg",ToString/@DateList[]]}]},
        Put[#,fn];Message[General::longmsg,bc,Row[{fn}]];Short[Shallow[#],1]],
      #]]&;
    

    这将正常打印消息,除非 ByteCount 太大(>65536),在这种情况下,它将打印两条消息:第一条消息通知您生成了一条大消息,并将其保存的文件提供给您。第二个是完整消息的截断版本。

        2
  •  2
  •   Andrew Moylan    15 年前

    我现在不在正确的电脑前,所以我不能确定。。。但我认为您可以完全定制消息处理行为,例如:

    Block[{Message = f}, ...]
    

    例如,您可以使用

    f[args___] := Print[{args}];
    

    再说一次,现在不在Mathematica面前。请随意用wiki编辑此答案。

        3
  •  2
  •   Alexey Popkov    14 年前

    未记录的函数 Internal`HandlerBlock ( uncovered Maxm RyTin)适用于这里:

    Off[FindMaximum::"nrnum"]
    Internal`HandlerBlock[{"Message", Print}, 
     Message[FindMaximum::"nrnum", arg1, arg2, arg3]]
    (* => Hold[Message[FindMaximum::nrnum,arg1,arg2,arg3],False]*)
    

    另一个处理程序类型是“MessageTextFilter”为调用“消息” 每个生成的消息都传递一个Hold[…]形式的参数。。。, …]指向handler函数,第二个元素设置为False 安静的消息。“对于 实际上,打印并用三个参数调用函数。

    马克西姆莱廷


    另一种可能是修改 $MessagePrePrint 以这样的方式,它将打印包含带截断参数的内联单元格的消息,这些参数在求值时可以扩展为完整参数。它可以用 Interpretation :

    truncatingRules = {lst : {x_, y__} /; 
         MatrixQ[lst, NumberQ] && Length[lst] > 3 :>
        {x /. v : {a_, b__} /; Length[v] > 3 :>
           {a, 
            Interpretation[Style[Skeleton[Length[{b}]], Gray], 
             Sequence @@ {b}]},
         Interpretation[Style[Skeleton[Length[{y}]], Gray], 
          Sequence @@ {y}]},
       lst : {x_, y__} /; VectorQ[lst, NumberQ] && Length[lst] > 3 :>
        {x, Interpretation[Style[Skeleton[Length[{y}]], Gray], 
          Sequence @@ {y}]}};
    
    InlineCellInsideMessage[expr_] := 
     Style[DisplayForm[
       Cell[BoxData[MakeBoxes[expr, StandardForm]], "Input"]], 
      FontWeight -> Bold, FontFamily -> "Courier", Background -> Yellow, 
      FontColor -> Red, FontSize -> 12, StripOnInput -> True, 
      AutoNumberFormatting -> True, ShowStringCharacters -> True]
    
    $MessagePrePrint = 
     Function[expr, 
      If[TrueQ[ByteCount[Unevaluated[expr]] < $OutputSizeLimit/20.], 
       InlineCellInsideMessage[expr],
       InlineCellInsideMessage[expr /. truncatingRules]
       ]]
    

    当然,上面的版本 $message预打印 只是一个草稿,但它说明了主要思想。

        4
  •  0
  •   darioo    15 年前

    引用 this link :

    “当你做符号计算时,很容易得到极其复杂的表达式。通常,您甚至不想看到计算的完整结果。”

    您可以使用 Short .

    Short[%, n] 将显示 n 你之前的结果,所以这可能是你需要的。

    更多信息 here .