Re: Can I speed up a matrix * tensor calculation?

*To*: mathgroup at smc.vnet.net*Subject*: [mg111364] Re: Can I speed up a matrix * tensor calculation?*From*: David Bailey <dave at removedbailey.co.uk>*Date*: Thu, 29 Jul 2010 06:42:25 -0400 (EDT)*References*: <i2m4je$o1h$1@smc.vnet.net>

On 27/07/10 09:15, Jagra wrote: > I have a tensor and a matrix. Both pretty large. Both have real > numbers with machine precision: > > Dimensions[matrix] > {10000, 5000} > > Dimensions[tensor] > {5000, 7, 10000} > > I need to do the following: > > Total[matrix]* Transpose[tensor {2, 3, 1}]]; > > but this runs very slowly. I've thought I needed to transpose the > tensor to multiply it properly. > The result should give me a vector of 5000 elements. > > Any suggestions that can speed this up? > > Thanks. > > J > The square brackets are not matched in your expression, so it is hard to know exactly what calculation you need to do. Also, is the '*' meant to be a matrix multiplication, and there seems to be a camma missing inside the Transpose operation! It would be best if you posted some slow but working code, with some idea of the speedup you are hoping to achieve. Nevertheless, there are a few general points to make here: 1) If your tensors are sparse, you may be better off to use SparseArray objects. 2) Check if your arrays are stored as packed arrays, testing with Developer`PackedArrayQ and converting if necessary. Note that real arrays containing the odd integer (such as 0 !) will not pack, and you may want to apply N to such an array prior to packing with Developer`ToPackedArray. The difference between working with packed and unpacked arrays of this size will be substantial. David Bailey http://www.dbaileyconsultancy.co.uk