开发者

Please explain this code in Mathematica that creates a heat / intensity map

开发者 https://www.devze.com 2023-02-14 05:00 出处:网络
Graphics@Flatten[Table[ (*colors, dont mind*) {ColorData[\"CMYKColors\"][(a[[r, t]] - .000007)/(.0003 - 0.000007)],
Graphics@Flatten[Table[

(*colors, dont mind*)
{ColorData["CMYKColors"][(a[[r, t]] - .000007)/(.0003 - 0.000007)], 

(*point size, dont mind*)
PointSize[1/Sqrt[r]/10], 

(*Coordinates for your points "a" is your data matrix *)
   Point[
        {(rr =Log[.025 + (.58 - .25)/64 r]) Cos@(tt = t 5 Degree), 
          rr Sin@tt}]
        } &@

 (*values for the iteration*)
 , {r, 7, 64}, {t, 1, 72}], 1] 

 (*Rotation, dont mind*)
 /. gg 开发者_C百科: Graphics[___] :> Rotate[gg, Pi/2]  


Okay, I'll bite. First, Mathematica allows functions to be applied via one of several forms: standard form - f[x], prefix form - f @ x, postfix form - f // x, and infix form - x ~ f ~ y. Belisarius's code uses both standard and prefix form.

So, let's look at the outermost functions first: Graphics @ x /. gg : Graphics[___]:> Rotate[gg,Pi/2], where x is everything inside of Flatten. Essentially, what this does is create a Graphics object from x and using a named pattern (gg : Graphics[___]) rotates the resulting Graphics object by 90 degrees.

Now, to create a Graphics object, we need to supply a bunch of primitives and this is in the form of a nested list, where each sublist describes some element. This is done via the Table command which has the form: Table[ expr, iterators ]. Iterators can have several forms, but here they both have the form {var, min, max}, and since they lack a 4th term, they take on each value between min and max in integer steps. So, our iterators are {r, 7, 64} and {t, 1, 72}, and expr is evaluated for each value that they take on. Since, we have two iterators this produces a matrix, which would confuse Graphics, so we using Flatten[ Table[ ... ], 1] we take every element of the matrix and put it into a simple list.

Each element that Table produces is simply: color (ColorData), point size (PointSize), and point location (Point). So, with Flatten, we have created the following:

Graphics[{{color, point size, point}, {color, point size, point}, ... }]

The color generation is taken from the data, and it assumes that the data has been put into a list called a. The individual elements of a are accessed through the Part construct: [[]]. On the surface, the ColorData construct is a little odd, but it can be read as ColorData["CMYKColors"] returns a ColorDataFunction that produces a CMYK color value when a value between 0 and 1 is supplied. That is why the data from a is scaled the way it is.

The point size is generated from the radial coordinate. You'd expect with 1/Sqrt[r] the point size should be getting smaller as r increases, but the Log inverts the scale.

Similarly, the point location is produced from the radial and angular (t) variables, but Point only accepts them in {x,y} form, so he needed to convert them. Two odd constructs occur in the transformation from {r,t} to {x,y}: both rr and tt are Set (=) while calculating x allowing them to be used when calculating y. Also, the term t 5 Degree lets Mathematica know that the angle is in degrees, not radians. Additionally, as written, there is a bug: immediately following the closing }, the terms & and @ should not be there.

Does that help?

0

精彩评论

暂无评论...
验证码 换一张
取 消