Complex matrix inversion with issues

Discussion around the SwissMicros DM42 calculator
jneven
Posts: 12
Joined: Sat Jan 23, 2021 6:32 pm
Location: Belgium

Complex matrix inversion with issues

Post by jneven »

Executing (on a DM42 with newest firmware) INVRT on a square (5x5) complex matrix gives a wrong result. When multiplying this result again with the original complex matrix, the result is not the unity matrix. Inverting the same complex matrix with another algorithm gives correct results but is not very optimal is space and time.

I don't see any option to give an attachment in this forum, so how can I send the matrix in question to allow for further inspection.

Kind regards.

Johan.

Here the copy/paste buffer of the matrix in question.

Code: Select all

0.001+0i	0+0i	-0.0005+0i	-0.0005+0i	0+0i
0+0i	1+9.999997292456363677763963228030219e-2i	-1+0i	0-9.999997292456363677763963228030219e-2i	0+0i
-0.0005+0i	-1+0i	1.0005+1.999999458491272735552792645606044i	0+0i	0-1.999999458491272735552792645606044i
-0.0005+0i	0-9.999997292456363677763963228030219e-2i	0+0i	2.46078431372549019607843137254902e-3+9.999997292456363677763963228030219e-2i	-1.96078431372549019607843137254902e-3+0i
0+0i	0+0i	0-1.999999458491272735552792645606044i	-1.96078431372549019607843137254902e-3+0i	3.670186023127199597787833081950729e-3+1.999999458491272735552792645606044i
whuyse
Posts: 198
Joined: Thu Dec 21, 2017 1:23 pm

Re: Complex matrix inversion with issues

Post by whuyse »

Update: even Free42 v3 has the problem.
e.g. A*inv(A)[2,1] = -1.2+i24.5e-3 instead of zero, or close.
In CODE format, to easily turn into a RAW file:

Code: Select all

00 { 688-Byte Prgm }
01▸LBL "CM"
02 5
03 ENTER
04 NEWMAT
05 ENTER
06 COMPLEX
07 EDIT
08 0.001
09 0
10 COMPLEX
11 →
12 0
13 0
14 COMPLEX
15 →
16 -0.0005
17 0
18 COMPLEX
19 →
20 -0.0005
21 0
22 COMPLEX
23 →
24 0
25 0
26 COMPLEX
27 →
28 0
29 0
30 COMPLEX
31 →
32 1
33 9.999997292456363677763963228030219ᴇ-2
34 COMPLEX
35 →
36 -1
37 0
38 COMPLEX
39 →
40 0
41 -9.999997292456363677763963228030219ᴇ-2
42 COMPLEX
43 →
44 0
45 0
46 COMPLEX
47 →
48 -0.0005
49 0
50 COMPLEX
51 →
52 -1
53 0
54 COMPLEX
55 →
56 1.0005
57 1.999999458491272735552792645606044
58 COMPLEX
59 →
60 0
61 0
62 COMPLEX
63 →
64 0
65 -1.999999458491272735552792645606044
66 COMPLEX
67 →
68 -0.0005
69 0
70 COMPLEX
71 →
72 0
73 -9.999997292456363677763963228030219ᴇ-2
74 COMPLEX
75 →
76 0
77 0
78 COMPLEX
79 →
80 2.46078431372549019607843137254902ᴇ-3
81 9.999997292456363677763963228030219ᴇ-2
82 COMPLEX
83 →
84 -1.96078431372549019607843137254902ᴇ-3
85 0
86 COMPLEX
87 →
88 0
89 0
90 COMPLEX
91 →
92 0
93 0
94 COMPLEX
95 →
96 0
97 -1.999999458491272735552792645606044
98 COMPLEX
99 →
100 -1.96078431372549019607843137254902ᴇ-3
101 0
102 COMPLEX
103 →
104 3.670186023127199597787833081950729ᴇ-3
105 1.999999458491272735552792645606044
106 COMPLEX
107 →
108 EXITALL
109 END
And there is indeed a problem.
But perhaps it is this, release notes from Free42 v2.5.21:
* Complex matrix division, INVRT, and DET would return incorrect results for
large matrices (20x20 or greater). Fixed.
Even if this is only a 5x5. And the DM42 has Free42 v2.5.20..
Thomas, what was the problem?

Werner
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
jneven
Posts: 12
Joined: Sat Jan 23, 2021 6:32 pm
Location: Belgium

Re: Complex matrix inversion with issues

Post by jneven »

whuyse wrote:
Thu Mar 04, 2021 6:07 pm
Update: even Free42 v3 has the problem.
e.g. A*inv(A)[2,1] = -1.2+i24.5e-3 instead of zero, or close.
In CODE format, to easily turn into a RAW file:

Code: Select all

00 { 688-Byte Prgm }
01▸LBL "CM"
02 5
03 ENTER
04 NEWMAT
05 ENTER
06 COMPLEX
07 EDIT
08 0.001
09 0
10 COMPLEX
11 →
12 0
13 0
14 COMPLEX
15 →
16 -0.0005
17 0
18 COMPLEX
19 →
20 -0.0005
21 0
22 COMPLEX
23 →
24 0
25 0
26 COMPLEX
27 →
28 0
29 0
30 COMPLEX
31 →
32 1
33 9.999997292456363677763963228030219ᴇ-2
34 COMPLEX
35 →
36 -1
37 0
38 COMPLEX
39 →
40 0
41 -9.999997292456363677763963228030219ᴇ-2
42 COMPLEX
43 →
44 0
45 0
46 COMPLEX
47 →
48 -0.0005
49 0
50 COMPLEX
51 →
52 -1
53 0
54 COMPLEX
55 →
56 1.0005
57 1.999999458491272735552792645606044
58 COMPLEX
59 →
60 0
61 0
62 COMPLEX
63 →
64 0
65 -1.999999458491272735552792645606044
66 COMPLEX
67 →
68 -0.0005
69 0
70 COMPLEX
71 →
72 0
73 -9.999997292456363677763963228030219ᴇ-2
74 COMPLEX
75 →
76 0
77 0
78 COMPLEX
79 →
80 2.46078431372549019607843137254902ᴇ-3
81 9.999997292456363677763963228030219ᴇ-2
82 COMPLEX
83 →
84 -1.96078431372549019607843137254902ᴇ-3
85 0
86 COMPLEX
87 →
88 0
89 0
90 COMPLEX
91 →
92 0
93 0
94 COMPLEX
95 →
96 0
97 -1.999999458491272735552792645606044
98 COMPLEX
99 →
100 -1.96078431372549019607843137254902ᴇ-3
101 0
102 COMPLEX
103 →
104 3.670186023127199597787833081950729ᴇ-3
105 1.999999458491272735552792645606044
106 COMPLEX
107 →
108 EXITALL
109 END
And there is indeed a problem.
But perhaps it is this, release notes from Free42 v2.5.21:
* Complex matrix division, INVRT, and DET would return incorrect results for
large matrices (20x20 or greater). Fixed.
Even if this is only a 5x5. And the DM42 has Free42 v2.5.20..
Thomas, what was the problem?

Werner
Thanks for your comment and code, Werner.
The inversion with following code gives a correct result but needs some more memory and execution-time.

Code: Select all

00 { 139-Byte Prgm }
01▸LBL "CINV"
02 COMPLEX
03 LSTO "B"
04 +/-
05 LSTO "BB"
06 R↓
07 LSTO "A"
08 DIM?
09 LSTO "D"
10 2
11 ×
12 ENTER
13 NEWMAT
14 LSTO "YYY"
15 INDEX "YYY"
16 RCL "A"
17 PUTM
18 1
19 RCL+ "D"
20 ENTER
21 STOIJ
22 RCL "A"
23 PUTM
24 1
25 ENTER
26 RCL+ "D"
27 STOIJ
28 RCL "BB"
29 PUTM
30 1
31 RCL+ "D"
32 1
33 STOIJ
34 RCL "B"
35 PUTM
36 RCL "YYY"
37 INVRT
38 LSTO "YYY"
39 INDEX "YYY"
40 RCL "D"
41 ENTER
42 GETM
43 LSTO "A"
44 1
45 RCL+ "D"
46 1
47 STOIJ
48 RCL "D"
49 ENTER
50 GETM
51 LSTO "B"
52 RCL "A"
53 X<>Y
54 COMPLEX
55 END
whuyse
Posts: 198
Joined: Thu Dec 21, 2017 1:23 pm

Re: Complex matrix inversion with issues

Post by whuyse »

Strange that only a single row of A*inv(A) is wrong, the second row.
Werner
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Complex matrix inversion with issues

Post by Thomas Okken »

The problem before 2.5.21 was state in the complex LU decomposition worker procedure that wasn't getting saved when it was suspended. This would not have affected a 5x5, because a matrix that small would be fully decomposed before the worker yielded the CPU.

https://github.com/thomasokken/free42/c ... 0dbf4ce9f3

So this must be a different issue. I'm looking into it...
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Complex matrix inversion with issues

Post by Thomas Okken »

The problem appears to affect only Free42/Plus42 Decimal, not the Binary version.
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Complex matrix inversion with issues

Post by Thomas Okken »

The problem does affect only the decimal version. The reason is that hypot() is broken in the Intel library. Yikes.

I'm implementing a workaround and building a new release.
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Complex matrix inversion with issues

Post by Thomas Okken »

Example: 1+9.999997292456363677763963228030219e-2i →POL returns magnitude exactly 1

This is the case where Johan's test case starts to go wrong in the LU decomposition.
Last edited by Thomas Okken on Fri Mar 05, 2021 12:33 pm, edited 1 time in total.
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Complex matrix inversion with issues

Post by Thomas Okken »

The new version is up on my web site and in Google Play now. The iOS version is waiting for approval was approved a bit later, on March 5 at 5:29 PM EST.
Last edited by Thomas Okken on Sat Mar 06, 2021 4:16 pm, edited 1 time in total.
jneven
Posts: 12
Joined: Sat Jan 23, 2021 6:32 pm
Location: Belgium

Re: Complex matrix inversion with issues

Post by jneven »

Thanks for your fast response, Thomas.
I downloaded the new version of Free42 and updated the Android App. It is running great.
I suppose the update of the DM42 firmware will take a little longer (I use the graphics of the DM42 in the program I'm busy with).

Again thanks and kind regards.
Johan.
Post Reply