Using a compiled enumerated function in non-tensor lists

I am interested in using the Listable Compiled function in lists, which do not have to be tensors. I want to understand why some functions work, where, like others, they do not and close 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 enumerated functions func1 and func2

 func1 = Compile[{{m, _Real, 2}, {v, _Real, 1}}, mv, RuntimeAttributes -> Listable ]; func2 = Compile[{{m, _Real, 2}, {v, _Real, 1}, {r, _Real}}, r*(mv), RuntimeAttributes -> Listable ]; 

func1 works with both tensor and non-tensor lists, as shown 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 tensor lists mList1 and vList1 and a 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 able to reuse one real 5.0.

However, the same function does not work on non-tensor lists mList2 and vList2. The following disables my kernel (Mathematica 8.0.4, on Windows Vista).

 func2[mList2, vList2, 5.0] 

Interestingly, the following work.

 In[18]:= func2[mList2, vList2, {5.0, 5.0}] Out[18]= {{11., 12.25, 10.75}, {7.25, 6.25}} 

Can anyone explain this behavior?

+6
source share
1 answer

This is a bug and fixed in the development version. For now, you should use version {5., 5.}.

+3
source

Source: https://habr.com/ru/post/902328/


All Articles