summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaron Kent-Dobias <jaron@kent-dobias.com>2025-02-10 08:24:43 -0300
committerJaron Kent-Dobias <jaron@kent-dobias.com>2025-02-10 08:24:43 -0300
commitefd5b5bea4640e498686f1c7f2eae44d2caad44b (patch)
treea9b6f4656c4961a0d9492127df511d25917993ea
parent9a9f982ff10e2130475204985c6f98637ce826b3 (diff)
downloadictp-saifr_colloquium-efd5b5bea4640e498686f1c7f2eae44d2caad44b.tar.gz
ictp-saifr_colloquium-efd5b5bea4640e498686f1c7f2eae44d2caad44b.tar.bz2
ictp-saifr_colloquium-efd5b5bea4640e498686f1c7f2eae44d2caad44b.zip
Slightly more effecient calculation of gradient
-rw-r--r--fits.cpp7
1 files changed, 4 insertions, 3 deletions
diff --git a/fits.cpp b/fits.cpp
index 370b63d..62d75c9 100644
--- a/fits.cpp
+++ b/fits.cpp
@@ -57,11 +57,12 @@ Real cost(const std::list<std::tuple<Real, Real>>& data, const Vector& coeffs) {
Vector dCost(const std::list<std::tuple<Real, Real>>& data, const Vector& coeffs) {
Vector dC = Vector::Zero(coeffs.size());
- for (unsigned j = 0; j < coeffs.size(); j++) {
- for (const std::tuple<Real, Real>& xy : data) {
+ for (const std::tuple<Real, Real>& xy : data) {
+ for (unsigned j = 0; j < coeffs.size(); j++) {
Real x = std::get<0>(xy);
Real y = std::get<1>(xy);
- dC[j] += (value(coeffs, x) - y) * basisFunctions(coeffs.size(), j, x);
+ Real Δc = value(coeffs, x) - y;
+ dC[j] += Δc * basisFunctions(coeffs.size(), j, x);
}
}