Multiply of a numeric vector times a unit fails.

Multiply of a numeric vector times a unit fails. - Messages

#1 Posted: 6/17/2025 4:34:26 PM
Martin Kraska

Martin Kraska

1,239 likes in 2,163 posts.

Group: Moderator

In the following worksheet the variable R points to a vector of numerical values (there is a glitch in the preview).

Multiplying this vector times a unit fails.

Mitigation is possible when using vectorize() or the custom simp() function. Yet it is strange why these tricks should be used. Multiplying a numerical vector times a scalar unit usually is no problem. Any idea?

1-2-Maßeinheiten.sm (116 KiB) downloaded 21 time(s).

image.png
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#2 Posted: 6/17/2025 6:16:37 PM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

1,011 likes in 1,680 posts.

Group: User

Hi Martin. You can use eval to avoid the error.

image.png

Best regards.
Alvaro.
2 users liked this post
sergio 6/17/2025 8:08:15 PM, Martin Kraska 6/17/2025 8:36:46 PM
#3 Posted: 6/17/2025 7:43:34 PM
Martin Kraska

Martin Kraska

1,239 likes in 2,163 posts.

Group: Moderator

Thanks, Alvaro.

Just like vectorize() this is another workaround. What is special with my vector R? It is stored as a column vector of numbers. Multiplying this with a unit should not require poking around for workarounds. I consider the behaviour being a bug.

Possibly, the preview (and also the storage display in the debugger) are unreliable and there is some hidden issue in the stored value of the variable R. That, however, would be a much bigger problem, because referring to the stored value for understanding what happens, would be at least partially pointless.

image.png
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
3 users liked this post
Alvaro Diaz Falconi 6/18/2025 12:16:36 PM, Davide Carpi 6/17/2025 10:46:58 PM, sergio 6/17/2025 8:08:30 PM
#4 Posted: 6/18/2025 2:55:03 AM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

1,011 likes in 1,680 posts.

Group: User

Wrote

... Just like vectorize() this is another workaround. What is special with my vector R? It is stored as a column vector of numbers. Multiplying this with a unit should not require poking around for workarounds. I consider the behaviour being a bug.


Yes, you're right: using eval is just another workaround, like the others that you present. I thought this might be a solution to the problem, but it really isn't. Perhaps I thought this because of the habit of resorting to evalb (eval as boolean) and evalm (eval as matrix) in older versions of Maple.

Wrote

... Possibly, the preview (and also the storage display in the debugger) are unreliable and there is some hidden issue in the stored value of the variable R. That, however, would be a much bigger problem, because referring to the stored value for understanding what happens, would be at least partially pointless.


If there's a bug, it might be in the multiple assignment. Using an intermediate variable, you can see why the dimensions don't match. However, since SMath doesn't run the calculations unless explicitly requested with eval, it's not clear why it works with eval(R)*'N, when by that theory it should be eval(R*'N). In the following image, all the optimizations are numerical.

image.png

Best regards.
Alvaro.
Edited 6/18/2025 2:55:43 AM
1 users liked this post
Martin Kraska 6/18/2025 9:29:29 PM
#5 Posted: 6/18/2025 9:27:37 PM
Martin Kraska

Martin Kraska

1,239 likes in 2,163 posts.

Group: Moderator

Wrote


If there's a bug, it might be in the multiple assignment. Using an intermediate variable, you can see why the dimensions don't match. However, since SMath doesn't run the calculations unless explicitly requested with eval, it's not clear why it works with eval(R)*'N, when by that theory it should be eval(R*'N). In the following image, all the optimizations are numerical.

image.png

Best regards.
Alvaro.




I think that the multiple assignment can't be blamed per se, because in my previous post the multiple assignment works without eval() or other tricks (demonstrated with the vector v). So my question still is the same: What makes my vector so special that the behaviour is different?
I think this is important for a predictable user experience.
Edited 6/18/2025 9:28:00 PM
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
1 users liked this post
Alvaro Diaz Falconi 6/20/2025 7:18:30 AM
#6 Posted: 6/19/2025 2:53:32 PM
overlord

overlord

564 likes in 1,338 posts.

Group: Moderator

Can roots() be the problem?
Actually, only solve() can do it right.

2025-06-19_17-57.png
Edited 6/19/2025 2:57:38 PM
#7 Posted: 6/20/2025 10:04:30 AM
Martin Kraska

Martin Kraska

1,239 likes in 2,163 posts.

Group: Moderator

Wrote

Can roots() be the problem?
Actually, only solve() can do it right.



I don't think that roots() or FindRoots() are to blame here. Other than solve() they don't scan a range for multiple solutions. Based on an initial guess they just return a single solution just like the Newton-Raphson algorithm. The dimension of the solution of roots() and FindRoots() matches the dimension of the vector of variables. Given a single scalar equation and a single variable there is no vector result to be expected, thus assigning the result to a vector of variables must fail.

Assignment of the solve() result succeeds because the equation happens to have two solutions in the search region (implicitly given by the UI setting).

Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
  • New Posts New Posts
  • No New Posts No New Posts