Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
21786f51
Unverified
Kaydet (Commit)
21786f51
authored
Agu 29, 2018
tarafından
Raymond Hettinger
Kaydeden (comit)
GitHub
Agu 29, 2018
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Improve commutativity of math.hypot() and math.dist() (GH-8984)
üst
124b9eb4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
13 deletions
+19
-13
mathmodule.c
Modules/mathmodule.c
+19
-13
No files found.
Modules/mathmodule.c
Dosyayı görüntüle @
21786f51
...
...
@@ -2037,26 +2037,32 @@ where *max* is the largest value in the vector, compute:
max * sqrt(sum((x / max) ** 2 for x in vec))
When a maximum value is found, it is swapped to the end. This
lets us skip one loop iteration and just add 1.0 at the end.
Saving the largest value for last also helps improve accuracy.
Kahan summation is used to improve accuracy. The *csum*
variable tracks the cumulative sum and *frac* tracks
fractional round-off error for the most recent addition.
The value of the *max* variable must be present in *vec*
or should equal to 0.0 when n==0. Likewise, *max* will
be INF if an infinity is present in the vec.
The *found_nan* variable indicates whether some member of
the *vec* is a NaN.
To improve accuracy and to increase the number of cases where
vector_norm() is commutative, we use a variant of Neumaier
summation specialized to exploit that we always know that
|csum| >= |x|.
The *csum* variable tracks the cumulative sum and *frac* tracks
the cumulative fractional errors at each step. Since this
variant assumes that |csum| >= |x| at each step, we establish
the precondition by starting the accumulation from 1.0 which
represents an entry equal to *max*. This also provides a nice
side benefit in that it lets us skip over a *max* entry (which
is swapped into *last*) saving us one iteration through the loop.
*/
static
inline
double
vector_norm
(
Py_ssize_t
n
,
double
*
vec
,
double
max
,
int
found_nan
)
{
double
x
,
csum
=
0
.
0
,
oldcsum
,
frac
=
0
.
0
,
last
;
double
x
,
csum
=
1
.
0
,
oldcsum
,
frac
=
0
.
0
,
last
;
Py_ssize_t
i
;
if
(
Py_IS_INFINITY
(
max
))
{
...
...
@@ -2078,14 +2084,14 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
last
=
max
;
}
x
/=
max
;
x
=
x
*
x
-
frac
;
x
=
x
*
x
;
assert
(
csum
>=
x
);
oldcsum
=
csum
;
csum
+=
x
;
frac
=
(
csum
-
oldcsum
)
-
x
;
frac
+=
(
oldcsum
-
csum
)
+
x
;
}
assert
(
last
==
max
);
csum
+=
1
.
0
-
frac
;
return
max
*
sqrt
(
csum
);
return
max
*
sqrt
(
csum
+
frac
);
}
#define NUM_STACK_ELEMS 16
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment