Skip to main content
added 493 characters in body
Source Link
Simon Woods
  • 85.4k
  • 8
  • 180
  • 326

How about this

furious[a_, b_] := Module[{a1, a2, a3, b1, b2, b3, c},
  {a1, a2, a3} = Transpose[a, {2, 3, 4, 1}];
  {b1, b2, b3} = Transpose[b, {2, 3, 4, 1}];
  c = {-a3 b2 + a2 b3, a3 b1 - a1 b3, -a2 b1 + a1 b2};
  Transpose[c, {4, 1, 2, 3}]]

Timing results (from march's answer) for version 10.4.1

list1 = RandomReal[{-1, 1}, {32, 32, 32, 3}];
list2 = RandomReal[{-1, 1}, {32, 32, 32, 3}];

l1 = listCrossMarch1[list1, list2]; // RepeatedTiming // First
l2 = listCrossMarch2[list1, list2]; // RepeatedTiming // First
l3 = listCross[list1, list2]; // RepeatedTiming // First
l4 = furious[list1, list2]; // RepeatedTiming // First

2.67

0.6064

0.0386

0.0015

How about this

furious[a_, b_] := Module[{a1, a2, a3, b1, b2, b3, c},
  {a1, a2, a3} = Transpose[a, {2, 3, 4, 1}];
  {b1, b2, b3} = Transpose[b, {2, 3, 4, 1}];
  c = {-a3 b2 + a2 b3, a3 b1 - a1 b3, -a2 b1 + a1 b2};
  Transpose[c, {4, 1, 2, 3}]]

How about this

furious[a_, b_] := Module[{a1, a2, a3, b1, b2, b3, c},
  {a1, a2, a3} = Transpose[a, {2, 3, 4, 1}];
  {b1, b2, b3} = Transpose[b, {2, 3, 4, 1}];
  c = {-a3 b2 + a2 b3, a3 b1 - a1 b3, -a2 b1 + a1 b2};
  Transpose[c, {4, 1, 2, 3}]]

Timing results (from march's answer) for version 10.4.1

list1 = RandomReal[{-1, 1}, {32, 32, 32, 3}];
list2 = RandomReal[{-1, 1}, {32, 32, 32, 3}];

l1 = listCrossMarch1[list1, list2]; // RepeatedTiming // First
l2 = listCrossMarch2[list1, list2]; // RepeatedTiming // First
l3 = listCross[list1, list2]; // RepeatedTiming // First
l4 = furious[list1, list2]; // RepeatedTiming // First

2.67

0.6064

0.0386

0.0015
Source Link
Simon Woods
  • 85.4k
  • 8
  • 180
  • 326

How about this

furious[a_, b_] := Module[{a1, a2, a3, b1, b2, b3, c},
  {a1, a2, a3} = Transpose[a, {2, 3, 4, 1}];
  {b1, b2, b3} = Transpose[b, {2, 3, 4, 1}];
  c = {-a3 b2 + a2 b3, a3 b1 - a1 b3, -a2 b1 + a1 b2};
  Transpose[c, {4, 1, 2, 3}]]