0
$\begingroup$

I have a function of the type:

f[a,b,c,d,e,f,g,h,i]

I would like to evaluate the function changing the values of the arguments automatically. For instance varying a from 0 to 1 with a given step, keeping the other arguments constant, and varying after also all the other arguments with different intervals and steps. For instance: {a,b,c,d,e,f} with intervals 0 to 1 with step 0.1; and {g,h,i} with intervals {0 to Pi/4}, with step Pi/16. I'm sure there is an efficient way to do this, without having to write an immense table.

UPDATE

My final goal is to random sample my set of arguments. As BlacKow pointed out, I would have 84375 points to evaluate. Since I have quite a heavy function, this would take way too much time. So I would rather attempt to random sample it. Following BlacKows definition, I would have something like,

RandomSample[points, 300];

Anyhow, I think both answers were very useful.

$\endgroup$
  • 1
    $\begingroup$ "Immense table"? I'm not sure why that would be bad. The other alternative would be to generate the ranges of values and MapThread your function over it, but it still seems to me that a Table with multiple iterators would be the most natural solution. $\endgroup$ – MarcoB May 11 '16 at 15:23
  • $\begingroup$ Yes, I already thought of table. Yet having there to be variables having the same interval and the same step, I thought it might have been possible to change such variables in a more tidy way. $\endgroup$ – Mirko Aveta May 11 '16 at 15:24
  • $\begingroup$ Would you please include appropriate Table code in your question that does what you want, so people know exactly what you want to accomplish and have a point of comparison for their improved solutions? $\endgroup$ – MarcoB May 11 '16 at 15:26
  • $\begingroup$ Sounds like a good use case for Manipulate. $\endgroup$ – chuy May 11 '16 at 16:11
1
$\begingroup$

Calculate argument vectors:

points = Partition[#, 8] &@
   Flatten@Tuples[{Tuples[Range[0, 1, 0.25], 5], 
      Tuples[Range[0, Pi/4, Pi/8], 3]}];

Dimensions[points]
(*{84375, 8} equals to 5^5*3^3 *)

and then f@@@points

Update If you need a random sample in the 8-dimensional space, you need following

samplPoints[n_] := # {1, 1, 1, 1, 1, Pi/4, Pi/4, Pi/4} & /@ 
   RandomReal[{0, 1}, {n, 8}];
samplPoints[10]

It will generate 10 points in 8D.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Your solution probably best fits my problem. Indeed my next step would be to use a RandomSample, or a Latin Hypercube. $\endgroup$ – Mirko Aveta May 11 '16 at 21:08
  • 1
    $\begingroup$ @MirkoAveta it's not much different form @kglr. Also you have to check which one is faster for your application. I just though applying Tuples to `Tuples would be cool. $\endgroup$ – BlacKow May 11 '16 at 21:14
  • $\begingroup$ Yes they both return the same result. Fair enough! I was just wondering which is best for random sampling. For my very heavy function, 84375 points is impossible! (time is time...) $\endgroup$ – Mirko Aveta May 11 '16 at 21:16
  • $\begingroup$ @MirkoAveta Now I'm confused. You don't want to apply you function to all of these points? You just want some sort of Monte-Carlo sample of your 8-dimensional space? Then, the code will be different... $\endgroup$ – BlacKow May 11 '16 at 21:27
  • 1
    $\begingroup$ @MirkoAveta if you use my updated code, the Pi related variables are always in the end. $\endgroup$ – BlacKow May 12 '16 at 12:52
1
$\begingroup$

smaller version of the problem with a function with 5 arguments:

h @@@ Tuples[Join[ConstantArray[Range[0, 1, .2], 3],
                  ConstantArray[Range[0, Pi/4, Pi/16], 2]]]

or

Tuples[h @@ Join[ConstantArray[Range[0, 1, .2], 3],
                ConstantArray[Range[0, Pi/4, Pi/16], 2]]]

Mathematica graphics

| improve this answer | |
$\endgroup$
  • $\begingroup$ kglr may I ask you why there are three at signs? $\endgroup$ – Mirko Aveta May 11 '16 at 20:54
  • 1
    $\begingroup$ @Mirko, regarding @@@ and @@ see the docs: Apply $\endgroup$ – kglr May 11 '16 at 21:17

Your Answer

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

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