Simple operations with symbolic variables
Mostrar comentarios más antiguos
Hello everybody,
I'm new to Matlab. I would like to ask if anyone knows what's the time complexity of the addition / subtraction operations with symbolic variables.
Example:
syms a b
A = a^2*b^2+a^3*b^3
B = a^2*b^2+a*b
I would like to know what's the time complexity of this simple operation:
A - B = a^3*b^3 - a*b
I'm not sure about how Matlab calculates it.
(note that each variable has no given value).
Thank you!
5 comentarios
John Chilleri
el 22 de Feb. de 2017
This may or may not be useful, but you can use tic and toc to measure how long the subtraction takes.
On my computer, it requires about .0012 seconds to complete the operation (really bad computer).
Karan Gill
el 23 de Feb. de 2017
Why do you need this information? Note that symbolic calculations are slower than numeric. Use numeric if you need speed.
Karan (Symbolic doc)
Karan Gill
el 2 de Mzo. de 2017
Ah then use tic and toc as John said.
Walter Roberson
el 2 de Mzo. de 2017
Unfortunately using tic and toc to measure algorithm complexity is tricky.
For example, the algorithm complexity of
for k =1:n
H(k) = 1;
end
is O(n). But if you code that and test with tic toc, you will find that the time is O(n^2) or worse, because of the way that MATLAB handles expanding arrays (notice that I did not preallocate, which is a matter irrelevant to algorithm complexity)
Respuesta aceptada
Más respuestas (1)
Vandana Rajan
el 22 de Feb. de 2017
1 voto
Hi,
To understand how much time a particular MATLAB code takes to execute, you can use the 'tic' and 'toc' functions.
https://in.mathworks.com/help/matlab/ref/tic.html
You may also go for MATLAB profiler to track execution time.
https://in.mathworks.com/help/matlab/ref/profile.html
6 comentarios
John Chilleri
el 22 de Feb. de 2017
Hello,
Would you mind clarifying your question:
1+9-6+26 = 30
is O(1) as it does not scale with any n.
I would suspect the above A-B is also O(1) as it will always take the same amount of time, as it is symbolic, and the sizes of A and B are irrelevant.
So what exactly do you mean by run complexity of these things?
Sorry if this was confusing, I just want to make sure we are on the same page!
Walter Roberson
el 22 de Feb. de 2017
Editada: Walter Roberson
el 23 de Feb. de 2017
The sizes are not irrelevant for symbolic work, when it comes to constants, as I explore in my Answer.
Also, when expressions are being added, there is also the need to find corresponding parts to add:
x + 2 + y
5*y + sin(x) + 9
it has to do matching to figure out to add the y and 5*y and the 2 and the 9. But how expensive is that? Well, first you have to ask whether x + 2 is represented the same was as 2 + x and whether the representation is always the same for those expressions. In some symbolic packages, whether x + 2 and 2 + x are both transformed to x + 2 or both to 2 + x depends on which version was seen first in the session. In the Symbolic Toolbox, there are rules about constant motion and ordering of terms that reduce the cost during processing, but the rules are undocumented and can be tricky to figure out (I explored the behavior of the order of terms in reciprocal of polynomials a couple of months ago; it was obviously consistent internally but what the rule was was far from clear.)
Walter Roberson
el 24 de Feb. de 2017
Documentation of the internal rules would not necessarily help much. I mentioned above a symbolic math package in which the ordering depends upon which version was seen first in the session: that package explicitly documents that fact and explicitly documents that given two equivalent expressions, the one that is chosen is the one that has the lower address (pointer). As the user has no control over the address something is placed at, this mostly acts to document that you cannot fight the system and cannot rely on the order of terms because it could change any time. It is good to know explicitly that taking shortcuts will probably fail, as it keeps you from wasting time trying to reverse-engineer rules, but really the same thing can be accomplished by just saying "These are internal matters, don't count on them" without documenting what happens in any given release.
Categorías
Más información sobre Utilities for the Solver en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!