Skip to main content
improved due comments
Source Link
m0nhawk
  • 3.9k
  • 1
  • 21
  • 35

You have plenty of redundant operations here:

The equivalent code is (where a is a matrix and b is a list of 3-vectors):

Abs[MatrixAMap[Norm[#]^2 &, a . VectorB]^2b];

I have:

n = 500;
a = RandomReal[1, {1500n, 1500n}];
b = RandomReal[1, 1500];{n, 3}];

oneDimArray = 
   Table[(Norm[Sum[a[[i, j]] b[[j]], {j, 1500n}]])^2, {i, 
    1500 n}]; // AbsoluteTiming
(* {246.907265618, Null} *)

Abs[aoneDimArrayMap = Map[Norm[#]^2 &, a.b]^2;b]; // AbsoluteTiming
(* {0.000943871000621823, Null} *)

oneDimArray == oneDimArrayMap
(* True *)

Thus, using Dot almost 300070000 times faster than manual loops.

You have plenty of redundant operations here:

The equivalent code is:

Abs[MatrixA . VectorB]^2

I have:

a = RandomReal[1, {1500, 1500}];
b = RandomReal[1, 1500];

Table[(Norm[Sum[a[[i, j]] b[[j]], {j, 1500}]])^2, {i, 
    1500}]; // AbsoluteTiming
(* {2.90726, Null} *)

Abs[a.b]^2; // AbsoluteTiming
(* {0.000943871, Null} *)

Thus, using Dot almost 3000 times faster than manual loops.

You have plenty of redundant operations here:

The equivalent code is (where a is a matrix and b is a list of 3-vectors):

Map[Norm[#]^2 &, a . b];

I have:

n = 500;
a = RandomReal[1, {n, n}];
b = RandomReal[1, {n, 3}];

oneDimArray = 
   Table[(Norm[Sum[a[[i, j]] b[[j]], {j, n}]])^2, {i, 
     n}]; // AbsoluteTiming
(* {46.5618, Null} *)

oneDimArrayMap = Map[Norm[#]^2 &, a.b]; // AbsoluteTiming
(* {0.000621823, Null} *)

oneDimArray == oneDimArrayMap
(* True *)

Thus, using Dot almost 70000 times faster than manual loops.

added 407 characters in body
Source Link
m0nhawk
  • 3.9k
  • 1
  • 21
  • 35

You have plenty of redundant operations here:

The equivalent code is:

Abs[MatrixA . VectorB]^2

I have:

a = RandomReal[1, {1500, 1500}];
b = RandomReal[1, 1500];

Table[(Norm[Sum[a[[i, j]] b[[j]], {j, 1500}]])^2, {i, 
    1500}]; // AbsoluteTiming
(* {2.90726, Null} *)

Abs[a.b]^2; // AbsoluteTiming
(* {0.000943871, Null} *)

Thus, using Dot almost 3000 times faster than manual loops.

You have plenty of redundant operations here:

The equivalent code is:

Abs[MatrixA . VectorB]^2

You have plenty of redundant operations here:

The equivalent code is:

Abs[MatrixA . VectorB]^2

I have:

a = RandomReal[1, {1500, 1500}];
b = RandomReal[1, 1500];

Table[(Norm[Sum[a[[i, j]] b[[j]], {j, 1500}]])^2, {i, 
    1500}]; // AbsoluteTiming
(* {2.90726, Null} *)

Abs[a.b]^2; // AbsoluteTiming
(* {0.000943871, Null} *)

Thus, using Dot almost 3000 times faster than manual loops.

deleted 174 characters in body
Source Link
m0nhawk
  • 3.9k
  • 1
  • 21
  • 35

You have plenty of redundant operations here:

The equivalent code is (taking in account that Norm squared is just Abs by default):

Abs[MatrixA . VectorB]^2

You have plenty of redundant operations here:

The equivalent code is (taking in account that Norm squared is just Abs by default):

Abs[MatrixA . VectorB]^2

You have plenty of redundant operations here:

The equivalent code is:

Abs[MatrixA . VectorB]^2
added 2 characters in body
Source Link
march
  • 24.2k
  • 2
  • 46
  • 102
Loading
Source Link
m0nhawk
  • 3.9k
  • 1
  • 21
  • 35
Loading