summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/asymptote/math.asy
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/asymptote/math.asy')
-rw-r--r--Master/texmf-dist/asymptote/math.asy26
1 files changed, 26 insertions, 0 deletions
diff --git a/Master/texmf-dist/asymptote/math.asy b/Master/texmf-dist/asymptote/math.asy
index 47044a75df1..9d10e2b7794 100644
--- a/Master/texmf-dist/asymptote/math.asy
+++ b/Master/texmf-dist/asymptote/math.asy
@@ -423,3 +423,29 @@ 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;
+}
+
+// Namespace
+struct rootfinder_settings {
+ static real roottolerance = 1e-4;
+}
+
+real findroot(real f(real), real a, real b,
+ real tolerance=rootfinder_settings.roottolerance,
+ real fa=f(a), real fb=f(b))
+{
+ return _findroot(f,a,b,tolerance,fa,fb);
+}