目录
回复内容:
首页 后端开发 Python教程 如何用数学软件画一个“圣诞树”?

如何用数学软件画一个“圣诞树”?

Jun 06, 2016 pm 04:22 PM

可以是字符的,也可以是图形的

相关问题如何用C语言画一个“圣诞树”? - 编程

回复内容:

我只是搬运codegolf.stackexchange.com上的答案,原作者是Silvia (@Silvia ),用的是Mathematica。
PD = .5;
s[t_, f_] := t^.6 - f
dt[cl_, ps_, sg_, hf_, dp_, f_, flag_] :=
    Module[{sv, basePt},
           {PointSize[ps],
            sv = s[t, f];
            Hue[cl (1 + Sin[.02 t])/2, 1, .3 + sg .3 Sin[hf sv]],
            basePt = {-sg s[t, f] Sin[sv], -sg s[t, f] Cos[sv], dp + sv};
            Point[basePt],
           If[flag,
              {Hue[cl (1 + Sin[.1 t])/2, 1, .6 + sg .4 Sin[hf sv]], PointSize[RandomReal[.01]],
               Point[basePt + 1/2 RotationTransform[20 sv, {-Cos[sv], Sin[sv], 0}][{Sin[sv], Cos[sv], 0}]]},
              {}]
          }]

frames = ParallelTable[
                       Graphics3D[Table[{
                                         dt[1, .01, -1, 1, 0, f, True], dt[.45, .01, 1, 1, 0, f, True],
                                         dt[1, .005, -1, 4, .2, f, False], dt[.45, .005, 1, 4, .2, f, False]},
                                        {t, 0, 200, PD}],
                                  ViewPoint -> Left, BoxRatios -> {1, 1, 1.3}, 
                                  ViewVertical -> {0, 0, -1},
                                  ViewCenter -> {{0.5, 0.5, 0.5}, {0.5, 0.55}}, Boxed -> False,
                                  PlotRange -> {{-20, 20}, {-20, 20}, {0, 20}}, Background -> Black],
                       {f, 0, 1, .01}];

Export["tree.gif", frames]
登录后复制
Mathematica版本:
打开一个notebook,然后长按CTRL+/,效果如下:
如何用数学软件画一个“圣诞树”?像不像一棵圣诞树呢? 这里有用SAS画的圣诞树,翻译成了Mathematica版的
如何用数学软件画一个“圣诞树”?
Clear["`*"];
ifs[prob_,A_,init_,max_]:=FoldList[#2.{#[[1]],#[[2]],1}&,init,RandomChoice[prob->A,max]];

L={{{0.03,0},{0,0.1}},{{0.85,0},{0,0.85}},{{0.8,0},{0,0.8}},{{0.2,-0.08},{0.15,0.22}},{{-0.2,0.08},{0.15,0.22}},{{0.25,-0.1},{0.12,0.25}},{{-0.2,0.1},{0.12,0.2}}};
B=Map[List,{{0,0},{0,1.5},{0,1.5},{0,0.85},{0,0.85},{0,0.3},{0,0.4}},{2}];

{A,prob,init,max}={N@Join[L,B,3],{2,60,10,7,7,7,7}/100.,{0.,2.},10^5};
pts=ifs[prob,A,init,max];//AbsoluteTiming
Graphics[{{Darker@Green,PointSize@Tiny,Point@pts},{Hue@Random[],PointSize@Large,Point@#}&/@RandomChoice[pts,200]},AspectRatio->1.5]
登录后复制
去年用R画了圣诞树送给教定量入门的教授,改了一下Wiekvoet: Merry Christmas的代码。
( 今年直接手动涂色送happy spring的卡片了,不是程序媛搞不出酷炫的东西好桑心) Wolfram Mathematica 算数学软件吧?

如何用数学软件画一个“圣诞树”?
还有这个:

如何用数学软件画一个“圣诞树”?
算不算抖机灵 = = 鸡汁的我
如何用数学软件画一个“圣诞树”? MATLAB ,名副其实的数学软件。
如何用数学软件画一个“圣诞树”?参考 File Exchange
by Anselm Ivanovas
====================================
%
<span class="k">function</span> <span class="nf">christmas</span>
<span class="c">% Anselm Ivanovas, anselm.ivanovas@student.unisg.ch</span>
<span class="c">%Basically just a nice plot for some christmas fun.</span>
<span class="c">%3D Plot of a hhristmas tree with some presents and snow</span>
<span class="c">%% setup</span>
<span class="n">snow</span><span class="p">=</span><span class="mi">800</span><span class="p">;</span> <span class="c">% number of snow flakes [0 .. 5000]</span>

<span class="c">%% draw tree</span>
<span class="n">h</span><span class="p">=</span><span class="mi">0</span><span class="p">:</span><span class="mf">0.2</span><span class="p">:</span><span class="mi">25</span><span class="p">;</span> <span class="c">%vertical grid</span>
<span class="p">[</span><span class="n">X</span><span class="p">,</span><span class="n">Y</span><span class="p">,</span><span class="n">Z</span><span class="p">]</span> <span class="p">=</span> <span class="n">cylinder</span><span class="p">(</span><span class="n">tree</span><span class="p">(</span><span class="n">h</span><span class="p">));</span> <span class="c">%produce a tree formed cylinder</span>
<span class="n">Z</span><span class="p">=</span><span class="n">Z</span><span class="o">*</span><span class="mi">25</span><span class="p">;</span> <span class="c">%scale to the right heigth</span>
<span class="c">%add some diffusion to the surface of the tree to make it look more real</span>
<span class="n">treeDiffusion</span><span class="p">=</span><span class="nb">rand</span><span class="p">(</span><span class="mi">126</span><span class="p">,</span><span class="mi">21</span><span class="p">)</span><span class="o">-</span><span class="mf">0.5</span><span class="p">;</span><span class="c">%some horizontal diffusion data</span>
<span class="c">%add diffusion to the grid points</span>
<span class="k">for</span> <span class="n">cnt1</span><span class="p">=</span><span class="mi">1</span><span class="p">:</span><span class="mi">21</span>

<span class="k">for</span> <span class="n">cnt2</span><span class="p">=</span><span class="mi">16</span><span class="p">:</span><span class="mi">126</span><span class="c">%starting above the trunk</span>
<span class="c">%get the angle to always diffuse in direction of the radius</span>
<span class="nb">angle</span><span class="p">=</span><span class="nb">atan</span><span class="p">(</span><span class="n">Y</span><span class="p">(</span><span class="n">cnt2</span><span class="p">,</span><span class="n">cnt1</span><span class="p">)</span><span class="o">/</span><span class="n">X</span><span class="p">(</span><span class="n">cnt2</span><span class="p">,</span><span class="n">cnt1</span><span class="p">));</span>
<span class="c">%split the diffusion in the two coordinates, depending on the angle</span>
<span class="n">X</span><span class="p">(</span><span class="n">cnt2</span><span class="p">,</span><span class="n">cnt1</span><span class="p">)=</span><span class="n">X</span><span class="p">(</span><span class="n">cnt2</span><span class="p">,</span><span class="n">cnt1</span><span class="p">)</span><span class="o">+</span><span class="nb">cos</span><span class="p">(</span><span class="nb">angle</span><span class="p">)</span><span class="o">*</span><span class="n">treeDiffusion</span><span class="p">(</span><span class="n">cnt2</span><span class="p">,</span><span class="n">cnt1</span><span class="p">);</span>
<span class="n">Y</span><span class="p">(</span><span class="n">cnt2</span><span class="p">,</span><span class="n">cnt1</span><span class="p">)=</span><span class="n">Y</span><span class="p">(</span><span class="n">cnt2</span><span class="p">,</span><span class="n">cnt1</span><span class="p">)</span><span class="o">+</span><span class="nb">sin</span><span class="p">(</span><span class="nb">angle</span><span class="p">)</span><span class="o">*</span><span class="n">treeDiffusion</span><span class="p">(</span><span class="n">cnt2</span><span class="p">,</span><span class="n">cnt1</span><span class="p">);</span>
<span class="c">%some Vertical diffusion for each point</span>
<span class="n">Z</span><span class="p">(</span><span class="n">cnt2</span><span class="p">,</span><span class="n">cnt1</span><span class="p">)=</span><span class="n">Z</span><span class="p">(</span><span class="n">cnt2</span><span class="p">,</span><span class="n">cnt1</span><span class="p">)</span><span class="o">+</span><span class="p">(</span><span class="nb">rand</span><span class="o">-</span><span class="mf">0.5</span><span class="p">)</span><span class="o">*</span><span class="mf">0.5</span><span class="p">;</span>
<span class="k">end</span>

<span class="k">end</span>
<span class="c">%draw the tree</span>
<span class="n">surfl</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="n">Y</span><span class="p">,</span><span class="n">Z</span><span class="p">,</span><span class="s">'light'</span><span class="p">)</span>
<span class="c">%% View and format</span>
<span class="c">%Use as nice green color map (darker at the bottom, lighter at the top)</span>
<span class="n">r</span><span class="p">=(</span><span class="mf">0.0430</span><span class="p">:(</span><span class="mf">0.2061</span><span class="o">/</span><span class="mi">50</span><span class="p">):</span><span class="mf">0.2491</span><span class="p">)</span><span class="o">'</span><span class="p">;</span><span class="c">%red component</span>
<span class="n">g</span><span class="p">=(</span><span class="mf">0.2969</span><span class="p">:(</span><span class="mf">0.4012</span><span class="o">/</span><span class="mi">50</span><span class="p">):</span><span class="mf">0.6981</span><span class="p">)</span><span class="o">'</span><span class="p">;</span><span class="c">%green component</span>
<span class="n">b</span><span class="p">=(</span><span class="mf">0.0625</span><span class="p">:(</span><span class="mf">0.2696</span><span class="o">/</span><span class="mi">50</span><span class="p">):</span><span class="mf">0.3321</span><span class="p">)</span><span class="o">'</span><span class="p">;</span><span class="c">%blue component</span>
<span class="n">map</span><span class="p">=[</span><span class="n">r</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="n">b</span><span class="p">];</span><span class="c">%join in a map</span>
<span class="k">for</span> <span class="n">cnt</span><span class="p">=</span><span class="mi">1</span><span class="p">:</span><span class="mi">6</span>
<span class="c">%change the lower part to brown for the trunk</span>
<span class="n">map</span><span class="p">(</span><span class="n">cnt</span><span class="p">,:)=[</span><span class="mi">77</span><span class="p">,</span><span class="mi">63</span><span class="p">,</span><span class="mi">5</span><span class="p">]</span><span class="o">/</span><span class="mi">265</span><span class="p">;</span>
<span class="k">end</span>
<span class="n">colormap</span><span class="p">(</span><span class="n">map</span><span class="p">)</span><span class="c">%set the map</span>
<span class="n">view</span><span class="p">([</span><span class="o">-</span><span class="mf">37.5</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span><span class="c">%Change the view to see a little more of the Actual 3D tree</span>
<span class="n">lighting</span> <span class="n">phong</span> <span class="c">%some nice lighting</span>
<span class="n">shading</span> <span class="n">interp</span> <span class="c">%remove grid and smoothen the surface color</span>
<span class="n">axis</span> <span class="n">equal</span> <span class="c">%takes care of display in the right proportion</span>
<span class="n">axis</span><span class="p">([</span><span class="o">-</span><span class="mi">10</span> <span class="mi">10</span> <span class="o">-</span><span class="mi">10</span> <span class="mi">10</span> <span class="mi">0</span> <span class="mi">30</span><span class="p">])</span> <span class="c">%give some more axis space (for the snow later)</span>
<span class="n">axis</span> <span class="n">off</span> <span class="c">%but don't show axis</span>
<span class="n">hold</span> <span class="n">on</span> <span class="c">%to draw the rest</span>
<span class="n">title</span><span class="p">(</span><span class="s">'Merry Christmas 知乎er'</span><span class="p">)</span><span class="c">%self explaining</span>
<span class="c">%% Presents</span>
<span class="c">%Draw some presents around the tree (each with random color)</span>
<span class="n">drawPresent</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span>
<span class="n">drawPresent</span><span class="p">(</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mf">1.5</span><span class="p">);</span>
<span class="n">drawPresent</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">);</span>
<span class="n">drawPresent</span><span class="p">(</span><span class="o">-</span><span class="mi">14</span><span class="p">,</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="n">drawPresent</span><span class="p">(</span><span class="o">-</span><span class="mi">9</span><span class="p">,</span><span class="o">-</span><span class="mi">10</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span>
<span class="n">drawPresent</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">);</span>
<span class="n">drawPresent</span><span class="p">(</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span><span class="o">-</span><span class="mi">13</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">);</span>
<span class="c">%% Snow</span>
<span class="c">%create some random 3D coordinates for the snow (amount as in setup above)</span>
<span class="n">snowX</span><span class="p">=(</span><span class="nb">rand</span><span class="p">(</span><span class="n">snow</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="mi">25</span><span class="o">-</span><span class="mf">12.5</span><span class="p">);</span>
<span class="n">snowY</span><span class="p">=(</span><span class="nb">rand</span><span class="p">(</span><span class="n">snow</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="mi">25</span><span class="o">-</span><span class="mf">12.5</span><span class="p">);</span>
<span class="n">snowZ</span><span class="p">=(</span><span class="nb">rand</span><span class="p">(</span><span class="n">snow</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="mi">27</span><span class="p">);</span>
<span class="c">%Note:Some flakes will end up IN the tree but just can't be seen then</span>
<span class="n">plot3</span><span class="p">(</span><span class="n">snowX</span><span class="p">,</span><span class="n">snowY</span><span class="p">,</span><span class="n">snowZ</span><span class="p">,</span><span class="s">'w*'</span><span class="p">)</span><span class="c">%plot coordinates as white snow flakes</span>
<span class="n">hold</span> <span class="n">off</span><span class="c">%Done</span>
<span class="k">end</span> <span class="c">% of function</span>

<span class="c">%% ============= private functions</span>
<span class="k">function</span><span class="w"> </span>r<span class="p">=</span><span class="nf">tree</span><span class="p">(</span>h<span class="p">)</span><span class="c">%Gives a profile for the tree</span>
<span class="k">for</span> <span class="n">cnt</span><span class="p">=</span><span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">h</span><span class="p">)</span>

<span class="k">if</span><span class="p">(</span><span class="n">h</span><span class="p">(</span><span class="n">cnt</span><span class="p">)</span><span class="o">==</span><span class="mi">0</span><span class="p">)</span><span class="c">%no Width at the bottom. Ensures a "closed" trunk</span>
<span class="n">r</span><span class="p">(</span><span class="n">cnt</span><span class="p">)=</span><span class="mi">0</span><span class="p">;</span>
<span class="k">end</span>
<span class="c">%smaller radius for the trunk</span>
<span class="k">if</span> <span class="p">(</span><span class="n">h</span><span class="p">(</span><span class="n">cnt</span><span class="p">)</span><span class="o">></span><span class="mi">0</span> <span class="o">&&</span> <span class="n">h</span><span class="p">(</span><span class="n">cnt</span><span class="p">)</span><span class="o"><</span><span class="p">=</span><span class="mi">3</span><span class="p">)</span>
<span class="n">r</span><span class="p">(</span><span class="n">cnt</span><span class="p">)=</span><span class="mf">1.5</span><span class="p">;</span>
<span class="k">end</span>
<span class="c">%reduce radius gradually from 8 to 0. Note: will only work with a trunk heigth</span>
<span class="c">%of 3 and a whole tree heigth of 25. Scale the height of the tree in</span>
<span class="c">%the "draw tree" section, since the cylinder command will return a 1</span>
<span class="c">%unit high cylinder anyway</span>
<span class="k">if</span><span class="p">(</span><span class="n">h</span><span class="p">(</span><span class="n">cnt</span><span class="p">)</span><span class="o">></span><span class="mi">3</span><span class="p">)</span>
<span class="n">r</span><span class="p">(</span><span class="n">cnt</span><span class="p">)=</span><span class="mi">8</span><span class="o">-</span><span class="p">(</span><span class="n">h</span><span class="p">(</span><span class="n">cnt</span><span class="p">)</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="mf">0.3636</span><span class="p">;</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span> <span class="c">% of function</span>
<span class="c">%Draws a present with the given coordinate + size in a random color</span>
<span class="c">%Note:Given coordinates apply to the lower front + left corner of the</span>
<span class="c">%present (the one closest to the viewer) as seen in the plot</span>
<span class="k">function</span><span class="w"> </span><span class="nf">drawPresent</span><span class="p">(</span>dx,dy,dz,scalex,scaley,scalez<span class="p">)</span><span class="w"></span>
<span class="c">%the standard present coordinates</span>
<span class="n">presentX</span><span class="p">=[</span><span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span><span class="p">;</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">;</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">;</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">;</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span><span class="p">];</span>
<span class="n">presentY</span><span class="p">=[</span><span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span><span class="p">;</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span><span class="p">;</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span><span class="p">;</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span><span class="p">;</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span><span class="p">];</span>
<span class="n">presentZ</span><span class="p">=[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">;</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">;</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span> <span class="mf">0.5</span><span class="p">;</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span><span class="p">;</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span><span class="p">];</span>
<span class="c">%draw some presents with random colors</span>
<span class="c">%scale present and move it to the right place and get the plot handle</span>
<span class="n">myHandle</span><span class="p">=</span><span class="n">surf</span><span class="p">((</span><span class="n">presentX</span><span class="o">*</span><span class="n">scalex</span><span class="o">+</span><span class="n">dx</span><span class="p">),(</span><span class="n">presentY</span><span class="o">*</span><span class="n">scaley</span><span class="o">+</span><span class="n">dy</span><span class="p">),</span> <span class="p">(</span><span class="n">presentZ</span><span class="o">*</span><span class="n">scalez</span><span class="o">+</span><span class="n">dz</span><span class="p">));</span>
<span class="c">%some random color map</span>
<span class="n">randColorMap</span><span class="p">(:,:,</span><span class="mi">1</span><span class="p">)=</span><span class="nb">repmat</span><span class="p">(</span><span class="nb">rand</span><span class="p">,[</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">]);</span><span class="c">%r component</span>
<span class="n">randColorMap</span><span class="p">(:,:,</span><span class="mi">2</span><span class="p">)=</span><span class="nb">repmat</span><span class="p">(</span><span class="nb">rand</span><span class="p">,[</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">]);</span><span class="c">%g component</span>
<span class="n">randColorMap</span><span class="p">(:,:,</span><span class="mi">3</span><span class="p">)=</span><span class="nb">repmat</span><span class="p">(</span><span class="nb">rand</span><span class="p">,[</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">]);</span><span class="c">%b component</span>
<span class="c">%Assign colormap just to the plot handle object of the present, so the tree</span>
<span class="c">%does not change color</span>
<span class="n">set</span><span class="p">(</span><span class="n">myHandle</span><span class="p">,</span><span class="s">'CData'</span><span class="p">,</span><span class="n">randColorMap</span><span class="p">)</span>
<span class="n">shading</span> <span class="n">interp</span> <span class="c">%Nice shding + without grid</span>
<span class="k">end</span> <span class="c">% of function</span>
登录后复制
Christmas Tree
R语言 能画,但是这种比较有什么意义呢..................................

如何用数学软件画一个“圣诞树”?
<span class="go">L <-  matrix(</span>
<span class="go">    c(0.03,  0,     0  ,  0.1,</span>
<span class="go">        0.85,  0.00,  0.00, 0.85,</span>
<span class="go">        0.8,   0.00,  0.00, 0.8,</span>
<span class="go">        0.2,  -0.08,  0.15, 0.22,</span>
<span class="go">        -0.2,   0.08,  0.15, 0.22,</span>
<span class="go">        0.25, -0.1,   0.12, 0.25,</span>
<span class="go">        -0.2,   0.1,   0.12, 0.2),</span>
<span class="go">    nrow=4)</span>
<span class="go"># ... and each row is a translation vector</span>
<span class="go">B <- matrix(</span>
<span class="go">    c(0, 0,</span>
<span class="go">        0, 1.5,</span>
<span class="go">        0, 1.5,</span>
<span class="go">        0, 0.85,</span>
<span class="go">        0, 0.85,</span>
<span class="go">        0, 0.3,</span>
<span class="go">        0, 0.4),</span>
<span class="go">    nrow=2)</span>

<span class="go">prob = c(0.02, 0.6,.08, 0.07, 0.07, 0.07, 0.07)</span>

<span class="go"># Iterate the discrete stochastic map </span>
<span class="go">N = 1e5 #5  #   number of iterations </span>
<span class="go">x = matrix(NA,nrow=2,ncol=N)</span>
<span class="go">x[,1] = c(0,2)   # initial point</span>
<span class="go">k <- sample(1:7,N,prob,replace=TRUE) # values 1-7 </span>

<span class="go">for (i in 2:N) </span>
<span class="go">  x[,i] = crossprod(matrix(L[,k[i]],nrow=2),x[,i-1]) + B[,k[i]] # iterate </span>

<span class="go"># Plot the iteration history </span>
<span class="go">png('card.png')</span>
<span class="go">par(bg='darkblue',mar=rep(0,4))    </span>
<span class="go">plot(x=x[1,],y=x[2,],</span>
<span class="go">    col=grep('green',colors(),value=TRUE),</span>
<span class="go">    axes=FALSE,</span>
<span class="go">    cex=.1,</span>
<span class="go">    xlab='',</span>
<span class="go">    ylab='' )#,pch='.')</span>

<span class="go">bals <- sample(N,20)</span>
<span class="go">points(x=x[1,bals],y=x[2,bals]-.1,</span>
<span class="go">    col=c('red','blue','yellow','orange'),</span>
<span class="go">    cex=2,</span>
<span class="go">    pch=19</span>
<span class="go">)</span>
<span class="go">text(x=-.7,y=8,</span>
<span class="go">    labels='Merry',</span>
<span class="go">    adj=c(.5,.5),</span>
<span class="go">    srt=45,</span>
<span class="go">    vfont=c('script','plain'),</span>
<span class="go">    cex=3,</span>
<span class="go">    col='gold'</span>
<span class="go">)</span>
<span class="go">text(x=0.7,y=8,</span>
<span class="go">    labels='Christmas',</span>
<span class="go">    adj=c(.5,.5),</span>
<span class="go">    srt=-45,</span>
<span class="go">    vfont=c('script','plain'),</span>
<span class="go">    cex=3,</span>
<span class="go">    col='gold'</span>
<span class="go">)</span>
<span class="go">dev.off()</span>
登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

我如何使用美丽的汤来解析HTML? 我如何使用美丽的汤来解析HTML? Mar 10, 2025 pm 06:54 PM

本文解释了如何使用美丽的汤库来解析html。 它详细介绍了常见方法,例如find(),find_all(),select()和get_text(),以用于数据提取,处理不同的HTML结构和错误以及替代方案(SEL)

Python中的数学模块:统计 Python中的数学模块:统计 Mar 09, 2025 am 11:40 AM

Python的statistics模块提供强大的数据统计分析功能,帮助我们快速理解数据整体特征,例如生物统计学和商业分析等领域。无需逐个查看数据点,只需查看均值或方差等统计量,即可发现原始数据中可能被忽略的趋势和特征,并更轻松、有效地比较大型数据集。 本教程将介绍如何计算平均值和衡量数据集的离散程度。除非另有说明,本模块中的所有函数都支持使用mean()函数计算平均值,而非简单的求和平均。 也可使用浮点数。 import random import statistics from fracti

python对象的序列化和避难所化:第1部分 python对象的序列化和避难所化:第1部分 Mar 08, 2025 am 09:39 AM

Python 对象的序列化和反序列化是任何非平凡程序的关键方面。如果您将某些内容保存到 Python 文件中,如果您读取配置文件,或者如果您响应 HTTP 请求,您都会进行对象序列化和反序列化。 从某种意义上说,序列化和反序列化是世界上最无聊的事情。谁会在乎所有这些格式和协议?您想持久化或流式传输一些 Python 对象,并在以后完整地取回它们。 这是一种在概念层面上看待世界的好方法。但是,在实际层面上,您选择的序列化方案、格式或协议可能会决定程序运行的速度、安全性、维护状态的自由度以及与其他系

如何使用TensorFlow或Pytorch进行深度学习? 如何使用TensorFlow或Pytorch进行深度学习? Mar 10, 2025 pm 06:52 PM

本文比较了Tensorflow和Pytorch的深度学习。 它详细介绍了所涉及的步骤:数据准备,模型构建,培训,评估和部署。 框架之间的关键差异,特别是关于计算刻度的

哪些流行的Python库及其用途? 哪些流行的Python库及其用途? Mar 21, 2025 pm 06:46 PM

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

如何使用Python创建命令行接口(CLI)? 如何使用Python创建命令行接口(CLI)? Mar 10, 2025 pm 06:48 PM

本文指导Python开发人员构建命令行界面(CLIS)。 它使用Typer,Click和ArgParse等库详细介绍,强调输入/输出处理,并促进用户友好的设计模式,以提高CLI可用性。

用美丽的汤在Python中刮擦网页:搜索和DOM修改 用美丽的汤在Python中刮擦网页:搜索和DOM修改 Mar 08, 2025 am 10:36 AM

该教程建立在先前对美丽汤的介绍基础上,重点是简单的树导航之外的DOM操纵。 我们将探索有效的搜索方法和技术,以修改HTML结构。 一种常见的DOM搜索方法是EX

解释Python中虚拟环境的目的。 解释Python中虚拟环境的目的。 Mar 19, 2025 pm 02:27 PM

文章讨论了虚拟环境在Python中的作用,重点是管理项目依赖性并避免冲突。它详细介绍了他们在改善项目管理和减少依赖问题方面的创建,激活和利益。

See all articles