411 REFERENCE · Wikibooks
Linear Algebra and the C Language/a0id
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
__NOTOC__
Install and compile this file in your working directory.
/* ------------------------------------ */
/* Save as : c00e.c */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
void fun(int r)
{
double **A = rdefinite_positive_mR( i_mR(r,r), 99);
double **SqrtA = i_mR(r,r);
double **invSqrtA = i_mR(r,r);
double **V = i_mR(r,r);
double **VT = i_mR(r,r);
double **EValue = i_mR(r,r);
double **f_EValue = i_mR(r,r);
double **T = i_mR(r,r);
clrscrn();
printf(" A positive-matrix is positive definite\n"
" if and only if its positive square root\n"
" is invertible.\n\n"
" A:");
p_mR(A, S6,P0, C8);
stop();
clrscrn();
/* Eigenvector */
eigs_V_mR(A,V);
transpose_mR(V,VT);
/* EigenValue = VT * A * V */
mul_mR(VT,A,T);
mul_mR(T,V,EValue);
printf(" sqrt(A) = V * sqrt(EValue) * VT\n");
f_eigs_mR(sqrt,EValue,f_EValue);
mul_mR(V,f_EValue,T);
mul_mR(T,VT, SqrtA);
p_mR(SqrtA, S10,P4, C8);
printf(" inv(sqrt(A)):");
inv_mR(SqrtA,invSqrtA);
p_mR(invSqrtA, S10,P4, C8);
printf(" IDE = sqrt(A) inv(sqrt(A))");
mul_mR(SqrtA,invSqrtA,T );
p_mR(T, S10,P4, C8);
f_mR(A);
f_mR(SqrtA);
f_mR(invSqrtA);
f_mR(V);
f_mR(VT);
f_mR(EValue);
f_mR(f_EValue);
f_mR(T);
}
/* ------------------------------------ */
int main(void)
{
time_t t;
srand(time(&t));
do
{
fun(RC4);
} while(stop_w());
return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */
A positive-matrix is positive definite if and only if its positive square root is invertible.
Screen output example:
A positive-matrix is positive definite
if and only if its positive square root
is invertible.
A:
+8879 +8199 -2826 -2143
+8199 +12315 -1166 +2745
-2826 -1166 +11572 +3808
-2143 +2745 +3808 +7469
Press return to continue.
sqrt(A) = V * sqrt(EValue) * VT
+77.3385 +48.9529 -12.1061 -18.8362
+48.9529 +97.1459 -4.9575 +21.3707
-12.1061 -4.9575 +104.8783 +20.0350
-18.8362 +21.3707 +20.0350 +79.0954
inv(sqrt(A)):
+0.0245 -0.0144 +0.0003 +0.0097
-0.0144 +0.0196 +0.0010 -0.0090
+0.0003 +0.0010 +0.0101 -0.0028
+0.0097 -0.0090 -0.0028 +0.0181
IDE = sqrt(A) inv(sqrt(A))
+1.0000 +0.0000 -0.0000 +0.0000
+0.0000 +1.0000 -0.0000 +0.0000
-0.0000 -0.0000 +1.0000 +0.0000
+0.0000 -0.0000 -0.0000 +1.0000
Press return to continue
Press X return to stop