# HP Forums

Full Version: HP15C strange complex number behavior
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I have a program to add n complex numbers, stored as magnitude and phase in the columns of an n x 2 matrix. It converts them one at a time to rectangular format and adds them to the stored sum. I'm setting register I based on the matrix dimension and using ISG to end the loop.

In each case, on the last step of the loop, the "I" key to move the value to the imaginary location fails. It doesn't do this if I perform all the calculations manually, only with the program, and only on the last row of the matrix. The problem seems independent of the matrix size. The sum is correct if I force the program to terminate on the next-to-last row rather than the last row of the matrix.

Here is the program, programmed and run in user mode. This is my first post, so I apologize if it doesn't conform to the nomenclature usually used here.

```LBL E
RCL DIM A   "Get the matrix dimensions"
X <> Y      "Move the number of rows into X"
.
0
0
1
*           "Multiply by .001 to set the loop counter
1
+
STO I       "Add 1 and store the loop counter as 1.00n in Reg I"
MATRIX 1    "Move to beginning of matrix"
0
ENTER       "Initialize the sum"
LBL 1       "Start of loop"
RCL A       "Recall magnitude from first column of matrix"
RCL A       "Recall phase from second column"
I           "Move into imaginary part of X"
-> R        "Convert to rectangular"
ISG I       "Check counter for loop end"
GTO 1       "Loop"
-> P        "Convert sum to polar form when finished"
RTN
```

To run the program, dimension the A matrix and load the complex numbers into it, then press "E" in user mode to execute the program. On the last row, it fails to move the second column into the imaginary part of x, instead adding it as a real number to the real part of x.

Anyone know what's going on here? This happens on my HP15C LE and on my DM-15CC. Haven't tried it yet on my original HP15C.

If I replace the 1 in the 9th instruction with 2, so that 2.00n is loaded into reg I and the loop terminates before reaching the last row of the matrix, the sum of the first n-1 rows is correct.

Mike

The second "RCL A" is skipping the next instruction when it hits the end of the matrix. When programmed in user mode, RCL and STO when applied to a matrix not only updates the row/column indices but skips the next instruction when running off the end of the matrix (sort of like ISG/DSE when hitting the end of a loop).

Quote:
The second "RCL A" is skipping the next instruction when it hits the end of the matrix. When programmed in user mode, RCL and STO when applied to a matrix not only updates the row/column indices but skips the next instruction when running off the end of the matrix (sort of like ISG/DSE when hitting the end of a loop).

Excellent information! Thanks!

I'll have to see if I can rewrite this to automatically detect the end of the matrix and eliminate querying the dimension at the beginning of the routine.

Mike

Hi.

The very first attempt on making it work is to add a 'ghost' label - an unused label - after the second RCL A. This way, whether it is 'wrapping' the matrix - i.e. jumping from the las element to the first one - or in the middle of it, the ghost label will be either skipped or 'executed', and in each case it will not disturb program execution. All you'll have is a bit slower execution for each time the 'ghost' label is found it will be 'executed'.

Make sure this label is not used elsewhere, or else it will be reached when not needed.

About the RCL/STO behavior in user mode: try p. 176 under 'Using Matrix Operation in a Program' and p. 177 (my HP15C manual is 00015-90001 Rev. E, February 1982), the first diagram, and you'll see the reference for using USER RCL/STO in a program. Also, p.144, last paragraph before the Example, bottom of the page:

Quote:
After the last element of the matrix has been accessed, the row and column numbers both return to 1.
In the HP15C LE manual you'll find it at the very same page.

One small trick: replace

```.
0
0
1
×```
to
```EEX
3
÷ (divide)```
and you'll save two bytes with the same operation. 8^)

Cheers.

Luiz (Brazil)

Edited: 11 Sept 2012, 12:43 a.m.

Please, check if this listing works the same:

```LBL E
MATRIX 1    "Move to beginning of matrix"
0
ENTER
ENTER       "Initialize the sum"
LBL 1       "Start of loop"
I           "Move into imaginary part of X"
-> R        "Convert to rectangular"
RCL A       "Recall magnitude from first column of matrix"
RCL A       "Recall phase from second column"
GTO 1       "Loop"
I           "if end of matrix, GTO 1 is skipped and program continues here"
->R         "repeats the three steps for adding last vector (unfortunately needed)
+
-> P        "Convert sum to polar form when finished"
RTN```
It is shorter because it does not use register [I], only the skipping feature after the last element is reached. I needed to repeat the three steps for adding the last vector, which is not elegant, but this listing is still 7 steps shorter. Note that I did not check it by running it, chances are it will not work.

Cheers.

Luiz (Brazil)

Edited: 11 Sept 2012, 12:44 a.m.

Quote:
Please, check if this listing works the same:
```LBL E
MATRIX 1    "Move to beginning of matrix"
0
ENTER
ENTER       "Initialize the sum"
LBL 1       "Start of loop"
I           "Move into imaginary part of X"
-> R        "Convert to rectangular"
RCL A       "Recall magnitude from first column of matrix"
RCL A       "Recall phase from second column"
GTO 1       "Loop"
I           "if end of matrix, GTO 1 is skipped and program continues here"
->R         "repeats the three steps for adding last vector (unfortunately needed)
+
-> P        "Convert sum to polar form when finished"
RTN```
It is shorter because it does not use register [I], only the skipping feature after the last element is reached. I needed to repeat the three steps for adding the last vector, which is not elegant, but this listing is still 7 steps shorter. Note that I did not check it by running it, chances are it will not work.

Cheers.

Luiz (Brazil)

Worked like a charm. Thanks, Luiz.

Mike