Optimizing a “Manipulate” in Mathematica

2019-05-11 02:12发布

I am looking to make a nice demo of the problem I mentioned in Integration in Mathematica but it is very slow yet and the Manipulate is not smooth at all.

Considering the below, is there means by which I could improve the situation. That is see a more continuous dynamic. Also I can't get to have the Manipulator open using

Control->Manipulator[Appearance->Open]

arrows = ParallelTable[{
RandomVariate[NormalDistribution[0, Sqrt[1]]],
RandomVariate[NormalDistribution[0, Sqrt[1]]]}, {20000}];

Manipulate[
           Graphics[{
                     White, Rectangle[{-5, -5}, {5, 5}],
                     Red, Disk[{0, 0}, 1],
                     Black, Point /@ (arrows[[;; i]]), 
                     Text[Style[
                               Total[
                                     If[# < 1, 1, 0] & /@  
                       (EuclideanDistance[{0, 0}, #] & /@ 
                       arrows[[;; i]])]/Length@arrows[[;; i]] // N, 
                          Bold, 18, "Helvetica"], {-4.5, 4.5}]}, 
           ImageSize -> 800],
{i, Range[2, 20000, 1]},
ControlType -> Manipulator,
SaveDefinitions -> True]

enter image description here

3条回答
叼着烟拽天下
2楼-- · 2019-05-11 02:23

The primary reason for the slowness is because you're calculating the EuclideanDistance of all the points till step i for every step i. You'll see a difference if you take this step out of the Manipulate.

prob = MapIndexed[#1/#2 &, Accumulate[
    EuclideanDistance[{0, 0}, #] < 1 & /@ arrows // Boole]] ~ N ~ 4;

Heike's solution is much smoother than yours or Nasser's so I'll use that as an example. You'd use the pre-calculated value of prob in it as:

Manipulate[
 Graphics[{White, Rectangle[{-5, -5}, {5, 5}], Red, Disk[{0, 0}, 1], 
   Black, Point[arrows[[;; i]]], 
   Text[Style[First@prob[[i]], Bold, 18, "Helvetica"], {-4.5, 4.5}]}, 
  ImageSize -> 200], {i, Range[2, 20000, 1]}, 
 ControlType -> Manipulator, SaveDefinitions -> True]

I've set the precision uniformly to 4 digits, because otherwise, you'll see the figure jump around when the number of significant digits changes.

查看更多
欢心
3楼-- · 2019-05-11 02:24

See if this is any better for you:

Manipulate[

 Graphics[{
   White,
   Rectangle[{-5, -5}, {5, 5}],
   Red,
   Disk[{0, 0}, 1],
   Black, Point /@ (arrows[[;; i]]), 
   Text[Style[
     Dynamic@Total[
         If[# < 1, 1, 0] & /@ (EuclideanDistance[{0, 0}, #] & /@ 
            arrows[[;; i]])]/Length@arrows[[;; i]] // N, Bold, 18, 
     "Helvetica"], {-4.5, 4.5}]}, ImageSize -> 200],

 {{i, 2, "i"}, 2, 20000, 1, Appearance -> "Labeled"},
 TrackedSymbols :> {i},
 SynchronousUpdating -> False,
 AppearanceElements -> All,


 Initialization :>
  (
   arrows = 
     ParallelTable[{RandomVariate[NormalDistribution[0, Sqrt[1]]], 
       RandomVariate[NormalDistribution[0, Sqrt[1]]]}, {20000}];
   )

 ]
查看更多
何必那么认真
4楼-- · 2019-05-11 02:26

Maybe something like this

Manipulate[
 Graphics[{White, Rectangle[{-5, -5}, {5, 5}],
   Red, Disk[{0, 0}, 1],
   Black, Point[arrows[[;; i]]], 
   Text[Style[Count[arrows[[;; i]], a_ /; (Norm[a] < 1)]/i // N, Bold,
      18, "Helvetica"], {-4.5, 4.5}]}, ImageSize -> 800], {i, 
  Range[2, 20000, 1]}, ControlType -> Manipulator, 
 SaveDefinitions -> True]
查看更多
登录 后发表回答