Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Possible Duplicate:
How can I get exactly 5 logarithmic divisions of an interval?

I want to use Table to generate a list of items, but want the indices to be logarithmically spaced. Is there a simple way of doing this, or will I have to explicitly run linearly spaced indices through Exp to get my list?

share|improve this question
5  
Duplicate: mathematica.stackexchange.com/q/13226/5 – rm -rf Dec 14 '12 at 14:20
Its asked in a different way though...more general for a start and it is targetted towards MATLABers. Personally i'd have left this up. – WalkingRandomly Dec 15 '12 at 9:36

marked as duplicate by acl, halirutan, Szabolcs, Artes, Mr.Wizard Dec 15 '12 at 0:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 15 down vote accepted

Here's a somewhat simpler way:

 logspace[a_, b_, n_] := 10.0^Range[a, b, (b - a)/(n - 1)]

This gives a sequence starting at 10^a, ending at 10^b, with n points logarithmically spaced, as does Matlab's logspace( ) function.

share|improve this answer

I did a direct translation from Matlab file logspace.m with additional Mathematica minor touches.

Mathematica Function

logspace[d1_?(Element[#, Reals] &), 
         dd2_?(Element[#, Reals] &),
         n_?(IntegerQ[#] && # > 0 &)] := Module[{d2 = dd2, i},  

         If[d2 == Pi, d2 = Log[10, d2]];
         Flatten@{Table[10^(d1 + i*(d2 - d1)/(n - 1)), {i, 0, n - 2}], 10^d2}
     ]

logspace[d1_?(Element[#, Reals] &), d2_?(Element[#, Reals] &)]:= logspace[d1, d2, 50];

Tests

In[3]:= logspace[.1, 3, 3]    
Out[3]= {1.25893, 35.4813, 1000}

In[4]:= logspace[.1, 6, 3]    
Out[4]= {1.25893, 1122.02, 1000000}

In[5]:= logspace[.1, 6, 10]    
Out[5]= {1.25893, 5.69581, 25.7698, 116.591, 527.5, 2386.59, 10797.8, \
48852.7, 221027., 1000000}

In[6]:= logspace[.1, .4, 10]    
Out[6]= {1.25893, 1.35936, 1.4678, 1.58489, 1.71133, 1.84785, \
1.99526, 2.15443, 2.32631, 2.51189}

Matlab:

EDU>> logspace(.1,3,3)
       1.2589       35.481         1000

EDU>> logspace(.1,6,3)
       1.2589         1122        1e+06

EDU>> logspace(.1,6,10)'
       1.2589
       5.6958
        25.77
       116.59
        527.5
       2386.6
        10798
        48853
   2.2103e+05
        1e+06

EDU>> logspace(.1,.4,10)'
       1.2589
       1.3594
       1.4678
       1.5849
       1.7113
       1.8478
       1.9953
       2.1544
       2.3263
       2.5119
share|improve this answer
For d1 and d2 why not just define as d1_Real and n_Integer/;n>0 – Mike Honeychurch Dec 14 '12 at 20:37
@MikeHoneychurch, I wanted to allow logspace(1, 10, 50) and also logspace(1.0,10.0,50). However, Head[1] is Integer and not Real, while Elements[1,Reals] is True. If I used d1_Real, then this would mean the first call would have failed. – Nasser Dec 14 '12 at 20:50
For the n_Integer/;n>0 , yes, I could have written it that way. As always, in Mathematica there are many ways to do the same thing. I just copied one from my current code I have open at the time to use. – Nasser Dec 14 '12 at 20:53
ok fair enough. I'd normally write that as d1 : (_Real | _Integer) but then it isn't really more concise than yours. Just personal preference :) ...although in some cases, not this function, I think just defining the pattern head would be better than using a pattern test. – Mike Honeychurch Dec 14 '12 at 20:55
@MikeHoneychurch, I had these tests written long time ago, and been using them. I do need to sit down one day and find if I can re-write them more efficient, may be just checking on the Head directly as you show. I know this was discussed here before in details and will use that as reference. I know there are subtle issues to watch for. I just never got around to it, and just keep using the same tests I have collected. – Nasser Dec 14 '12 at 21:03
show 4 more comments

I don't belive there is a build in function for this, however you can easily do it using Range

 fSpace[min_, max_, steps_, f_: Log] :=  
      InverseFunction[f] /@ Range[f@min, f@max, (f@max - f@min)/(steps - 1)]

Inverse functions are being used so it'll give warnings in cases where you should be cautius, however it works for Log and other invertible functions.

 fSpace[1, 1000, 4]

{1, 10, 100, 1000}

{fSpace[1, 1000, 30, Sqrt[#] &], fSpace[1, 1000, 30]} // ListPlot

Image showing both logspacing and Sqrt spacing

Update

I just discovered that you can in fact do even better out of the box by inverting the function only on the input range:

 fSpace[min_, max_, steps_, f_: Log] := 
  InverseFunction[ConditionalExpression[f[#], min < # < max] &] /@ 
  Range[f@min, f@max, (f@max - f@min)/(steps - 1)]

This still doesn't work arbitrarily, however it does help for instance selecting the positive square root in #^2&.

share|improve this answer
Thanks, this is just what I wanted! – Will Vousden Dec 14 '12 at 14:15

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