/// <summary>
        /// /////PCA Generation//////////
        /// </summary>
        public void pca()
        {
            for (int i = 0; i < height * width; i++)
            {
                float sum = 0;
                for (int j = 0; j < trainData; j++)
                {
                    sum += C[i, j];
                }
                mean[i] = (float)(sum / trainData);
            }
            //-------------deviation from mean----------------------
            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    dC[i, j] = (C[i, j] - mean[i]);
                }
            }
            //--------------multiply transpose(dC)*dC---------------

            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    tdC[j, i] = dC[i, j];
                }
            }

            for (int i = 0; i < trainData; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    float sum = 0;
                    for (int k = 0; k < height * width; k++)
                    {
                        sum += tdC[i, k] * dC[k, j];
                    }
                    L[i, j] = sum / (trainData);
                }
            }
            //-------------------calculate Eigenvector of L----------------//
            MWArray       c  = null;
            Eutilityclass Ev = new Eutilityclass();

            c = Ev.Eigenvtr1((MWNumericArray)L);
            MWNumericArray D  = (MWNumericArray)c;
            Array          pA = D.ToVector(MWArrayComponent.Real);

            float[] doubleArray1 = (float[])pA;
            int     icount       = 0;

            for (int i = 0; i < trainData; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    V[j, i] = doubleArray1[icount];
                    sV[j, trainData - i - 1] = doubleArray1[icount];
                    icount++;
                }
            }
            savp("realEig.txt", (float[, ])sV.Clone());

            /*
             * //Eigen value
             * c = Ev.Eigenvlu1((MWNumericArray)L);
             * float[] eigenval = new float[L.GetLength(0)];
             * D = (MWNumericArray)c;
             * pA = D.ToVector(MWArrayComponent.Real);
             * doubleArray1 = (float[])pA;
             * icount = 0;
             * int count = 0;
             * for (int i = 0; i < L.GetLength(0); i++)
             * {
             *  for (int j = 0; j < L.GetLength(1); j++)
             *  {
             *      if (j == i)
             *      {
             *          eigenval[count] = Math.Abs(doubleArray1[icount]);
             *          count++;
             *      }
             *      icount++;
             *  }
             * }
             * Vd = (float[])eigenval;
             * //----------------------V=dC*V----------------------------
             */
            float[] sm  = new float[trainData];
            float[] ssm = new float[trainData];



            temp  = matmul(ref dC, ref V);
            stemp = matmul(ref dC, ref sV);
            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    sm[j]  += temp[i, j] * temp[i, j];
                    ssm[j] += stemp[i, j] * stemp[i, j];
                }
            }
            //Improving classification by dividing by length (i think mormalization)

            /*
             * for (int j = 0; j < trainData; j++)
             * {
             *
             *  sm[j] = (float)Math.Pow(sm[j], .5);
             *  ssm[j] = (float)Math.Pow(ssm[j], .5);
             *
             * }
             * */
            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    temp[i, j]  = temp[i, j] / (float)Math.Pow(sm[j], .5);
                    stemp[i, j] = stemp[i, j] / (float)Math.Pow(ssm[j], .5);
                }
            }

            // obtaining the value of 'r'=tDc*V

            /*
             * float summ = 0;
             *
             * for (int i = 0; i < trainData; i++)
             * {
             *  for (int j = 0; j < trainData; j++)
             *  {
             *
             *      for (int k = 0; k < height * width; k++)
             *      {
             *          summ += tdC[i, k] * stemp[k, j];
             *
             *      }
             *      rc[i, j] = summ;
             *      summ = 0;
             *  }
             * }
             *
             */
            rc = matmul(ref tdC, ref stemp);
        }
        public void execute()
        {
            double[,] nm;
            int m_b;// = noOfSuspects - 1;

            double[,] Phi_w = new double[height * width, noOfImages];
            int[,] t;
            double[,] one;
            double[,] mc;
            double[,] res;
            double[,] Phi_b;
            int person = 0;

            double[] eigenval;
            double[,] eigenvtr;
            double[] eigenval_sbt;
            double[,] eigenvtr_sbt;
            int[]    index;
            double[] aa;
            double[] bb;
            double[] tmp;
            double[,] D_b;
            double[,] mU_Sw_U;
            double[] evl;
            double[,] evr;
            double[,] A;
            double[] D_w;

            //int m_b;
            //int Sb_t;

            //static int l=height*width;
            //static int b=
            for (int i = 0; i < noOfSuspects; i++)
            {
                breadth = imagePerClass[i];
                t       = new int[height * width, breadth];
                one     = new double[1, breadth];
                for (int j = 0; j < breadth; j++)
                {
                    one[0, j] = 1;
                }

                mc = new double[height * width, 1];
                for (int j = 0; j < height * width; j++)
                {
                    mc[j, 0] = mean_c[j, i];
                    for (int k = 0; k < imagePerClass[i]; k++)
                    {
                        t[j, k] = images[j, person + k];
                    }
                }
                nm = new double[mc.GetLength(0) * one.GetLength(0), mc.GetLength(1) * one.GetLength(1)];
                k_t_p(ref mc, ref one, ref nm);
                res = new double[nm.GetLength(0), nm.GetLength(1)];

                for (int j = 0; j < nm.GetLength(0); j++)
                {
                    for (int k = 0; k < nm.GetLength(1); k++)
                    {
                        res[j, k] = t[j, k] - nm[j, k];
                    }
                }
                for (int j = 0; j < height * width; j++)
                {
                    for (int k = person; k < person + imagePerClass[i]; k++)
                    {
                        Phi_w[j, k] = res[j, k - person];
                    }
                }
                person = person + imagePerClass[i];
            }

            //int oo = 0;
            for (int j = 0; j < Phi_w.GetLength(0); j++)
            {
                for (int k = 0; k < Phi_w.GetLength(1); k++)
                {
                    Phi_w[j, k] = Phi_w[j, k] / Math.Sqrt(noOfImages);
                }
            }
            one = new double[1, noOfSuspects];
            for (int j = 0; j < noOfSuspects; j++)
            {
                one[0, j] = 1;
            }
            nm           = new double[mean_i.GetLength(0) * one.GetLength(0), mean_c.GetLength(1) * one.GetLength(1)];
            double[,] mi = new double[height * width, 1];
            for (int j = 0; j < mi.GetLength(0); j++)
            {
                mi[j, 0] = mean_i[j];
            }
            k_t_p(ref mi, ref one, ref nm);
            Phi_b = new double[mean_c.GetLength(0), mean_c.GetLength(1)];
            for (int j = 0; j < mean_c.GetLength(0); j++)
            {
                for (int k = 0; k < mean_c.GetLength(1); k++)
                {
                    Phi_b[j, k] = mean_c[j, k] - nm[j, k];
                }
            }

            for (int i = 0; i < noOfSuspects; i++)
            {
                for (int j = 0; j < Phi_b.GetLength(0); j++)
                {
                    Phi_b[j, i] = Phi_b[j, i] * Math.Sqrt((double)imagePerClass[i] / (double)noOfImages);
                }
            }
            nm  = new double[Phi_b.GetLength(1), Phi_b.GetLength(0)];
            res = new double[nm.GetLength(0), Phi_b.GetLength(1)];
            transpose(ref Phi_b, ref nm);
            matrix_multiply(ref nm, ref Phi_b, ref res);

            ///calculation of Eigenvector of
            MWArray       c  = null;
            Eutilityclass Ev = new Eutilityclass();

            c = Ev.Eigenvtr1((MWNumericArray)res);
            MWNumericArray D  = (MWNumericArray)c;
            Array          pA = D.ToVector(MWArrayComponent.Real);

            double[] doubleArray1 = (double[])pA;
            int      icount       = 0;

            nm = new double[res.GetLength(0), res.GetLength(1)];//eigenvector
            for (int i = 0; i < res.GetLength(0); i++)
            {
                for (int j = 0; j < res.GetLength(1); j++)
                {
                    nm[j, i] = doubleArray1[icount];
                    icount++;
                }
            }
            c        = Ev.Eigenvlu1((MWNumericArray)res);
            eigenval = new double[res.GetLength(0)];

            D            = (MWNumericArray)c;
            pA           = D.ToVector(MWArrayComponent.Real);
            doubleArray1 = (double[])pA;
            icount       = 0;
            int count = 0;

            for (int i = 0; i < res.GetLength(0); i++)
            {
                for (int j = 0; j < res.GetLength(1); j++)
                {
                    if (j == i)
                    {
                        eigenval[count] = Math.Abs(doubleArray1[icount]);
                        count++;
                    }
                    icount++;
                }
            }
            eigenval_sbt = new double[res.GetLength(1)];
            eigenval_sbt = (double[])eigenval.Clone();
            Array.Sort(eigenval);
            index = new int[res.GetLength(0)];

            for (int i = 0; i < eigenval_sbt.GetLength(0); i++)
            {
                index[i]        = eigenval_sbt.GetLength(0) - 1 - Array.IndexOf(eigenval, eigenval[i]);
                eigenval_sbt[i] = eigenval[eigenval_sbt.GetLength(0) - i - 1];
            }
            aa           = new double[res.GetLength(0)];
            eigenvtr_sbt = new double[res.GetLength(0), res.GetLength(1)];
            count        = 0;

            for (int i = 0; i < res.GetLength(0); i++)
            {
                for (int j = 0; j < res.GetLength(1); j++)
                {
                    eigenvtr_sbt[j, (int)index[i]] = nm[j, i];
                }
                aa[i] = eigenval_sbt[i] / eigenval_sbt[0];
                if (aa[i] < thresh_eigval_sb)
                {
                    count++;
                }
            }
            bb    = new double[count];
            m_b   = (int)((Math.Round(((double)(noOfSuspects - 1)) * remain_eigvec)));
            count = 0;
            //double temp;
            for (int i = 0; i < eigenval_sbt.GetLength(0); i++)
            {
                if (aa[i] < thresh_eigval_sb)
                {
                    bb[count] = i;
                    count++;
                }
                //temp=ei
            }
            //MessageBox.Show(""+m_b);
            eigenvtr = new double[eigenvtr_sbt.GetLength(0), m_b];
            eigenval = new double[m_b];
            D_b      = new double[m_b, m_b];
            for (int j = 0; j < m_b; j++)
            {
                for (int i = 0; i < eigenvtr_sbt.GetLength(0); i++)
                {
                    eigenvtr[i, j] = eigenvtr_sbt[i, j];
                    if (i == j)
                    {
                        D_b[i, j] = Math.Pow(eigenval_sbt[j], -1);
                    }
                }
                eigenval[j] = eigenval_sbt[j];
            }
            eigenvtr_sbt = new double[Phi_b.GetLength(0), eigenvtr.GetLength(1)];
            matrix_multiply(ref Phi_b, ref eigenvtr, ref eigenvtr_sbt);
            nm = new double[eigenvtr_sbt.GetLength(0), D_b.GetLength(0)]; ///Z
            matrix_multiply(ref eigenvtr_sbt, ref D_b, ref (nm));
            res = new double[nm.GetLength(1), nm.GetLength(0)];           //Z'
            transpose(ref nm, ref res);
            mc = new double[res.GetLength(0), Phi_w.GetLength(1)];        //mT
            matrix_multiply(ref res, ref Phi_w, ref mc);

            mU_Sw_U = new double[mc.GetLength(0), mc.GetLength(0)];
            res     = new double[mc.GetLength(1), mc.GetLength(0)];//mT'
            transpose(ref mc, ref res);
            matrix_multiply(ref mc, ref res, ref mU_Sw_U);
            Phi_w        = null;
            Phi_b        = null;
            D_b          = null;
            eigenvtr     = null;
            eigenvtr_sbt = null;


            icount       = 0;
            c            = Ev.Eigenvlu1((MWNumericArray)mU_Sw_U);
            evl          = new double[mU_Sw_U.GetLength(1)];
            D            = (MWNumericArray)c;
            pA           = D.ToVector(MWArrayComponent.Real);
            doubleArray1 = (double[])pA;
            for (int i = 0; i < mU_Sw_U.GetLength(0); i++)
            {
                for (int j = 0; j < mU_Sw_U.GetLength(1); j++)
                {
                    if (i == j)
                    {
                        evl[j] = Math.Abs(doubleArray1[icount]);
                    }
                    icount++;
                }
            }

            c            = Ev.Eigenvtr1((MWNumericArray)mU_Sw_U);
            evr          = new double[mU_Sw_U.GetLength(0), mU_Sw_U.GetLength(1)];
            D            = (MWNumericArray)c;
            pA           = D.ToVector(MWArrayComponent.Real);
            doubleArray1 = (double[])pA;
            count        = 0;
            for (int i = 0; i < (mU_Sw_U.GetLength(0)); i++)
            {
                for (int j = 0; j < (mU_Sw_U.GetLength(1)); j++)
                {
                    evr[j, i] = (doubleArray1[count]);
                    count++;
                }
            }
            tmp = new double[evl.GetLength(0)];
            Array.Sort(evl);
            index = new int[evl.GetLength(0)];

            for (int i = 0; i < evl.GetLength(0); i++)
            {
                index[i] = Array.IndexOf(evl, evl[i]);
                //tmp[i] = evl[evl.GetLength(0) - i-1];
            }
            //evl = (double[])tmp.Clone();
            eigenvtr = new double[evr.GetLength(0), evr.GetLength(1)];//U_Vec
            for (int i = 0; i < evr.GetLength(0); i++)
            {
                for (int j = 0; j < evr.GetLength(1); j++)
                {
                    eigenvtr[j, (int)index[i]] = evr[j, i];
                }
            }
            res = new double[nm.GetLength(0), eigenvtr.GetLength(1)];
            matrix_multiply(ref nm, ref eigenvtr, ref res);
            nm = null;
            A  = new double[res.GetLength(1), res.GetLength(0)];
            transpose(ref res, ref A);
            res      = null;
            evr      = null;
            eigenvtr = null;

            D_w = new double[evl.GetLength(0)];

            for (int i = 0; i < D_w.GetLength(0); i++)
            {
                //D_w[i]=eta_sw+evl[i];
                D_w[i] = Math.Pow(eta_sw + evl[i], -(1.0 / 2.0));
            }
            res = new double[D_w.GetLength(0), D_w.GetLength(0)];
            for (int i = 0; i < D_w.GetLength(0); i++)
            {
                res[i, i] = D_w[i];
            }
            D_b        = null;
            D_w        = null;
            DFLD_Trans = new double[res.GetLength(0), A.GetLength(1)];
            matrix_multiply(ref res, ref A, ref DFLD_Trans);
            res = null;
            A   = null;

            y = new double[DFLD_Trans.GetLength(0), mean_c.GetLength(1)];
            matrix_multiply(ref DFLD_Trans, ref mean_c, ref y);
            //int aaaa;
        }
        public void execute()
        {
            double[,] nm;
            int m_b;// = noOfSuspects - 1;
            double[,] Phi_w = new double[height * width, noOfImages];
            int[,] t;
            double[,] one;
            double[,] mc;
            double[,] res;
            double[,] Phi_b;
            int person = 0;
            double[] eigenval;
            double[,] eigenvtr;
            double[] eigenval_sbt;
            double[,] eigenvtr_sbt;
            int[] index;
            double[] aa;
            double[] bb;
            double[] tmp;
            double[,] D_b;
            double[,] mU_Sw_U;
            double[] evl;
            double[,] evr;
            double[,] A;
            double[] D_w;

            //int m_b;
            //int Sb_t;

            //static int l=height*width;
            //static int b=
            for (int i = 0; i < noOfSuspects; i++)
            {
                breadth = imagePerClass[i];
                t = new int[height * width, breadth];
                one = new double[1, breadth];
                for (int j = 0; j < breadth; j++)
                {
                    one[0, j] = 1;
                }

                mc = new double[height * width, 1];
                for (int j = 0; j < height * width; j++)
                {
                    mc[j, 0] = mean_c[j, i];
                    for (int k = 0; k < imagePerClass[i]; k++)
                    {
                        t[j, k] = images[j, person + k];
                    }
                }
                nm = new double[mc.GetLength(0) * one.GetLength(0), mc.GetLength(1) * one.GetLength(1)];
                k_t_p(ref mc, ref one, ref nm);
                res = new double[nm.GetLength(0), nm.GetLength(1)];

                for (int j = 0; j < nm.GetLength(0); j++)
                {
                    for (int k = 0; k < nm.GetLength(1); k++)
                    {
                        res[j, k] = t[j, k] - nm[j, k];
                    }
                }
                for (int j = 0; j < height * width; j++)
                {
                    for (int k = person; k < person + imagePerClass[i]; k++)
                    {
                        Phi_w[j, k] = res[j, k - person];
                    }
                }
                person = person + imagePerClass[i];


            }

            //int oo = 0;
            for (int j = 0; j < Phi_w.GetLength(0); j++)
            {
                for (int k = 0; k < Phi_w.GetLength(1); k++)
                {
                    Phi_w[j, k] = Phi_w[j, k] / Math.Sqrt(noOfImages);
                }
            }
            one = new double[1, noOfSuspects];
            for (int j = 0; j < noOfSuspects; j++)
            {
                one[0, j] = 1;
            }
            nm = new double[mean_i.GetLength(0) * one.GetLength(0), mean_c.GetLength(1) * one.GetLength(1)];
            double[,] mi = new double[height * width, 1];
            for (int j = 0; j < mi.GetLength(0); j++)
            {
                mi[j, 0] = mean_i[j];
            }
            k_t_p(ref mi, ref one, ref nm);
            Phi_b = new double[mean_c.GetLength(0), mean_c.GetLength(1)];
            for (int j = 0; j < mean_c.GetLength(0); j++)
            {
                for (int k = 0; k < mean_c.GetLength(1); k++)
                {
                    Phi_b[j, k] = mean_c[j, k] - nm[j, k];
                }

            }

            for (int i = 0; i < noOfSuspects; i++)
            {
                for (int j = 0; j < Phi_b.GetLength(0); j++)
                {
                    Phi_b[j, i] = Phi_b[j, i] * Math.Sqrt((double)imagePerClass[i] / (double)noOfImages);
                }
            }
            nm = new double[Phi_b.GetLength(1), Phi_b.GetLength(0)];
            res = new double[nm.GetLength(0), Phi_b.GetLength(1)];
            transpose(ref Phi_b, ref nm);
            matrix_multiply(ref nm, ref Phi_b, ref res);

            ///calculation of Eigenvector of
            MWArray c = null;
            Eutilityclass Ev = new Eutilityclass();
            c = Ev.Eigenvtr1((MWNumericArray)res);
            MWNumericArray D = (MWNumericArray)c;
            Array pA = D.ToVector(MWArrayComponent.Real);
            double[] doubleArray1 = (double[])pA;
            int icount = 0;
            nm = new double[res.GetLength(0), res.GetLength(1)];//eigenvector
            for (int i = 0; i < res.GetLength(0); i++)
            {
                for (int j = 0; j < res.GetLength(1); j++)
                {
                    nm[j, i] = doubleArray1[icount];
                    icount++;
                }
            }
            c = Ev.Eigenvlu1((MWNumericArray)res);
            eigenval = new double[res.GetLength(0)];

            D = (MWNumericArray)c;
            pA = D.ToVector(MWArrayComponent.Real);
            doubleArray1 = (double[])pA;
            icount = 0;
            int count = 0;
            for (int i = 0; i < res.GetLength(0); i++)
            {
                for (int j = 0; j < res.GetLength(1); j++)
                {
                    if (j == i)
                    {
                        eigenval[count] = Math.Abs(doubleArray1[icount]);
                        count++;
                    }
                    icount++;
                }
            }
            eigenval_sbt = new double[res.GetLength(1)];
            eigenval_sbt = (double[])eigenval.Clone();
            Array.Sort(eigenval);
            index = new int[res.GetLength(0)];

            for (int i = 0; i < eigenval_sbt.GetLength(0); i++)
            {
                index[i] = eigenval_sbt.GetLength(0) - 1 - Array.IndexOf(eigenval, eigenval[i]);
                eigenval_sbt[i] = eigenval[eigenval_sbt.GetLength(0) - i - 1];
            }
            aa = new double[res.GetLength(0)];
            eigenvtr_sbt = new double[res.GetLength(0), res.GetLength(1)];
            count = 0;

            for (int i = 0; i < res.GetLength(0); i++)
            {
                for (int j = 0; j < res.GetLength(1); j++)
                {
                    eigenvtr_sbt[j, (int)index[i]] = nm[j, i];
                }
                aa[i] = eigenval_sbt[i] / eigenval_sbt[0];
                if (aa[i] < thresh_eigval_sb)
                {
                    count++;
                }
            }
            bb = new double[count];
            m_b = (int)((Math.Round(((double)(noOfSuspects - 1)) * remain_eigvec)));
            count = 0;
            //double temp;
            for (int i = 0; i < eigenval_sbt.GetLength(0); i++)
            {
                if (aa[i] < thresh_eigval_sb)
                {
                    bb[count] = i;
                    count++;
                }
                //temp=ei
            }
            //MessageBox.Show(""+m_b);
            eigenvtr = new double[eigenvtr_sbt.GetLength(0), m_b];
            eigenval = new double[m_b];
            D_b = new double[m_b, m_b];
            for (int j = 0; j < m_b; j++)
            {
                for (int i = 0; i < eigenvtr_sbt.GetLength(0); i++)
                {
                    eigenvtr[i, j] = eigenvtr_sbt[i, j];
                    if (i == j)
                    {
                        D_b[i, j] = Math.Pow(eigenval_sbt[j], -1);
                    }
                }
                eigenval[j] = eigenval_sbt[j];

            }
            eigenvtr_sbt = new double[Phi_b.GetLength(0), eigenvtr.GetLength(1)];
            matrix_multiply(ref Phi_b, ref eigenvtr, ref eigenvtr_sbt);
            nm = new double[eigenvtr_sbt.GetLength(0), D_b.GetLength(0)];///Z
            matrix_multiply(ref eigenvtr_sbt, ref D_b, ref(nm));
            res = new double[nm.GetLength(1), nm.GetLength(0)];//Z'
            transpose(ref nm, ref res);
            mc = new double[res.GetLength(0), Phi_w.GetLength(1)];//mT
            matrix_multiply(ref res, ref Phi_w, ref mc);

            mU_Sw_U = new double[mc.GetLength(0), mc.GetLength(0)];
            res = new double[mc.GetLength(1), mc.GetLength(0)];//mT'
            transpose(ref mc, ref res);
            matrix_multiply(ref mc, ref res, ref mU_Sw_U);
            Phi_w = null;
            Phi_b = null;
            D_b = null;
            eigenvtr = null;
            eigenvtr_sbt = null;


            icount = 0;
            c = Ev.Eigenvlu1((MWNumericArray)mU_Sw_U);
            evl = new double[mU_Sw_U.GetLength(1)];
            D = (MWNumericArray)c;
            pA = D.ToVector(MWArrayComponent.Real);
            doubleArray1 = (double[])pA;
            for (int i = 0; i < mU_Sw_U.GetLength(0); i++)
            {
                for (int j = 0; j < mU_Sw_U.GetLength(1); j++)
                {
                    if (i == j)
                    {
                        evl[j] = Math.Abs(doubleArray1[icount]);
                    }
                    icount++;
                }
            }

            c = Ev.Eigenvtr1((MWNumericArray)mU_Sw_U);
            evr = new double[mU_Sw_U.GetLength(0), mU_Sw_U.GetLength(1)];
            D = (MWNumericArray)c;
            pA = D.ToVector(MWArrayComponent.Real);
            doubleArray1 = (double[])pA;
            count = 0;
            for (int i = 0; i < (mU_Sw_U.GetLength(0)); i++)
            {
                for (int j = 0; j < (mU_Sw_U.GetLength(1)); j++)
                {

                    evr[j, i] = (doubleArray1[count]);
                    count++;
                }
            }
            tmp = new double[evl.GetLength(0)];
            Array.Sort(evl);
            index = new int[evl.GetLength(0)];

            for (int i = 0; i < evl.GetLength(0); i++)
            {
                index[i] = Array.IndexOf(evl, evl[i]);
                //tmp[i] = evl[evl.GetLength(0) - i-1];
            }
            //evl = (double[])tmp.Clone();
            eigenvtr = new double[evr.GetLength(0), evr.GetLength(1)];//U_Vec
            for (int i = 0; i < evr.GetLength(0); i++)
            {
                for (int j = 0; j < evr.GetLength(1); j++)
                {
                    eigenvtr[j, (int)index[i]] = evr[j, i];

                }

            }
            res = new double[nm.GetLength(0), eigenvtr.GetLength(1)];
            matrix_multiply(ref nm, ref eigenvtr, ref res);
            nm = null;
            A = new double[res.GetLength(1), res.GetLength(0)];
            transpose(ref res, ref A);
            res = null;
            evr = null;
            eigenvtr = null;

            D_w = new double[evl.GetLength(0)];

            for (int i = 0; i < D_w.GetLength(0); i++)
            {
                //D_w[i]=eta_sw+evl[i];
                D_w[i] = Math.Pow(eta_sw + evl[i], -(1.0 / 2.0));
            }
            res = new double[D_w.GetLength(0), D_w.GetLength(0)];
            for (int i = 0; i < D_w.GetLength(0); i++)
            {
                res[i, i] = D_w[i];
            }
            D_b = null;
            D_w = null;
            DFLD_Trans = new double[res.GetLength(0), A.GetLength(1)];
            matrix_multiply(ref res, ref A, ref DFLD_Trans);
            res = null;
            A = null;

            y = new double[DFLD_Trans.GetLength(0), mean_c.GetLength(1)];
            matrix_multiply(ref DFLD_Trans, ref mean_c, ref y);
            //int aaaa;
        }
        /// <summary>
        /// /////PCA Generation//////////
        /// </summary>
        public void pca()
        {

            for (int i = 0; i < height * width; i++)
            {
                float sum = 0;
                for (int j = 0; j < trainData; j++)
                {
                    sum += C[i, j];

                }
                mean[i] = (float)(sum / trainData);

            }
            //-------------deviation from mean----------------------
            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {

                    dC[i, j] = (C[i, j] - mean[i]);

                }

            }
            //--------------multiply transpose(dC)*dC---------------

            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    tdC[j, i] = dC[i, j];
                }
            }

            for (int i = 0; i < trainData; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    float sum = 0;
                    for (int k = 0; k < height * width; k++)
                    {
                        sum += tdC[i, k] * dC[k, j];

                    }
                    L[i, j] = sum / (trainData);
                }
            }
            //-------------------calculate Eigenvector of L----------------//
            MWArray c = null;
            Eutilityclass Ev = new Eutilityclass();
            c = Ev.Eigenvtr1((MWNumericArray)L);
            MWNumericArray D = (MWNumericArray)c;
            Array pA = D.ToVector(MWArrayComponent.Real);
            float[] doubleArray1 = (float[])pA;
            int icount = 0;
            for (int i = 0; i < trainData; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    V[j, i] = doubleArray1[icount];
                    sV[j, trainData - i - 1] = doubleArray1[icount];
                    icount++;
                }
            }
            savp("realEig.txt", (float[,])sV.Clone());
            /*
            //Eigen value
            c = Ev.Eigenvlu1((MWNumericArray)L);
            float[] eigenval = new float[L.GetLength(0)];
            D = (MWNumericArray)c;
            pA = D.ToVector(MWArrayComponent.Real);
            doubleArray1 = (float[])pA;
            icount = 0;
            int count = 0;
            for (int i = 0; i < L.GetLength(0); i++)
            {
                for (int j = 0; j < L.GetLength(1); j++)
                {
                    if (j == i)
                    {
                        eigenval[count] = Math.Abs(doubleArray1[icount]);
                        count++;
                    }
                    icount++;
                }
            }
            Vd = (float[])eigenval;
            //----------------------V=dC*V----------------------------
            */
            float[] sm = new float[trainData];
            float[] ssm = new float[trainData];



            temp = matmul(ref dC, ref V);
            stemp = matmul(ref dC, ref sV);
            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {

                    sm[j] += temp[i, j] * temp[i, j];
                    ssm[j] += stemp[i, j] * stemp[i, j];

                }
            }
            //Improving classification by dividing by length (i think mormalization)
            /*
            for (int j = 0; j < trainData; j++)
            {

                sm[j] = (float)Math.Pow(sm[j], .5);
                ssm[j] = (float)Math.Pow(ssm[j], .5);

            }
             * */
            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {

                    temp[i, j] = temp[i, j] / (float)Math.Pow(sm[j], .5);
                    stemp[i, j] = stemp[i, j] / (float)Math.Pow(ssm[j], .5);

                }
            }

            // obtaining the value of 'r'=tDc*V
            /*
            float summ = 0;
            
            for (int i = 0; i < trainData; i++)
            {
                for (int j = 0; j < trainData; j++)
                {

                    for (int k = 0; k < height * width; k++)
                    {
                        summ += tdC[i, k] * stemp[k, j];

                    }
                    rc[i, j] = summ;
                    summ = 0;
                }
            }
             * 
             */
            rc = matmul(ref tdC, ref stemp);


        }