Is the Matrix Positive-Definite?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

up vote
18
down vote

favorite

Introduction

Today we’re gonna take care of the bane of first-year linear algebra students: matrix definiteness! Apparently this doesn’t yet have a challenge so here we go:

Input

  • A $ntimes n$ symmetric Matrix $A$ in any convenient format (you may also of course only take the upper or the lower part of the matrix)
  • Optionally: the size of the matrix $n$

What to do?

The challenge is simple: Given a real-valued matrix $ntimes n$ Matrix decide whether it is positive definite by outputting a truthy value if so and a falsey value if not.

You may assume your built-ins to actually work precisely and thus don’t have to account for numerical issues which could lead to the wrong behaviour if the strategy / code “provably” should yield the correct result.

Who wins?

This is code-golf, so the shortest code in bytes (per-language) wins!


What is a positive-definite Matrix anyways?

There are apparently 6 equivalent formulations of when a symmetric matrix is positive-definite. I shall reproduce the three easier ones and reference you to Wikipedia for the more complex ones.

  • If $forall vinmathbb R^nsetminus {0}: v^T Av>0$ then $A$ is positive-definite.
    This can be re-formulated as:
    If for every non-zero vector $v$ the (standard) dot product of $v$ and $Av$ is positive then $A$ is positive-definite.
  • Let $lambda_iquad iin{1,ldots,n}$ be the eigenvalues of $A$, if now $forall iin{1,ldots,n}:lambda_i>0$ (that is all eigenvalues are positive) then $A$ is positive-definite.
    If you don’t know what eigenvalues are I suggest you use your favourite search engine to find out, because the explanation (and the needed computation strategies) is too long to be contained in this post.
  • If the Cholesky-Decomposition of $A$ exists, i.e. there exists a lower-triangular matrix $L$ such that $LL^T=A$ then $A$ is positive-definite. Note that this is equivalent to early-returning “false” if at any point the computation of the root during the algorithm fails due to a negative argument.

Examples

For truthy output

begin{pmatrix}1&0&0\0&1&0\0&0&1end{pmatrix}

begin{pmatrix}1&0&0&0\0&2&0&0\0&0&3&0\0&0&0&4end{pmatrix}

begin{pmatrix}5&2&-1\2&1&-1\-1&-1&3end{pmatrix}

begin{pmatrix}1&-2&2\-2&5&0\2&0&30end{pmatrix}

begin{pmatrix}7.15&2.45\2.45&9.37end{pmatrix}

For falsey output

(at least one eigenvalue is 0 / positive semi-definite)
begin{pmatrix}3&-2&2\-2&4&0\2&0&2end{pmatrix}

(eigenvalues have different signs / indefinite)
begin{pmatrix}1&0&0\0&-1&0\0&0&1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-1&0&0\0&-1&0\0&0&-1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-2&3&0\3&-5&0\0&0&-1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-7.15&-2.45\-2.45&-9.37end{pmatrix}

(three positive, one negative eigenvalue / indefinite)
begin{pmatrix}7.15&2.45&1.23&3.5\2.45&9.37&2.71&3.14\1.23&2.71&0&6.2\3.5&3.14&6.2&0.56end{pmatrix}

share|improve this question

  • This challenge was sandboxed.
    – SEJPM
    Sep 23 at 11:48

  • You need to provide a better definition of what we’re supposed to be looking for rather than assuming we can all read mathematical notation (or all know what an “eigenvalue” is). A worked example would be useful too.
    – Shaggy
    Sep 23 at 15:53

  • 9

    @Shaggy I think the challenge is better without all the background to clog it up. There are many existing explanations of what an eigenvalue is elsewhere, and this post is already really large.
    – W W
    Sep 23 at 16:37

  • 1

    The challenge would have been nicer hadn’t you restrict the input to symmetric matrices.
    – polfosol à° _à° 
    Sep 24 at 15:55

  • 1

    I meant just checking for the sign of eigenvalues is also boring. Different tastes I know 😉
    – polfosol à° _à° 
    Sep 24 at 16:11

up vote
18
down vote

favorite

Introduction

Today we’re gonna take care of the bane of first-year linear algebra students: matrix definiteness! Apparently this doesn’t yet have a challenge so here we go:

Input

  • A $ntimes n$ symmetric Matrix $A$ in any convenient format (you may also of course only take the upper or the lower part of the matrix)
  • Optionally: the size of the matrix $n$

What to do?

The challenge is simple: Given a real-valued matrix $ntimes n$ Matrix decide whether it is positive definite by outputting a truthy value if so and a falsey value if not.

You may assume your built-ins to actually work precisely and thus don’t have to account for numerical issues which could lead to the wrong behaviour if the strategy / code “provably” should yield the correct result.

Who wins?

This is code-golf, so the shortest code in bytes (per-language) wins!


What is a positive-definite Matrix anyways?

There are apparently 6 equivalent formulations of when a symmetric matrix is positive-definite. I shall reproduce the three easier ones and reference you to Wikipedia for the more complex ones.

  • If $forall vinmathbb R^nsetminus {0}: v^T Av>0$ then $A$ is positive-definite.
    This can be re-formulated as:
    If for every non-zero vector $v$ the (standard) dot product of $v$ and $Av$ is positive then $A$ is positive-definite.
  • Let $lambda_iquad iin{1,ldots,n}$ be the eigenvalues of $A$, if now $forall iin{1,ldots,n}:lambda_i>0$ (that is all eigenvalues are positive) then $A$ is positive-definite.
    If you don’t know what eigenvalues are I suggest you use your favourite search engine to find out, because the explanation (and the needed computation strategies) is too long to be contained in this post.
  • If the Cholesky-Decomposition of $A$ exists, i.e. there exists a lower-triangular matrix $L$ such that $LL^T=A$ then $A$ is positive-definite. Note that this is equivalent to early-returning “false” if at any point the computation of the root during the algorithm fails due to a negative argument.

Examples

For truthy output

begin{pmatrix}1&0&0\0&1&0\0&0&1end{pmatrix}

begin{pmatrix}1&0&0&0\0&2&0&0\0&0&3&0\0&0&0&4end{pmatrix}

begin{pmatrix}5&2&-1\2&1&-1\-1&-1&3end{pmatrix}

begin{pmatrix}1&-2&2\-2&5&0\2&0&30end{pmatrix}

begin{pmatrix}7.15&2.45\2.45&9.37end{pmatrix}

For falsey output

(at least one eigenvalue is 0 / positive semi-definite)
begin{pmatrix}3&-2&2\-2&4&0\2&0&2end{pmatrix}

(eigenvalues have different signs / indefinite)
begin{pmatrix}1&0&0\0&-1&0\0&0&1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-1&0&0\0&-1&0\0&0&-1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-2&3&0\3&-5&0\0&0&-1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-7.15&-2.45\-2.45&-9.37end{pmatrix}

(three positive, one negative eigenvalue / indefinite)
begin{pmatrix}7.15&2.45&1.23&3.5\2.45&9.37&2.71&3.14\1.23&2.71&0&6.2\3.5&3.14&6.2&0.56end{pmatrix}

share|improve this question

  • This challenge was sandboxed.
    – SEJPM
    Sep 23 at 11:48

  • You need to provide a better definition of what we’re supposed to be looking for rather than assuming we can all read mathematical notation (or all know what an “eigenvalue” is). A worked example would be useful too.
    – Shaggy
    Sep 23 at 15:53

  • 9

    @Shaggy I think the challenge is better without all the background to clog it up. There are many existing explanations of what an eigenvalue is elsewhere, and this post is already really large.
    – W W
    Sep 23 at 16:37

  • 1

    The challenge would have been nicer hadn’t you restrict the input to symmetric matrices.
    – polfosol à° _à° 
    Sep 24 at 15:55

  • 1

    I meant just checking for the sign of eigenvalues is also boring. Different tastes I know 😉
    – polfosol à° _à° 
    Sep 24 at 16:11

up vote
18
down vote

favorite

up vote
18
down vote

favorite

Introduction

Today we’re gonna take care of the bane of first-year linear algebra students: matrix definiteness! Apparently this doesn’t yet have a challenge so here we go:

Input

  • A $ntimes n$ symmetric Matrix $A$ in any convenient format (you may also of course only take the upper or the lower part of the matrix)
  • Optionally: the size of the matrix $n$

What to do?

The challenge is simple: Given a real-valued matrix $ntimes n$ Matrix decide whether it is positive definite by outputting a truthy value if so and a falsey value if not.

You may assume your built-ins to actually work precisely and thus don’t have to account for numerical issues which could lead to the wrong behaviour if the strategy / code “provably” should yield the correct result.

Who wins?

This is code-golf, so the shortest code in bytes (per-language) wins!


What is a positive-definite Matrix anyways?

There are apparently 6 equivalent formulations of when a symmetric matrix is positive-definite. I shall reproduce the three easier ones and reference you to Wikipedia for the more complex ones.

  • If $forall vinmathbb R^nsetminus {0}: v^T Av>0$ then $A$ is positive-definite.
    This can be re-formulated as:
    If for every non-zero vector $v$ the (standard) dot product of $v$ and $Av$ is positive then $A$ is positive-definite.
  • Let $lambda_iquad iin{1,ldots,n}$ be the eigenvalues of $A$, if now $forall iin{1,ldots,n}:lambda_i>0$ (that is all eigenvalues are positive) then $A$ is positive-definite.
    If you don’t know what eigenvalues are I suggest you use your favourite search engine to find out, because the explanation (and the needed computation strategies) is too long to be contained in this post.
  • If the Cholesky-Decomposition of $A$ exists, i.e. there exists a lower-triangular matrix $L$ such that $LL^T=A$ then $A$ is positive-definite. Note that this is equivalent to early-returning “false” if at any point the computation of the root during the algorithm fails due to a negative argument.

Examples

For truthy output

begin{pmatrix}1&0&0\0&1&0\0&0&1end{pmatrix}

begin{pmatrix}1&0&0&0\0&2&0&0\0&0&3&0\0&0&0&4end{pmatrix}

begin{pmatrix}5&2&-1\2&1&-1\-1&-1&3end{pmatrix}

begin{pmatrix}1&-2&2\-2&5&0\2&0&30end{pmatrix}

begin{pmatrix}7.15&2.45\2.45&9.37end{pmatrix}

For falsey output

(at least one eigenvalue is 0 / positive semi-definite)
begin{pmatrix}3&-2&2\-2&4&0\2&0&2end{pmatrix}

(eigenvalues have different signs / indefinite)
begin{pmatrix}1&0&0\0&-1&0\0&0&1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-1&0&0\0&-1&0\0&0&-1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-2&3&0\3&-5&0\0&0&-1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-7.15&-2.45\-2.45&-9.37end{pmatrix}

(three positive, one negative eigenvalue / indefinite)
begin{pmatrix}7.15&2.45&1.23&3.5\2.45&9.37&2.71&3.14\1.23&2.71&0&6.2\3.5&3.14&6.2&0.56end{pmatrix}

share|improve this question

Introduction

Today we’re gonna take care of the bane of first-year linear algebra students: matrix definiteness! Apparently this doesn’t yet have a challenge so here we go:

Input

  • A $ntimes n$ symmetric Matrix $A$ in any convenient format (you may also of course only take the upper or the lower part of the matrix)
  • Optionally: the size of the matrix $n$

What to do?

The challenge is simple: Given a real-valued matrix $ntimes n$ Matrix decide whether it is positive definite by outputting a truthy value if so and a falsey value if not.

You may assume your built-ins to actually work precisely and thus don’t have to account for numerical issues which could lead to the wrong behaviour if the strategy / code “provably” should yield the correct result.

Who wins?

This is code-golf, so the shortest code in bytes (per-language) wins!


What is a positive-definite Matrix anyways?

There are apparently 6 equivalent formulations of when a symmetric matrix is positive-definite. I shall reproduce the three easier ones and reference you to Wikipedia for the more complex ones.

  • If $forall vinmathbb R^nsetminus {0}: v^T Av>0$ then $A$ is positive-definite.
    This can be re-formulated as:
    If for every non-zero vector $v$ the (standard) dot product of $v$ and $Av$ is positive then $A$ is positive-definite.
  • Let $lambda_iquad iin{1,ldots,n}$ be the eigenvalues of $A$, if now $forall iin{1,ldots,n}:lambda_i>0$ (that is all eigenvalues are positive) then $A$ is positive-definite.
    If you don’t know what eigenvalues are I suggest you use your favourite search engine to find out, because the explanation (and the needed computation strategies) is too long to be contained in this post.
  • If the Cholesky-Decomposition of $A$ exists, i.e. there exists a lower-triangular matrix $L$ such that $LL^T=A$ then $A$ is positive-definite. Note that this is equivalent to early-returning “false” if at any point the computation of the root during the algorithm fails due to a negative argument.

Examples

For truthy output

begin{pmatrix}1&0&0\0&1&0\0&0&1end{pmatrix}

begin{pmatrix}1&0&0&0\0&2&0&0\0&0&3&0\0&0&0&4end{pmatrix}

begin{pmatrix}5&2&-1\2&1&-1\-1&-1&3end{pmatrix}

begin{pmatrix}1&-2&2\-2&5&0\2&0&30end{pmatrix}

begin{pmatrix}7.15&2.45\2.45&9.37end{pmatrix}

For falsey output

(at least one eigenvalue is 0 / positive semi-definite)
begin{pmatrix}3&-2&2\-2&4&0\2&0&2end{pmatrix}

(eigenvalues have different signs / indefinite)
begin{pmatrix}1&0&0\0&-1&0\0&0&1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-1&0&0\0&-1&0\0&0&-1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-2&3&0\3&-5&0\0&0&-1end{pmatrix}

(all eigenvalues smaller than 0 / negative definite)
begin{pmatrix}-7.15&-2.45\-2.45&-9.37end{pmatrix}

(three positive, one negative eigenvalue / indefinite)
begin{pmatrix}7.15&2.45&1.23&3.5\2.45&9.37&2.71&3.14\1.23&2.71&0&6.2\3.5&3.14&6.2&0.56end{pmatrix}

code-golf math decision-problem matrix

share|improve this question

share|improve this question

share|improve this question

share|improve this question

edited Sep 23 at 19:00

asked Sep 23 at 11:47

SEJPM

1,63811234

1,63811234

  • This challenge was sandboxed.
    – SEJPM
    Sep 23 at 11:48

  • You need to provide a better definition of what we’re supposed to be looking for rather than assuming we can all read mathematical notation (or all know what an “eigenvalue” is). A worked example would be useful too.
    – Shaggy
    Sep 23 at 15:53

  • 9

    @Shaggy I think the challenge is better without all the background to clog it up. There are many existing explanations of what an eigenvalue is elsewhere, and this post is already really large.
    – W W
    Sep 23 at 16:37

  • 1

    The challenge would have been nicer hadn’t you restrict the input to symmetric matrices.
    – polfosol à° _à° 
    Sep 24 at 15:55

  • 1

    I meant just checking for the sign of eigenvalues is also boring. Different tastes I know 😉
    – polfosol à° _à° 
    Sep 24 at 16:11

  • This challenge was sandboxed.
    – SEJPM
    Sep 23 at 11:48

  • You need to provide a better definition of what we’re supposed to be looking for rather than assuming we can all read mathematical notation (or all know what an “eigenvalue” is). A worked example would be useful too.
    – Shaggy
    Sep 23 at 15:53

  • 9

    @Shaggy I think the challenge is better without all the background to clog it up. There are many existing explanations of what an eigenvalue is elsewhere, and this post is already really large.
    – W W
    Sep 23 at 16:37

  • 1

    The challenge would have been nicer hadn’t you restrict the input to symmetric matrices.
    – polfosol à° _à° 
    Sep 24 at 15:55

  • 1

    I meant just checking for the sign of eigenvalues is also boring. Different tastes I know 😉
    – polfosol à° _à° 
    Sep 24 at 16:11

This challenge was sandboxed.
– SEJPM
Sep 23 at 11:48

This challenge was sandboxed.
– SEJPM
Sep 23 at 11:48

You need to provide a better definition of what we’re supposed to be looking for rather than assuming we can all read mathematical notation (or all know what an “eigenvalue” is). A worked example would be useful too.
– Shaggy
Sep 23 at 15:53

You need to provide a better definition of what we’re supposed to be looking for rather than assuming we can all read mathematical notation (or all know what an “eigenvalue” is). A worked example would be useful too.
– Shaggy
Sep 23 at 15:53

9

9

@Shaggy I think the challenge is better without all the background to clog it up. There are many existing explanations of what an eigenvalue is elsewhere, and this post is already really large.
– W W
Sep 23 at 16:37

@Shaggy I think the challenge is better without all the background to clog it up. There are many existing explanations of what an eigenvalue is elsewhere, and this post is already really large.
– W W
Sep 23 at 16:37

1

1

The challenge would have been nicer hadn’t you restrict the input to symmetric matrices.
– polfosol à° _à° 
Sep 24 at 15:55

The challenge would have been nicer hadn’t you restrict the input to symmetric matrices.
– polfosol à° _à° 
Sep 24 at 15:55

1

1

I meant just checking for the sign of eigenvalues is also boring. Different tastes I know 😉
– polfosol à° _à° 
Sep 24 at 16:11

I meant just checking for the sign of eigenvalues is also boring. Different tastes I know 😉
– polfosol à° _à° 
Sep 24 at 16:11

12 Answers
12

active

oldest

votes

up vote
11
down vote

C, 108 bytes

-1 byte thanks to Logern
-3 bytes thanks to ceilingcat

f(M,n,i)double**M;{for(i=n*n;i--;)M[i/n][i%n]-=M[n][i%n]*M[i/n][n]/M[n][n];return M[n][n]>0&(!n||f(M,n-1));}

Try it online!

Performs Gaussian elimination and checks whether all diagonal elements are positive (Sylvester’s criterion). Argument n is the size of the matrix minus one.

share|improve this answer

  • Perhaps save a character with float instead of double?
    – Jens
    Sep 23 at 15:10

  • 106 bytes – Try it online!
    – Logern
    Sep 23 at 15:35

  • You can shave another character if you drop i=0 in the for loop, make the recursive call f(M,n-1,0) and the initial call with 0 as the third arg.
    – Jens
    Sep 23 at 19:36

  • @Jens 1. Using floats instead of doubles can quickly lead to noticable rounding errors, so I don’t think the one byte saved is worth it. 2. Initializing a variable via an additional argument looks like cheating to me.
    – nwellnhof
    Sep 24 at 17:42

  • @Logern I refuse to use the “omit the return statement” trick in my C answers. But thanks for the other byte saved.
    – nwellnhof
    Sep 24 at 17:45

up vote
9
down vote

MATLAB/Octave, 19 17 12 bytes

@(A)eig(A)>0

Try it online!

The function eig provides the eigenvalues in ascending order, so if the first eigenvalue is greater than zero, the other ones are too.

share|improve this answer

  • You can drop the f= at the beginning – anonymous functions are generally accepted as answers.
    – Delfad0r
    Sep 23 at 15:05

  • Thanks for the tip!
    – Daniel Turizo
    Sep 23 at 15:11

  • Even if its a vector? Interesting
    – Daniel Turizo
    Sep 23 at 15:17

  • 1

    +1. I’ve added a link for trying it online. Hope you don’t mind. Note that it is also proving that the output values despite being arrays do count as the correct “truthy” or “falsey” values as per the link @Delfad0r posted.
    – Tom Carpenter
    Sep 23 at 17:03

  • 2

    Having said that, it fails for the first “falsey” test case on TIO. I’m guessing due to a precision issue – one of the Eigen values comes out as 8.9219e-17 rather than 0.
    – Tom Carpenter
    Sep 23 at 17:05

up vote
7
down vote

Jelly, 11 10 bytes

ṖṖ€$ƬÆḊṂ>0

Uses Sylvester’s criterion.

Try it online!

How it works

ṖṖ€$ƬÆḊṂ>0  Main link. Argument: M (matrix)

   $Ƭ       Do the following until a fixed point is encountered.
Ṗ             Pop; remove the last row of the matrix.
 Ṗ€           Pop each; remove the last entry of each row.
     ÆḊ     Take the determinants of the resulting minors.
       Ṃ    Take the minimum.
        >0  Test if the least determinant is positive, i.e., if all determinants are.

share|improve this answer

    up vote
    6
    down vote

    R, 29 bytes

    function(m)all(eigen(m)$va>0)
    

    Try it online!


    Alternative using cholesky :

    R, 34 33 bytes

    function(m)is.array(try(chol(m)))
    

    Try it online!

    -1 byte thanks to @Giuseppe

    share|improve this answer

      up vote
      6
      down vote

      Haskell, 56 bytes

      f((x:y):z)=x>0&&f[zipWith(-)v$map(u/x*)y|u:v<-z]
      f=1>0
      

      Try it online!

      Basically a port of nwellnhof’s answer. Performs gaussian elimination and checks whether the elements on the main diagonal are positive.

      Fails the first falsey output because of rounding errors, but it would theoretically work with infinite precision. Thanks to Curtis Bechtel’s suggestion, now the outputs are all correct.

      share|improve this answer

      • 2

        you can add inputs :: [[[Rational]]] to get correct answers
        – Curtis Bechtel
        Sep 23 at 20:38

      up vote
      4
      down vote

      Wolfram Language (Mathematica), 20 bytes

      0<Min@Eigenvalues@#&
      

      Try it online!

      share|improve this answer

      • Should 4th test case be False?
        – tsh
        Sep 23 at 14:48

      • @tsh Fixed, I am dumb!
        – Mr. Xcoder
        Sep 23 at 14:57

      • 8

        Funny how Mathematica has a builtin for this, but its name is longer than your solution.
        – Federico Poloni
        Sep 23 at 15:04

      • @FedericoPoloni: Wouldn’t a solution using NullSpace or MatrixRank be even shorter? If Null space is zero then the matrix is positive definite.
        – Phil H
        Sep 24 at 15:41

      • @PhilH No, I’m afraid that doesn’t work by itself. For instance, the second falsey example (diagonal matrix with (1,-1,1)) has rank 3, but is not positive definite.
        – Federico Poloni
        Sep 24 at 16:39

      up vote
      3
      down vote

      MATL, 4 bytes

      Yv0>
      

      Try it online!

      I’ve noticed that for the [3 -2 2; -2 4 0; 2 0 2] test case, MATL calculates the 0 eigenvalue with a very small inaccuracy, computing something as low as about $10^{-18}$. This therefore fails the first falsy test case due to precision issues. Thanks to Luis Mendo for pointing out that a non-empty array is truthy iff all its entries differ from 0, saving 1 byte.

      share|improve this answer

      • 1

        @LuisMendo Thanks, I learned something new about MATL today!
        – Mr. Xcoder
        Sep 26 at 21:53

      • My pleasure 🙂 Here’s a better explanation by Suever. I forgot to say that for complex-valued arrays only the real part is compared against zero. So [1 2 3j] is falsey
        – Luis Mendo
        Sep 26 at 22:04

      up vote
      2
      down vote

      Mathematica, 29 28 bytes

      AllTrue[Eigenvalues@#,#>0&]&
      

      Definition 2.

      share|improve this answer

        up vote
        2
        down vote

        Julia 1.0, 28 bytes

        using LinearAlgebra
        isposdef
        

        Try it online!


        Julia 0.6, 8 bytes

        isposdef
        

        Try it online!

        share|improve this answer

          up vote
          2
          down vote

          MATL, 6 bytes

          It is possible to do it using even less bytes, @Mr. Xcoder managed to find a 5 byte MATL answer!

          YvX<0>
          

          Explanation

          Yv     compute eigenvalues
            X<   take the minimum
              0> check whether it is greather than zero
          

          Try it online!

          share|improve this answer

          • Fails on the first falsy test case. See my deleted answer.
            – Mr. Xcoder
            Sep 25 at 6:59

          • 1

            @Mr.Xcoder Oh, you even submitted an answer before me. I think you should undelete your answer as it just depends on rounding issues. (I think you can expect answers to use limited precision arithmetic – I think only the CAS languages here use exact computations.)
            – flawr
            Sep 25 at 18:02

          • Following your advice, I’ve undeleted it.
            – Mr. Xcoder
            Sep 25 at 21:18

          up vote
          1
          down vote

          Maple, 33 bytes

          (i.e. my 2 cents)

          with(LinearAlgebra):
          IsDefinite(A)
          

          share|improve this answer

          • Hello and welcome to PPCG; I am unfamiliar with Maple, though is the newline necessary?
            – Jonathan Frech
            Sep 24 at 16:06

          • @JonathanFrech Hello and thanks. No it’s not. I didn’t count it btw.
            – polfosol à° _à° 
            Sep 24 at 16:09

          • To me your current byte count seems to reflect the newline character.
            – Jonathan Frech
            Sep 24 at 16:16

          • @JonathanFrech Duh, my bad
            – polfosol à° _à° 
            Sep 24 at 16:18

          • 1

            Well … now your code and your byte count disagree.
            – Jonathan Frech
            Sep 24 at 17:07

          up vote
          0
          down vote

          JavaScript (ES6),  99 95  88 bytes

          Same method as nwellnhof’s answer. Returns $0$ or $1$.

          f=(m,n=0,R=m[n])=>R?f(m,n+1)&R[m.map((r,y)=>y<n&&R.map((v,x)=>r[x]-=v*r[n]/R[n])),n]>0:1
          

          Try it online!

          share|improve this answer

            Your Answer

            StackExchange.ifUsing(“editor”, function () {
            return StackExchange.using(“mathjaxEditing”, function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [[“\$”, “\$”]]);
            });
            });
            }, “mathjax-editing”);

            StackExchange.ifUsing(“editor”, function () {
            StackExchange.using(“externalEditor”, function () {
            StackExchange.using(“snippets”, function () {
            StackExchange.snippets.init();
            });
            });
            }, “code-snippets”);

            StackExchange.ready(function() {
            var channelOptions = {
            tags: “”.split(” “),
            id: “200”
            };
            initTagRenderer(“”.split(” “), “”.split(” “), channelOptions);

            StackExchange.using(“externalEditor”, function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using(“snippets”, function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: ‘answer’,
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: “”,
            onDemand: true,
            discardSelector: “.discard-answer”
            ,immediatelyShowMarkdownHelp:true
            });

            }
            });

             
            draft saved
            draft discarded

            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin(‘.new-post-login’, ‘https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f172677%2fis-the-matrix-positive-definite%23new-answer’, ‘question_page’);
            }
            );

            Post as a guest

            12 Answers
            12

            active

            oldest

            votes

            12 Answers
            12

            active

            oldest

            votes

            active

            oldest

            votes

            active

            oldest

            votes

            up vote
            11
            down vote

            C, 108 bytes

            -1 byte thanks to Logern
            -3 bytes thanks to ceilingcat

            f(M,n,i)double**M;{for(i=n*n;i--;)M[i/n][i%n]-=M[n][i%n]*M[i/n][n]/M[n][n];return M[n][n]>0&(!n||f(M,n-1));}
            

            Try it online!

            Performs Gaussian elimination and checks whether all diagonal elements are positive (Sylvester’s criterion). Argument n is the size of the matrix minus one.

            share|improve this answer

            • Perhaps save a character with float instead of double?
              – Jens
              Sep 23 at 15:10

            • 106 bytes – Try it online!
              – Logern
              Sep 23 at 15:35

            • You can shave another character if you drop i=0 in the for loop, make the recursive call f(M,n-1,0) and the initial call with 0 as the third arg.
              – Jens
              Sep 23 at 19:36

            • @Jens 1. Using floats instead of doubles can quickly lead to noticable rounding errors, so I don’t think the one byte saved is worth it. 2. Initializing a variable via an additional argument looks like cheating to me.
              – nwellnhof
              Sep 24 at 17:42

            • @Logern I refuse to use the “omit the return statement” trick in my C answers. But thanks for the other byte saved.
              – nwellnhof
              Sep 24 at 17:45

            up vote
            11
            down vote

            C, 108 bytes

            -1 byte thanks to Logern
            -3 bytes thanks to ceilingcat

            f(M,n,i)double**M;{for(i=n*n;i--;)M[i/n][i%n]-=M[n][i%n]*M[i/n][n]/M[n][n];return M[n][n]>0&(!n||f(M,n-1));}
            

            Try it online!

            Performs Gaussian elimination and checks whether all diagonal elements are positive (Sylvester’s criterion). Argument n is the size of the matrix minus one.

            share|improve this answer

            • Perhaps save a character with float instead of double?
              – Jens
              Sep 23 at 15:10

            • 106 bytes – Try it online!
              – Logern
              Sep 23 at 15:35

            • You can shave another character if you drop i=0 in the for loop, make the recursive call f(M,n-1,0) and the initial call with 0 as the third arg.
              – Jens
              Sep 23 at 19:36

            • @Jens 1. Using floats instead of doubles can quickly lead to noticable rounding errors, so I don’t think the one byte saved is worth it. 2. Initializing a variable via an additional argument looks like cheating to me.
              – nwellnhof
              Sep 24 at 17:42

            • @Logern I refuse to use the “omit the return statement” trick in my C answers. But thanks for the other byte saved.
              – nwellnhof
              Sep 24 at 17:45

            up vote
            11
            down vote

            up vote
            11
            down vote

            C, 108 bytes

            -1 byte thanks to Logern
            -3 bytes thanks to ceilingcat

            f(M,n,i)double**M;{for(i=n*n;i--;)M[i/n][i%n]-=M[n][i%n]*M[i/n][n]/M[n][n];return M[n][n]>0&(!n||f(M,n-1));}
            

            Try it online!

            Performs Gaussian elimination and checks whether all diagonal elements are positive (Sylvester’s criterion). Argument n is the size of the matrix minus one.

            share|improve this answer

            C, 108 bytes

            -1 byte thanks to Logern
            -3 bytes thanks to ceilingcat

            f(M,n,i)double**M;{for(i=n*n;i--;)M[i/n][i%n]-=M[n][i%n]*M[i/n][n]/M[n][n];return M[n][n]>0&(!n||f(M,n-1));}
            

            Try it online!

            Performs Gaussian elimination and checks whether all diagonal elements are positive (Sylvester’s criterion). Argument n is the size of the matrix minus one.

            share|improve this answer

            share|improve this answer

            share|improve this answer

            edited Sep 26 at 9:54

            answered Sep 23 at 13:24

            nwellnhof

            3,963715

            3,963715

            • Perhaps save a character with float instead of double?
              – Jens
              Sep 23 at 15:10

            • 106 bytes – Try it online!
              – Logern
              Sep 23 at 15:35

            • You can shave another character if you drop i=0 in the for loop, make the recursive call f(M,n-1,0) and the initial call with 0 as the third arg.
              – Jens
              Sep 23 at 19:36

            • @Jens 1. Using floats instead of doubles can quickly lead to noticable rounding errors, so I don’t think the one byte saved is worth it. 2. Initializing a variable via an additional argument looks like cheating to me.
              – nwellnhof
              Sep 24 at 17:42

            • @Logern I refuse to use the “omit the return statement” trick in my C answers. But thanks for the other byte saved.
              – nwellnhof
              Sep 24 at 17:45

            • Perhaps save a character with float instead of double?
              – Jens
              Sep 23 at 15:10

            • 106 bytes – Try it online!
              – Logern
              Sep 23 at 15:35

            • You can shave another character if you drop i=0 in the for loop, make the recursive call f(M,n-1,0) and the initial call with 0 as the third arg.
              – Jens
              Sep 23 at 19:36

            • @Jens 1. Using floats instead of doubles can quickly lead to noticable rounding errors, so I don’t think the one byte saved is worth it. 2. Initializing a variable via an additional argument looks like cheating to me.
              – nwellnhof
              Sep 24 at 17:42

            • @Logern I refuse to use the “omit the return statement” trick in my C answers. But thanks for the other byte saved.
              – nwellnhof
              Sep 24 at 17:45

            Perhaps save a character with float instead of double?
            – Jens
            Sep 23 at 15:10

            Perhaps save a character with float instead of double?
            – Jens
            Sep 23 at 15:10

            106 bytes – Try it online!
            – Logern
            Sep 23 at 15:35

            106 bytes – Try it online!
            – Logern
            Sep 23 at 15:35

            You can shave another character if you drop i=0 in the for loop, make the recursive call f(M,n-1,0) and the initial call with 0 as the third arg.
            – Jens
            Sep 23 at 19:36

            You can shave another character if you drop i=0 in the for loop, make the recursive call f(M,n-1,0) and the initial call with 0 as the third arg.
            – Jens
            Sep 23 at 19:36

            @Jens 1. Using floats instead of doubles can quickly lead to noticable rounding errors, so I don’t think the one byte saved is worth it. 2. Initializing a variable via an additional argument looks like cheating to me.
            – nwellnhof
            Sep 24 at 17:42

            @Jens 1. Using floats instead of doubles can quickly lead to noticable rounding errors, so I don’t think the one byte saved is worth it. 2. Initializing a variable via an additional argument looks like cheating to me.
            – nwellnhof
            Sep 24 at 17:42

            @Logern I refuse to use the “omit the return statement” trick in my C answers. But thanks for the other byte saved.
            – nwellnhof
            Sep 24 at 17:45

            @Logern I refuse to use the “omit the return statement” trick in my C answers. But thanks for the other byte saved.
            – nwellnhof
            Sep 24 at 17:45

            up vote
            9
            down vote

            MATLAB/Octave, 19 17 12 bytes

            @(A)eig(A)>0
            

            Try it online!

            The function eig provides the eigenvalues in ascending order, so if the first eigenvalue is greater than zero, the other ones are too.

            share|improve this answer

            • You can drop the f= at the beginning – anonymous functions are generally accepted as answers.
              – Delfad0r
              Sep 23 at 15:05

            • Thanks for the tip!
              – Daniel Turizo
              Sep 23 at 15:11

            • Even if its a vector? Interesting
              – Daniel Turizo
              Sep 23 at 15:17

            • 1

              +1. I’ve added a link for trying it online. Hope you don’t mind. Note that it is also proving that the output values despite being arrays do count as the correct “truthy” or “falsey” values as per the link @Delfad0r posted.
              – Tom Carpenter
              Sep 23 at 17:03

            • 2

              Having said that, it fails for the first “falsey” test case on TIO. I’m guessing due to a precision issue – one of the Eigen values comes out as 8.9219e-17 rather than 0.
              – Tom Carpenter
              Sep 23 at 17:05

            up vote
            9
            down vote

            MATLAB/Octave, 19 17 12 bytes

            @(A)eig(A)>0
            

            Try it online!

            The function eig provides the eigenvalues in ascending order, so if the first eigenvalue is greater than zero, the other ones are too.

            share|improve this answer

            • You can drop the f= at the beginning – anonymous functions are generally accepted as answers.
              – Delfad0r
              Sep 23 at 15:05

            • Thanks for the tip!
              – Daniel Turizo
              Sep 23 at 15:11

            • Even if its a vector? Interesting
              – Daniel Turizo
              Sep 23 at 15:17

            • 1

              +1. I’ve added a link for trying it online. Hope you don’t mind. Note that it is also proving that the output values despite being arrays do count as the correct “truthy” or “falsey” values as per the link @Delfad0r posted.
              – Tom Carpenter
              Sep 23 at 17:03

            • 2

              Having said that, it fails for the first “falsey” test case on TIO. I’m guessing due to a precision issue – one of the Eigen values comes out as 8.9219e-17 rather than 0.
              – Tom Carpenter
              Sep 23 at 17:05

            up vote
            9
            down vote

            up vote
            9
            down vote

            MATLAB/Octave, 19 17 12 bytes

            @(A)eig(A)>0
            

            Try it online!

            The function eig provides the eigenvalues in ascending order, so if the first eigenvalue is greater than zero, the other ones are too.

            share|improve this answer

            MATLAB/Octave, 19 17 12 bytes

            @(A)eig(A)>0
            

            Try it online!

            The function eig provides the eigenvalues in ascending order, so if the first eigenvalue is greater than zero, the other ones are too.

            share|improve this answer

            share|improve this answer

            share|improve this answer

            edited Sep 23 at 17:03

            Tom Carpenter

            3,870720

            3,870720

            answered Sep 23 at 15:01

            Daniel Turizo

            1012

            1012

            • You can drop the f= at the beginning – anonymous functions are generally accepted as answers.
              – Delfad0r
              Sep 23 at 15:05

            • Thanks for the tip!
              – Daniel Turizo
              Sep 23 at 15:11

            • Even if its a vector? Interesting
              – Daniel Turizo
              Sep 23 at 15:17

            • 1

              +1. I’ve added a link for trying it online. Hope you don’t mind. Note that it is also proving that the output values despite being arrays do count as the correct “truthy” or “falsey” values as per the link @Delfad0r posted.
              – Tom Carpenter
              Sep 23 at 17:03

            • 2

              Having said that, it fails for the first “falsey” test case on TIO. I’m guessing due to a precision issue – one of the Eigen values comes out as 8.9219e-17 rather than 0.
              – Tom Carpenter
              Sep 23 at 17:05

            • You can drop the f= at the beginning – anonymous functions are generally accepted as answers.
              – Delfad0r
              Sep 23 at 15:05

            • Thanks for the tip!
              – Daniel Turizo
              Sep 23 at 15:11

            • Even if its a vector? Interesting
              – Daniel Turizo
              Sep 23 at 15:17

            • 1

              +1. I’ve added a link for trying it online. Hope you don’t mind. Note that it is also proving that the output values despite being arrays do count as the correct “truthy” or “falsey” values as per the link @Delfad0r posted.
              – Tom Carpenter
              Sep 23 at 17:03

            • 2

              Having said that, it fails for the first “falsey” test case on TIO. I’m guessing due to a precision issue – one of the Eigen values comes out as 8.9219e-17 rather than 0.
              – Tom Carpenter
              Sep 23 at 17:05

            You can drop the f= at the beginning – anonymous functions are generally accepted as answers.
            – Delfad0r
            Sep 23 at 15:05

            You can drop the f= at the beginning – anonymous functions are generally accepted as answers.
            – Delfad0r
            Sep 23 at 15:05

            Thanks for the tip!
            – Daniel Turizo
            Sep 23 at 15:11

            Thanks for the tip!
            – Daniel Turizo
            Sep 23 at 15:11

            Even if its a vector? Interesting
            – Daniel Turizo
            Sep 23 at 15:17

            Even if its a vector? Interesting
            – Daniel Turizo
            Sep 23 at 15:17

            1

            1

            +1. I’ve added a link for trying it online. Hope you don’t mind. Note that it is also proving that the output values despite being arrays do count as the correct “truthy” or “falsey” values as per the link @Delfad0r posted.
            – Tom Carpenter
            Sep 23 at 17:03

            +1. I’ve added a link for trying it online. Hope you don’t mind. Note that it is also proving that the output values despite being arrays do count as the correct “truthy” or “falsey” values as per the link @Delfad0r posted.
            – Tom Carpenter
            Sep 23 at 17:03

            2

            2

            Having said that, it fails for the first “falsey” test case on TIO. I’m guessing due to a precision issue – one of the Eigen values comes out as 8.9219e-17 rather than 0.
            – Tom Carpenter
            Sep 23 at 17:05

            Having said that, it fails for the first “falsey” test case on TIO. I’m guessing due to a precision issue – one of the Eigen values comes out as 8.9219e-17 rather than 0.
            – Tom Carpenter
            Sep 23 at 17:05

            up vote
            7
            down vote

            Jelly, 11 10 bytes

            ṖṖ€$ƬÆḊṂ>0
            

            Uses Sylvester’s criterion.

            Try it online!

            How it works

            ṖṖ€$ƬÆḊṂ>0  Main link. Argument: M (matrix)
            
               $Ƭ       Do the following until a fixed point is encountered.
            Ṗ             Pop; remove the last row of the matrix.
             Ṗ€           Pop each; remove the last entry of each row.
                 ÆḊ     Take the determinants of the resulting minors.
                   Ṃ    Take the minimum.
                    >0  Test if the least determinant is positive, i.e., if all determinants are.
            

            share|improve this answer

              up vote
              7
              down vote

              Jelly, 11 10 bytes

              ṖṖ€$ƬÆḊṂ>0
              

              Uses Sylvester’s criterion.

              Try it online!

              How it works

              ṖṖ€$ƬÆḊṂ>0  Main link. Argument: M (matrix)
              
                 $Ƭ       Do the following until a fixed point is encountered.
              Ṗ             Pop; remove the last row of the matrix.
               Ṗ€           Pop each; remove the last entry of each row.
                   ÆḊ     Take the determinants of the resulting minors.
                     Ṃ    Take the minimum.
                      >0  Test if the least determinant is positive, i.e., if all determinants are.
              

              share|improve this answer

                up vote
                7
                down vote

                up vote
                7
                down vote

                Jelly, 11 10 bytes

                ṖṖ€$ƬÆḊṂ>0
                

                Uses Sylvester’s criterion.

                Try it online!

                How it works

                ṖṖ€$ƬÆḊṂ>0  Main link. Argument: M (matrix)
                
                   $Ƭ       Do the following until a fixed point is encountered.
                Ṗ             Pop; remove the last row of the matrix.
                 Ṗ€           Pop each; remove the last entry of each row.
                     ÆḊ     Take the determinants of the resulting minors.
                       Ṃ    Take the minimum.
                        >0  Test if the least determinant is positive, i.e., if all determinants are.
                

                share|improve this answer

                Jelly, 11 10 bytes

                ṖṖ€$ƬÆḊṂ>0
                

                Uses Sylvester’s criterion.

                Try it online!

                How it works

                ṖṖ€$ƬÆḊṂ>0  Main link. Argument: M (matrix)
                
                   $Ƭ       Do the following until a fixed point is encountered.
                Ṗ             Pop; remove the last row of the matrix.
                 Ṗ€           Pop each; remove the last entry of each row.
                     ÆḊ     Take the determinants of the resulting minors.
                       Ṃ    Take the minimum.
                        >0  Test if the least determinant is positive, i.e., if all determinants are.
                

                share|improve this answer

                share|improve this answer

                share|improve this answer

                edited Sep 23 at 14:57

                answered Sep 23 at 14:26

                Dennis♦

                182k32292725

                182k32292725

                    up vote
                    6
                    down vote

                    R, 29 bytes

                    function(m)all(eigen(m)$va>0)
                    

                    Try it online!


                    Alternative using cholesky :

                    R, 34 33 bytes

                    function(m)is.array(try(chol(m)))
                    

                    Try it online!

                    -1 byte thanks to @Giuseppe

                    share|improve this answer

                      up vote
                      6
                      down vote

                      R, 29 bytes

                      function(m)all(eigen(m)$va>0)
                      

                      Try it online!


                      Alternative using cholesky :

                      R, 34 33 bytes

                      function(m)is.array(try(chol(m)))
                      

                      Try it online!

                      -1 byte thanks to @Giuseppe

                      share|improve this answer

                        up vote
                        6
                        down vote

                        up vote
                        6
                        down vote

                        R, 29 bytes

                        function(m)all(eigen(m)$va>0)
                        

                        Try it online!


                        Alternative using cholesky :

                        R, 34 33 bytes

                        function(m)is.array(try(chol(m)))
                        

                        Try it online!

                        -1 byte thanks to @Giuseppe

                        share|improve this answer

                        R, 29 bytes

                        function(m)all(eigen(m)$va>0)
                        

                        Try it online!


                        Alternative using cholesky :

                        R, 34 33 bytes

                        function(m)is.array(try(chol(m)))
                        

                        Try it online!

                        -1 byte thanks to @Giuseppe

                        share|improve this answer

                        share|improve this answer

                        share|improve this answer

                        edited Sep 23 at 16:12

                        answered Sep 23 at 14:34

                        digEmAll

                        1,89147

                        1,89147

                            up vote
                            6
                            down vote

                            Haskell, 56 bytes

                            f((x:y):z)=x>0&&f[zipWith(-)v$map(u/x*)y|u:v<-z]
                            f=1>0
                            

                            Try it online!

                            Basically a port of nwellnhof’s answer. Performs gaussian elimination and checks whether the elements on the main diagonal are positive.

                            Fails the first falsey output because of rounding errors, but it would theoretically work with infinite precision. Thanks to Curtis Bechtel’s suggestion, now the outputs are all correct.

                            share|improve this answer

                            • 2

                              you can add inputs :: [[[Rational]]] to get correct answers
                              – Curtis Bechtel
                              Sep 23 at 20:38

                            up vote
                            6
                            down vote

                            Haskell, 56 bytes

                            f((x:y):z)=x>0&&f[zipWith(-)v$map(u/x*)y|u:v<-z]
                            f=1>0
                            

                            Try it online!

                            Basically a port of nwellnhof’s answer. Performs gaussian elimination and checks whether the elements on the main diagonal are positive.

                            Fails the first falsey output because of rounding errors, but it would theoretically work with infinite precision. Thanks to Curtis Bechtel’s suggestion, now the outputs are all correct.

                            share|improve this answer

                            • 2

                              you can add inputs :: [[[Rational]]] to get correct answers
                              – Curtis Bechtel
                              Sep 23 at 20:38

                            up vote
                            6
                            down vote

                            up vote
                            6
                            down vote

                            Haskell, 56 bytes

                            f((x:y):z)=x>0&&f[zipWith(-)v$map(u/x*)y|u:v<-z]
                            f=1>0
                            

                            Try it online!

                            Basically a port of nwellnhof’s answer. Performs gaussian elimination and checks whether the elements on the main diagonal are positive.

                            Fails the first falsey output because of rounding errors, but it would theoretically work with infinite precision. Thanks to Curtis Bechtel’s suggestion, now the outputs are all correct.

                            share|improve this answer

                            Haskell, 56 bytes

                            f((x:y):z)=x>0&&f[zipWith(-)v$map(u/x*)y|u:v<-z]
                            f=1>0
                            

                            Try it online!

                            Basically a port of nwellnhof’s answer. Performs gaussian elimination and checks whether the elements on the main diagonal are positive.

                            Fails the first falsey output because of rounding errors, but it would theoretically work with infinite precision. Thanks to Curtis Bechtel’s suggestion, now the outputs are all correct.

                            share|improve this answer

                            share|improve this answer

                            share|improve this answer

                            edited Sep 23 at 22:23

                            answered Sep 23 at 15:04

                            Delfad0r

                            773213

                            773213

                            • 2

                              you can add inputs :: [[[Rational]]] to get correct answers
                              – Curtis Bechtel
                              Sep 23 at 20:38

                            • 2

                              you can add inputs :: [[[Rational]]] to get correct answers
                              – Curtis Bechtel
                              Sep 23 at 20:38

                            2

                            2

                            you can add inputs :: [[[Rational]]] to get correct answers
                            – Curtis Bechtel
                            Sep 23 at 20:38

                            you can add inputs :: [[[Rational]]] to get correct answers
                            – Curtis Bechtel
                            Sep 23 at 20:38

                            up vote
                            4
                            down vote

                            Wolfram Language (Mathematica), 20 bytes

                            0<Min@Eigenvalues@#&
                            

                            Try it online!

                            share|improve this answer

                            • Should 4th test case be False?
                              – tsh
                              Sep 23 at 14:48

                            • @tsh Fixed, I am dumb!
                              – Mr. Xcoder
                              Sep 23 at 14:57

                            • 8

                              Funny how Mathematica has a builtin for this, but its name is longer than your solution.
                              – Federico Poloni
                              Sep 23 at 15:04

                            • @FedericoPoloni: Wouldn’t a solution using NullSpace or MatrixRank be even shorter? If Null space is zero then the matrix is positive definite.
                              – Phil H
                              Sep 24 at 15:41

                            • @PhilH No, I’m afraid that doesn’t work by itself. For instance, the second falsey example (diagonal matrix with (1,-1,1)) has rank 3, but is not positive definite.
                              – Federico Poloni
                              Sep 24 at 16:39

                            up vote
                            4
                            down vote

                            Wolfram Language (Mathematica), 20 bytes

                            0<Min@Eigenvalues@#&
                            

                            Try it online!

                            share|improve this answer

                            • Should 4th test case be False?
                              – tsh
                              Sep 23 at 14:48

                            • @tsh Fixed, I am dumb!
                              – Mr. Xcoder
                              Sep 23 at 14:57

                            • 8

                              Funny how Mathematica has a builtin for this, but its name is longer than your solution.
                              – Federico Poloni
                              Sep 23 at 15:04

                            • @FedericoPoloni: Wouldn’t a solution using NullSpace or MatrixRank be even shorter? If Null space is zero then the matrix is positive definite.
                              – Phil H
                              Sep 24 at 15:41

                            • @PhilH No, I’m afraid that doesn’t work by itself. For instance, the second falsey example (diagonal matrix with (1,-1,1)) has rank 3, but is not positive definite.
                              – Federico Poloni
                              Sep 24 at 16:39

                            up vote
                            4
                            down vote

                            up vote
                            4
                            down vote

                            Wolfram Language (Mathematica), 20 bytes

                            0<Min@Eigenvalues@#&
                            

                            Try it online!

                            share|improve this answer

                            Wolfram Language (Mathematica), 20 bytes

                            0<Min@Eigenvalues@#&
                            

                            Try it online!

                            share|improve this answer

                            share|improve this answer

                            share|improve this answer

                            edited Sep 23 at 14:57

                            answered Sep 23 at 12:57

                            Mr. Xcoder

                            30.6k758194

                            30.6k758194

                            • Should 4th test case be False?
                              – tsh
                              Sep 23 at 14:48

                            • @tsh Fixed, I am dumb!
                              – Mr. Xcoder
                              Sep 23 at 14:57

                            • 8

                              Funny how Mathematica has a builtin for this, but its name is longer than your solution.
                              – Federico Poloni
                              Sep 23 at 15:04

                            • @FedericoPoloni: Wouldn’t a solution using NullSpace or MatrixRank be even shorter? If Null space is zero then the matrix is positive definite.
                              – Phil H
                              Sep 24 at 15:41

                            • @PhilH No, I’m afraid that doesn’t work by itself. For instance, the second falsey example (diagonal matrix with (1,-1,1)) has rank 3, but is not positive definite.
                              – Federico Poloni
                              Sep 24 at 16:39

                            • Should 4th test case be False?
                              – tsh
                              Sep 23 at 14:48

                            • @tsh Fixed, I am dumb!
                              – Mr. Xcoder
                              Sep 23 at 14:57

                            • 8

                              Funny how Mathematica has a builtin for this, but its name is longer than your solution.
                              – Federico Poloni
                              Sep 23 at 15:04

                            • @FedericoPoloni: Wouldn’t a solution using NullSpace or MatrixRank be even shorter? If Null space is zero then the matrix is positive definite.
                              – Phil H
                              Sep 24 at 15:41

                            • @PhilH No, I’m afraid that doesn’t work by itself. For instance, the second falsey example (diagonal matrix with (1,-1,1)) has rank 3, but is not positive definite.
                              – Federico Poloni
                              Sep 24 at 16:39

                            Should 4th test case be False?
                            – tsh
                            Sep 23 at 14:48

                            Should 4th test case be False?
                            – tsh
                            Sep 23 at 14:48

                            @tsh Fixed, I am dumb!
                            – Mr. Xcoder
                            Sep 23 at 14:57

                            @tsh Fixed, I am dumb!
                            – Mr. Xcoder
                            Sep 23 at 14:57

                            8

                            8

                            Funny how Mathematica has a builtin for this, but its name is longer than your solution.
                            – Federico Poloni
                            Sep 23 at 15:04

                            Funny how Mathematica has a builtin for this, but its name is longer than your solution.
                            – Federico Poloni
                            Sep 23 at 15:04

                            @FedericoPoloni: Wouldn’t a solution using NullSpace or MatrixRank be even shorter? If Null space is zero then the matrix is positive definite.
                            – Phil H
                            Sep 24 at 15:41

                            @FedericoPoloni: Wouldn’t a solution using NullSpace or MatrixRank be even shorter? If Null space is zero then the matrix is positive definite.
                            – Phil H
                            Sep 24 at 15:41

                            @PhilH No, I’m afraid that doesn’t work by itself. For instance, the second falsey example (diagonal matrix with (1,-1,1)) has rank 3, but is not positive definite.
                            – Federico Poloni
                            Sep 24 at 16:39

                            @PhilH No, I’m afraid that doesn’t work by itself. For instance, the second falsey example (diagonal matrix with (1,-1,1)) has rank 3, but is not positive definite.
                            – Federico Poloni
                            Sep 24 at 16:39

                            up vote
                            3
                            down vote

                            MATL, 4 bytes

                            Yv0>
                            

                            Try it online!

                            I’ve noticed that for the [3 -2 2; -2 4 0; 2 0 2] test case, MATL calculates the 0 eigenvalue with a very small inaccuracy, computing something as low as about $10^{-18}$. This therefore fails the first falsy test case due to precision issues. Thanks to Luis Mendo for pointing out that a non-empty array is truthy iff all its entries differ from 0, saving 1 byte.

                            share|improve this answer

                            • 1

                              @LuisMendo Thanks, I learned something new about MATL today!
                              – Mr. Xcoder
                              Sep 26 at 21:53

                            • My pleasure 🙂 Here’s a better explanation by Suever. I forgot to say that for complex-valued arrays only the real part is compared against zero. So [1 2 3j] is falsey
                              – Luis Mendo
                              Sep 26 at 22:04

                            up vote
                            3
                            down vote

                            MATL, 4 bytes

                            Yv0>
                            

                            Try it online!

                            I’ve noticed that for the [3 -2 2; -2 4 0; 2 0 2] test case, MATL calculates the 0 eigenvalue with a very small inaccuracy, computing something as low as about $10^{-18}$. This therefore fails the first falsy test case due to precision issues. Thanks to Luis Mendo for pointing out that a non-empty array is truthy iff all its entries differ from 0, saving 1 byte.

                            share|improve this answer

                            • 1

                              @LuisMendo Thanks, I learned something new about MATL today!
                              – Mr. Xcoder
                              Sep 26 at 21:53

                            • My pleasure 🙂 Here’s a better explanation by Suever. I forgot to say that for complex-valued arrays only the real part is compared against zero. So [1 2 3j] is falsey
                              – Luis Mendo
                              Sep 26 at 22:04

                            up vote
                            3
                            down vote

                            up vote
                            3
                            down vote

                            MATL, 4 bytes

                            Yv0>
                            

                            Try it online!

                            I’ve noticed that for the [3 -2 2; -2 4 0; 2 0 2] test case, MATL calculates the 0 eigenvalue with a very small inaccuracy, computing something as low as about $10^{-18}$. This therefore fails the first falsy test case due to precision issues. Thanks to Luis Mendo for pointing out that a non-empty array is truthy iff all its entries differ from 0, saving 1 byte.

                            share|improve this answer

                            MATL, 4 bytes

                            Yv0>
                            

                            Try it online!

                            I’ve noticed that for the [3 -2 2; -2 4 0; 2 0 2] test case, MATL calculates the 0 eigenvalue with a very small inaccuracy, computing something as low as about $10^{-18}$. This therefore fails the first falsy test case due to precision issues. Thanks to Luis Mendo for pointing out that a non-empty array is truthy iff all its entries differ from 0, saving 1 byte.

                            share|improve this answer

                            share|improve this answer

                            share|improve this answer

                            edited Sep 26 at 21:55

                            answered Sep 23 at 12:32

                            Mr. Xcoder

                            30.6k758194

                            30.6k758194

                            • 1

                              @LuisMendo Thanks, I learned something new about MATL today!
                              – Mr. Xcoder
                              Sep 26 at 21:53

                            • My pleasure 🙂 Here’s a better explanation by Suever. I forgot to say that for complex-valued arrays only the real part is compared against zero. So [1 2 3j] is falsey
                              – Luis Mendo
                              Sep 26 at 22:04

                            • 1

                              @LuisMendo Thanks, I learned something new about MATL today!
                              – Mr. Xcoder
                              Sep 26 at 21:53

                            • My pleasure 🙂 Here’s a better explanation by Suever. I forgot to say that for complex-valued arrays only the real part is compared against zero. So [1 2 3j] is falsey
                              – Luis Mendo
                              Sep 26 at 22:04

                            1

                            1

                            @LuisMendo Thanks, I learned something new about MATL today!
                            – Mr. Xcoder
                            Sep 26 at 21:53

                            @LuisMendo Thanks, I learned something new about MATL today!
                            – Mr. Xcoder
                            Sep 26 at 21:53

                            My pleasure 🙂 Here’s a better explanation by Suever. I forgot to say that for complex-valued arrays only the real part is compared against zero. So [1 2 3j] is falsey
                            – Luis Mendo
                            Sep 26 at 22:04

                            My pleasure 🙂 Here’s a better explanation by Suever. I forgot to say that for complex-valued arrays only the real part is compared against zero. So [1 2 3j] is falsey
                            – Luis Mendo
                            Sep 26 at 22:04

                            up vote
                            2
                            down vote

                            Mathematica, 29 28 bytes

                            AllTrue[Eigenvalues@#,#>0&]&
                            

                            Definition 2.

                            share|improve this answer

                              up vote
                              2
                              down vote

                              Mathematica, 29 28 bytes

                              AllTrue[Eigenvalues@#,#>0&]&
                              

                              Definition 2.

                              share|improve this answer

                                up vote
                                2
                                down vote

                                up vote
                                2
                                down vote

                                Mathematica, 29 28 bytes

                                AllTrue[Eigenvalues@#,#>0&]&
                                

                                Definition 2.

                                share|improve this answer

                                Mathematica, 29 28 bytes

                                AllTrue[Eigenvalues@#,#>0&]&
                                

                                Definition 2.

                                share|improve this answer

                                share|improve this answer

                                share|improve this answer

                                answered Sep 23 at 12:04

                                Shieru Asakoto

                                1,690311

                                1,690311

                                    up vote
                                    2
                                    down vote

                                    Julia 1.0, 28 bytes

                                    using LinearAlgebra
                                    isposdef
                                    

                                    Try it online!


                                    Julia 0.6, 8 bytes

                                    isposdef
                                    

                                    Try it online!

                                    share|improve this answer

                                      up vote
                                      2
                                      down vote

                                      Julia 1.0, 28 bytes

                                      using LinearAlgebra
                                      isposdef
                                      

                                      Try it online!


                                      Julia 0.6, 8 bytes

                                      isposdef
                                      

                                      Try it online!

                                      share|improve this answer

                                        up vote
                                        2
                                        down vote

                                        up vote
                                        2
                                        down vote

                                        Julia 1.0, 28 bytes

                                        using LinearAlgebra
                                        isposdef
                                        

                                        Try it online!


                                        Julia 0.6, 8 bytes

                                        isposdef
                                        

                                        Try it online!

                                        share|improve this answer

                                        Julia 1.0, 28 bytes

                                        using LinearAlgebra
                                        isposdef
                                        

                                        Try it online!


                                        Julia 0.6, 8 bytes

                                        isposdef
                                        

                                        Try it online!

                                        share|improve this answer

                                        share|improve this answer

                                        share|improve this answer

                                        answered Sep 24 at 5:40

                                        Lyndon White

                                        866512

                                        866512

                                            up vote
                                            2
                                            down vote

                                            MATL, 6 bytes

                                            It is possible to do it using even less bytes, @Mr. Xcoder managed to find a 5 byte MATL answer!

                                            YvX<0>
                                            

                                            Explanation

                                            Yv     compute eigenvalues
                                              X<   take the minimum
                                                0> check whether it is greather than zero
                                            

                                            Try it online!

                                            share|improve this answer

                                            • Fails on the first falsy test case. See my deleted answer.
                                              – Mr. Xcoder
                                              Sep 25 at 6:59

                                            • 1

                                              @Mr.Xcoder Oh, you even submitted an answer before me. I think you should undelete your answer as it just depends on rounding issues. (I think you can expect answers to use limited precision arithmetic – I think only the CAS languages here use exact computations.)
                                              – flawr
                                              Sep 25 at 18:02

                                            • Following your advice, I’ve undeleted it.
                                              – Mr. Xcoder
                                              Sep 25 at 21:18

                                            up vote
                                            2
                                            down vote

                                            MATL, 6 bytes

                                            It is possible to do it using even less bytes, @Mr. Xcoder managed to find a 5 byte MATL answer!

                                            YvX<0>
                                            

                                            Explanation

                                            Yv     compute eigenvalues
                                              X<   take the minimum
                                                0> check whether it is greather than zero
                                            

                                            Try it online!

                                            share|improve this answer

                                            • Fails on the first falsy test case. See my deleted answer.
                                              – Mr. Xcoder
                                              Sep 25 at 6:59

                                            • 1

                                              @Mr.Xcoder Oh, you even submitted an answer before me. I think you should undelete your answer as it just depends on rounding issues. (I think you can expect answers to use limited precision arithmetic – I think only the CAS languages here use exact computations.)
                                              – flawr
                                              Sep 25 at 18:02

                                            • Following your advice, I’ve undeleted it.
                                              – Mr. Xcoder
                                              Sep 25 at 21:18

                                            up vote
                                            2
                                            down vote

                                            up vote
                                            2
                                            down vote

                                            MATL, 6 bytes

                                            It is possible to do it using even less bytes, @Mr. Xcoder managed to find a 5 byte MATL answer!

                                            YvX<0>
                                            

                                            Explanation

                                            Yv     compute eigenvalues
                                              X<   take the minimum
                                                0> check whether it is greather than zero
                                            

                                            Try it online!

                                            share|improve this answer

                                            MATL, 6 bytes

                                            It is possible to do it using even less bytes, @Mr. Xcoder managed to find a 5 byte MATL answer!

                                            YvX<0>
                                            

                                            Explanation

                                            Yv     compute eigenvalues
                                              X<   take the minimum
                                                0> check whether it is greather than zero
                                            

                                            Try it online!

                                            share|improve this answer

                                            share|improve this answer

                                            share|improve this answer

                                            edited Sep 26 at 16:41

                                            answered Sep 23 at 20:41

                                            flawr

                                            25.7k562177

                                            25.7k562177

                                            • Fails on the first falsy test case. See my deleted answer.
                                              – Mr. Xcoder
                                              Sep 25 at 6:59

                                            • 1

                                              @Mr.Xcoder Oh, you even submitted an answer before me. I think you should undelete your answer as it just depends on rounding issues. (I think you can expect answers to use limited precision arithmetic – I think only the CAS languages here use exact computations.)
                                              – flawr
                                              Sep 25 at 18:02

                                            • Following your advice, I’ve undeleted it.
                                              – Mr. Xcoder
                                              Sep 25 at 21:18

                                            • Fails on the first falsy test case. See my deleted answer.
                                              – Mr. Xcoder
                                              Sep 25 at 6:59

                                            • 1

                                              @Mr.Xcoder Oh, you even submitted an answer before me. I think you should undelete your answer as it just depends on rounding issues. (I think you can expect answers to use limited precision arithmetic – I think only the CAS languages here use exact computations.)
                                              – flawr
                                              Sep 25 at 18:02

                                            • Following your advice, I’ve undeleted it.
                                              – Mr. Xcoder
                                              Sep 25 at 21:18

                                            Fails on the first falsy test case. See my deleted answer.
                                            – Mr. Xcoder
                                            Sep 25 at 6:59

                                            Fails on the first falsy test case. See my deleted answer.
                                            – Mr. Xcoder
                                            Sep 25 at 6:59

                                            1

                                            1

                                            @Mr.Xcoder Oh, you even submitted an answer before me. I think you should undelete your answer as it just depends on rounding issues. (I think you can expect answers to use limited precision arithmetic – I think only the CAS languages here use exact computations.)
                                            – flawr
                                            Sep 25 at 18:02

                                            @Mr.Xcoder Oh, you even submitted an answer before me. I think you should undelete your answer as it just depends on rounding issues. (I think you can expect answers to use limited precision arithmetic – I think only the CAS languages here use exact computations.)
                                            – flawr
                                            Sep 25 at 18:02

                                            Following your advice, I’ve undeleted it.
                                            – Mr. Xcoder
                                            Sep 25 at 21:18

                                            Following your advice, I’ve undeleted it.
                                            – Mr. Xcoder
                                            Sep 25 at 21:18

                                            up vote
                                            1
                                            down vote

                                            Maple, 33 bytes

                                            (i.e. my 2 cents)

                                            with(LinearAlgebra):
                                            IsDefinite(A)
                                            

                                            share|improve this answer

                                            • Hello and welcome to PPCG; I am unfamiliar with Maple, though is the newline necessary?
                                              – Jonathan Frech
                                              Sep 24 at 16:06

                                            • @JonathanFrech Hello and thanks. No it’s not. I didn’t count it btw.
                                              – polfosol à° _à° 
                                              Sep 24 at 16:09

                                            • To me your current byte count seems to reflect the newline character.
                                              – Jonathan Frech
                                              Sep 24 at 16:16

                                            • @JonathanFrech Duh, my bad
                                              – polfosol à° _à° 
                                              Sep 24 at 16:18

                                            • 1

                                              Well … now your code and your byte count disagree.
                                              – Jonathan Frech
                                              Sep 24 at 17:07

                                            up vote
                                            1
                                            down vote

                                            Maple, 33 bytes

                                            (i.e. my 2 cents)

                                            with(LinearAlgebra):
                                            IsDefinite(A)
                                            

                                            share|improve this answer

                                            • Hello and welcome to PPCG; I am unfamiliar with Maple, though is the newline necessary?
                                              – Jonathan Frech
                                              Sep 24 at 16:06

                                            • @JonathanFrech Hello and thanks. No it’s not. I didn’t count it btw.
                                              – polfosol à° _à° 
                                              Sep 24 at 16:09

                                            • To me your current byte count seems to reflect the newline character.
                                              – Jonathan Frech
                                              Sep 24 at 16:16

                                            • @JonathanFrech Duh, my bad
                                              – polfosol à° _à° 
                                              Sep 24 at 16:18

                                            • 1

                                              Well … now your code and your byte count disagree.
                                              – Jonathan Frech
                                              Sep 24 at 17:07

                                            up vote
                                            1
                                            down vote

                                            up vote
                                            1
                                            down vote

                                            Maple, 33 bytes

                                            (i.e. my 2 cents)

                                            with(LinearAlgebra):
                                            IsDefinite(A)
                                            

                                            share|improve this answer

                                            Maple, 33 bytes

                                            (i.e. my 2 cents)

                                            with(LinearAlgebra):
                                            IsDefinite(A)
                                            

                                            share|improve this answer

                                            share|improve this answer

                                            share|improve this answer

                                            edited Sep 24 at 16:17

                                            answered Sep 24 at 16:03

                                            polfosol à° _à° 

                                            1114

                                            1114

                                            • Hello and welcome to PPCG; I am unfamiliar with Maple, though is the newline necessary?
                                              – Jonathan Frech
                                              Sep 24 at 16:06

                                            • @JonathanFrech Hello and thanks. No it’s not. I didn’t count it btw.
                                              – polfosol à° _à° 
                                              Sep 24 at 16:09

                                            • To me your current byte count seems to reflect the newline character.
                                              – Jonathan Frech
                                              Sep 24 at 16:16

                                            • @JonathanFrech Duh, my bad
                                              – polfosol à° _à° 
                                              Sep 24 at 16:18

                                            • 1

                                              Well … now your code and your byte count disagree.
                                              – Jonathan Frech
                                              Sep 24 at 17:07

                                            • Hello and welcome to PPCG; I am unfamiliar with Maple, though is the newline necessary?
                                              – Jonathan Frech
                                              Sep 24 at 16:06

                                            • @JonathanFrech Hello and thanks. No it’s not. I didn’t count it btw.
                                              – polfosol à° _à° 
                                              Sep 24 at 16:09

                                            • To me your current byte count seems to reflect the newline character.
                                              – Jonathan Frech
                                              Sep 24 at 16:16

                                            • @JonathanFrech Duh, my bad
                                              – polfosol à° _à° 
                                              Sep 24 at 16:18

                                            • 1

                                              Well … now your code and your byte count disagree.
                                              – Jonathan Frech
                                              Sep 24 at 17:07

                                            Hello and welcome to PPCG; I am unfamiliar with Maple, though is the newline necessary?
                                            – Jonathan Frech
                                            Sep 24 at 16:06

                                            Hello and welcome to PPCG; I am unfamiliar with Maple, though is the newline necessary?
                                            – Jonathan Frech
                                            Sep 24 at 16:06

                                            @JonathanFrech Hello and thanks. No it’s not. I didn’t count it btw.
                                            – polfosol à° _à° 
                                            Sep 24 at 16:09

                                            @JonathanFrech Hello and thanks. No it’s not. I didn’t count it btw.
                                            – polfosol à° _à° 
                                            Sep 24 at 16:09

                                            To me your current byte count seems to reflect the newline character.
                                            – Jonathan Frech
                                            Sep 24 at 16:16

                                            To me your current byte count seems to reflect the newline character.
                                            – Jonathan Frech
                                            Sep 24 at 16:16

                                            @JonathanFrech Duh, my bad
                                            – polfosol à° _à° 
                                            Sep 24 at 16:18

                                            @JonathanFrech Duh, my bad
                                            – polfosol à° _à° 
                                            Sep 24 at 16:18

                                            1

                                            1

                                            Well … now your code and your byte count disagree.
                                            – Jonathan Frech
                                            Sep 24 at 17:07

                                            Well … now your code and your byte count disagree.
                                            – Jonathan Frech
                                            Sep 24 at 17:07

                                            up vote
                                            0
                                            down vote

                                            JavaScript (ES6),  99 95  88 bytes

                                            Same method as nwellnhof’s answer. Returns $0$ or $1$.

                                            f=(m,n=0,R=m[n])=>R?f(m,n+1)&R[m.map((r,y)=>y<n&&R.map((v,x)=>r[x]-=v*r[n]/R[n])),n]>0:1
                                            

                                            Try it online!

                                            share|improve this answer

                                              up vote
                                              0
                                              down vote

                                              JavaScript (ES6),  99 95  88 bytes

                                              Same method as nwellnhof’s answer. Returns $0$ or $1$.

                                              f=(m,n=0,R=m[n])=>R?f(m,n+1)&R[m.map((r,y)=>y<n&&R.map((v,x)=>r[x]-=v*r[n]/R[n])),n]>0:1
                                              

                                              Try it online!

                                              share|improve this answer

                                                up vote
                                                0
                                                down vote

                                                up vote
                                                0
                                                down vote

                                                JavaScript (ES6),  99 95  88 bytes

                                                Same method as nwellnhof’s answer. Returns $0$ or $1$.

                                                f=(m,n=0,R=m[n])=>R?f(m,n+1)&R[m.map((r,y)=>y<n&&R.map((v,x)=>r[x]-=v*r[n]/R[n])),n]>0:1
                                                

                                                Try it online!

                                                share|improve this answer

                                                JavaScript (ES6),  99 95  88 bytes

                                                Same method as nwellnhof’s answer. Returns $0$ or $1$.

                                                f=(m,n=0,R=m[n])=>R?f(m,n+1)&R[m.map((r,y)=>y<n&&R.map((v,x)=>r[x]-=v*r[n]/R[n])),n]>0:1
                                                

                                                Try it online!

                                                share|improve this answer

                                                share|improve this answer

                                                share|improve this answer

                                                edited Sep 23 at 15:24

                                                answered Sep 23 at 14:19

                                                Arnauld

                                                65.7k583278

                                                65.7k583278

                                                     
                                                    draft saved
                                                    draft discarded

                                                     

                                                    draft saved

                                                    draft discarded

                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin(‘.new-post-login’, ‘https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f172677%2fis-the-matrix-positive-definite%23new-answer’, ‘question_page’);
                                                    }
                                                    );

                                                    Post as a guest

                                                    Related Post

                                                    Leave a Reply

                                                    Your email address will not be published. Required fields are marked *