Weird behaviour - variables arbitrarily rewritten

Executing the following code , variables get reasigned with values form other variables.

x = MxFromString( "{{1,1,1},{1,1,1}}" ) ;
w = MxFromString( "{{1,1}}" );

for( i = 0 ; i < 10 ; i ++ )
{

    z = w@x;	
    t = -1 * z ;
    e = exp( t ) ;
    d = 1 + e;
    y = 1 / d;  

}

image

1 Like

This is what I get for the same code.

image

Hi @aron,

I'm using AB 6.28.0 64bit, and am getting the same results as you - it seems to be occuring randomly - I stepped through your code several times in debug mode, and the unusual result in z occurred 3 times during one run, and only once on the others.

I did the matrix calcs by hand, and got a 3x1 vector of {2,2,2}, which is the same as what appears in z most of the time.

Instantiating z beforehand, with z = Matrix(3, 1, Null) ; outside the for() loop, seems to make no difference.

EDIT1:
Doing some more testing, stepping through it in debug mode again, the problem occurs randomly, but what seems to be happening, is that even though the debugger executes the line with t, for example, one of the other variables also gets changed. It doesn't just happen with the z variable.

Yes, there is a randomness to it, but the frequency is a bit lesser.
Could it be related to load or is it just x64?

@travick - what do you mean by "load".

I don't have any other AB activity happening on my PC, other than (inactive) charts, no other processing in other software, ...

By the way, it's curious that you get array type for e, d, y, yet @aron and I get matrix!

I'm having trouble pasting screen captures into this tool (Discourse), otherwise I'd show you.

That's tested on 6.20 x32 to see if there is a change from then.

I don't run 64bit as it isn't required hence wanted to know various versions and if it varies between bitness ie. 32 or 64

@phase21

variables : z, t, e , are all affected,
and I forgot to mention that I am on version 6.30.0, 64 bit

@travick,

as @phase21 already mentioned all variables are expected to be matrices. This is another anomaly.

Thanks to @fxshrat pointing me to changes in 6.25, I noticed that math functions such as sin(), cos(), log(), ln(), tanh(), sqrt(), etc.. with matrices as argument behave as if argument is passed as reference, modifying arguments' value

image

while passing arguments as reference does not seem to change anything

image

4 Likes

@phase21 What kind of problem it is? Pasting screenshots here is very easy - piece of cake. In some cases you can even drag and drop images straight to the editor. Just remember not to exceed allowed size (currently 1MB)

Here's an example of the screenshot with some more details regarding this topic: :wink:

and it comes from here:

Thank you for this report. This is being investigated. In the meantime a workaround exists. If you call single-argument standard math functions (sqrt, exp, log, sin/cos/tan/atan, etc) with matrices as input you can do this

x = MxFromString( "{{1,1,1},{1,1,1}}" ) ;
w = MxFromString( "{{1,1}}" );

for( i = 0 ; i < 10 ; i ++ )
{
    z = w@x;	
    t = -1 * z ;
    e = exp( t + 0 ) ; // Workaround: create temporary value that is passed as arg
    d = 1 + e;
    y = 1 / d;  
}

UPDATE: The problem was diagnosed. When matrix variable was directly passed to single-argument math function (sqrt, exp, log, trigonometric, etc) its reference count was decreased so it could get freed and re-used prematurely. The above workaround is 100% safe solution for any version. The native fix will be included in 6.30.1 official release, so starting from 6.30.1 you won't need workaround.

6 Likes

Awesome,
Thank you Tomasz.

2 Likes

@Tomasz I suggest taking this opportunity also to update the online reference pages and help/user guide for the single-argument standard math functions, adding the matrices as a possible input.

4 Likes