Observation Regarding NEWMAT, STO and PUTMI

87 views
Skip to first unread message

Johan Neven

unread,
May 6, 2025, 3:39:41 AMMay 6
to Free42 & Plus42
While testing a small routine, I noticed something curious that might be of interest. The program below uses NEWMAT to create a new matrix, which is then stored under two different names ("XVec" and "YVec"):  

00 { 75-Byte Prgm }
01▸LBL "TEST"
02 2
03 2
04 NEWMAT
05 STO "XVec"
06 STO "YVec"
07 1
08 1
09 21.234
10 PUTMI "XVec"
11 2
12 2
13 42.654
14 PUTMI "YVec"
15 PRV "XVec"
16 PRV "YVec"
17 RTN
18 .END.

After running this, I observed that both matrices appear to contain the same values, even though each value was written to only one of them.

XVec=     [ 2x2 Matrix ]
1:1=             21.23ᴇ0
1:2=             0.000ᴇ0
2:1=             0.000ᴇ0
2:2=             42.65ᴇ0
YVec=     [ 2x2 Matrix ]
1:1=             21.23ᴇ0
1:2=             0.000ᴇ0
2:1=             0.000ᴇ0
2:2=             42.65ᴇ0

This suggests that "XVec" and "YVec" may both refer to the same matrix object, rather than two distinct copies.

Creating a second matrix with NEWMAT before storing "YVec" results in the expected independent behavior. I'm not sure whether this is by design, but I thought it might be worth sharing.

Kind regards,

Johan.

Thomas Okken

unread,
May 6, 2025, 4:15:17 AMMay 6
to Free42 & Plus42

This suggests that "XVec" and "YVec" may both refer to the same matrix object, rather than two distinct copies.


That is correct, and that is by design. When matrices and lists are copied, the original and the copy both point to the same data object. This saves memory and makes many matrix-related operations much more efficient than they would be otherwise.

But of course this does require that operations that modify matrices make sure to create an actual distinct copy before applying their modification, and the call to make that happen is missing from PUTMI.

I'll put out a release that fixes this right away, for both Free42 and Plus42, since both have this bug.

Vincent Weber

unread,
May 6, 2025, 4:18:04 AMMay 6
to Free42 & Plus42
Hi, 

Interesting. 

I noticed you are using the new function PUTMI, which is not in the original 42S set. If you use INDEX, STOIJ and STOEL instead, you get the expected result. So it must be a bug (or feature?) of the new functions. Thomas will tell us 😊

Cheers

Thomas Okken

unread,
May 6, 2025, 5:35:50 AMMay 6
to Free42 & Plus42
I uploaded bug fix updates to my web site, Free42 3.3.4 and Plus42 1.3.4.
The Android and iOS versions in their relative app stores are currently waiting for approval.

Johan Neven

unread,
May 6, 2025, 8:19:03 AMMay 6
to Free42 & Plus42

Thanks for the suggestion! I tested the INDEX, STOIJ, STOEL sequence alongside PUTMI — and to my surprise, the performance is pretty much the same. But hey, PUTMI lets me skip the whole STOIJ choreography, so I’ll happily stick with the lazy route 😄

Still, it’s great to know there’s more than one way to fill a matrix — or as they say, all roads lead to Rome!

Cheers,

Johan.


Op dinsdag 6 mei 2025 om 10:18:04 UTC+2 schreef vweb...@gmail.com:
Reply all
Reply to author
Forward
0 new messages