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: 173
Joined: Fri Jun 23, 2017 3:10 am

Adding elements to matrix programmatically

Post by mcc » Fri Sep 21, 2018 1:14 am

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 v.:3.11. / 3.11. as compiled by SwissMicros

dlachieze
Posts: 137
Joined: Thu May 04, 2017 10:20 am
Location: France

Re: Adding elements to matrix programmatically

Post by dlachieze » Fri Sep 21, 2018 4:30 am

mcc wrote:
Fri Sep 21, 2018 1: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 SN: 00425

User avatar
pica
Posts: 20
Joined: Fri Mar 30, 2018 9:36 am
Location: eSwatini

Re: Adding elements to matrix programmatically

Post by pica » Fri Sep 21, 2018 8:27 am

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?

Thomas Okken
Posts: 486
Joined: Tue May 02, 2017 3:48 pm
Location: New Jersey, USA
Contact:

Re: Adding elements to matrix programmatically

Post by Thomas Okken » Fri Sep 21, 2018 5:14 pm

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: 173
Joined: Fri Jun 23, 2017 3:10 am

Re: Adding elements to matrix programmatically

Post by mcc » Wed Sep 26, 2018 4:32 pm

Hi all,

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

Cheers!
Meino
DM 42 - SN: 00373, Firmware v.:3.11. / 3.11. as compiled by SwissMicros

Post Reply