如何在Mathematica笔记本中找到发生错误的行? [英] How to find line where error occurred in Mathematica notebook?

查看:623
本文介绍了如何在Mathematica笔记本中找到发生错误的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为myUsefulFunctions.m的Mathematica文件,其中包含一个名为mySuperUsefulFunction的函数。假设我在笔记本电脑中调用mySuperUsefulFunction,并得到以下错误:

I have a Mathematica file called myUsefulFunctions.m containing, for example, a function called mySuperUsefulFunction. Suppose I call mySuperUsefulFunction in a notebook and get the following error:

Part::pspec: Part specification #1 is neither an integer nor a list of integers. >>

有没有办法在myUsefulFunctions.m中找到发生错误的行?

Is there a way to find the line in myUsefulFunctions.m where this error occurred?

推荐答案

我不知道在文件中找到一行的方式,我假设是没有错误地读取。

I don't know of a way to find the line in the file, which I assume was read without error.

然而,您可以使用 Trace 和相关函数来查看评估链中出现错误的位置。

You can however use Trace and related functions to see where in the evaluation chain the error occurs.

示例:

myFunc1[x_, y_] := x[[y]]
myFunc2[a_List, n_] := Pick[a, a, myFunc1[a, n]]

myFunc2[Range@10, #1]

During evaluation of In[4]:= Part::pspec: Part specification #1 is neither an integer nor a list of integers. >>

使用跟踪

myFunc2[Range@10, #1] // Trace // Column

{Range[10], {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}
myFunc2[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, #1]
Pick[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, myFunc1[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, #1]]
{myFunc1[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, #1], {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}[[#1]], {Message[Part::pspec, #1], {MakeBoxes[Part::pspec: Part specification #1 is neither an integer nor a list of integers. >>, StandardForm], RowBox[{RowBox[{Part, ::, "pspec"}], : , "\!\(\*StyleBox[\"\\\"Part specification \\\"\",  \"MT\"]\)\!\(\*StyleBox[\!\(#1\),  \"MT\"]\)\!\(\*StyleBox[\"\\\" is neither an integer nor a list of integers.\\\"\",  \"MT\"]\) \!\(\*ButtonBox[\">>\",  ButtonStyle->\"Link\",  ButtonFrame->None,  ButtonData:>\"paclet:ref/message/General/pspec\",  ButtonNote -> \"Part::pspec\"]\)"}]}, Null}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}[[#1]]}

你可以看到,在消息[Part :: pspec,#1] 之前,会导致格式化的时间很长,我们有:

You can see that just before Message[Part::pspec, #1] is called, which results in a long mess of formatting, we had:

{myFunc1 [{1,2,3,4,5,6,7,8,9,10},#1],{1, 2,3,4,5,6,7,8,9,10} [[#1]]

这表明 myFunc1 [{1,2,3,4,5,6,7,8,9,10},#1] 被调用,这导致评估 {1,2,3,4,5,6,7,8,9,10} [[#1]] 显然是错误的。

This shows that myFunc1[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, #1] is called, and this causes evaluation of {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}[[#1]] which is clearly in error.

请看这个问题及其答案,方便使用 Trace

Please see this question and its answers for a more convenient use of Trace:

表达数学评估序列的最清晰的方式

这篇关于如何在Mathematica笔记本中找到发生错误的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆