## Determinate if a Matrix has an invers.

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

### Determinate if a Matrix has an invers.

3 3 matrix:
Coefs
3 ¯2 4
2 4 ¯4
5 ¯1 1

Is the following expression correct ?

(⌹Coefs) +.× Coefs = Coefs +.× (⌹Coefs)
gives:
0 0 0
0 0 0
0 0 0
All zero's thus my assumption is that this matrix has an invers ?

another matrix :
Coefs
2 ¯1 3 ¯1
4 2 ¯1 ¯3
5 1 1 ¯1
1 1 ¯1 ¯1
The same expression as above gives now :
0 0 8.881784197E¯16 0
0 0 1.000000000E0 0
0 0 5.000000000E¯1 0
0 0 5.000000000E¯1 0
Thus this matrix has no invers ?

Or is there another way with APL to find out if a matrix has an invers ?

Thanks very much to have a look at this.

Henk.
hbarkhof

Posts: 44
Joined: Mon Apr 09, 2018 8:37 am

### Re: Determinate if a Matrix has an invers.

Henk,

If your matrix has an inverse then ⌹Coefs will return the inverse. If it does not have an inverse (we say it is *singular*) then ⌹Coefs will generate a DOMAIN ERROR. You could write a dfn to test whether a matrix is singular like this:

`      singular←{          11::1  ⍝ DOMAIN ERROR means it's singular          0⊣⌹⍵   ⍝ otherwise it's not      }`

It gets a bit more complicated if your matrix is very large, or numerically very close to being singular, but for small simple matrices the above method should be fine.

Jay.
Jay|Dyalog

### Re: Determinate if a Matrix has an invers.

Thanks Jay. Than I have a follow-up question.
How to determine if the matrix gives a non-singular or singular ?
hbarkhof

Posts: 44
Joined: Mon Apr 09, 2018 8:37 am

### Re: Determinate if a Matrix has an invers.

Type this into your APL session. (I am defining the dfn all on one line, because it's easier to type into the session.)

`      singular←{11::1 ⋄ 0⊣⌹⍵}      singular Coefs0`

The result 0 here means that the matrix Coefs is not singular.

Is this what you want?

Jay.
Jay|Dyalog

### Re: Determinate if a Matrix has an invers.

That is what I am looking for! Great. Many thanks!
hbarkhof

Posts: 44
Joined: Mon Apr 09, 2018 8:37 am

### Re: Determinate if a Matrix has an invers.

In addition to the function that already Jay gave for determining whether a matrix is singular, the following is also relevant: The expression

Code: Select all
`(⌹Coefs) +.× Coefs = Coefs +.× (⌹Coefs)`

does not do what you think it does in APL, because the function = has no special status and has scope similar to other functions such as + or × . APL interprets it as follows, fully parenthesized:

Code: Select all
`(⌹Coefs) +.× (Coefs = (Coefs +.× (⌹Coefs)))`

an unusual computation. Whereas I think what was intended is:

Code: Select all
`((⌹Coefs) +.× Coefs) = (Coefs +.× (⌹Coefs))`

However, even this last expression would not always give the expected answer, given the vicissitudes of working with floating point numbers. It's better to use:

Code: Select all
`⌈/ | , ((⌹Coefs) +.× Coefs) - (Coefs +.× (⌹Coefs))`

the maximum absolute difference between the "LHS" and the "RHS", or

Code: Select all
`I←{∘.=⍨⍳⍵}  ⍝ identity matrix of order ⍵⌈/ | , (I ≢Coefs) -   Coefs  +.× (⌹Coefs)⌈/ | , (I ≢Coefs) - (⌹Coefs) +.×   Coefs`

the maximum absolute difference between the identity matrix and a matrix times its inverse.
Roger|Dyalog

Posts: 215
Joined: Thu Jul 28, 2011 10:53 am