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?