返回非常大数组的 Mathematica 函数非常慢

发布于 2024-12-05 01:13:48 字数 767 浏览 2 评论 0原文

我正在寻找一种重写 Mathematica 函数的方法,该函数读取数值文本文件(大小为几百 Mb)并从中创建一个 Mathematica 数组。我使用 ReadList 来读取文件,这会像人们预期的那样快,但我的问题是如何将此数组返回到该函数外部的变量而不被复制或类似的愚蠢事情?

目前我的函数是这样写的

LoadData[vars_...]:=Module[{localvars_...},...Return[giant array];];

,并尝试将巨型数组分配给这样的变量:

table = LoadData[vars_...]

但是,从“巨型数组”中取出数组并将其放入“表”中所花费的时间比文件读取本身要长。显然,整个事情都被复制了或者有些愚蠢的事情;如果我处理更大的数据,它可能会溢出内存。

我怎样才能防止这种情况发生?如何让 Mathematica 只重命名数组而不是复制所有数据?如果相关,该函数是在与我使用它的包不同的包中定义的。

已解决:

啊,谢谢贝利撒留,关于在函数之间传递数据的简单问题确实给了我需要的提示,即

Attributes[LoadData]={HoldFirst}

包中的设置似乎使函数表现得更明智。

I am looking for a way to rewrite a Mathematica function which reads a text file of numerical values (several hundred Mb in size) and creates a Mathematica array out of it. I use ReadList to read the file and this goes as quickly as one could expect, but my problem is how do I get this array back out to a variable outside this function without it being copied or anything silly like that?

Currently my function is written like

LoadData[vars_...]:=Module[{localvars_...},...Return[giant array];];

and try to assign the giant array to a variable like this:

table = LoadData[vars_...]

However the time spent taking the array from "giant array" and putting it in "table" is longer than the file read itself. Clearly the whole thing is being copied or something silly; it will probably overflow the memory if I go to much larger data.

How can I prevent this happening? How can I get Mathematica to just rename the array rather than copy all the data? In case it is relevant, the function is defined in a separate package to the one where I am using it.

SOLVED:

Ahh, thanks belisarius, simple question on passing data between functions indeed gives me the hint I need, i.e. setting

Attributes[LoadData]={HoldFirst}

in the package seems to make the function behave sensibly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾忌 2024-12-12 01:13:48

您还可以使变量动态并从函数内更新它 - 直接将数据加载到其中。但 Unevaluated 对于这个原因也同样有效。

You can also make the variable Dynamic and update it from within the function - load data straight into it. But Unevaluated works just as well for this cause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文