Is the Matrix Positive-Definite?
Clash 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
code-golf math decision-problem matrix
add a comment |Â
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
code-golf math decision-problem matrix
This challenge was sandboxed.
– SEJPM
1 hour ago
add a comment |Â
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
code-golf math decision-problem matrix
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
code-golf math decision-problem matrix
edited 1 hour ago
asked 1 hour ago


SEJPM
1,5631232
1,5631232
This challenge was sandboxed.
– SEJPM
1 hour ago
add a comment |Â
This challenge was sandboxed.
– SEJPM
1 hour ago
This challenge was sandboxed.
– SEJPM
1 hour ago
This challenge was sandboxed.
– SEJPM
1 hour ago
add a comment |Â
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.
add a comment |Â
up vote
1
down vote
Wolfram Language (Mathematica), 20 bytes
#>0&@@Eigenvalues@#&
Try it online!
add a comment |Â
up vote
0
down vote
Mathematica, 29 28 bytes
AllTrue[Eigenvalues@#,#>0&]&
Definition 2.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered 17 mins ago
nwellnhof
3,673714
3,673714
add a comment |Â
add a comment |Â
up vote
1
down vote
Wolfram Language (Mathematica), 20 bytes
#>0&@@Eigenvalues@#&
Try it online!
add a comment |Â
up vote
1
down vote
Wolfram Language (Mathematica), 20 bytes
#>0&@@Eigenvalues@#&
Try it online!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Wolfram Language (Mathematica), 20 bytes
#>0&@@Eigenvalues@#&
Try it online!
Wolfram Language (Mathematica), 20 bytes
#>0&@@Eigenvalues@#&
Try it online!
answered 44 mins ago


Mr. Xcoder
30.5k758194
30.5k758194
add a comment |Â
add a comment |Â
up vote
0
down vote
Mathematica, 29 28 bytes
AllTrue[Eigenvalues@#,#>0&]&
Definition 2.
add a comment |Â
up vote
0
down vote
Mathematica, 29 28 bytes
AllTrue[Eigenvalues@#,#>0&]&
Definition 2.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Mathematica, 29 28 bytes
AllTrue[Eigenvalues@#,#>0&]&
Definition 2.
Mathematica, 29 28 bytes
AllTrue[Eigenvalues@#,#>0&]&
Definition 2.
answered 1 hour ago
Shieru Asakoto
1,600311
1,600311
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
This challenge was sandboxed.
– SEJPM
1 hour ago