Karim Belabas on Mon, 29 Oct 2012 11:38:01 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Matrix positive?


* Dirk Laurie [2012-10-29 11:08]:
> What's the quick way of testing whether every element
> of a matrix is positive?

Since we want an "early abort" if an element is non-positive, I see 
no direct way.

Here's a solution: 

  matispositive(M) =
  { my (m,n); [m,n] = matsize(M);
    for(i=1,m, for(j=1,n, if (M[i,j] <= 0, return(0))));
    return(1);
  }

(requires pari-2.6 because of multiple assignment [m,n]=...; adapt for
older versions)

Cheers,

    K.B.

P.S: One *might* overload comparison operators to act componentwise for vector
matrices. Currently, we have

  ? matid(2) > 0
  ***   at top-level: matid(2)>0
  ***                         ^--
  *** _>_: forbidden comparison t_MAT (2x2) , t_INT.

I don't like the idea (and it's inconsistent with the way == currently
works: for matrices M == 2 tests whether M is twice the identity; and for a
column vector, whether it is of the form [2, 0, ..., 0]~)
--
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite Bordeaux 1          Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation    http://www.math.u-bordeaux1.fr/~belabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux1.fr/  [PARI/GP]
`