Skip to main content

simplifying expressions - How to find the most compact form of an equation


I would like to know whether there is any general approach to obtain the most compact form of an equation with Mathematica.


I used to believe that the FullSimplify command would do that for me, but I recently found out that it will not necessarily give the most compact form. I found out with the following equation, which has been FullSimplified:


$$\left[\sec (\alpha ) \left(\coth \left(L \sqrt{\text{Bo} \cos (\alpha )}\right)+\text{csch}\left(L \sqrt{\text{Bo} \cos (\alpha )}\right) (\text{Bo} L R \sin (\alpha )-1)-R \sin (\alpha ) \sqrt{\text{Bo} \sec (\alpha )}\right)\right]\left(R \sqrt{\text{Bo} \sec (\alpha )}\right)^{-1}$$


Or, in inputform for your convenience:



eq1 = (Sec[α] (Coth[L Sqrt[Bo Cos[α]]] - R Sqrt[Bo Sec[α]] Sin[α] + 
Csch[L Sqrt[Bo Cos[α]]] (-1 + Bo L R Sin[α])))/(R Sqrt[Bo Sec[α]])

However, if I now run the following:


Collect[eq1,Bo] //FullSimplify

I get a much more compact form, namely: $$ -\tan (\alpha )+L \sin (\alpha ) \sqrt{\text{Bo} \sec (\alpha )} \text{csch}\left(L \sqrt{\text{Bo} \cos (\alpha )}\right)+\frac{\tanh \left(\frac{1}{2} L \sqrt{\text{Bo} \cos (\alpha )}\right)}{R \sqrt{\text{Bo} \cos (\alpha )}}$$


Can someone explain why I get a more compact solution when I first collect the equation for a certain parameter and then run FullSimplify and whether there is a general 'recipe' to get the most compact form of an equation?


P.S. sorry for the long equation, but it seems that most of the smaller equations do not have this behaviour which makes sort of intuitive sense



Answer




There are at least two aspects to this. The first is the fact that FullSimplify will not try all possible transformations, even those it is aware of. See: Why does Simplify ignore an assumption? The second is that Mathematica does not see "compact" and "simple" as the same thing; if you give it a different ComplexityFunction it will do much better in this case. Compare:


eq1 = (Sec[α] (Coth[L Sqrt[Bo Cos[α]]] - R Sqrt[Bo Sec[α]] Sin[α] + 
Csch[L Sqrt[Bo Cos[α]]] (-1 + Bo L R Sin[α])))/(R Sqrt[Bo Sec[α]])

short1 = Collect[eq1, Bo] // FullSimplify


-Tan[α] + (
Sec[α] (Bo L R Csch[L Sqrt[Bo Cos[α]]] Sin[α] +
Tanh[1/2 L Sqrt[Bo Cos[α]]]))/(R Sqrt[Bo Sec[α]])


short2 = FullSimplify[eq1, ComplexityFunction -> Composition[StringLength, ToString]]


(Csch[
L Sqrt[Bo Cos[α]]] Sec[α] (-1 + Cosh[L Sqrt[Bo Cos[α]]] +
Bo L R Sin[α]))/(R Sqrt[Bo Sec[α]]) - Tan[α]

The three expressions measured by both LeafCount and my ComplexityFunction:


LeafCount /@ {eq1, short1, short2}

Composition[StringLength, ToString] /@ {eq1, short1, short2}


{59, 51, 49}

{275, 374, 212}

Note that though short1 and short2 appear to be roughly the same length my ComplexityFunction sees short1 as being much longer. That is because it is not a very good metric, and it is using the OutputForm of the expression rendering "2D" text like this:


ToString[short1]



                                                               L Sqrt[Bo Cos[α]]
Sec[α] (Bo L R Csch[L Sqrt[Bo Cos[α]]] Sin[α] + Tanh[-----------------])
2
-Tan[α] + ------------------------------------------------------------------------
R Sqrt[Bo Sec[α]]

The point of all of this is that you need to be quite specific with ComplexityFunction if you want the expression simplified in the way you see as simple or compact.


Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.