Multiply vectors in a matrix by a matrix
You can actually use Dot
directly. It works in unintuitive ways with tensors.
n = 1000;
mat1 = Table[Table[RandomReal[1], {2}], {n}, {n}];
matRot = Map[RotationMatrix[3 Degree].# &, mat1, {2}]; // AbsoluteTiming
{79.8211, Null}
matRot2 = Map[RotationTransform[3. Degree], mat1, {2}]; // AbsoluteTiming
{9.04459, Null}
matRot3 = mat1.RotationMatrix[3 Degree]\[Transpose]; // AbsoluteTiming
{0.072949, Null}
matRot == matRot2 == matRot3
True
EDIT: While this is not what you asked, might I suggest you build mat1
directly with RandomReal
? Bottlenecks are probably elsewhere, but anyways.
SeedRandom[16];
mat1 = Table[Table[RandomReal[1], {2}], {n}, {n}]; // AbsoluteTiming
{0.638727, Null}
SeedRandom[16];
mat = RandomReal[1, {n, n, 2}]; // AbsoluteTiming
{0.015406, Null}
mat == mat1
True
n = 1000;
mat1 = Table[Table[RandomReal[1], {2}], {n}, {n}];
matRot = Map[RotationMatrix[3 Degree].# &, mat1, {2}]; // AbsoluteTiming
{110.570004, Null}
matRot2 = Map[RotationTransform[3. Degree], mat1, {2}]; // AbsoluteTiming
{13.946951, Null}
matRot == matRot2
True