简体   繁体   English

为什么 ToMatlab[] 在 Matlab 中随机产生无效矩阵(vertcat 错误)? (包括错误修正)

[英]Why does ToMatlab[] randomly yield invalid matrices in Matlab (vertcat error)? (bugfix included)

I've been using ToMatlab[], available at http://library.wolfram.com/infocenter/MathSource/577/ , to convert Mathematica expressions to Matlab in order to numerically simulate a model.我一直在使用 ToMatlab[],可在http://library.wolfram.com/infocenter/MathSource/577/ 获得,将 Mathematica 表达式转换为 Matlab 以数值模拟 Z4949DB0F35E6830FDC33

I recently ran into a bug: I try to have Matlab evaluate a function produced by ToMatlab[], but it gives me the following error:我最近遇到了一个错误:我尝试让 Matlab 评估由 ToMatlab [] 生成的 function,但它给了我以下错误:

??? Error using ==> vertcat
CAT arguments dimensions are not consistent.

which means I have a non-square matrix (ie, not a matrix).这意味着我有一个非方阵(即不是矩阵)。 Searched around and found (one) mention of the bug, but no solution is mentioned: http://groups.google.com/forum/#.searchin/comp.soft-sys.math.mathematica/tomatlab/comp.soft-sys.math.mathematica/cxNZC9IYgfQ/2Q6jP4U4hzkJ搜索并发现(一个)提及该错误,但未提及解决方案: http://groups.google.com/forum/#.searchin/comp.soft-sys.math.mathematica/tomatlab/comp.soft- sys.math.mathematica/cxNZC9IYgfQ/2Q6jP4U4hzkJ

Dug into it a little more, and discovered what the problem was: Converting a symbolic matrix using the original ToMatlab[] routine can introduce an error if the foldlines[] routine folds a line with the "..." literal right before an operator that is both binary and unary (such as + and -) and is not separated from its right operand by whitespace (eg, "+5" instead of "+ 5").再深入研究一下,发现问题出在哪里:如果 foldlines[] 例程在运算符之前折叠带有“...”字面量的行,则使用原始 ToMatlab[] 例程转换符号矩阵可能会引入错误它既是二元又是一元(例如 + 和 -),并且没有用空格与其右操作数分隔(例如,“+5”而不是“+5”)。

Reason being is that the Dot-Dot-Dot symbol "..." are interpreted as spaces in matrices, according to the Matlab documentation.根据 Matlab 文档,原因是点-点-点符号“...”被解释为矩阵中的空格。 This causes the extra element to be introduced.这会导致引入额外的元素。 See the appended examples for an illustration.有关说明,请参见附加示例。

I was able to fix this bug by padding the "+" operator by replacing "+" with " + " in the Plus conversion pattern "ToMatlabaux[e_ /; Head[e] === Plus]".我可以通过在 Plus 转换模式“ToMatlabaux[e_ /; Head[e] === Plus]”中将“+”替换为“+”来填充“+”运算符来修复此错误。 I made this and similar (but otherwise insignificant) style modifications to the original ToMatlab[] routine and uploaded it to pastebin here:我对原始 ToMatlab[] 例程进行了这个和类似(但在其他方面无关紧要)的样式修改,并将其上传到此处的 pastebin:

http://pastebin.com/TcjErHVT - ToMatlab Bugfix http://pastebin.com/TcjErHVT - ToMatlab 修正

I attempted to contact Dr. Ojanen about this, but I received an error from his old mail server saying that his username no longer existed there.我试图就此联系 Ojanen 博士,但我从他的旧邮件服务器收到一条错误消息,说他的用户名不再存在。

I informed Wolfram about this too, but they said that it is up to the contributor to update their code in the Library Archive.我也通知了 Wolfram,但他们说由贡献者来更新他们在图书馆档案中的代码。

[Appended examples] [附加示例]

EDU>> [3 + 5]
ans =
     8

EDU>> [3 + ...
5]
ans =
     8

EDU>> [3 +...
5]
ans =
     3     5

EDU>> [3 (+ 5)]
ans =
     3     5

EDU>> [3 ...
+5]
ans =
     3     5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM