; ; the idea is that you copy and paste these into your idl session ; to see what happens. ; You can do all at once (but I do not recommend it) by doing ; IDL> @maths ; (after you've copied maths.pro to your directory) ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; + floating point and integer arithmetic + ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; ; floating point (note the "," to separate commands from values and keywords) x = 2. y = 3. print,' x = ',x,' y = ',y,' x/y=', x/y ; idl does not does not require explicit variable ; declaration (unlike fortran or c). x=2. will assume ; that x is a floating point variable (single precision), since 2. is ; (note the dot) ; integer x = 2 y = 3 print,' x = ',x,' y = ',y,' x/y=', x/y ; ; Unexpected? Since 2 and 3 are integers, so are x and y, hence 2/3 -> 0. ; Needles to say, this is the root of many an error! ; Try using the index to find which variable types exist, for example ; do ?data types ; you can also use strings string1 = 'Hello ' string2 = 'World!' print,string1+string2 ; Functions ; Most standard arithmetic functions exist, for example print,' sqrt(-1) = ',sqrt(complex(-1,0)) print,' atan(-1)/Pi = ',atan(-1)/!PI ; short integers: another source of errors. ; try this i = 1 print,' Integers: i = ',i,' i+1 = ',i+1 ; Good! But now try this: i = 32767 print,' Integers: i = ',i,' i+1 = ',i+1 ; BAD! By default, integers in idl are short, i.e. they ; have values ranging between -32768:32767 ; The following gives you what you intended (note the ell in 32767l) i =32767l print,' Long integers: i = ',i,' i+1 = ',i+1 ; I most often make an error involving sort integers when using i as ; the index in a loop, or, when addressing an element in a vector. ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; + Vectors and matrices + ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; A great advange of idl is that all functions work on vectors/arrays ; as well. ; ; Notice the [] brackets for defing a vector,and the () for a function x = float([1,2,3,4,5]) print,' vector x = ',x print,' x^2 = ',x^2 print,' 1/x = ',1./x ; another commonly made mistake: print,' x[1] = ',x[1] ; idl indexes like C: zero based. Thus print,' x[0] = ',x[0] ; This difference with Fortran is an important source of error when ; tanslating idl programmes to F90 ; some more nice stuff print,' min(x) = ',min(x) print,' max(x) = ',max(x) print,' sum of all x = ',total(x) print,' reverse(x) = ',reverse(x) print,' shift x by one ',shift(x,-1) print,' statistics of x : ',moment(x) ; use ?moment to find which moments are computed ; you can enquire about a variable using help: help,x ; y = replicate(2.,n_elements(x)) print,' x = ',x print,' y = ',y print,' y^x = ',y^x ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; + operations on vectors + ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; generate nran random numbers with a given seed. ; (uniformly spaced in ]0,1[ ) nran = 5 myseed = - 1234 x = randomu(myseed,nran) ; setting the seed guarantees you get the same sequence everytime ; randomu(seed,nran) (without declaring seed) gives a different ; sequence everytime. print,nran,' random numbers ' print,' x = ',x index = sort(x) xs = x[index] print,' sorting x : ',xs print,' where is x > 0.5: ',where(x gt 0.5) print,' and its values are :',x[where(x gt 0.5)] ; the "where" statement is *very* powerful, I use it *very* often ; Note that you find the indices also this way index_where_x_gt_onehalf = where(x gt 0.5) print,' x = ',x print,' x is gt 1/2 at indices :',where(x gt 0.5) ; what happens if no indices qualify? index = where(x gt 1) print,' index where x > 1 = ',index ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; + simple loops + ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; idl has ; -- do loops for i=0,2 do print,' i = ',i ; -- while loops i = 0 while (i lt 10) do begin print,' i = ',i & i = i + 1 ; -- if/else/endif ; -- case (see the index and try them) ; -- repeat until ; -- many others