Adding elements to matrix programmatically

Post here to share useful tips and tricks, to ask questions about using your DM42 or to report software-related problems
Post Reply
mcc
Posts: 277
Joined: Fri Jun 23, 2017 5:10 am

Adding elements to matrix programmatically

Post by mcc »

Hi,

currently I am programming a integer factorization program for fun.
The resulting factors including the integer value being factorization
as first element should be inserted into a [n,1] matix an passed to
the user when the program has done its work.

Roughly I do this:
GROW
1
1
DIM "factors"
INDEX "factors"
...
RCL01
->
...
LBL 00
<do factorization of one factor here>
RCL "factor"
->
...
GTO 00

But I am always ending with a additional "0" in the matrix.

Since the loop should run as fast as possible I want to
avoid expensive handling of I and J inside it.

Is there a way to insert an element into the matrix by using GROW mode
without creating a new one afterwards ?
Is there a way to first create a new empty matrix element and then insert
the new value right into it?

Thanks a lot for any help in advance!
Cheers,
Meino
DM 42 - SN: 00373, Firmware release v.:3.22. / DMCP 3.24. as compiled by SwissMicros
dlachieze
Posts: 613
Joined: Thu May 04, 2017 12:20 pm
Location: France

Re: Adding elements to matrix programmatically

Post by dlachieze »

mcc wrote:
Fri Sep 21, 2018 3:14 am
Is there a way to insert an element into the matrix by using GROW mode
without creating a new one afterwards ?
Is there a way to first create a new empty matrix element and then insert
the new value right into it?
I would say no to these two questions, but you can simply add DELR at the end of your loop, after GTO 00.
DM42: 00425 - DM41X: β00066 - WP43: 00042
User avatar
pica
Posts: 70
Joined: Fri Mar 30, 2018 11:36 am
Location: Eswatini

Re: Adding elements to matrix programmatically

Post by pica »

1. You can avoid the extra row by careful placing of the -> commands in your program, since that is what creates a new row when the last one is full.

2. Can't you create a new matrix using by placing the rows and columns in the x and y registers, and using the NEWMAT command?
HP50G HP35s Free42 DM42.
Testing WP43S, C43.
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Adding elements to matrix programmatically

Post by Thomas Okken »

You can prevent the creation of the superfluous row by creating new rows just before you need them, and skipping that step on the first iteration since matrices always start out with at least one row:

1
1
DIM "factors"
INDEX "factors"
GROW
SF 00

... start loop ...

FC?C 00
J+
STOEL

... end loop ...

Note the use of J+ and STOEL, instead of →, so you don't have to worry about the contents of the previous element.
mcc
Posts: 277
Joined: Fri Jun 23, 2017 5:10 am

Re: Adding elements to matrix programmatically

Post by mcc »

Hi all,

thanks a lot for you help.
I will enter (it) The Matrix, hehehe :::)))

Cheers!
Meino
DM 42 - SN: 00373, Firmware release v.:3.22. / DMCP 3.24. as compiled by SwissMicros
Post Reply