How Does the 'All' Option Affect the Result of Symbolic simplify?

35 visualizaciones (últimos 30 días)
Paul
Paul el 14 de Oct. de 2025 a las 2:24
Comentada: Paul el 27 de Oct. de 2025 a las 12:00
Start with a simple expression
syms alpha beta real
f = cos(beta)-cos(beta)*cos(alpha)^2
f = 
and try to simplify it:
simplify(f)
ans = 
I was expecting the obvious substitution of sin(alpha)^2, but maybe not so obvious to the symbolic engine.
Try 20 steps and return all of the results.
A1 = simplify(f,20,'All',true);
Check the first result
A1(1)
ans = 
Do the same with 1000 steps
A2 = simplify(f,1000,'All',true);
A2(1)
ans = 
I was expecting that the first result, if not the first 20 results, would be the same form both calls to @doc:simplify.
Why does asking for more steps change the result in the first step?
Its looks odd that it takes more than 200 steps to return (arguably?) the simplest result that I suspect most users would be expecting.
simplify(f,200)
ans = 

Respuestas (1)

Soumya
Soumya el 27 de Oct. de 2025 a las 7:14
Hi Paul,
When applying simplify to the expression 'cos(beta) - cos(beta)*cos(alpha)^2', the MATLAB Symbolic Math Toolbox does not immediately produce the equivalent form 'cos(beta)*sin(alpha)^2'. This is due to the way 'simplify' operates: it performs a heuristic search through algebraically equivalent expressions and ranks them using an internal complexity metric, applying a transformation only when it determines the resulting form to be simpler according to that metric. The number of allowed simplification steps, specified by the Steps parameter, controls the breadth of this search. The documentation notes that “By default, simplify uses one internal simplification step. You can get different, often shorter, simplification results by increasing the number of simplification steps” and that “Simplification of a mathematical expression is not a clearly defined subject. There is no universal idea as to which form of an expression is simplest.” Because the first result returned when using 'All', true corresponds to the earliest lower-complexity form encountered in the search, increasing the number of steps (for example, from 20 to 1000) can alter that ordering and yield a different initial result. As a consequence, the form involving sin(alpha)^2 appears only once the search depth is sufficient, typically above approximately 200 steps.
You can refer to the following documentation for more information:
I hope this helps!
  1 comentario
Paul
Paul el 27 de Oct. de 2025 a las 12:00
"applying a transformation"
What exactly does this mean? Once the ranking of the expressions is complete, what further "transformation" would be needed?
Are you saying that A1 and A2 in the examples in the question are returned in a sorted order from least to most complex in accordance with the "internal complexity metric"?

Iniciar sesión para comentar.

Categorías

Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2025b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by