# [SciPy-user] Matrix sign function

Pearu Peterson scipy-user@scipy.net
Wed Sep 24 12:31:57 CDT 2003

```On Wed, 24 Sep 2003, Nils Wagner wrote:

> > > Anyway, I think scipy should have a generalized sign function as well.
> > >
> > > A suitable test case is the following matrix
> > >
> > > array([[ 29.2, -24.2,  69.5,  49.8,   7. ],
> > >        [ -9.2,   5.2, -18. , -16.8,  -2. ],
> > >        [-10. ,   6. , -20. , -18. ,  -2. ],
> > >        [ -9.6,   9.6, -25.5, -15.4,  -2. ],
> > >        [  9.8,  -4.8,  18. ,  18.2,   2. ]])
> >
> > What would be the correct result for signm(<given matrix>)?
> > signm gives:
> >
> > >>> signm(a)
> > array([[ 20.728,  -6.576,  29.592,  32.88 ,  -6.576],
> >        [ -5.808,   2.936,  -8.712,  -9.68 ,   1.936],
> >        [ -6.24 ,   2.08 ,  -8.36 , -10.4  ,   2.08 ],
> >        [ -7.344,   2.448, -11.016, -11.24 ,   2.448],
> >        [  6.192,  -2.064,   9.288,  10.32 ,  -1.064]])
> >
> > Is this correct?
> >
> The result of the generalized sign algorithm is
>
> array([[ 11.94933333,  -2.24533333,  15.31733333,  21.65333333,
> -2.24533333],
>        [ -3.84266667,   0.49866667,  -4.59066667,  -7.18666667,
> 0.49866667],
>        [ -4.08      ,   0.56      ,  -4.92      ,  -7.6       ,
> 0.56      ],
>        [ -4.03466667,   1.04266667,  -5.59866667,  -7.02666667,
> 1.04266667],
>        [  4.15733333,  -0.50133333,   4.90933333,   7.81333333,
> -0.50133333]])

The results were different due to roundoff errors. Note that sign
function is very sensitive to that. So, here is an improved signm
that gives the above result:

>>> signm=lambda a:linalg.funm(a,lambda v:sign(around(real(v),12))+0j)
>>> signm(a)
array([[ 11.94933333,  -2.24533333,  15.31733333,  21.65333333,
-2.24533333],
[ -3.84266667,   0.49866667,  -4.59066667,  -7.18666667,
0.49866667],
[ -4.08      ,   0.56      ,  -4.92      ,  -7.6       ,   0.56
],
[ -4.03466667,   1.04266667,  -5.59866667,  -7.02666667,
1.04266667],
[  4.15733333,  -0.50133333,   4.90933333,   7.81333333,
-0.50133333]])

Pearu

```