Page 1 of 1

Matrix in program

Posted: Wed Jan 26, 2022 1:13 pm
by rudi
Is it possible to somehow include a matrix in a program?

Something like this:

Code: Select all

01 LBL "TEST"
02 MATRIX[[0,1,2],[3,4,5],[6,7,8]]
03 END

Re: Matrix in program

Posted: Wed Jan 26, 2022 8:08 pm
by Thomas Okken
No, the best you can do is use the usual matrix functions to create and populate the matrix. Which is still pretty efficient, but not very elegant:

Code: Select all

00 { 48-Byte Prgm }
01▸LBL "M"
02 3
03 ENTER
04 NEWMAT
05 EDIT
06 0
07 →
08 1
09 →
10 2
11 →
12 3
13 →
14 4
15 →
16 5
17 →
18 6
19 →
20 7
21 →
22 8
23 EXITALL
24 END

Re: Matrix in program

Posted: Thu Jan 27, 2022 6:18 am
by rudi
Thanks Thomas, just as I figured ;-) Was hoping, that there was a new smart feature in DM42 that I had overlooked ;-)
I need a rather large matrix (1x1000+) to store some hard coded numbers in and hoped for the "elegant" solution ;-)

But I guess that I can store a lazy initialized matrix in memory, and create it at runtime if it is missing.

Code: Select all

01 LBL "TEST"
02 SF 25    @Ignore errors
03 RCL "MatrixX" @Will fail if Nonexistent, but how to detect?
...
04 if "MatrixX" doesn't exist, then
05 XEQ 00  @create and initialize "MatrixX"
However, even if it is possible, I do not like this solution, since it ends up having the numbers doubled up in memory, 1x in the code and 1x in the matrix. I will try thinking of another way.

Re: Matrix in program

Posted: Thu Jan 27, 2022 8:04 am
by Florian
Quoting from the HP-42S Owner's Manual, Appendix C: Flags
You can detect an error by setting flag 25 just before an instruction
and then testing the flag after the instruction to see if it has been
cleared. (Generally, you should test and clear flag 25—you risk los-
ing data if you choose to ignore unanticipated errors.) This enables
a program to branch rather than to stop execution in case of an
error.
:D

Re: Matrix in program

Posted: Thu Jan 27, 2022 8:23 am
by rudi
Thanks Florian - embarasing that I didn't remember this and even more, that I didn't bother to read the HP-42S manual ;-)
I'll use FC?C to ensure that Flag 25 is cleared after the test, to avoid it remaining set and causing undesired errors ahead.

Code: Select all

01 LBL "TEST"
02 SF 25    @Ignore errors
03 RCL "MatrixX"
04 FC?C 25
05 XEQ 00  @create and initialize "MatrixX"
...

Re: Matrix in program

Posted: Thu Jan 27, 2022 9:10 am
by Florian
Glad I could be of help.

Still, I am with you in hoping that one day there might be a more ... compact ... way of including matrix data in programs. :)

Re: Matrix in program

Posted: Thu Jan 27, 2022 10:09 pm
by Guenter
rudi wrote:
Thu Jan 27, 2022 6:18 am

However, even if it is possible, I do not like this solution, since it ends up having the numbers doubled up in memory, 1x in the code and 1x in the matrix. I will try thinking of another way.
I think you could create a program just for creating the hard-coded matrix. Once it is done you could delete this program, backup on your computer in case ... Then you have the matrix available as a variable and any program can access it as you like.

Günter

Re: Matrix in program

Posted: Wed Feb 02, 2022 5:26 am
by bob38
How to Add a Big Matrix to the DM42 from a Computer

Reference: https://thomasokken.com/free42
"Loading, Saving, and Switching States"
"The Hidden Powers of Copy and Paste"
"Matrices"

Well, I wouldn't want to have to do this frequently, but it should have worked for
my purposes, except that at Step #12 below I got the DM42 error "State File Too New".
I'm guessing that I have to install a more recent version of something on my DM42,
which I'll do when my mind is a little fresher.

This is what I did:

1. Compose/save the matrix in file "mymatrix.tsv" in any text editor on my computer.
Columns are tab-delimited, rows are newline-delimited.
2. Save the state of my DM42 to file "OLD.f42" on my DM42.
3. Activate USB disk between my DM42 and my computer.
4. Copy file "OLD.f42" to my computer.
5. Launch Free42 (either "Decimal" or "Binary") on my computer.
6. Under menu "File / States...", Import file "OLD.f42".
7. In the text editor on my computer, copy file "mymatrix.tsv" to the copy/paste buffer.
8. Paste to Free42. Matrix in now in X on Free42.
9. Optional step: store X in some global variable in Free42.
10. Save the state of Free42 to file "NEW.f42" on my computer.
11. Copy file "NEW.f42" from computer to DM42 via USB disk.
12. Deactivate USB disk between computer and DM42.
13. Restore the state of my DM42 from file "NEW.f42".
The only change to the DM42 is the new matrix.
14. Cleanup: delete files "OLD.f42" & "NEW.f42" on DM42.

Re: Matrix in program

Posted: Sat Feb 05, 2022 1:10 am
by bob38
How to Add a Big Matrix to the DM42 from a Computer

Reference: https://thomasokken.com/free42
"Loading, Saving, and Switching States"
"The Hidden Powers of Copy and Paste"
"Matrices"

Well, I wouldn't want to have to do this frequently, but it should have worked for
my purposes, except that at Step #12 below I got the DM42 error "State File Too New".
I'm guessing that I have to install a more recent version of something on my DM42,
which I'll do when my mind is a little fresher.

This is what I did:

1. Compose/save the matrix in file "mymatrix.tsv" in any text editor on my computer.
Columns are tab-delimited, rows are newline-delimited.
2. Save the state of my DM42 to file "OLD.f42" on my DM42.
3. Activate USB disk between my DM42 and my computer.
4. Copy file "OLD.f42" to my computer.
5. Launch Free42 (either "Decimal" or "Binary") on my computer.
6. Under menu "File / States...", Import file "OLD.f42".
7. In the text editor on my computer, copy file "mymatrix.tsv" to the copy/paste buffer.
8. Paste to Free42. Matrix in now in X on Free42.
9. Optional step: store X in some global variable in Free42.
10. Save the state of Free42 to file "NEW.f42" on my computer.
11. Copy file "NEW.f42" from computer to DM42 via USB disk.
12. Deactivate USB disk between computer and DM42.
13. Restore the state of my DM42 from file "NEW.f42".
The only change to the DM42 is the new matrix.
14. Cleanup: delete files "OLD.f42" & "NEW.f42" on DM42.