gradsearch: fixing normalization to improve convergence
/*
- * Copyright (c) 2007 - 2019 Joseph Gaeddert
+ * Copyright (c) 2007 - 2021 Joseph Gaeddert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -328,19 +328,19 @@ float gradsearch_linesearch(utility_function _utility,
float gradsearch_norm(float * _v,
unsigned int _n)
{
- // start with a small value still big enough
- // to avoid warning in gradstep function
- float vnorm = 1.0e-7f;
-
+ // compute l2-norm
+ float vnorm = 0.0f;
unsigned int i;
for (i=0; i<_n; i++)
vnorm += _v[i]*_v[i];
-
vnorm = sqrtf(vnorm);
+ // scale values (avoiding division by zero)
+ float scale = vnorm == 0.0f ? 0.0f : 1.0f / vnorm;
for (i=0; i<_n; i++)
- _v[i] /= vnorm;
+ _v[i] *= scale;
+ // return normalization
return vnorm;
}
