在 Mathematica 中,如何为任意数量的参数编译函数 Outer[]?
如果我想从两个列表 list1
和 list2
中查找所有可能的和,我可以使用 Outer[]
函数以及 规范Plus
作为组合运算符:
In[1]= list1 = {a, b};列表2 = {c, d}; Outer[Plus, list1, list2]
Out[1]= {{a + c, a + d}, {b + c, b + d}}
如果我想成为能够处理任意数量的列表,比如列表列表,
In[2]= listOfLists={list1, list2};
那么我知道如何找到所有可能的总和的唯一方法是使用Apply[]
函数(简写为 @@
)以及 Join
:
In[3]=argumentsToPass=Join [{Plus},listOfLists]
Out[3]= {Plus, {a, b}, {c, d}}
In[4]= 外部@@ argumentToPass
Out[4]= {{a + c, a + d}, {b + c, b + d}}
或简单地
In[5]= 外部@@ Join[{Plus},listOfLists]
Out[5]= {{a + c, a + d}, {b + c, b + d}}
当我尝试编译:
In[6]= Compile[ ..... Outer @@ Join[{Plus},listOfLists] .... ]
Compile::cpapot: "编译函数参数 Outer 不支持 Outer@@Join[{Plus},listOfLists]] 唯一支持的函数参数是 Times、Plus 或 List。评估将使用未编译的函数。 “
问题是,我正在使用受支持的函数,即 Plus
。问题似乎仅在于 Apply[]
> 函数。因为如果我给它固定数量的列表到 external-plus 一起,它就可以正常工作
In[7]= Compile[{{bob, _Integer, 1}, {joe, _Integer, 1}}, Outer[Plus, bob, joe]]
Out[7]= CompiledFunction[{bob, joe}, Outer[Plus, bob, joe],-CompiledCode-]
但只要我使用 Apply
,它破坏了
In[8]= Compile[{{bob, _Integer, 1}, {joe, _Integer, 1}}, Outer @@ Join[{Plus}, {bob, joe}]]
Out[8]= Compile::cpapot: "函数参数 Outer 不支持 Outer@@Join[{Plus},{bob,joe}] 的编译。唯一支持的函数参数是 Times、Plus 或 List。评估将使用未编译的函数。”
所以我的问题是:有没有办法避免这个错误,或者有没有办法计算从任意数字中提取的元素的所有可能总和编译函数中的列表?
(另外,我不确定“编译”是否是一个合适的标签。请告知。)
非常感谢。
If I want to find all possible sums from two lists list1
and list2
, I use the Outer[]
function with the specification of Plus
as the combining operator:
In[1]= list1 = {a, b}; list2 = {c, d}; Outer[Plus, list1, list2]
Out[1]= {{a + c, a + d}, {b + c, b + d}}
If I want to be able to handle an arbitrary number of lists, say a list of lists,
In[2]= listOfLists={list1, list2};
then the only way I know how to find all possible sums is to use the Apply[]
function (which has the short hand @@
) along with Join
:
In[3]= argumentsToPass=Join[{Plus},listOfLists]
Out[3]= {Plus, {a, b}, {c, d}}
In[4]= Outer @@ argumentsToPass
Out[4]= {{a + c, a + d}, {b + c, b + d}}
or simply
In[5]= Outer @@ Join[{Plus},listOfLists]
Out[5]= {{a + c, a + d}, {b + c, b + d}}
The problem comes when I try to compile:
In[6]= Compile[ ..... Outer @@ Join[{Plus},listOfLists] .... ]
Compile::cpapot: "Compilation of Outer@@Join[{Plus},listOfLists]] is not supported for the function argument Outer. The only function arguments supported are Times, Plus, or List. Evaluation will use the uncompiled function. "
The thing is, I am using a supported function, namely Plus
. The problem seems to be solely with the Apply[]
function. Because if I give it a fixed number of lists to outer-plus together, it works fine
In[7]= Compile[{{bob, _Integer, 1}, {joe, _Integer, 1}}, Outer[Plus, bob, joe]]
Out[7]= CompiledFunction[{bob, joe}, Outer[Plus, bob, joe],-CompiledCode-]
but as soon as I use Apply
, it breaks
In[8]= Compile[{{bob, _Integer, 1}, {joe, _Integer, 1}}, Outer @@ Join[{Plus}, {bob, joe}]]
Out[8]= Compile::cpapot: "Compilation of Outer@@Join[{Plus},{bob,joe}] is not supported for the function argument Outer. The only function arguments supported are Times, Plus, or List. Evaluation will use the uncompiled function."
So my questions is: Is there a way to circumvent this error or, alternatively, a way to compute all possible sums of elements pulled from an arbitrary number of lists in a compiled function?
(Also, I'm not sure if "compilation" is an appropriate tag. Please advise.)
Thanks so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
With
的一种方法是,以编程方式创建编译函数:它可能可以做得更漂亮,但它确实有效。例如:
HTH
编辑:
您可以将已编译的函数隐藏在另一个函数后面,以便它是在运行时创建的,而您实际上看不到它:
在这种情况下,您将面临编译的开销,因为您创建了一个已编译的函数每次都重新运行。您可以通过记忆化相当优雅地应对这种开销,使用
Module
变量进行持久化,以本地化您的记忆化定义:One way it to use
With
, to create a compiled function programmatically:It can probably be done prettier, but it works. For example:
HTH
Edit:
You can hide the compiled function behind another one, so that it is created at run-time and you don't actually see it:
You face an overhead of compiling in this case, since you create then a compiled function afresh every time. You can fight this overhead rather elegantly with memoization, using
Module
variables for persistence, to localize your memoized definitions:这是我的第一篇文章。我希望我能做对。
如果您的输入是整数列表,我对编译此函数的价值持怀疑态度,至少在 Mathematica 7 中
是这样。例如:
这两个 Timings 对我来说并没有显着不同,但这当然只是一个示例。重点是,与编译版本相比,Outer 已经相当快了。
如果除了编译速度之外还有其他原因,您可能会在 Tuples 而不是 Outer 中找到一些用处,但是您仍然受到需要张量输入的编译函数的限制。
如果您的输入很大,那么可能需要采用不同的方法。给定一个整数列表,该函数将返回可能的总和以及获得每个总和的方法数量:
在许多情况下,这应该证明内存效率更高。
这花费了 3 秒和最少的内存,但尝试将 Outer 应用到 a2 会终止内核,并显示“没有更多可用内存”。
This is my first post. I hope I get this right.
If your inputs are lists of integers, I am skeptical of the value of compiling this function, at least in Mathematica 7.
For example:
The two Timings are not significantly different for me, but of course this is only one sample. The point is merely that Outer is already fairly fast compared to the compiled version.
If you have reasons other than speed for compilation, you may find some use in Tuples instead of Outer, but you still have the constraint of compiled functions requiring tensor input.
If your inputs are large, then a different approach may be in order. Given a list of lists of integers, this function will return the possible sums and the number of ways to get each sum:
This should prove to be far more memory efficient in many cases.
This took 3 seconds and minimal memory, but attempting the application of Outer to a2 terminated the kernel with "No more memory available."