Efficient alternative to Outer on sparse arrays in Mathematica?

前端 未结 3 1217
挽巷
挽巷 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:25

    If lst1 and lst2 are your lists,

    Reap[
       Do[Sow[f[#1[[i]], #2[[j]]]],
           {i, 1, Length@#1},
           {j, 1, Length@#2}
           ] &[lst1, lst2];
       ] // Last // Last
    

    does the job and may be more memory-efficient. On the other hand, maybe not. Nasser is right, an explicit example would be useful.

    EDIT: Using Nasser's randomly-generated arrays, and for len=200, MaxMemoryUsed[] indicates that this form needs 170MB while the Outer form in the question takes 435MB.

提交回复
热议问题