Saturday, June 24, 2023

Import animated GIF into a Mathematica Notebook

Here's the animated image of a black hole I wanted to Import:

https://qph.cf2.quoracdn.net/main-qimg-7a4350884a5333a141e4faa234f45861

ChatGTP-3.5

To copy an animated GIF and paste it into a Mathematica notebook, you can follow these steps:

1. Find the animated GIF you want to use and open it in a web browser or an image viewer.

2. Right-click on the GIF and select "Copy" or "Copy Image" from the context menu. Alternatively, you can also use keyboard shortcuts like Ctrl+C (Windows) or Command+C (Mac) to copy the image.

3. Open your Mathematica notebook and navigate to the desired location where you want to paste the animated GIF.

4. Right-click in the notebook and select "Paste" or use the keyboard shortcuts Ctrl+V (Windows) or Command+V (Mac) to paste the image.

5. By default, Mathematica treats pasted images as static. However, you can utilize the `Import` function to import the GIF file and display it as an animation.

6. Place the cursor immediately after the pasted image, and enter the following code on a new line:

Import["clipboard", "Animation"]

7. Select the code you just entered and evaluate it by pressing Shift+Enter (Windows) or Shift+Return (Mac) or by clicking the "Evaluate" button in the toolbar.

Mathematica will now import the image from the clipboard as an animation and display it in the notebook. You can resize or reposition the animation by manipulating the cell containing the code and the imported image.

Note: If the image doesn't animate after evaluating the code, it's possible that the GIF is not recognized as an animation. In such cases, you might need to save the GIF as a separate file on your computer and use the file path in the `Import` function instead of `"clipboard"`. For example:

Import["C:/path/to/your/animated.gif", "Animation"]

Make sure to replace `"C:/path/to/your/animated.gif"` with the actual file path of your saved GIF.

Wednesday, April 26, 2023

Mathematica Bibliography

This list includes my favorite Mathematica books as well as a few others on good coding practices and algorithms. There is also my article on the Wolfram Language syntax using Prefx (@) and Postfix-Function (//f [ #, params]&) syntax that I use. (Older list, annotated)

Abell, M. L. (2017). Mathematica by Example. Waltham, MA, Elsevier Inc.
Abell, M. L. and J. P. Braselton (2023). Differential Equations with Mathematica. London, United Kingdom ; San Diego, CA, United States, Academic Press is an imprint of Elsevier.
Abell, M. L., J. P. Braselton and J. A. Rafter (1999). Statistics with Mathematica. San Diego, Academic Press.
Bahder, T. B. (1995). Mathematica for scientists and engineers. Reading, Mass., Addison-Wesley Pub. Co.
Carlson, K. W. (2007) "A New Mathematica Programming Style." Wolfram Technology Conference.
Cheung, C. K. (2009). Getting Started with Mathematica. Hoboken, NJ, J. Wiley.
Crandall, R. E. (1991). Mathematica for the Sciences. Redwood City, Calif., Addison-Wesley, Advanced Book Program.
Crandall, R. E. (1994). Projects in Scientific Computation. Santa Clara, Calif., TELOS.
Crandall, R. E. (1996). Advanced Scientific Computation. Santa Clara CA, Springer-Verlag/TELOS.
Crandall, R. E. (1996). Topics in Advanced Scientific Computation. New York, N.Y., Springer-Telos.
Don, E. (2019). Mathematica. New York, McGraw-Hill.
Foxall, J. D. (2003). Practical Standards for Microsoft Visual Basic .NET. Redmond, Wash., Microsoft Press.
Friedl, J. E. F. (2006). Mastering regular expressions. Sebastapol, CA, O'Reilly.
Gray, J. W. (1998). Mastering Mathematica: Programming Methods and Applications. San Diego, Academic Press.
Jacob, C. (2001). Illustrating Evolutionary Computation with Mathematica. San Francisco, Morgan Kaufmann Pub.
Maeder, R. (1994). The Mathematica Programmer. Boston, AP Professional.
Maeder, R. (1996). The Mathematica Programmer II. San Diego, Calif., Academic Press.
Maeder, R. (1997). Programming in Mathematica. Reading, Mass., Addision Wesley Pub.
Maeder, R. (2000). Computer Science with Mathematica : Theory and Practice for Science, Mathematics, and Engineering. Cambridge, U.K. ; New York, Cambridge University Press.
Mangano, S. (2010). Mathematica Cookbook. Sebastopol, CA, O'Reilly.
McConnell, S. (2004). Code Complete. Redmond, Wash., Microsoft Press.
McDowell, G. L. v. (2016). Cracking the Coding Interview : 189 Programming Questions and Solutions. Palo Alto, CA, CareerCup, LLC.
Roughgarden, T. (2017). Algorithms Illuminated : Part 1, The Basics. San Francisco, CA, Soundlikeyourself Publishing LLC.
Roughgarden, T. (2019). Algorithms Illuminated. : Part 3: Greedy Algorithms. San Francisco, CA, Soundlikeyourself Publishing, LLC.
Ruskeepää, H. (2009). Mathematica Navigator : Mathematics, Statistics, and Graphics. Amsterdam ; Boston, Elsevier/Academic Press.
Tam, P. (1997). A Physicist's Guide to Mathematica. San Diego, Academic Press.
Torrence, B. F. and E. A. Torrence (2019). The Student's Introduction to Mathematica and the Wolfram Language. Cambridge, United Kingdom ; New York, NY, Cambridge University Press.
Trott, M. (2004). The Mathematica Guidebook for Graphics. New York, Springer.
Trott, M. (2004). The Mathematica GuideBook for Programming. New York, NY, Springer New York : Imprint: Springer.
Trott, M. and SpringerLink (2006). The Mathematica GuideBook for Symbolics. Mathematics and Statisics. New York, NY, Springer Science+Business Media, Inc
Springer New York.
Trott, M. P. D. and SpringerLink (2006). The Mathematica Guidebook for Numerics. Mathematics and Statistics. New York, NY, Springer
Springer New York.
Wagner, D. B. (1996). Power programming with Mathematica : the Kernel. New York, McGraw-Hill.
Wagon, S. (2010). Mathematica in Action: Problem Solving through Visualization and Computation. New York, Springer.
Wellin, P. R., R. J. Gaylord, S. N. Kamin and R. J. Gaylord (2005). An Introduction to Programming with Mathematica. Cambridge, England ; New York, Cambridge University Press.
Zimmerman, R. L. and F. I. Olness (2002). Mathematica for Physics. San Francisco, Addison Wesley.

Sunday, April 24, 2016

How It Works: Trapezoid Integration

How It Works : Integrate by the Trapezoid Rule

In related post I used a numerical integration function from Bahder, Mathematica for Scientists and Engineers, to integrate a sawtooth function. This is a great book. Despite having been written in 1995, it's a wealth of 'under the hood' insights into the Wolfram Language. Here is a second function from Bahder for integration via the trapezoid rule that is more sophisticated than the first one. And due to using built-in Listable functionality in Dot and Plus it's about twice as fast

The syntax of the parameter uses a named Pattern to check the data parameter to see if it is a List of 2-element (x,y) pairs, then uses Condition to make sure that data contains more than one point.

Here is the formula for integration by the trapezoid rule:


And here is Bahder's code. The syntax of the parameter uses a named Pattern to check data to see if it is a List of 2-element (x,y) pairs, then uses Condition to make sure that data contains more than one point.

Clear@integrateTrapezoid;
integrateTrapezoid[data:{{_,_}..}/;Length@data>1]:=Module[{xDifferences,ySums},
xDifferences=Dot[{-1,1},#]&/@Partition[First/@data,2,1];
ySums=Apply[Plus,Partition[Last/@data,2,1],{1}];
Plus@@(xDifferences,ySums)/2
]

The Dot product multiplies each vector (List) term by term and sums them.

Dot[{a,b,c},{x,y,z}]

a x+b y+c z

Let's check to make sure the abbreviated operators for Apply and Divide will work as intended; which they will. Apply is 'stickier' than Divide and so Plus will be evaluated before taking the average (dividing by 2).

Precedence/@{Apply,Divide}

{620.,470.}

We test it on his data and.. it doesn't work. There is a bug.

data1={{1.2,51.3},{2.3,72.8},{3.0,87.1},{3.7,53.2}};

integrateTrapezoid@data1

{62.6,80.3,70.5}

Let's add some Print statements, which suffice for debugging all pure functional programs, that is, short ones.

Clear@integrateTrapezoid;integrateTrapezoid[data:{{_,_}..}/;Length@data>1]:=Module[{xDifferences,ySums},
xDifferences=Dot[{-1,1},#]&/@Partition[First/@data,2,1];
Print@xDifferences;
ySums=Apply[Plus,Partition[Last/@data,2,1],{1}];
Print@ySums;
Plus@@(xDifferences,ySums)/2
]

With the Print statement results it's easy to see that xDifferences is indeed the difference between successive x-points and ySums is the sum of each pair of successive y-values.

integrateTrapezoid@data1

{1.1,0.7,0.7}
{124.1,159.9,140.3}
{62.6,80.3,70.5}

I see the bug. We should be multiplying xDifferences by ySums, not listing them. There's a lesson: Always use a multiplication symbol in code rather than a space, which might be mis-read as I did in Bahder's code. We'll add the multiplication and remove the Print statements.

Clear@integrateTrapezoid;integrateTrapezoid[data:{{_,_}..}/;Length@data>1]:=Module[{xDifferences,ySums},
xDifferences=Dot[{-1,1},#]&/@Partition[First/@data,2,1];
ySums=Apply[Plus,Partition[Last/@data,2,1],{1}];
Print@(xDifferences*ySums);
Plus@@(xDifferences*ySums)/2
]

Now we get the correct answer.

integrateTrapezoid@data1

{136.51,111.93,98.21}
173.325

Let's try it on something else.

data2={{0.0,0.0},{1.0,1.0},{2.0,0.0},{3.0,2.0},{4.0,0.0},{5.0,2.0},{6.0,1.0},{7.0,3.0},{8.0,0.0},{9.0,2.0},{10.0,0.0}};
Graphics[Line@data2,Axes->True]



integrateTrapezoid@data2

{1.,1.,2.,2.,2.,3.,4.,3.,2.,2.}
11.

Trusting we got xDifferences and ySums right, we can copy and paste them into the program final line by hand to double-check its validity.

Plus@@{1.`,1.`,2.`,2.`,2.`,3.`,4.`,3.`,2.`,2.`}/2

11.

Recommended:

Saturday, April 23, 2016

Compiling a Function: Mandelbrot Set

Computing the Mandelbrot set is a classic example of the need to Compile a Mathematica function (see Ruskeepää, p. 451). You just need to get the syntax right for a successful Compile. Below, the first argument is the parameter, c, and its type, Complex. The entire Module is the second argument, the expression to be compiled. Definitely try this at home!

Clear@mandelbrot;mandelbrot=Compile[{{c,_Complex}},Module[{z=0+0I,n=0},While [Abs@z<2&&n<50,z=z^2+c;n++];n  ] ]

CompiledFunction[Argument count: 1
Argument types: {_Complex}

The CompiledFunction can be applied to individual numbers. The criteria for inclusion in the Mandelbrot set is, after a limited number of iterations of z = z^2 + c (in this case 50), is its absolute value less than a given number (in this case 2). The absolute value of a complex number is its Euclidean distance from the origin. If the absolute value of a number exceeds 2 before 50 iterations, e.g. 4, it is excluded.

mandelbrot[0.2+I]

4

mandelbrot[0.2+0.2I]

50

Let's do some heavy-duty computing. As a precaution we'll increase the RAM allocated to Mathematica.

Needs@"JLink`";
ReinstallJava[JVMArguments->"-Xmx6000m"]

LinkObject[[]

Start with a couple of individual plots. I use ImageSize->Full so when you do this yourself a large image will automatically be generated. On my Dell Optiplex 900 these take 2.68 seconds each.

Timing[DensityPlot[mandelbrot[x+I y],{x,-2,1},{y,-1.5,1.5},ColorFunction->Hue,PlotPoints->200,Mesh->False,ImageSize->Full,FrameTicks->{{-2,-1,0,1},{-1,0,1}}]]//Column


 Timing[DensityPlot[mandelbrot[x+I y],{x,-2,1},{y,-1.5,1.5},ColorFunction->"Rainbow",PlotPoints->200,Mesh->False,ImageSize->Full,FrameTicks->{{-2,-1,0,1},{-1,0,1}}]]//Column

 I'm kind of curious about all the built-in ColorFunctions. Let's plot Mandelbrot sets using each of them. Here they are. I reverse the order since I like some of the ones at the end best.

colorFunctions=ColorData@"Gradients"

{AlpineColors,Aquamarine,ArmyColors,AtlanticColors,AuroraColors,AvocadoColors,BeachColors,BlueGreenYellow,BrassTones,BrightBands,BrownCyanTones,CandyColors,CherryTones,CMYKColors,CoffeeTones,DarkBands,DarkRainbow,DarkTerrain,DeepSeaColors,FallColors,FruitPunchColors,FuchsiaTones,GrayTones,GrayYellowTones,GreenBrownTerrain,GreenPinkTones,IslandColors,LakeColors,LightTemperatureMap,LightTerrain,MintColors,NeonColors,Pastel,PearlColors,PigeonTones,PlumColors,Rainbow,RedBlueTones,RedGreenSplit,RoseColors,RustTones,SandyTerrain,SiennaTones,SolarColors,SouthwestColors,StarryNightColors,SunsetColors,TemperatureMap,ThermometerColors,ValentineTones,WatermelonColors}

Length@colorFunctions

51

I'll make a Grid of them that will correspond to the Grid of the plots themselves so we can easily look up the ColorFunction of one we like by finding its name in the corresponding place in the Grid. Although each row will have 3 plots and 3 divides 51 evenly, by habit I use the syntax for Partition that would not drop an overhang of a ragged List (the empty List {} in the position of the padding argument).

Reverse@colorFunctions//Partition[#,3,3,1,{}]&//Grid

















The iterator, predefined, in this Table iterates over all the ColorFunctions. Note you do not need to use the cumbersome {i, Length@colorFunctions} requiring the iterator to be a number.

Table[DensityPlot[mandelbrot[x+I y],{x,-2,1},{y,-1.5,1.5},ColorFunction->predefined,PlotPoints->200,Mesh->False,FrameTicks->{{-2,-1,0,1},{-1,0,1}},ImageSize->Medium],{predefined,Reverse@colorFunctions}]//Partition[#,3,3,1,{}]&//Grid




























































































Recommended:

Thursday, April 21, 2016

Example of a Package in a Notebook: Trapezoid Integration of Data Points

Integrate by the Trapezoid Rule


This example of writing your own numerical integration function is from Bahder's excellent book, Mathematica for Scientists and Engineers. I use it to show how to write a Package in a Notebook, as opposed to in a .m file.

While it always pays to see if Mathematica has a built-in function to do what you want, I don't see that NIntegrate can integrate over data points, although it has this nice introduction for the TrapezoidRule: "The trapezoidal rule for integral estimation is one of the simplest and oldest rules (possibly used by the Babylonians and certainly by the ancient Greek mathematicians)."

I needed a function to integrate over data points from an irregular, piecewise, sawtooth-type function. Bahder notes that integrating via the trapezoid rule approximates the integral over a list of (x,y) data points that may be irregularly spaced. Here is the formula. Keyboard shortcuts are faster than a palette; here they are Ctrl+- for subscript and Ctrl+spacebar to exit a subscript. Remember to use a double equal sign for 'equals'.



Here is the numerical integral in a Package. The Package is in one Cell and I set the Cell to Initialization (right-click on the Cell and select Initialization Cell) to automatically load the Package when I evaluate any function in the Notebook.

Note Bahder's use of a named Pattern and Repeated to type-check the data argument. It compares a List of repeated two-element Lists to the pairs that we give it. He then makes sure that there is more than one data pair using Condition. Some say the Wolfram Language is weakly-typed. That is incorrect; it has powerful type-creation and type-checking capabilities.

BeginPackage@"BahderMSE`";

IntegrateTrapezoid::usage="IntegrateTrapezoid[data:{{_,_}..}/;Length@data>1] takes a list of (x,y) pairs, x1 < x2 <x3 ..., that are not necessarily evenly spaced and returns a numerical approximation to their integral using the trapezoid rule.";

Begin@"`Private`";

IntegrateTrapezoid[data:{{_,_}..}/;Length@data>1]:=1/2 Sum[(data[[n+1,1]]-data[[n,1]])(data[[n,2]]+data[[n+1,2]]),{n,Length[data]-1}]

End[];
EndPackage[]

Let's check the usage statement.

In[7]:= ?IntegrateTrapezoid

IntegrateTrapezoid[data:{{_,_}..}/;Length@data>1] takes a list of (x,y) pairs, x1 < x2 <x3 ..., that are not necessarily evenly spaced and returns a numerical approximation to their integral using the trapezoid rule.

Now test it on the same data Bahder used...

In[8]:= data1={{1.2,51.3},{2.3,72.8},{3.0,87.1},{3.7,53.2}};

And we do get the same result.

In[9]:= IntegrateTrapezoid@data1

Out[9]= 173.325

Let's try a different example using Mathematica's SawtoothWave function. This is a typical use of Thread to combine the x and y points in their own Lists.

In[10]:= Thread@{Range[0,3,0.1],SawtoothWave@#&/@Range[0,3,0.1]}

Out[10]= {{0.,0.},{0.1,0.1},{0.2,0.2},{0.3,0.3},{0.4,0.4},{0.5,0.5},{0.6,0.6},{0.7,0.7},{0.8,0.8},{0.9,0.9},{1.,0.},{1.1,0.1},{1.2,0.2},{1.3,0.3},{1.4,0.4},{1.5,0.5},{1.6,0.6},{1.7,0.7},{1.8,0.8},{1.9,0.9},{2.,0.},{2.1,0.1},{2.2,0.2},{2.3,0.3},{2.4,0.4},{2.5,0.5},{2.6,0.6},{2.7,0.7},{2.8,0.8},{2.9,0.9},{3.,0.}}

We see that it is a numerical approximation of the full SawtoothWave where the peaks fall short of 1.









There are three triangles of unit length and height = 0.9, so the total area by a = 1/2 b * h is 1.35. The function works for this example but would need to be validated and its accuracy evaluated for others.

IntegrateTrapezoid@%%

Out[12]= 1.35


Thomas Bahder, Mathematica for Scientists and Engineers


Thursday, February 4, 2016

Wolfram Language Attribute: OneIdentity

Attributes modify the behavior of entire classes of functions. Some Attributes cause functions to have well-known mathematical properties such as commutativity, associativity, or idempotency, notably for valid pattern-matching. Others control the way the Wolfram Language evaluates or does not evaluate a function's arguments, which if you study that you will see is at the very core of Wolfram Language operation.

Listable is a valuable functional language Attribute since it incorporates Thread into a function so it automatically maps over a List and instead of taking a List apart, we apply a function to it as a gestalt.

NumericFunction is an Attribute of all Wolfram Language mathematical functions and is True when all of their arguments are numerical. I suspect it is integral to the interaction of many functions with the underlying knowledge base of Mathematica and therefore to many functions' valid behavior (e.g. Piecewise – always define piecewise mathematical functions with Piecewise).

Attribute: OneIdentity


OneIdentity is the mathematical property of idempotency, which dictates that applying a function any number of times to its argument (i.e. Nest) returns the argument unchanged. Examples are adding zero to any number, or multiplying any number by one, or raising any number to 1st or 0th power (except 0 - someone explain that inconsistency to me!).

0^0

During evaluation of In[549]:= Power::indet: Indeterminate expression 0^0 encountered. >>

Out[549]= Indeterminate

The Doc Center says that OneIdentity is necessary for pattern-matching to work with the default of Times, Plus, and Power, which is a period. I.e., x_+ y_. => x + 0, x_ * y_. => x * 1, and x_^y_. => x^1.

MatchQ[x, Times[n_. , x_]]

True

You can see that OneIdentity removes nested Heads. It says to the evaluator, 'If the given pattern x_ matches any of these patterns, return True.'

a+b+c/.x_:>u/;Print@FullForm[Plus[x]//HoldForm]

HoldForm[Plus[Plus[a,b,c]]]
HoldForm[Plus[Plus]]
HoldForm[Plus[a]]
HoldForm[Plus[b]]
HoldForm[Plus[c]]

a+b+c

Here are related posts on Flat and Orderless. This post shows how to reveal all built-in functions with a given Attribute, categorized by the Attribute.

This is an excellent out-of-print book but there is plenty to learn from it. I'd compare it to David Wagner's Power Programming in Mathematica in its insight to how the Wolfram Language works.

Bahder, Thomas B. Mathematica for Scientists and Engineers. Reading, MA: Addison-Wesley. 1994.




Get new posts by email:

Wolfram Language Attribute: Flat

Attributes modify the behavior of entire classes of functions. Some Attributes cause functions to have well-known mathematical properties such as commutativity, associativity, or idempotency, notably for valid pattern-matching. Others control the way the Wolfram Language evaluates or does not evaluate a function's arguments, which if you study that you will see is at the very core of Wolfram Language operation.

Listable is a valuable functional language Attribute since it incorporates Thread into a function so it automatically maps over a List and instead of taking a List apart, we apply a function to it as a gestalt.

NumericFunction is an Attribute of all Wolfram Language mathematical functions and is True when all of their arguments are numerical. I suspect it is integral to the interaction of many functions with the underlying knowledge base of Mathematica and therefore to many functions' valid behavior (e.g. Piecewise – always define piecewise mathematical functions with Piecewise).

Attribute: Flat


Flat is the mathematical property of associativity, which dictates that different groupings of numbers to specify the order in which the operation is performed, return the same result. E.g. ((a+b)+c) = (a+(b+c)) and (2x3)x4 = 2x(3x4).

a+(b+c)==(a+b)+c

True

Thomas Bahder uses a neat trick to show how Attributes work and how some affect pattern-matching. Here is the trick revealing the mechanism of Flat. Bahder uses a Condition that will never be True, which causes Wolfram Language to try all permutations of the function's arguments that are valid under Orderless. In the false predicate he inserts a Print statement to reveal how the Wolfram Language evaluator works. HoldForm is necessary to stop the evaluator from putting the arguments into standard order before printing them.

Flat tells the Wolfram Language evaluator, 'If the given pattern x_ + y_ matches any of the following patterns, return True.'

a+b+c/.x_+y_:>u/;Print@FullForm[Plus[x,y]//HoldForm]

HoldForm[Plus[a,Plus[b,c]]]
HoldForm[Plus[b,Plus[a,c]]]
HoldForm[Plus[c,Plus[a,b]]]
HoldForm[Plus[Plus[a,b],c]]
HoldForm[Plus[Plus[a,c],b]]
HoldForm[Plus[Plus[b,c],a]]
HoldForm[Plus[a,b]]
HoldForm[Plus[b,a]]
HoldForm[Plus[a,c]]
HoldForm[Plus[c,a]]
HoldForm[Plus[b,c]]
HoldForm[Plus[c,b]]

a+b+c

Here are related posts on Orderless and OneIdentity, and this post shows how to reveal all built-in functions with Attributes categorize by their Attribute.

This is an excellent out-of-print book but there is plenty to learn from it. I'd compare it to David Wagner's Power Programming in Mathematica in its insight to how the Wolfram Language works.

Bahder, Thomas B. Mathematica for Scientists and Engineers. Reading, MA: Addison-Wesley. 1994.