1 Pages (4 items)
Assigning vectors to a variable - Assigning vectors to a variable gives incorrect results - Messages
#1 Posted: 3/9/2013 7:07:54 AM
Hello,
I downloaded SMath Studio 2 days ago and I am really having fun using it. Thank you for making it available for everybody.
Currently, I am using it for calculations with vectors. It looks that there is an error, when vector or vector of vectors is assigned to a variable. You can see different results than expected in variables M and N:

I have the latest version of SMath - 0.95 (build: 4594), running on Windows XP.
Thank you for reviewing this behavior, Fanda
I downloaded SMath Studio 2 days ago and I am really having fun using it. Thank you for making it available for everybody.
Currently, I am using it for calculations with vectors. It looks that there is an error, when vector or vector of vectors is assigned to a variable. You can see different results than expected in variables M and N:

I have the latest version of SMath - 0.95 (build: 4594), running on Windows XP.
Thank you for reviewing this behavior, Fanda
#2 Posted: 3/9/2013 7:46:52 AM
Hello,
the problem is in the default optimization setting of the definition of M. By default, optimization is set to "symbolic" in definitions (assignments with :=) and to "numeric" in evaluations by "=". As such, this should not create any problems if the symbolic engine was reliable. Unfortunately it is not. In particular, the engine is unaware of the different meaning of the multiplication dots depending on operator type. You can see that by removing the defintions of the vectors and hovering with the mouse over the definition of M. As you see, the braces are gone.
If you switch to numeric in the context menu, you get the correct result.
This is not yet the whole story. If you set a defintion to symbolic and evaluate it later in the sheet, the value depends on the variables as defined at the place, where the evaluation is made. That means, if you change the vectors a, b or c later, M has different ddisplayed values beyond that changes.
Now, if your definition is set to numeric, then two things may happen:
a ) all variables in the definition have a value, then M is evaluated immediately and the result is stored as value of M.
Later modifications of the variables do not have any effect.
b ) at least one variable is undefined. Then the definition is stored as given (without spoiling it by wrong simplifications) and you get the expected behaviour.
Again, all this can be seen in the dynamic assistant or in the definition preview (mouse hovering)

the problem is in the default optimization setting of the definition of M. By default, optimization is set to "symbolic" in definitions (assignments with :=) and to "numeric" in evaluations by "=". As such, this should not create any problems if the symbolic engine was reliable. Unfortunately it is not. In particular, the engine is unaware of the different meaning of the multiplication dots depending on operator type. You can see that by removing the defintions of the vectors and hovering with the mouse over the definition of M. As you see, the braces are gone.
If you switch to numeric in the context menu, you get the correct result.
This is not yet the whole story. If you set a defintion to symbolic and evaluate it later in the sheet, the value depends on the variables as defined at the place, where the evaluation is made. That means, if you change the vectors a, b or c later, M has different ddisplayed values beyond that changes.
Now, if your definition is set to numeric, then two things may happen:
a ) all variables in the definition have a value, then M is evaluated immediately and the result is stored as value of M.
Later modifications of the variables do not have any effect.
b ) at least one variable is undefined. Then the definition is stored as given (without spoiling it by wrong simplifications) and you get the expected behaviour.
Again, all this can be seen in the dynamic assistant or in the definition preview (mouse hovering)
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#3 Posted: 3/9/2013 9:09:58 AM
Thank you for answering, I understand the problem now.
As a workaround, I tried:
[MATH lang=eng]M2:mat((a*b)*c,c*(a*b+0),2,1)[/MATH]
Without a,b,c defined, hovering over shows the same simplification, but the numerical result is correct.
Also, when I set the Optimization on M to None, brackets stay and the result is also correct.
Greetings, Fanda
As a workaround, I tried:
[MATH lang=eng]M2:mat((a*b)*c,c*(a*b+0),2,1)[/MATH]
Without a,b,c defined, hovering over shows the same simplification, but the numerical result is correct.
Also, when I set the Optimization on M to None, brackets stay and the result is also correct.
Greetings, Fanda
#4 Posted: 3/11/2013 4:06:20 PM
If there was a dot product operator just for vectors, we could write
a * (b dot c)
and symbolic engine would not simplify it to
a * b * c
which means
(a dot b ) * c
Another workaround therefore is to define a dot product on our own:
[MATH lang=eng]dot(u,v):sum(el(u,k)*el(v,k),k,1,rows(u))[/MATH]
And we get what we were looking for:
[MATH lang=eng]M3:mat(dot(a,b)*c,c*dot(a,b),2,1)[/MATH]
[MATH lang=eng]M3=mat(mat(-30,30,30,3,1),mat(-30,30,30,3,1),2,1)[/MATH]
PS: Simpler version [MATH lang=eng]dot(u,v):u*v[/MATH] didn't work - it is also substituted and we get a wrong result.
a * (b dot c)
and symbolic engine would not simplify it to
a * b * c
which means
(a dot b ) * c
Another workaround therefore is to define a dot product on our own:
[MATH lang=eng]dot(u,v):sum(el(u,k)*el(v,k),k,1,rows(u))[/MATH]
And we get what we were looking for:
[MATH lang=eng]M3:mat(dot(a,b)*c,c*dot(a,b),2,1)[/MATH]
[MATH lang=eng]M3=mat(mat(-30,30,30,3,1),mat(-30,30,30,3,1),2,1)[/MATH]
PS: Simpler version [MATH lang=eng]dot(u,v):u*v[/MATH] didn't work - it is also substituted and we get a wrong result.
1 Pages (4 items)
-
New Posts
-
No New Posts