I am fairly new to MMA-12 and have written the following code to calculate possible coordinates (for given constraints [see If[]] in the code) and get the corresponding values as output.

vol[t_, w_] := 
 Module[{ntrans = t, nrot = w, trans, rot, Data0, Data1, Data2, Data3,
    Data4, Data5, Data6, dl1, dl2, dl3, dl4, dl5, dl6},
  trans = {};  rot = {};
  Data0 = {};
  Data1 = {}; Data2 = {}; Data3 = {}; Data4 = {}; Data5 = {};Data6 = {};
  dl1 = {}; dl2 = {}; dl3 = {}; dl4 = {}; dl5 = {}; dl6 = {};

  Do[trans = tkwert[[j, All]];

   Do[rot = wwert[[i, All]];

    Data0 = {Flatten[{trans, rot}]};
    Data1 = {Flatten[{Data0, lk1}]};
    dl1 = Flatten[Map[rks3d, Data1]] - lr1;
    Data2 = {Flatten[{Data0, lk2}]};
    dl2 = Flatten[Map[rks3d, Data2]] - lr2;
    Data3 = {Flatten[{Data0, lk3}]};
    dl3 = Flatten[Map[rks3d, Data3]] - lr3; 
    Data4 = {Flatten[{Data0, lk4}]};
    dl4 = Flatten[Map[rks3d, Data4]] - lr4; 
    Data5 = {Flatten[{Data0, lk5}]};
    dl5 = Flatten[Map[rks3d, Data5]] - lr5; 
    Data6 = {Flatten[{Data0, lk6}]};
    dl6 = Flatten[Map[rks3d, Data6]] - lr6;

    If[Between[Norm[dl1], {lmin, lmax}] && 
      Between[Norm[dl2], {lmin, lmax}] && 
      Between[Norm[dl3], {lmin, lmax}] && 
      Between[Norm[dl4], {lmin, lmax}] && 
      Between[Norm[dl5], {lmin, lmax}] && 
      Between[Norm[dl6], {lmin, lmax}], Sow[Data0], ## &[]];

    , {i, 1, nrot, 1}]
   , {j, 1, ntrans, 1}];
  ](* end *)

The equation rks3d contains 6 variables and is given as follows:

rks3d[{tx_, ty_, tz_, alpha_, beta_, gamma_, px_, py_, pz_}]

Things I know:

  • it is not required to declare the variables in the Module-head following ntrans and nrot, as well as defining them.
  • the computation of the given code takes fairly long for bigger values of ntrans and nrot.
  • I most likely have to further improve my knowledge of MMA.

Things I suspect:

  • there possibly is a better way to implement the 6-way && in If[].
  • my code is probably sh**, because it takes longer to evaluate when using Compile[].
  • the Do[ Do[] ] could be done in another and/or better way.

Things I would like to know:

  • is there any better way to implement the conditional If[]?
  • is there any better way to implement Reap[]/ Sow[] (values=Reap[vol[t,w]]).
  • is there any optimization regarding big inputs for ntrans and nrot (values > 200k ).
  • general optimization of the code as a whole.

I want to thank everyone in advance who takes his time to think about and/or answer my question.

If my question or formatting needs improvement, I will be happy to improve it.


  • $\begingroup$ This is missing the full code needed to run it so it's hard to say anything definitive. One thing of note is that appending via the Flatten[{list,newelement}] usage is going to be O(n) for length n lists. And it is not really needed. Since the computations center on norms of these growing lists, they can be updated element by element. $\endgroup$ – Daniel Lichtblau Jan 28 '20 at 15:46
  • $\begingroup$ I did not include the full code because I think that it is rather long. But i will be happy to supply a link to the notebook. $\endgroup$ – The_German Jan 28 '20 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.