The Wolfram|Alpha Blog is now part of the Wolfram Blog. Join us there for the latest on Wolfram|Alpha and other Wolfram offerings »
Michael Trott

Using Formulas… for Everything—From a Complex Analysis Class to Political Cartoons to Music Album Covers

June 10, 2013 —
Comments Off

In my last blog post, I discussed how to construct closed-form trigonometric formulas for sketches of people’s faces. Using similar techniques, Wolfram|Alpha has recently added a collection of hundreds of such closed-form curves for faces, shapes, animals, logos, and signatures. In today’s post, I want to show some of the entertaining things one can do with these parametrized curves. Although these are just simple curves, a large variety of fun images (and animations) can be constructed from them. These can then be used, for example, in political cartoons, talk shows, posters, music album covers, or just to spice up an advanced calculus or first-year theoretical mechanics class. I will first discuss the fun applications, and then the more mathematical ones.

Wolfram|Alpha has hundreds of curves that can be used for the variations discussed below. Here are some example categories.

Pokemon, people, fictional characters' curves
My little pony, food, shape and logo curves

I will start by implementing two simple functions. We will work with the individual curve segments rather than with one monolithic curve, because for most of the applications below, the concrete parametrization of the whole curves does not matter. The parametrizations returned by Wolfram|Alpha are a set of truncated Fourier sums multiplied by unit step functions. The unit step functions ensure that we can use a single parametrization and nevertheless have individual disconnected line segments. The function extractCurveSegments gives a list of the individual curve segments that a curve is made of. And the getSegmentLength gives the length of a segment.

extractCurveSegments and getSegmentLength

As with most Wolfram|Alpha functionality, it’s easy to import a computable form of the data into Mathematica. So, let’s import the parametrization of the face of Barack Obama.

Barack Obama curve

We extract the individual curve segments for further use. Each curve segment is an expression in t that, when plotted for t from 0 to 2π, draws the visual representation of the segment.

Individual curve segments for Obama curve

Let us now use the segment parametrizations to build some variations of the original image. First we plot the line segments as 3D tubes of finite thickness. Although simple, this makes the curves look already a bit more interesting.

Line segments as 3D tubes of finite thickness for Obama curve

Another way to make a more interesting-looking graphic: the next graphic shows each curve segment as a spiral that originates from the segment’s center of mass.

Each curve segment as a spiral that originates from the segment's center of mass for Obama curve

If we sample the curve segments coarsely and position chains of disks at each curve point, we can construct some Medusa-like images. We position a disk at each point of the parametrized curve, and from each of these disks we attach a sequence of smaller and smaller disks.

Disks in 3D at curve points for Obama curve

And here the same is done with spheres instead of disks in 3D.

Spheres in 3D at curve points for Obama curve

If we sample the curve segments densely, we can build a 3D plot of the distance of all points {x,y} of the plane to the nearest (or second nearest, …) curve segment points. To do so efficiently, we use Nearest.

make3DNearestDistancePlot of Obama curve

The resulting image looks more interesting than the curves themselves, as we now also see some structure in the regions between the curves.

3D nearest distance plot of Obama curve

So far, we have kept the curve points and placed and added various additions to the plot. We can also change the actual coordinates of the curve points. Having the coordinates of the curve points in explicit form at hand, we can easily take the square root of the curves. The next graphic shows the resulting curves for taking the square root in x,y coordinates and in polar coordinates.

Obama curve taking the square root in x,y coordinates and in polar coordinates

Generalizing the map to arbitrary power functions is straightforward, and the following interactive example allows arbitrary reference points and exponents. For some more visual variety, we use some current and former presidents and prime ministers. In addition to the power function mapping parameters, we allow the interactive change of the coordinate system origin through a locator.

Generalizing the map to arbitrary power functions for Obama curve
Generalizing the map to arbitrary power functions for Obama curve
Generalizing the map to arbitrary power functions for Obama curve
Generalizing the map to arbitrary power functions for Obama curve

Now let’s switch to another person curve. This is a sketch of Wolfram|Alpha’s creator, Stephen Wolfram.

Stephen Wolfram curve

We again extract the individual curve segments for further use.

Individual line segments for Wolfram curve

We plot again the distances of points in the plane to the nearest line segment. The middle and right image show the distances to the second-nearest and the third-nearest curve segments. Not unexpectedly, measuring the distance to more distant line segments loses important facial structures.

Wolfram plot using the distances of points in the plane to the nearest line segment

Here are two more uses of the distance array; this time, we make three array plots from the data that was used to make the last plot.

Array plots of Wolfram

Here is a version of Stephen in a Roman mosaic fresco style. To construct it, we start with a semi-random array of points. For these points we build a Voronoi diagram of these points (using ListContourPlot[points, InterpolationOrder->0]). We map the individual polygons of the Voronoi diagram into 3D (to get a more realistic mosaic feeling) and  color them according to the distance to the points of the curve segments.

Roman mosaic fresco style version of Wolfram
Roman mosaic fresco style version of Wolfram

And here is a more modern art version of Stephen Wolfram. On a square grid, we imprint the curve segments as if they act gravitationally on the grid vertices and slightly deform the grid.
Modern art version of Wolfram

Since the sketch of Stephen Wolfram’s face is given as a set of curves, we can apply various differential geometric operations. The next graphic shows the pedal curve and the contrapedal curve of the face curves, meaning that the curves are defined in such a way that a point on the new curve is perpendicular to the tangent (normal) of the original curve with respect to a point P (the locator). (Initially, we put the pedal point in Stephen’s left nostril.)

Pedal curve and contrapedal curve of Wolfram curves
Pedal curve and the contrapedal curve of Wolfram parametrization

Rotating the curves that form the face outline around the z axis gives an interesting-looking 3D image of Stephen Wolfram’s face.

3D image of Wolfram

Now let’s take the last two face curves, the ones from Wolf Blitzer and Anderson Cooper, and morph them into each other. Because the parametrizations don’t allow a one-to-one map, we can’t directly interpolate between the parametrized curves, but we can easily interpolate between the points of the curves. First we rescale the curves, so that the two faces are identically positioned and scaled.

Wolf Blitzer and Anderson Cooper curves

We select 20,000 points on each face curve set and find the nearest point on the other face curve set. Then we interpolate between these point pairs.

20,000 points for Blitzer and Cooper face curves

These are the lines of interpolation between the point pairs.

Lines of interpolations between the pairs for Blitzer and Cooper plots

Using the just-calculated transition and boundary points, we can quickly generate relief plots of their faces.

Relief plots of Blitzer and Cooper

And here again is a transition between the two faces.

Transition between Blitzer and Cooper curves

The following interactive version allows us to change the transition parameter and connects the resulting interpolating point set.

Transition between Blitzer and Cooper curves

Or we could use a drawing of Jay Leno.

Jay Leno curve

Here is again the nearest curve segment plot of its defining line segments.

Nearest curve segment plot of Leno

Here are two modified interactive “line drawings” of Jay. In the first graphic, the line numbers, line lengths, and line angles can be changed interactively.

Interactive line drawings of Leno
Interactive line drawings of Leno

We can also braid tubes along our curve segments. Or depict a more prickly Leno made from thousands of little arrows.

Leno with braid tubes along curve segments and prickly Leno made from thousands of little arrows

And here is his portrait rotated in 3D along a snail-shell-like curve.

Leno 3D along a snail-shell-like curve

We continue with some variations of Lady Gaga.

Variations of Lady Gaga curve

She surely would not want to be displayed as a uniform thin, plain blue curve. So, let’s broaden and color the line segments.

Colorized Lady Gaga curve

The next graphic is made with 50,000 randomly placed and colored ngons. The size and the opacity of the ngons are a function of the distance to the nearest point of one of the curves that make the face sketch.

Plot of Lady Gaga with ngons

And here are two calls to ListContourPlot with the points of Lady Gaga’s curves. We select the points for the plot randomly from a rectangular array of points according to distance-dependent probability distributions. The right image we color according to her infamous meat dress.

ListContourPlot of Lady Gaga

Using the polygons from the left graphic and round corners, we obtain the following pebble-stone-like images.

Pebble-stone-like images of Lady Gaga

Two more variations on Lady Gaga—cones and cylinders randomly placed along the defining curves embedded in 3D.

More variations on Lady Gaga curve using cones and cylinders

Next, we import the image of a feather.

Feather image

By randomly re-coloring this feather, we can place colored feathers along line segments of the curve. The sizes of the feathers are determined by the size of the original curve segment.

Colored feathers along line segments of the Lady Gaga curve
Colored feathers along line segments of the Lady Gaga curve

We can make similar images for other curves, not just for people, and also for fictional characters (for example, for Tinky Winky).

Here is a 3D picture of Tinky Winky.

3D image of Tinky Winky

And in the next image, we carry out an inversion on Tinky Winky.

Inversion on Tinky Winky
Inversion on Tinky Winky

Or we could use Miss Piggy.

Miss Piggy curve

And we can use the points from less densely sampled segments to make a Voronoi tessellation of the data.
Voronoi tessellation of Miss Piggy
Voronoi tessellation of Miss Piggy

Let’s contract Miss Piggy’s curves recursively.

 Contract Miss Piggy's curves recursively
Contract Miss Piggy's curves recursively

And the next image interpolates between the curve points and the curve points mapped to the nearest point on a larger, all-enclosing circle.

Interpolates between the curve points and the curve points mapped to the nearest point on Miss Piggy curve

Next, we will charge (electrically) Miss Piggy and calculate the resulting electric field from the charged discretized curve segments. Here is the electric field of a charged line segment.

Charge (electrically) Miss Piggy

These are the lines of equal field-strength magnitude.

ListPlot3D of Miss Piggy

Or, we can make use of a parametrization of Dilbert.

Here is Dilbert with spheres of radii that are proportional to the distance to the nearest curve points. The left graphic uses randomly placed spheres and the right graphic uses spheres with centers on a square grid.

Parametrization of Dilbert

Having a Dilbert curve at hand, what would be more natural than to unite it with a Hilbert curve, the well-known space-filling curve?

Hilbert curve

We imprint the Dilbert curve into the Hilbert curve by elevating the points near to the Dilbert curve.

Imprint the Dilbert curve into the Hilbert curve

And here is a logo curve transformed (pun intended) into a 3D plot.

Logo curve transformed into a 3D plo

There are many more real and fictional celebrities that we could sketch and modify. Here are three. We encode their names. Do you recognize them? Click here for the spoiler.

Curves for fictional and real celebrities
Curves for fictional and real celebrities

And here is a fictional character. Again, click for the spoiler.

Fictional character curves

Instead of drawing the curves, we draw many (about twenty-five thousand) osculating circles at points from the defining curves to obtain a natural bubble environment.

Fictional character curve with osculating circles

Yet another possible curve usage is the making of coloring pages. The next input implements the mapping of a randomly selected Pokémon figure into a polygon.

Pokémon curves for coloring pages

And here is a possible arrangement of Pokémon characters to be colored.

Pokémon curves for coloring pages

Some apply some unconventional coloring.

Pokémon curves colored

We’ve had our fun with people and fictional characters. But before having more fun with them in animations, let’s do some more mathematics related to curves. For example, we could use the curves to do a bit of complex analysis. Let’s start with a curve that approximates the letter π.

Pi curve

In the next graphic, we map the plane into the complex plane and plot the argument of the modular function inverse elliptic nome. The dense set of singularities on the natural boundary of analyticity of the inverse elliptic nome gives the intricate fine structure that we see along the outline of π.

Map the plane into the complex plane and plot the argument of the modular function inverse elliptic nome

Next, we form a rational function in z based on the Fourier series.

Rational function in z based on the Fourier series

The rational function evaluated at z = exp(i φ) traces out the letter π. For z = R exp(i φ), we obtain different-looking curves. Similarly, rational functions and, after normalization, even polynomials could be used for people curves, shape curves… So, if we need polynomials for some curve applications, we can always construct them from the trigonometric sine sums.

Plotting Pi curves

In the next graphic we form a polynomial of degree 128 with simple roots along the outline of π.

piZeroPoly

Here is a plot of the curves of constant real and imaginary parts of the last polynomial.

Plot of the curves of constant real and imaginary parts of piZeroPoly

Let’s carry out another complex analysis example. We know that with a conformal map, we can map any nice, single-connected region of the complex plane to the unit disk. And in many instances, a series approximation of the conformal map will give good results. For instance, here is the exact map that maps a square to a disk. It’s a bit complicated and contains Jacobi elliptic functions.

squareToDiskMap

The series approximation is quite simple.

Series approximation for squareToDiskMap

And visually, the series with only the two leading terms already gives a reasonable approximation of the map.

Visualize series approximation for squareToDiskMap

So, let us to try to find complex maps that map person A into person B. Because we cannot independently map the exterior boundaries and interior features, the mapping will definitely not be perfect. But watching the algorithm that tries to minimize the difference between person A and person B carrying out the conformal map on person A or B is entertaining at least. So we implement the finding of such a conformal map. Here are eight pairs of persons to be mapped.

Complex maps that map person A into person B
Complex maps that map person A into person B
Complex maps that map person A into person B
Complex maps that map person A into person B

Let’s solve some partial differential equation calculations with the Newton curves to honor the co-inventor of differentiation. In preparation, let’s consider the 2D Fourier series of the curves that define Newton’s face, which we constructed in the last blog post.

Isaac Newton curve

We will rescale the curves to fit into [0,1] X [0,1] and use the (unnormalized) 2D Fourier basis functions ψk, m = sin(k π x) sin(m π y). Effectively, we want to approximate a Dirac delta function along the defining curves and the zero function anywhere else.

Approximate a Dirac delta function along Isaac Newton curve

We calculate the 2D Fourier coefficients using numerical integration. We will calculate quite a few coefficients, namely 2002.

Calculate the 2D Fourier coefficients on Isaac Newton curve

To assemble the Fourier approximations, we use a tensor product grid in the x,y plane for efficiency. The function xyArray takes an optional argument “FourierCoefficientMultiplicator” -> value that we will use in a moment.

Assemble Fourier approximations for Isaac Newton curve

This is the resulting Fourier approximation of the 2D curves shown in 3D.

Fourier approximation of the 2D curves shown in 3D for Isaac Newton curve

The next plot shows how the (scaled) function values increase with an increasing number of Fourier modes taken into account. We see that we need many more Fourier coefficients for a good-looking image than we needed Fourier modes for the curves themselves.

ListContourPlot3D with Isaac Newton curve

Remembering the separation of variables method to solve partial differential equations, we can use the Fourier coefficients to straightforwardly obtain the time-dependent solutions of the wave equation and the heat equation. The above shown Fourier expansion is the initial conditions at t=0.

Here is the time-evolved version of Newton’s face under the wave equation. (We assume vanishing initial velocities everywhere.)

Time-evolved version of Newton's face under the wave equation

And here is the time-evolved version of Newton’s face under the heat equation–it seems most suited for Halloween.

Time-evolved version of Newton's face under the heat equation

And if Newton’s face were a microparticle and would obey the Schrödinger equation, then quickly the localized face would flow apart and after a short time would look so:

If Newton's face were a microparticle and would obey the Schrödinger equation

We now end our advanced calculus applications of the curves.

With a bit more effort, we can try to animate some of the curves instead of just constructing static images. For instance, we could take three closed curves and morph them. This is done here using a pig, a unicorn, and a yeti silhouette.

Pig, unicorn, and yeti silhouette

We first normalize the curve in size and then form the superposition chimera cpig curvepig + cunicorn curveunicorn + cyeti curveyeti.

Pig, unicorn, and yeti silhouette
Pig, unicorn, and yeti silhouette
Pig, unicorn, and yeti silhouette

Above we had fun with people’s faces; we used some curves to solve partial differential equations, so let’s now do some physics, especially mechanics.

Assuming a flexible Barack Obama, we could model the defining curves as discretized elastic springs with harmonic nearest neighbor forces. We also assume a coupling from the end of each line segment to the beginning of the next line segment (the complex-valued connections in the parametrizations). The resulting mechanical system is described by Newton’s equation of motion m X“(t)=F(X(t)), where X(t) is the vector of the positions of all the points on the curve segments. Of special interest for a mechanics class are the eigenmodes of the resulting mechanical system, generalizing the eigenmodes of the linear chain.

Model the defining curves as discretized elastic springs with harmonic nearest neighbor forces
Model the defining curves as discretized elastic springs with harmonic nearest neighbor forces
Model the defining curves as discretized elastic springs with harmonic nearest neighbor forces
Model the defining curves as discretized elastic springs with harmonic nearest neighbor forces

Or we can get a bit crazier with the animations: let’s marry two curves (each in its 2D plane) in 4D. First we select two shapes, say π and the hammer and sickle symbol.

Marry two curves in 4D

Then we form a two-dimensional surface in a four-dimensional space. The four coordinates of the surface are {cπ,x(s),cπ,y(s), c hs,x(t), chs,y(t)}, where {cπ,x(s), cπ,y(s)} is the parametrization of the pi curve and {c hs,x(t), chs,y(t)} is the parametrization of the hammer and sickle curve. We then look at this surface by projecting it into a three-dimensional subspace specified by rotations in the four-dimensional space.

Marry two curves in 4D
Marry two curves in 4D
Marry two curves in 4D

We could do something similar with two faces and amalgamate them into a Janus-type 4D object. The resulting surface will look quite intricate, and we encourage the reader to find some appropriate pairs of people to unite in higher dimensions.

We end today’s blog post with a curve that is nearly begging to be animated: Psy’s dance.

Psy's dance

We do not go to great length with character animations here. Instead, we just rotate parts of his arms and legs at the elbow and knee joints. To achieve a more realistic Gangnam dance animation, we would have to add rotations around the hip.

Psy's dance
Psy's dance
Psy's dance

In the next blog post in this series, we will look at laminae, meaning curves together with their insides. For their constructions, quite different mathematical techniques will be used.

To interact with the examples above, first, download the Wolfram CDF Player. Then, download this post as a Computable Document Format (CDF) file.

2 Comments

It is more difficult than it seems to draw the face of Obama with formulas 😉 It is funny it transform their face to square root or make vertical asymptote.
Anyway, I wanted to thank you for your tools. Math will never be the same with WolframAlpha.
Best regards, Brunner Nathan

Posted by Brunner Nathan June 13, 2013 at 1:01 am

To achieve a more realistic Gangnam dance animation, we would have to add rotations around the hip.

Posted by topdiablo3 June 28, 2013 at 2:55 am