Efficient alternative to Outer on sparse arrays in Mathematica?

前端 未结 3 1219
挽巷
挽巷 2021-02-08 23:45

Suppose I have two very large lists {a1, a2, …} and {b1, b2, …} where all ai and bj are large sparse arrays. For the sake of memory efficiency I store each list as one comprehen

3条回答
  •  长发绾君心
    2021-02-09 00:39

    Using your example list data, I believe that you will find the ability to Append to a SparseArray quite helpful.

    acc = SparseArray[{}, {1, 2, 2, 2, 2, 2, 2}]
    
    Do[AppendTo[acc, i.j], {i, list}, {j, list}]
    
    Rest[acc]
    

    I need Rest to drop the first zero-filled tensor in the result. The second argument of the seed SparseArray must be the dimensions of each of your elements with a prefixed 1. You may need to explicitly specify a background for the seed SparseArray to optimize performance.

提交回复
热议问题