# HP Forums

Full Version: [42S] Test for COMPLEX matrix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This must've come up before, I'm sure.
Take the sequence:

Code:
```COMPLEX +/- COMPLEX```
It will return the complex conjugate of either a matrix or a number.
If - like me - you're writing code that must run for real input as well as for complex,
you test your input. But REAL? sadly only tests for real numbers, not matrices.

So my question, or challenge if you want, is:
Write a routine that will deliver the complex conjugate of a complex matrix, and will leave a real matrix alone. You cannot EDIT or INDEX the matrix, and you can use only two stack levels.

In: Complex matrix
Out: Complex conjugate matrix

In: real matrix
Out: matrix untouched.

Anyone?

Cheers, Werner
I can do it, but it's not pretty. I hope I'm missing something.
(03-23-2015 02:05 PM)Werner Wrote: [ -> ]But REAL? sadly only tests for real numbers, not matrices

What about ENTER DOT then REAL? or COMPLEX? and LAST x for a copy of the original matrix?
But DOT takes a while to process, especially for larger matrices, so this might not be a good idea...

Cheers,

Gerson.
That's what I meant when I said it wasn't pretty... it's all I've been able to come up with as well.
Werner
(03-23-2015 02:05 PM)Werner Wrote: [ -> ]So my question, or challenge if you want, is:
Write a routine that will deliver the complex conjugate of a complex matrix, and will leave a real matrix alone. You cannot EDIT or INDEX the matrix, and you can use only two stack levels.

In: Complex matrix
Out: Complex conjugate matrix

In: real matrix
Out: matrix untouched.

SF 25
ABS
FS?C 25
GTO 01
COMPLEX
+/-
COMPLEX
RTN
LBL 01
CLX
LASTX
RTN

J-F
Aha, ABS doesn't work on a complex matrix, I didn't know that.
For an NxM matrix:
ENTER DOT will perform N*M operations, in both real and complex cases.
ABS will also perform N*M (simpler) operations AND create a new matrix, but only in the real case. So, better and worse.

Werner
Hi Werner
This is not pretty either,
but perhaps faster

CF 00
X <> "REGS"
STO ST L
CLX
RCL 00
CPX?
SF 00
X <>L
X <> "REGS"
FS? 00
COMPLEX
FS? 00
+/-
FS?C 00
COMPLEX

Can definately be improved...
best regards
Gjermund
Like this? Remember you can use two stack levels
Code:
``` X<> "REGS"  RCL 00  REAL?  GTO 00  XEQ 00  COMPLEX  +/-  COMPLEX  RTN *LBL 00  RDN  X<> "REGS"  RTN```

or this

Code:
``` X<> "REGS"  RCL 00  AOFF  CPX?  AON  RDN  X<> "REGS"  FC? 48  RTN  AOFF  COMPLEX  +/-  COMPLEX  RTN```

Unfortunately, I'm afraid X<> "REGS" makes a copy of the matrix as well, at least I think it does. It doesn't work with a matrix that takes up slightly over half the available memory, it returns 'Insufficient Memory'. Still, I use it in cases where I need access to two matrices at a time.

Cheers, Werner
I see. Like your use og AON as a "free" flag. And it gives me an idea.
How about using the complex matrix descriptor
CLA
ARCL ST X
67
POSA
X>0?
...

Best regards
Gjermund
Yes, that's it!!
No speed penalty, no matrix duplication, and only 2 stack levels.
I would never have thought of that, thanks Gjermund!

Werner
(03-25-2015 10:42 AM)Werner Wrote: [ -> ]Yes, that's it!!
No speed penalty, no matrix duplication, and only 2 stack levels.
I would never have thought of that, thanks Gjermund!

Werner

Yes, a very elegant solution.

But you might have thought of it if you'd asked the question "How do I know it's a complex matrix?".
No, I wasn't aware you could ARCL a matrix.
BTW this may be the time and place to express my admiration for Free42: the excruciating detail in which it mimicks the true 42S is unbelievable. It even implements the hidden [MAX], [MIN] and [FIND] commands. And it also returns NO when trying CPX? on a complex matrix ;-) Fantastic job, Thomas Okken.

Cheers, Werner
The Free42 is really a fantastic piece of software. And running it on a smartphone is amazing. Solving a 1000 degree system is far outside the original design parameters, yet still it works and with high accuracy too!
Sometimes I even contemplate putting my thrusty hp50g in a drawer.
Brgds
Gjermund
So, for posterity, the CMAT? function:
CMAT? returns 1 for a complex matrix, 0 for everything else.

Code:
```         L       X       Y       Z       T       A In:             [[]]    y       z       t Out:    -       0/1     [[]]    y       z       - { 23-Byte Prgm } 00*LBL"CMAT?" 01 CLA 02 MAT? 03 ARCL ST X 04 67 05 POSA 06 X<0? 07 ASTO ST X 08 SIGN 09 END```

Cheers, Werner
Reference URL's
• HP Forums: https://www.hpmuseum.org/forum/index.php
• :