0
$\begingroup$

I need to make this code faster and consume less memory.

 makeBZ001[x_] := Module[{dat = x, rt1, rt2, rt3, temp1, temp2, temp3,rm, tempfull,full, bool1, res}, 
 rt1 = ReflectionTransform[{1, -1}]; temp1 = dat;
 temp1[[All, 1 ;; 2]] = rt1[temp1[[All, 1 ;; 2]]]; 
 rt2 = ReflectionTransform[{1, 0}]; 
 temp2 = DeleteDuplicates[Join[dat, temp1]]; 
 temp2[[All, 1 ;; 2]] = rt2[temp2[[All, 1 ;; 2]]]; 
 rt3 = ReflectionTransform[{0, -1}]; 
 temp3 = DeleteDuplicates[Join[dat, temp1, temp2]]; 
 temp3[[All, 1 ;; 2]] = rt3[temp3[[All, 1 ;; 2]]]; 
 tempfull = DeleteDuplicates[Join[dat, temp1, temp2, temp3]]; 
 bool1 = RegionMember[Polygon[{{0., 1.}, {-1., 0.}, {0., -1.},{1.,0.}}]]/@ tempfull[[All, 1 ;; 2]]; 
 res = Pick[tempfull, bool1];
 res
 ]

 n = 10^5;
 data1 = ConstantArray[RandomInteger[{-2, 2}], {n, 2}];
 data2 = ConstantArray[RandomInteger[{0, 1}], {n, 21}];
 array = {};
 If[Dimensions[data1][[1]] == Dimensions[data2][[1]], 
 Do[temp = ArrayFlatten[{{data1, Transpose[{data2[[All, j]]}]}}]; 
 temp2 = makeBZ001[temp]; 
 pick = Boole[(NumberQ[#1] &) /@ temp2[[All, -1]]]; 
 temp2[[All, -1]] = temp2[[All, -1]] pick; 
 AppendTo[array, temp2], {j, 2., 21.}];, 
 Print["Dimensions of data1 and data2 are incompatible."]; Abort[];];
 link = Association[];
 m = 1;
 Do[link[ToString[n]] = m; 
 m = m + 1;
 , {n, {j11, j21, j12, j22, L21, L12, R21, R12, l021, l012, r021, r012, jl11, jl21, jl12, jl22, jr11, jr21, jr12, jr22}}
 ]

The main idea is to create 20 {~4n,3} lists and reference them by name, such that array[link[ToString[j11]]] is just array[[1]]. I had to add a part, pick variable, to check for numbers like 1.34343-256 which create errors in post processing. I'm looking for any exploit or shortcut to make this faster. Thanks in advance.

Edit Just realized the sample data wont work. Here is actual data you can use to test the code.

data1

data2

$\endgroup$

2 Answers 2

4
$\begingroup$

Another speed up is to use the "listable" version of RegionMemberFunction instead of using Map. For example:

rmf = RegionMember[Polygon[{{0.,1.},{-1.,0.},{0.,-1.},{1.,0.}}]];

pts = RandomReal[{-1,2}, {10^5,2}];

r1 = rmf /@ pts; //AbsoluteTiming
r2 = rmf[pts]; //AbsoluteTiming

r1===r2

{0.078793, Null}

{0.005577, Null}

True

$\endgroup$
3
$\begingroup$

To start with something: Linear algebra is much faster than this fancy ReflectionTransform:

x = RandomReal[{-1, 1}, {1000000, 2}];
y1 = ReflectionTransform[{1, 0}][x]; // AbsoluteTiming
y2 = x.Transpose[ReflectionMatrix[{1, 0}]]; // AbsoluteTiming
y1 == y2

x = RandomReal[{-1, 1}, {1000000, 2}];
y1 = ReflectionTransform[{1, 0}][x]; // RepeatedTiming // First
y2 = x.Transpose[ReflectionMatrix[{1, 0}]]; // RepeatedTiming // First
y1 == y2

0.135

0.0045

True

$\endgroup$
1
  • 1
    $\begingroup$ Dot products are very nice in Mathematica, so one should always try to use them. $\endgroup$ Commented Sep 24, 2018 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.