📜 ⬆️ ⬇️

Building analytical expressions ... for any objects - from the Pythagorean theorem to the pink panther and Sir Isaac Newton in Wolfram Language (Mathematica)


Translation of Michael Trott's post “ Making Formulas ... for Everything — From Pi to the Pink Panther to Sir Isaac Newton ."
Thanks for the help in translating Sylvia Torosyan .
Download the translation in the form of a Mathematica document that contains all the code used in the article here (archive, ~ 7 MB).

At Wolfram Research and Wolfram | Alpha, we love math and computation. Our favorite topics are algorithms that follow formulas and equations. For example, Mathematica can calculate millions of integrals (more precisely, an infinite number found in practice), and also Wolfram | Alpha knows hundreds of thousands of mathematical formulas (from Euler’s formula and BBP formulas for Pi to complex defined integrals containing sin (x) ) and a plurality of formulas physics (e.g., from Poiseuille's law to classical mechanics solutions spot particles in a rectangle or inverse distance capacity in four-dimensional space, in hyperspherical coordinates ), as well as less well-known formulas, such as formula d I rate wet dog shaking , the maximum height of the sand castle , or the time of cooking a turkey .

We recently added formulas for a variety of different shapes and objects. The Wolfram Blog | Alpha showed some examples of the formation of figures that were asked using mathematical equations and inequalities. Among the curves constructed are curves of images of fictional characters :

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_1.png
')
making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_2.png

Curves outlines of objects :

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_3.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_4.png

and, the most popular among our users, the curves of the images of real people :

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_5.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_6.png

Since these curves, in the mathematical sense, are similar to a lemniscate or a sheet of Descartes , they are more interesting from the point of view of their graphic representation than by their mathematical properties.

After Richard’s blog post was published, my colleague asked me, “How do you make an equation describing Stephen Wolfram’s face?” After a moment thinking about this question, I realized that it’s really amazing that the question is not that there is an analytical expression: a digital image (suppose for simplicity that it is black and white) is a rectangular array of gray values. Using this array, one can construct an interpolating function, even a polynomial. But such an explicit function would be very huge, hundreds of pages long, and would not be suitable for any practical application. In fact, the difficulty is how to obtain an analytical expression that would resemble a person’s face, so that it fits on one page and has a simple structure. An analytical expression for a curve that depicts Stephen Wolfram's face, approximately one page long, which is comparable to the size of a complex physics formula, such as, say, the cube's gravitational potential .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_7.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_8.png

In this article I want to show how to generate this kind of equation. It is not at all surprising that in the article that tells “how to perform calculations ...” you will see a significant amount of Mathematica code, but I will start with simple introductory explanations.

Suppose you draw pencil lines on a piece of paper and assume that you draw only lines; no shading and no filling do not. Then the drawing will be made from a number of segments of the curve. A mathematical concept such as Fourier series allows us to write a finite mathematical formula for each of these line segments, which will describe them as you like.

As a simple example, consider a series of functions. making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_9.png ,

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_10.png

which are the sum of sinusoids of various frequencies and amplitudes. Here are the first few members of this function chain:

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_11.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_12.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_13.png

The graphical construction of this sequence of functions shows that with increasing n, the function making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_14.png tends to triangular function.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_15.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_16.png

The sine function is an odd function, and as a result, any sums of functions of the form sin ( kx ) are also odd functions. If we use the cosine function, we’ll get even functions instead. The composition of the sine and cosine values ​​allows us to approximate more general curve shapes.

Summarizing the above, we replace the multiply in the series under consideration by making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_17.png and also we will set two rows, from sines and cosines, respectively:

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_18.gif

These functions allow you to approximate a wider class of functions:

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_19.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_20.png

It can be shown that any smooth curve y ( x ) can be arbitrarily well approximated on any interval making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_21.png using the Fourier series. Moreover, for smooth curves, the values ​​of the coefficients sin (kx) and cos (kx) tend to zero for large values ​​of k .

Now, considering a parametric curve making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_22.png , we can use this kind of superposition of sine and cosine functions independent for horizontal making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_23.png and vertical making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_24.png components.

The use of the sum of three sines and three cosines in each of the components already covers a wide variety of shapes, including circles and ellipses:

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_25.gif

The following interactive demonstration allows us to explore the space of possible forms. 2D sliders change the corresponding coefficients before the functions of cosine and sine:

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_26.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_27.png

If we terminate the Fourier series expansion of a curve, say on the first n terms of the series, we get 4 n free parameters. In the space of all possible curves, most of the curves will look uninteresting, but for some values ​​of the coefficients, expansions will look like shapes in which you can see familiar shapes. However, even small changes in the coefficients of decomposition greatly change the shape. The following interactive example allows changing the initial 4 × 16 = 64 coefficients of a Fourier series of a curve. Using special sets of coefficients of Fourier series, we can get various recognizable figures.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_28.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_29.png

If we now take more than one curve, we will have all the necessary components to build a face-like image. The following interactive demonstration uses two eyes, two pupils, a nose and a mouth.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_30.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_31.png

Now we will do the opposite: arrange a row of points (blue crosses) so that a line is formed that can be changed and build a curve that will approximate the given curve in Fourier series:

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_32.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_33.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_34.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_35.png

Note: Fourier series are not the only way to define approximating curves. We could use wavelets or splines , or encode curves piecewise through circular segments . Or with sufficient patience, with the help of the universality of the Riemann zeta function , we could find any figure inside the critical strip . (Surprisingly, any possible (fairly smooth) image, such as Jesus on toast, exists for some values ​​of the Riemann's zeta function ( s ) in the 0≤Re (s) ≤1 band, but we have no constructive way to find him.)

To demonstrate how to find simple formulas based on the Fourier series that approximate these figures, we start with an example: a figure with precise, clearly defined boundaries is a short formula. More specifically, we will use the well-known formula: the Pythagorean theorem.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_36.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_37.png

Rasterizing the equation will give the original image, which we will use:

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_38.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_39.png

It is easy to get a set of all points describing the edges of characters using the EdgeDetect function.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_40.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_41.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_42.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_43.png

Now that we have points that define edges, we can connect them with straight (or curvilinear) segments. The following pointListToLines function performs this operation. We start from a randomly selected point and find all the nearest points to it (using the fast Nearest function). We will continue this process until we find points of all fairly close points. Also make sure that there are no 180 degrees turns. To observe how curves are formed, we use the Monitor function.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_44.png

For the image of the recording of the Pythagorean theorem, we obtain 11 specific curves from boundary points.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_45.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_46.png

Combining the sets of points and coloring the obtained curves, we will see the quite expected set of curves: the outer boundaries of the letters, the inner boundaries of the letters a and b , three squares, plus an equal sign.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_47.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_48.png

Now for each segment of the curve we need to find the Fourier series (x and y components), which approximates the segment. Guided by the usual definition of the Fourier series of the function f (x), we know that the coefficients of the series are integrals of the function f (x) multiplied by cos (kx) and sin (kx). But while we have a lot of points, not functions. To convert them into functions that we can integrate, create B- splines of the curve for each of its segments. The parametrized variable B -spline of the curve will be the variable of integration. (Using B- splines instead of piecewise linear interpolation between points will give us additional advantages in approximating strongly broken curves.)

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_49.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_50.png

We can find the integrals needed to obtain the Fourier coefficients using numerical integration, however, a faster way is to use the Fast Fourier Transform ( FFT ) to obtain the Fourier coefficients.

To obtain more uniform curves, we will take another step: re-parameterize the interpolated spline curve, consisting of a set of curve segments, with the help of a new parameter - the arc length. The fourierComponents function implements the creation of a B-spline curve , re-parameterization by arc length and calculates FTT to get the Fourier coefficients. We also consider whether a segment of a curve is open or closed to avoid the Gibbs phenomenon . (The above demonstration of the approximation of a pentagram clearly shows the Gibbs phenomenon in case the “close curve” box is unchecked.)

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_51.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_52.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_53.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_54.png

For continuous function, we expect the average value of the rate of decrease making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_55.png for the kth coefficient of the Fourier series. This effect should also be retained for the coefficients of the Fourier series calculated by us. This means that on average, the 10th Fourier coefficient accounts for only 1% of the absolute value compared with the first. This makes it possible to trim the Fourier series in not very high orders, since we do not want to get formulas that are too large. The expression below calculates the average exponent of the rate of decrease of the Fourier components for the above curve. making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_56.png . (The value is slightly less than 2 due to the discretization of points in the B-spline curves.)

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_57.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_58.png

Below is a graph on a logarithmic scale along both axes with the absolute values ​​of the coefficients of the Fourier series for the first three curves. However, the general trend with respect to, say, quadratic attenuation for Fourier coefficients shows that the magnitude of neighboring coefficients often fluctuates more than the average value of the loss of the coefficients as a whole.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_59.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_60.png

Multiplying the Fourier coefficients by cos (kt) and sin (kt) and summing these expressions gives us the desired parametrization of the curves.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_61.png

The makeFourierSeriesApproximationManipulate function visualizes curve approximations depending on how many row members are taken.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_62.png

For the picture corresponding to the Pythagorean theorem, starting with a dozen ellipses, we quickly form the original image, increasing the number of members of the Fourier series.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_63.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_64.png

We want to define an analytical expression for the whole equation, even if this expression consists of non-intersecting segments of a curve. To achieve this, we use the periodicity of the members of the Fourier series, having a period of 2 π , in order to depict each of the segments on the corresponding segment: [0,2π], [4 π, 6 π], [8 π, 10 π], ..., and in the intervals (2π, 4π), (6 π, 8 π), ..., we will have purely imaginary coordinates of the curve. On these segments, the curve cannot be depicted, which means that we get a set of non-intersecting segments of the curve. Below this construction is shown for a set of two circles:

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_65.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_66.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_67.png

The graph below shows separately the real and imaginary parts of the complex-valued parametrization constructed above. The red line shows the purely imaginary parameter values ​​from the interval [2π, 4π].

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_68.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_69.png

Since we want the final formula for the curves to look as short and simple as possible, we replace the sum in the formula a cos (kt) + b sin (kt) with A sin (k t + φ) using the sinAmplitudeForm function, as well as rounds the approximate coefficients of the Fourier series to the nearest rational numbers. Instead of Piecewise , we use the UnitStep function in the final formula to separate different segments of the curve from each other. We enumerate the real segments explicitly, and all the segments that should not be drawn are given by the expression making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_70.png .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_71.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_72.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_73.png

Now we have everything to write the final parametrization {x (t), y (t)} of the picture, which shows the equality that defines the Pythagorean theorem:

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_74.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_75.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_76.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_77.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_78.png

After we have discussed the basic ideas of parametrization, let's look at a funnier example, say Pink Panther . Looking through the image search results by the Bing search engine, we quickly find an image that is suitable for parameterization in the form of a " closed form ."

Let's use the following image:

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_79.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_80.png

Use the EdgeDetect function to find all the borders of a panther’s face.

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_81.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_82.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_83.png

Connecting the edges of the curve, we get about 20 segments. (By changing the additional second and third argument of the pointListToLines function, we get fewer or more segments.)

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_84.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_85.png

. , ; pointListToLines . .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_86.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_87.png

, , .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_88.png

, , 20 , , .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_89.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_90.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_91.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_92.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_93.png

, , .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_94.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_95.png

. makeSilhouetteFourierResult . : 1) ; 2) , . .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_96.png

. 4 : ; -; , .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_97.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_98.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_99.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_100.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_101.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_102.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_103.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_104.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_105.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_106.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_107.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_108.png

. . , . , , . ( . .) , :

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_109.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_110.png

, , , .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_111.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_112.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_113.png

, .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_114.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_115.png

, 16 . .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_116.png

, :

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_117.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_118.png

. , , . , .

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_119.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_120.png

50 :

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_121.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_122.png

, , :

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_123.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_124.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_125.png

:

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_126.png

making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton_127.png

This concludes today's discussion on how to construct curves that resemble the faces of people, fictional characters, animals, or other forms. Next time we will discuss the infinite graphical possibilities that arise from the above formulas and how you can use this kind of equation for a variety of images.

Source: https://habr.com/ru/post/254841/


All Articles