コード例 #1
0
        // End Initialize() in MDSLinearAlgebra

        //  MaxIndicator = 0 Find Maximum Eigenvalue of Matrix
        //  MaxIndicator = 1 Find Maximum Eigenvalue of (Maximizr - Matrix)
        public static int PowerIterate(Desertwind Solution, int MaxIndicator, double Maximizer,
                                       out double PowerEigenvalue)
        {
            if (DistributedNewIteratedVector == null)
            {
                Initialize();
            }
            Hotsun.UseDiagonalScaling       = true;
            Hotsun.AddMarquardtQDynamically = false;

            //  Set up Initial Power Vectors
            SetInitialPowerVector(DistributedNewIteratedVector);
            double AdotA = SALSABLAS.VectorScalarProduct(DistributedNewIteratedVector, DistributedNewIteratedVector);

            int somethingtodo       = 0;
            int PowerIterationCount = 0;

            PowerEigenvalue = -1.0;
            while (true)
            {
                // Iterate over Power Multiplications by Chisq Matrix

                //  Normalize Current A and move from New to Old

                double OldNorm = 1.0 / Math.Sqrt(AdotA);
                SALSABLAS.LinearCombineVector(DistributedOldIteratedVector, OldNorm,
                                              DistributedNewIteratedVector, 0.0, DistributedNewIteratedVector);

                //  Make a Global Vector of DistributedOldIteratedVector
                ManxcatCentral.MakeVectorGlobal(DistributedOldIteratedVector, GlobalOldIteratedVector);

                //  Form Chisq Matrix Product with Old Vector
                if (Hotsun.FullSecondDerivative)
                {
                    UserRoutines.GlobalMatrixVectorProduct(DistributedNewIteratedVector, Solution, true,
                                                           Hotsun.GlobalParameter, GlobalOldIteratedVector);
                }
                else
                {
                    UserRoutines.GlobalMatrixVectorProduct(DistributedNewIteratedVector, Solution, false,
                                                           Hotsun.GlobalParameter, GlobalOldIteratedVector);
                }

                //  Correct case MaxIndicator = 1
                if (MaxIndicator > 0)
                {
                    SALSABLAS.LinearCombineVector(DistributedNewIteratedVector, -1.0, DistributedNewIteratedVector,
                                                  Maximizer, DistributedOldIteratedVector);
                }
                SALSABLAS.LinearCombineVector(DistributedNewIteratedVector, 1.0, DistributedNewIteratedVector,
                                              Hotsun.addonforQcomputation, DistributedOldIteratedVector);

                //  Form Scalar Products
                double NewEigenvalue = SALSABLAS.VectorScalarProduct(DistributedNewIteratedVector,
                                                                     DistributedOldIteratedVector);
                AdotA = SALSABLAS.VectorScalarProduct(DistributedNewIteratedVector, DistributedNewIteratedVector);

                ++PowerIterationCount;
                somethingtodo = -1;
                if (SALSAUtility.MPI_Rank == 0)
                {
                    if (PowerIterationCount > 10 && (NewEigenvalue > 0.0))
                    {
                        // Arbitary criteria for starting +tests
                        somethingtodo = 0;
                        double scaleit = 1.0;
                        if (MaxIndicator > 0)
                        {
                            scaleit = Hotsun.extraprecision;
                        }
                        if (Math.Abs(NewEigenvalue - PowerEigenvalue) > PowerEigenvalue * scaleit * Hotsun.eigenvaluechange)
                        {
                            ++somethingtodo;
                        }
                        double delta = AdotA - NewEigenvalue * NewEigenvalue; // (Ax- Eigenvalue*Axold)**2
                        if (Math.Abs(delta) > NewEigenvalue * NewEigenvalue * scaleit * Hotsun.eigenvectorchange)
                        {
                            ++somethingtodo;
                        }
                    }
                }
                PowerEigenvalue = NewEigenvalue;

                if (SALSAUtility.MPI_Size > 1)
                {
                    SALSAUtility.StartSubTimer(SALSAUtility.MPIBROADCASTTiming);
                    SALSAUtility.MPI_communicator.Broadcast(ref somethingtodo, 0);
                    SALSAUtility.StopSubTimer(SALSAUtility.MPIBROADCASTTiming);
                }
                if (PowerIterationCount >= Hotsun.PowerIterationLimit)
                {
                    somethingtodo = -2;
                    break;
                }
                if (somethingtodo == 0)
                {
                    somethingtodo = PowerIterationCount;
                    break;
                }
            } // End while over PowerIterationCounts

            return(somethingtodo);
        }
コード例 #2
0
        // End VectorSum(double[] VTotal, double[] V2, double sign, double[] V1)

        public static void InitializeParameters(Desertwind Solution, int CountStartingPoints)
        {
            // Inversion
            InvertSolution = false;
            if ((CountStartingPoints / 2) * 2 != CountStartingPoints)
            {
                InvertSolution = true;
            }

            for (int LocalVectorIndex1 = 0; LocalVectorIndex1 < PointVectorDimension; LocalVectorIndex1++)
            {
                for (int LocalVectorIndex2 = 0; LocalVectorIndex2 < PointVectorDimension; LocalVectorIndex2++)
                {
                    CurrentCosmicScaling[LocalVectorIndex1, LocalVectorIndex2] = 0.0;
                }
                CurrentCosmicScaling[LocalVectorIndex1, LocalVectorIndex1] = 1.0;
            }
            if (InvertSolution)
            {
                for (int LocalVectorIndex1 = 0; LocalVectorIndex1 < PointVectorDimension; LocalVectorIndex1++)
                {
                    CurrentCosmicScaling[LocalVectorIndex1, LocalVectorIndex1] = -1.0;
                }
                if (PointVectorDimension == 2)
                {
                    CurrentCosmicScaling[0, 0] = 1.0;
                }
            }
            for (int LocalVectorIndex1 = 0; LocalVectorIndex1 < PointVectorDimension; LocalVectorIndex1++)
            {
                for (int LocalVectorIndex2 = 0; LocalVectorIndex2 < PointVectorDimension; LocalVectorIndex2++)
                {
                    SavedCosmicScaling[CountStartingPoints][LocalVectorIndex1, LocalVectorIndex2] =
                        CurrentCosmicScaling[LocalVectorIndex1, LocalVectorIndex2];
                }
            }

            //  Scaling
            double Scale = FirstScale / SecondScale;

            if (ScaleOption == 0)
            {
                Solution.param[ScalePosition][0] = Scale;
            }
            else
            {
                ScaleA1 = 0.5 * (ScaleAlpha + 1.0 / ScaleAlpha);
                ScaleA2 = 0.5 * Math.Abs(ScaleAlpha - 1.0 / ScaleAlpha);
                Solution.param[ScalePosition][0] = Math.Asin((ScaleA1 - 1.0) / ScaleA2);
                ScaleA1 *= Scale;
                ScaleA2 *= Scale;
            }

            var Randobject   = new Random();
            var RandomAngles = new double[3];

            if (SALSAUtility.MPI_Rank == 0)
            {
                for (int irand = 0; irand < 3; irand++)
                {
                    RandomAngles[irand] = Randobject.NextDouble();
                }
            }
            if (SALSAUtility.MPI_Size > 1)
            {
                SALSAUtility.StartSubTimer(SALSAUtility.MPIBROADCASTTiming);
                SALSAUtility.MPI_communicator.Broadcast(ref RandomAngles, 0);
                SALSAUtility.StopSubTimer(SALSAUtility.MPIBROADCASTTiming);
            }


            //  Translation and Rotation
            for (int LocalVectorIndex = 0; LocalVectorIndex < PointVectorDimension; LocalVectorIndex++)
            {
                Solution.param[LocalVectorIndex][0] = FirstMean[LocalVectorIndex] - Scale *
                                                      CurrentCosmicScaling[LocalVectorIndex, LocalVectorIndex] *
                                                      SecondMean[LocalVectorIndex];
                if (PointVectorDimension == 3)
                {
                    Solution.param[LocalVectorIndex + PointVectorDimension][0] = 0.0;
                    if (CountStartingPoints > 1)
                    {
                        Solution.param[LocalVectorIndex + PointVectorDimension][0] = Math.PI *
                                                                                     RandomAngles[LocalVectorIndex];
                    }
                }
            }
            if (PointVectorDimension == 2)
            {
                Solution.param[2][0] = 0.0;
                if (CountStartingPoints > 1)
                {
                    Solution.param[2][0] = Math.PI * RandomAngles[0];
                }
            }
        }