예제 #1
0
        /// <summary>
        /// Calculates the eigenvalues and the eigenfaces.
        /// </summary>
        private void CalculateEigenValues()
        {
            //25 = magicnumber, just needed to initialize double array
            this._eigenValues = new double[25];
            _s.Diagonal.ExportValues(ref this._eigenValues);
            var eigenValuesPow = new double[_eigenValues.Length];

            for (var i = 0; i < eigenValuesPow.Length; i++)
            {
                eigenValuesPow[i] = (double)Math.Pow((double)_eigenValues[i], -0.5);
            }

            this._eigenVectors.Clear();

            for (var i = 0; i < eigenValuesPow.Length; i++)
            {
                var oneEigenVector = (ILArray <double>)_v.Subarray(new string[]
                {
                    ":",
                    i.ToString()
                });
                var oneFace  = ILMath.multiply(_a, oneEigenVector);
                var oneFace2 = ILMath.multiply(oneFace, eigenValuesPow[i]);

                //25 = magicnumber, just needed to initialize double array
                var eigenVectorArray = new double[25];
                oneFace2.ExportValues(ref eigenVectorArray);

                var distance = 0.0;
                for (var j = 0; j < eigenVectorArray.Length; j++)
                {
                    distance += Math.Pow((double)eigenVectorArray[j], 2.0);
                }

                distance = Math.Sqrt(distance);
                for (var j = 0; j < eigenVectorArray.Length; j++)
                {
                    eigenVectorArray[j] /= (float)distance;
                }
                this._eigenVectors.Add(eigenVectorArray);
            }

            this._eigenWeights.Clear();

            for (var i = 0; i < this._vectorSet.Count; i++)
            {
                var existingWeight = this.GetEigenWeight((byte[])this._vectorSet[i], this._eigenVectors.Count);
                this._eigenWeights.Add(existingWeight);
            }
        }