예제 #1
0
파일: KMeans.cs 프로젝트: GitZHCODE/zCode
        /// <summary>
        ///
        /// </summary>
        private void UpdateClusterCenters()
        {
            _clusterSizes.Clear();

            for (int i = 0; i < _clusterCenters.Length; i++)
            {
                _clusterCenters[i].Clear();
            }

            // aggregate
            for (int i = 0; i < _points.Length; i++)
            {
                var point   = _points[i];
                int nearest = _nearestClusters[i];

                var center = _clusterCenters[nearest];
                ArrayMath.Add(center, point, center);

                _clusterSizes[nearest]++;
            }

            // average
            for (int i = 0; i < _clusterCenters.Length; i++)
            {
                var size = _clusterSizes[i];
                if (size == 0)
                {
                    continue;
                }

                var center = _clusterCenters[i];
                ArrayMath.Scale(center, 1.0 / size, center);
            }
        }
예제 #2
0
            /// <summary>
            ///
            /// </summary>
            public static bool Unitize(double[] vector, int count, double[] result)
            {
                double d = ArrayMath.Dot(vector, vector, count);

                if (d > 0.0)
                {
                    Scale(vector, 1.0 / Math.Sqrt(d), count, result);
                    return(true);
                }

                return(false);
            }
예제 #3
0
 /// <summary>
 ///
 /// </summary>
 public static void Slerp(double[] v0, double[] v1, double t, double[] result)
 {
     Slerp(v0, v1, t, ArrayMath.Angle(v0, v1), v0.Length, result);
 }