matlabFunction: Why the extra ".0" and why only sometimes?

2 Ansichten (letzte 30 Tage)
James Akula
James Akula am 18 Jan. 2023
Kommentiert: James Akula am 19 Jan. 2023
f = @(x)2.^x.*x.^2
f = function_handle with value:
@(x)2.^x.*x.^2
f = sym(f)
f = 
f = matlabFunction(f)
f = function_handle with value:
@(x)2.0.^x.*x.^2
While functionally irrelevant, it makes formulae difficult to read. Why does it happen, and why only sometimes? More importantly, is there any way to stop it?

Akzeptierte Antwort

John D'Errico
John D'Errico am 18 Jan. 2023
Bearbeitet: John D'Errico am 18 Jan. 2023
Does it really, really, really matter? Actually, there is a (subtle) reason for the 2.0.
You should recognize there is some potential ambiguity in the subexpression
2.*x
Was that intended to be seen as the number 2. multiplied by x using the .* operator, or the integer 2, multipled, using the * operator? And yes, you might decide the difference is not relevant. But might it be relevant? For example:
x = 1:5;
How should this line be evaluated?
x*2.*x'
ans = 5×5
2 4 6 8 10 4 8 12 16 20 6 12 18 24 30 8 16 24 32 40 10 20 30 40 50
In terms of the order of operations, MATLAB will multiply x. by 2. Then it needs to multiply that result by the vector transpose(x). But should it use the .* operator? Or the * operator? That is, should the result be:
(x*2).*(x')
ans = 5×5
2 4 6 8 10 4 8 12 16 20 6 12 18 24 30 8 16 24 32 40 10 20 30 40 50
In MATLAB, this operation would yield a 5x5 array. But the alternative would be written as
(x*2.) * (x')
ans = 110
And that operation yields a scalar.
The thing is, it very much matters how you interprret that line. Now, consider the expression:
x*2.0.*x
Now there can be no ambiguity, since we know that 2.0 is the number 2. AND that the operator that follows is the .* operator.
Looking at the code you have, again, the 2.0 should always eliminate any confusion.
  4 Kommentare
John D'Errico
John D'Errico am 19 Jan. 2023
The problem is, 2. is an entirely valid way to write a number. You may not love it. As Per points out, some languages use that as a way to distinguish an integer from a floating point number. MATLAB does not really care in that respect, because 2 and 2. are both used to represent the same number, in both cases stored in a double. Regardless, MATLAB needs to use that form to resolve an ambiguity in MATLAB. If the cost lies in some people not liking the way it looks, it was a choice TMW made and therefore something we need to accept. Oh well.
James Akula
James Akula am 19 Jan. 2023
Thank you both for the interesting comments. Just knowing there's a reason makes it less frustrating to see. That said, I'm not sure the real reason has been identified. At least, I don't think its the only reason. Are all the instances of ".0" in the below really disambiguating things? Maybe I misunderstand, but I think not.
syms x
f = 2 + 2*x + 2*x^2 + 2^(x - 2) + 2^(2 - x)
f = 
f = matlabFunction(f)
f = function_handle with value:
@(x)x.*2.0+2.0.^(x-2.0)+2.0.^(-x+2.0)+x.^2.*2.0+2.0
f = sym(f)
f = 
f = simplify(f, 'Steps', 6400)
f = 
f = matlabFunction(f)
f = function_handle with value:
@(x)x.*2.0+cosh((log(2.0).*(x-2.0))./2.0).^2.*4.0+x.^2.*2.0
f = sym(f)
f = 
f = matlabFunction(f)
f = function_handle with value:
@(x)x.*2.0+x.^2.*2.0+cosh(x.*3.465735902799726e-1-6.931471805599453e-1).^2.*4.0
Lots of "interesting" decisions being made there WRT representing values in numerical vs. symbolic space, including when to put, and not to put, a decimal.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by