Is the Matrix Positive-Definite?

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











up vote
3
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.



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.

  • Let $lambda_iquad iin1,ldots,n$ be the eigenvalues of $A$, if now $forall iin1,ldots,n:lambda_i>0$ (that is all eigenvalues are positive) then $A$ is positive-definite.

  • 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.

Examples



For truthy output



beginpmatrix1&0&0\0&1&0\0&0&1endpmatrix



beginpmatrix5&2&-1\2&1&-1\-1&-1&3endpmatrix



beginpmatrix1&-2&2\-2&5&0\2&0&30endpmatrix



For falsey output



(at least one eigenvalue is 0 / positive semi-definite)
beginpmatrix3&-2&2\-2&4&0\2&0&2endpmatrix



(eigenvalues have different signs / indefinite)
beginpmatrix1&0&0\0&-1&0\0&0&1endpmatrix



(all eigenvalues smaller than 0 / negative definite)
beginpmatrix-1&0&0\0&-1&0\0&0&-1endpmatrix



(all eigenvalues smaller than 0 / negative definite)
beginpmatrix-2&3&0\3&-5&0\0&0&-1endpmatrix










share|improve this question























  • This challenge was sandboxed.
    – SEJPM
    1 hour ago















up vote
3
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.



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.

  • Let $lambda_iquad iin1,ldots,n$ be the eigenvalues of $A$, if now $forall iin1,ldots,n:lambda_i>0$ (that is all eigenvalues are positive) then $A$ is positive-definite.

  • 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.

Examples



For truthy output



beginpmatrix1&0&0\0&1&0\0&0&1endpmatrix



beginpmatrix5&2&-1\2&1&-1\-1&-1&3endpmatrix



beginpmatrix1&-2&2\-2&5&0\2&0&30endpmatrix



For falsey output



(at least one eigenvalue is 0 / positive semi-definite)
beginpmatrix3&-2&2\-2&4&0\2&0&2endpmatrix



(eigenvalues have different signs / indefinite)
beginpmatrix1&0&0\0&-1&0\0&0&1endpmatrix



(all eigenvalues smaller than 0 / negative definite)
beginpmatrix-1&0&0\0&-1&0\0&0&-1endpmatrix



(all eigenvalues smaller than 0 / negative definite)
beginpmatrix-2&3&0\3&-5&0\0&0&-1endpmatrix










share|improve this question























  • This challenge was sandboxed.
    – SEJPM
    1 hour ago













up vote
3
down vote

favorite









up vote
3
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.



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.

  • Let $lambda_iquad iin1,ldots,n$ be the eigenvalues of $A$, if now $forall iin1,ldots,n:lambda_i>0$ (that is all eigenvalues are positive) then $A$ is positive-definite.

  • 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.

Examples



For truthy output



beginpmatrix1&0&0\0&1&0\0&0&1endpmatrix



beginpmatrix5&2&-1\2&1&-1\-1&-1&3endpmatrix



beginpmatrix1&-2&2\-2&5&0\2&0&30endpmatrix



For falsey output



(at least one eigenvalue is 0 / positive semi-definite)
beginpmatrix3&-2&2\-2&4&0\2&0&2endpmatrix



(eigenvalues have different signs / indefinite)
beginpmatrix1&0&0\0&-1&0\0&0&1endpmatrix



(all eigenvalues smaller than 0 / negative definite)
beginpmatrix-1&0&0\0&-1&0\0&0&-1endpmatrix



(all eigenvalues smaller than 0 / negative definite)
beginpmatrix-2&3&0\3&-5&0\0&0&-1endpmatrix










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.



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.

  • Let $lambda_iquad iin1,ldots,n$ be the eigenvalues of $A$, if now $forall iin1,ldots,n:lambda_i>0$ (that is all eigenvalues are positive) then $A$ is positive-definite.

  • 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.

Examples



For truthy output



beginpmatrix1&0&0\0&1&0\0&0&1endpmatrix



beginpmatrix5&2&-1\2&1&-1\-1&-1&3endpmatrix



beginpmatrix1&-2&2\-2&5&0\2&0&30endpmatrix



For falsey output



(at least one eigenvalue is 0 / positive semi-definite)
beginpmatrix3&-2&2\-2&4&0\2&0&2endpmatrix



(eigenvalues have different signs / indefinite)
beginpmatrix1&0&0\0&-1&0\0&0&1endpmatrix



(all eigenvalues smaller than 0 / negative definite)
beginpmatrix-1&0&0\0&-1&0\0&0&-1endpmatrix



(all eigenvalues smaller than 0 / negative definite)
beginpmatrix-2&3&0\3&-5&0\0&0&-1endpmatrix







code-golf math decision-problem matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked 1 hour ago









SEJPM

1,5631232




1,5631232











  • This challenge was sandboxed.
    – SEJPM
    1 hour ago

















  • This challenge was sandboxed.
    – SEJPM
    1 hour ago
















This challenge was sandboxed.
– SEJPM
1 hour ago





This challenge was sandboxed.
– SEJPM
1 hour ago











3 Answers
3






active

oldest

votes

















up vote
2
down vote














C (gcc), 112 bytes





f(M,n,i)double**M;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



























    up vote
    1
    down vote














    Wolfram Language (Mathematica), 20 bytes



    #>0&@@Eigenvalues@#&


    Try it online!






    share|improve this answer



























      up vote
      0
      down vote













      Mathematica, 29 28 bytes



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


      Definition 2.






      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






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote














        C (gcc), 112 bytes





        f(M,n,i)double**M;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
























          up vote
          2
          down vote














          C (gcc), 112 bytes





          f(M,n,i)double**M;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






















            up vote
            2
            down vote










            up vote
            2
            down vote










            C (gcc), 112 bytes





            f(M,n,i)double**M;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 (gcc), 112 bytes





            f(M,n,i)double**M;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










            answered 17 mins ago









            nwellnhof

            3,673714




            3,673714




















                up vote
                1
                down vote














                Wolfram Language (Mathematica), 20 bytes



                #>0&@@Eigenvalues@#&


                Try it online!






                share|improve this answer
























                  up vote
                  1
                  down vote














                  Wolfram Language (Mathematica), 20 bytes



                  #>0&@@Eigenvalues@#&


                  Try it online!






                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote










                    Wolfram Language (Mathematica), 20 bytes



                    #>0&@@Eigenvalues@#&


                    Try it online!






                    share|improve this answer













                    Wolfram Language (Mathematica), 20 bytes



                    #>0&@@Eigenvalues@#&


                    Try it online!







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 44 mins ago









                    Mr. Xcoder

                    30.5k758194




                    30.5k758194




















                        up vote
                        0
                        down vote













                        Mathematica, 29 28 bytes



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


                        Definition 2.






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          Mathematica, 29 28 bytes



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


                          Definition 2.






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            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 1 hour ago









                            Shieru Asakoto

                            1,600311




                            1,600311



























                                 

                                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













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                List of Gilmore Girls characters

                                One-line joke