请在 Mathematica 中解释这个创建热/强度图的代码 [英] Please explain this code in Mathematica that creates a heat / intensity map

查看:28
本文介绍了请在 Mathematica 中解释这个创建热/强度图的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Graphics@Flatten[Table[(*颜色,别介意*){ColorData["CMYKColors"][(a[[r, t]] - .000007)/(.0003 - 0.000007)],(*点大小,别介意*)点大小[1/Sqrt[r]/10],(*您点a"的坐标是您的数据矩阵*)观点[{(rr =Log[.025 + (.58 - .25)/64 r]) Cos@(tt = t 5 度),rr罪@tt}]} &@(*迭代的值*), {r, 7, 64}, {t, 1, 72}], 1](*轮换,别介意*)/.gg:图形[___]:>旋转 [gg, Pi/2]

解决方案

好的,我会说的.首先,Mathematica 允许通过几种形式之一应用函数:标准形式 -f[x],前缀形式——f@x,后缀形式——f//x,中缀形式——x~f~y.贝利撒留的代码同时使用标准和前缀形式.

那么,我们先来看看最外层的函数:Graphics@x/.gg:图形[___]:>Rotate[gg,Pi/2],其中 xFlatten 中的所有内容.本质上,它的作用是从 x 并使用 命名模式 (gg : Graphics[___]) 将生成的 Graphics 对象旋转90度.

现在,要创建一个 Graphics 对象,我们需要提供一堆基元,这是一个嵌套列表的形式,其中每个子列表描述一些元素.这是通过 Table 完成的 命令的形式为:Table[ expr, iterators ].迭代器可以有多种形式,但在这里它们都具有 {var, min, max} 形式,并且由于它们缺少第 4 项,因此它们采用 min 之间的每个值和 max 以整数步长.因此,我们的迭代器是 {r, 7, 64}{t, 1, 72},并且 expr 会针对每个他们承担.因为,我们有两个迭代器,这会产生一个矩阵,这会混淆 Graphics,所以我们使用 Flatten[ Table[ ... ], 1] 我们取每个元素矩阵并将其放入一个简单的列表中.

Table 产生的每个元素很简单:颜色(ColorData)、点大小(PointSize)和点位置().因此,使用 Flatten,我们创建了以下内容:

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

颜色生成是从数据中提取的,它假定数据已放入名为a 的列表中.a 的各个元素通过 Part 结构访问:[[]].从表面上看,ColorData 结构有点奇怪,但可以理解为 ColorData["CMYKColors"] 返回一个 ColorDataFunction 当值在 0 之间时产生 CMYK 颜色值并提供 1 个.这就是为什么来自 a 的数据按原样缩放的原因.

点大小是从径向坐标生成的.您希望 1/Sqrt[r] 点大小应该随着 r 的增加而变小,但 Log 会反转比例.

同样,点位置是由径向和角度(t)变量产生的,但Point只在{x,y}中接受它们代码> 形式,所以他需要转换它们.在从 {r,t}{x,y} 的转换中出现了两个奇怪的结构:rrttSet (=) 在计算 x 时允许在计算 y 时使用它们.此外,术语 t 5 Degree 让 Mathematica 知道角度以度为单位,而不是弧度.此外,正如所写的那样,有一个错误:紧跟在结束的 } 之后,术语 &@ 不应该存在.>

这有帮助吗?

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 : 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?

这篇关于请在 Mathematica 中解释这个创建热/强度图的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆