Post Reply 
[42S] Test for COMPLEX matrix
03-23-2015, 02:05 PM
Post: #1
[42S] Test for COMPLEX matrix
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.

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
03-23-2015, 03:17 PM
Post: #2
RE: [42S] Test for COMPLEX matrix
(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.
Find all posts by this user
Quote this message in a reply
03-23-2015, 06:05 PM
Post: #3
RE: [42S] Test for COMPLEX matrix
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

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
03-23-2015, 07:44 PM
Post: #4
RE: [42S] Test for COMPLEX matrix
(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.

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

J-F
Visit this user's website Find all posts by this user
Quote this message in a reply
03-24-2015, 07:12 AM
Post: #5
RE: [42S] Test for COMPLEX matrix
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

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
03-24-2015, 06:36 PM
Post: #6
RE: [42S] Test for COMPLEX matrix
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
Find all posts by this user
Quote this message in a reply
03-24-2015, 08:11 PM
Post: #7
RE: [42S] Test for COMPLEX matrix
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

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
03-25-2015, 08:39 AM
Post: #8
RE: [42S] Test for COMPLEX matrix
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
Find all posts by this user
Quote this message in a reply
03-25-2015, 10:42 AM
Post: #9
RE: [42S] Test for COMPLEX matrix
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

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
03-25-2015, 11:03 AM (This post was last modified: 03-25-2015 11:03 AM by Gerald H.)
Post: #10
RE: [42S] Test for COMPLEX matrix
(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?".
Find all posts by this user
Quote this message in a reply
03-25-2015, 03:54 PM
Post: #11
RE: [42S] Test for 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

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
03-25-2015, 06:42 PM
Post: #12
RE: [42S] Test for COMPLEX matrix
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
Find all posts by this user
Quote this message in a reply
03-26-2015, 12:27 PM
Post: #13
RE: [42S] Test for COMPLEX matrix
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

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)