1 страниц (5 вхождений)
local vars inside loops problem? - Сообщения
#1 Опубликовано: 25.02.2012 16:47:47
Hello,
Here is a picture of one of my test files (SMath 0.92)

Take a closer look please. It does not seem to be any problem. Actually, the problem is that the two function calls (bordered regions) give the same results (look ate the vectors "b" please) although the function Xmat() was called with different arguments. The problem is that the variable with the same name "X" appeared in the function Xmat() definition as a returning value and as a result in the two callings (bordered region). I do not know why is this happening. The resolution of the problem is to put X:=0 or X:=matrix(n,k) or something else for X as a first statement in the function Xmat() definition.

I do not understand this because matrix "X" appeared as a local variable in the function definition (although inside loops) and in spite of this it makes problems.
Here is the file> needs matrix().sm
I suppose this might be regarded as a bug.
Regards,
Radovan
Here is a picture of one of my test files (SMath 0.92)

Take a closer look please. It does not seem to be any problem. Actually, the problem is that the two function calls (bordered regions) give the same results (look ate the vectors "b" please) although the function Xmat() was called with different arguments. The problem is that the variable with the same name "X" appeared in the function Xmat() definition as a returning value and as a result in the two callings (bordered region). I do not know why is this happening. The resolution of the problem is to put X:=0 or X:=matrix(n,k) or something else for X as a first statement in the function Xmat() definition.

I do not understand this because matrix "X" appeared as a local variable in the function definition (although inside loops) and in spite of this it makes problems.
Here is the file> needs matrix().sm
I suppose this might be regarded as a bug.
Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#2 Опубликовано: 25.02.2012 17:21:26
Regards.
#3 Опубликовано: 25.02.2012 18:17:30
Thanks Andrey,
As far as I understood, if a want a vector/matrix to be local inside function definition or defining a variable by using line(), and in order not to make a mess up with them - it is not advisable to have any assignment of a vector/matrix element by itself before we put some assignments to all the vector elements at once. We should preserve any problem of this kind by simply assign something to the entire vector/matrix at once (maybe just give to all the vector/matrix elements zero values first - something like A:=0 or A:=matrix(n,k)). By assigning vector/matrix particular elements only, the vector/matrix is still global - not local. In my example the second call of the Xmat() just give the results from the first call - as the variable "X" become a global variable on the first call. I hope I am right about it.
To be honest, my example still puzzled me. Take a look at this please.

It is still working correctly in spite of that that the variable "something" is global and undefined? How that can be?
Regards,
Radovan
As far as I understood, if a want a vector/matrix to be local inside function definition or defining a variable by using line(), and in order not to make a mess up with them - it is not advisable to have any assignment of a vector/matrix element by itself before we put some assignments to all the vector elements at once. We should preserve any problem of this kind by simply assign something to the entire vector/matrix at once (maybe just give to all the vector/matrix elements zero values first - something like A:=0 or A:=matrix(n,k)). By assigning vector/matrix particular elements only, the vector/matrix is still global - not local. In my example the second call of the Xmat() just give the results from the first call - as the variable "X" become a global variable on the first call. I hope I am right about it.
To be honest, my example still puzzled me. Take a look at this please.

It is still working correctly in spite of that that the variable "something" is global and undefined? How that can be?
Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#4 Опубликовано: 25.02.2012 19:38:25
This is because all the expressions with errors just don't used in evaluations at all.
And it doesn't matter if you test it within the line(..) or simply on the worksheet. Please see the screenshot below:

Regards.
And it doesn't matter if you test it within the line(..) or simply on the worksheet. Please see the screenshot below:
Regards.
1 пользователям понравился этот пост
Radovan Omorjan 25.02.2012 19:46:00
#5 Опубликовано: 26.02.2012 04:14:17
Hello Andrey,
I am following your comment and just to make this clear to myself - and maybe to other users - here is another example:
Here "a" changed but only with those "x" elements not defined by "a". Here "x" still have the influence on "a".

Here we first "destroy" the variable "x" in the definition of "a" - we could do that even with a nonexisting variable (actually "x" becomes undefined then). This way changing of "x" will not have any effect on "a".

To make long story short, if we want to use vector/matrix locally not influencing other global variables - we have to "destroy" it first.
Regards,
Radovan
I am following your comment and just to make this clear to myself - and maybe to other users - here is another example:
Here "a" changed but only with those "x" elements not defined by "a". Here "x" still have the influence on "a".

Here we first "destroy" the variable "x" in the definition of "a" - we could do that even with a nonexisting variable (actually "x" becomes undefined then). This way changing of "x" will not have any effect on "a".

To make long story short, if we want to use vector/matrix locally not influencing other global variables - we have to "destroy" it first.
Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
1 пользователям понравился этот пост
Andrey Ivashov 26.02.2012 05:46:00
1 страниц (5 вхождений)
-
Новые сообщения
-
Нет новых сообщений