在非张量列表上使用已编译的可列表函数
我有兴趣在不需要张量的列表上使用可列表编译函数。我想了解为什么某些功能可以工作,而其他功能则不能工作并关闭内核。这是一个例子。
假设我们有两个矩阵 m1 和 m2,如下所示。
m1 = {{1.0, 0.5, 0.5}, {0.5, 1.0, 0.5}, {0.5, 0.5, 1.0}};
m2 = {{1.0, 0.5}, {0.5, 1.0}};
我们可以创建两个不同的列表,第一个是张量,第二个不是。
In[3]:= mList1 = {m1, m1};
In[4]:= TensorQ[mList1]
Out[4]= True
In[5]:= mList2 = {m1, m2};
In[6]:= TensorQ[mList2]
Out[6]= False
类似地,令 v1 和 v2 为两个向量,vList1 和 vList2 为两个列表,如下所示
v1 = {1.0, 1.5, 0.9};
v2 = {1.1, 0.7};
In[9]:= vList1 = {v1, v1};
In[10]:= TensorQ[vList1]
Out[10]= True
In[11]:= vList2 = {v1, v2};
In[12]:= TensorQ[vList2]
Out[12]= False
现在我们定义两个可列表函数 func1 和 func2
func1 = Compile[{{m, _Real, 2}, {v, _Real, 1}},
m.v,
RuntimeAttributes -> Listable
];
func2 = Compile[{{m, _Real, 2}, {v, _Real, 1}, {r, _Real}},
r*(m.v),
RuntimeAttributes -> Listable
];
func1 适用于张量列表和非张量列表,如下所示
In[15]:= func1[mList1, vList1]
Out[15]= {{2.2, 2.45, 2.15}, {2.2, 2.45, 2.15}}
In[16]:= func1[mList2, vList2]
Out[16]= {{2.2, 2.45, 2.15}, {1.45, 1.25}}
func2 适用于张量列表 mList1 和 vList1和一个实数常量,如下所示
In[17]:= func2[mList1, vList1, 5.0]
Out[17]= {{11., 12.25, 10.75}, {11., 12.25, 10.75}}
该函数能够重复使用单个实数 5.0。
但是,相同的函数不适用于非张量列表 mList2 和 vList2。以下命令关闭我的内核(Mathematica 8.0.4,在 Windows Vista 上)。
func2[mList2, vList2, 5.0]
有趣的是,以下作品。
In[18]:= func2[mList2, vList2, {5.0, 5.0}]
Out[18]= {{11., 12.25, 10.75}, {7.25, 6.25}}
有人能解释这种行为吗?
I am interested in using a Listable Compiled function on lists that need not be tensors. I want to understand why some functions work, where as others do not and shut down the kernel. Here is an example.
Suppose we have two matrices m1 and m2 as follows.
m1 = {{1.0, 0.5, 0.5}, {0.5, 1.0, 0.5}, {0.5, 0.5, 1.0}};
m2 = {{1.0, 0.5}, {0.5, 1.0}};
We can make two different lists, the first is a tensor and the second is not.
In[3]:= mList1 = {m1, m1};
In[4]:= TensorQ[mList1]
Out[4]= True
In[5]:= mList2 = {m1, m2};
In[6]:= TensorQ[mList2]
Out[6]= False
Similarly, let v1 and v2 be two vectors and vList1 and vList2 be two lists as follows
v1 = {1.0, 1.5, 0.9};
v2 = {1.1, 0.7};
In[9]:= vList1 = {v1, v1};
In[10]:= TensorQ[vList1]
Out[10]= True
In[11]:= vList2 = {v1, v2};
In[12]:= TensorQ[vList2]
Out[12]= False
Now we define two listable functions func1 and func2
func1 = Compile[{{m, _Real, 2}, {v, _Real, 1}},
m.v,
RuntimeAttributes -> Listable
];
func2 = Compile[{{m, _Real, 2}, {v, _Real, 1}, {r, _Real}},
r*(m.v),
RuntimeAttributes -> Listable
];
func1 works on both tensor and non tensor lists as can be seen below
In[15]:= func1[mList1, vList1]
Out[15]= {{2.2, 2.45, 2.15}, {2.2, 2.45, 2.15}}
In[16]:= func1[mList2, vList2]
Out[16]= {{2.2, 2.45, 2.15}, {1.45, 1.25}}
func2 works on the tensor lists mList1 and vList1 and an real constant as follows
In[17]:= func2[mList1, vList1, 5.0]
Out[17]= {{11., 12.25, 10.75}, {11., 12.25, 10.75}}
The function is capable of using the single real 5.0, repeatedly.
However, the same function does not work on the non-tensor lists mList2 and vList2. The following shuts down my kernel (Mathematica 8.0.4, on Windows Vista).
func2[mList2, vList2, 5.0]
Interestingly, the following works.
In[18]:= func2[mList2, vList2, {5.0, 5.0}]
Out[18]= {{11., 12.25, 10.75}, {7.25, 6.25}}
Can anybody explain this behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个错误,已在开发版本中修复。现在您必须使用 {5.,5.} 版本。
This is a bug and has been fixed in the development version. For now you have to use the {5.,5.} version.