summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/base/math.asy
diff options
context:
space:
mode:
Diffstat (limited to 'Build/source/utils/asymptote/base/math.asy')
-rw-r--r--Build/source/utils/asymptote/base/math.asy14
1 files changed, 14 insertions, 0 deletions
diff --git a/Build/source/utils/asymptote/base/math.asy b/Build/source/utils/asymptote/base/math.asy
index 47044a75df1..8cfd61f4344 100644
--- a/Build/source/utils/asymptote/base/math.asy
+++ b/Build/source/utils/asymptote/base/math.asy
@@ -423,3 +423,17 @@ pair[][] fft(pair[][] a, int sign=1)
}
return transpose(A);
}
+
+// Given a matrix A with independent columns, return
+// the unique vector y minimizing |Ay - b|^2 (the L2 norm).
+// If the columns of A are not linearly independent,
+// throw an error (if warn == true) or return an empty array
+// (if warn == false).
+real[] leastsquares(real[][] A, real[] b, bool warn=true)
+{
+ real[] solution=solve(AtA(A),b*A,warn=false);
+ if (solution.length == 0 && warn)
+ abort("Cannot compute least-squares approximation for " +
+ "a matrix with linearly dependent columns.");
+ return solution;
+}