about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/KPMtools/checkpsd.m
diff options
context:
space:
mode:
Diffstat (limited to 'sourcecodes/bnt-master/KPMtools/checkpsd.m')
-rw-r--r--sourcecodes/bnt-master/KPMtools/checkpsd.m15
1 files changed, 15 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/KPMtools/checkpsd.m b/sourcecodes/bnt-master/KPMtools/checkpsd.m
new file mode 100644
index 00000000..cbd35203
--- /dev/null
+++ b/sourcecodes/bnt-master/KPMtools/checkpsd.m
@@ -0,0 +1,15 @@
+function s = checkpsd(s)
+
+if (any(isnan(s) | isinf(s) | ~isreal(s)))
+  warning('S contains complex numbers, Inf, or NaN'); 
+end
+% Drop any negative eigenvalues.
+[V, D] = eig(full(s));
+d = real(diag(D));
+if (any(d < 0))
+  warning(sprintf(['S is not positive semidefinite (min. eig. =' ...
+		   ' %0.5g); projecting.'], min(d)));
+  d(find(d < 0)) = 0;
+  D = diag(d);
+  s = V * D * V';
+end