MATLAB Answers


Display numbers as power of N in a symbolic expression?

Asked by Giuseppe Antonio on 29 Jun 2019
Latest activity Edited by John D'Errico
on 29 Jun 2019
Hello everybody,
if I have a symbolic expression, how can I display all the numbers as a power of a given base (for example 2)?
For example, if inside the expression there is a number "79228162514264337593543950336" this should be written as 2^96.
Is it possible?


Sign in to comment.




1 Answer

Answer by John D'Errico
on 29 Jun 2019
Edited by John D'Errico
on 29 Jun 2019
 Accepted Answer

I don't think you can, at least, not easily. You can do pretty much anything with sufficient effort of course.
syms X
expr = expand((X+2)^12)
expr =
X^12 + 24*X^11 + 264*X^10 + 1760*X^9 + 7920*X^8 + 25344*X^7 + 59136*X^6 + 101376*X^5 + 126720*X^4 + 112640*X^3 + 67584*X^2 + 24576*X + 4096
So given the expression expr (which may be quite complicated, not something as simple as this one) now you want to write the coefficients as powers of 2, for example? Or do you want to factor all coefficients?
For example, the constant coefficient (4096) is clearly 2^12. But the coefficient of x^11 is 24=3*2^3. Do you want to write 24 as
c_11 = log2(24)
c_11 =
So if we raise 2 to the power of c_11, we get 24.
ans =
So you could easily enough extract the coefficients in that simple expression as
C = coeffs(expr)
C =
[ 4096, 24576, 67584, 112640, 126720, 101376, 59136, 25344, 7920, 1760, 264, 24, 1]
You could compute the factors of those numbers.
ans =
ans =
[ 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 5, 11]
And then you could recover the number 126720=2^8*3^2*5*11.
Be careful, as the order of the numbers produced in that list of coefficients is not the same as the order the expression is displayed. Or you could then do this:
ans =
[ 12, log(24576)/log(2), log(67584)/log(2), log(112640)/log(2), log(126720)/log(2), log(101376)/log(2), log(59136)/log(2), log(25344)/log(2), log(7920)/log(2), log(1760)/log(2), log(264)/log(2), log(24)/log(2), 0]
So that is a list of the exponents of 2 for each coefficient.
However, I honestly think this is not what you are asking either. I think your question is to somehow automatically tell MATLAB to display that expression with the coefficients carefully factored and in an exponential form. You can't do that, at least not in any simple way that I know of.


Sign in to comment.