Example #1
0
        public override double[] GetPriorProbabilities(OptimizationParameterList discreteParameters)
        {
            const double eps = 0.0001;

            double[] priors = new double[NonMissingClassCount];

            try
            {
                EigenPair       eig         = LinearAlgebra.ComputeSparseEigenPair(LinearAlgebra.Transpose(GetTransitionProbabilityMatrix(discreteParameters, 1)));
                ComplexNumber[] eigenValues = eig.EigenValues;
                for (int i = 0; i < 4; i++)
                {
                    if (ComplexNumber.ApproxEqual(eigenValues[i], 1, eps))
                    {
                        priors = LinearAlgebra.Abs(LinearAlgebra.ComplexToDouble(LinearAlgebra.Transpose(eig.EigenVectors)[i]));
                        break;
                    }
                }

                priors = LinearAlgebra.Normalize(priors);
            }
            catch (Exception e)
            {
                throw new NotComputableException("Problem computing the prior: " + e.Message);
            }

            return(priors);
        }
        public bool?Update(Dictionary <string, double> potentials, string perturbationTarget)
        {
            const double eps = 1.0e-4;

            foreach (var p in potentials)
            {
                _ddeSolver.SetPotential(p.Key, p.Value);
            }
            var nominalJacobian = _ddeSolver.GetJacobianMatrix();

            foreach (var p in potentials)
            {
                if (p.Key == perturbationTarget)
                {
                    _ddeSolver.SetPotential(p.Key, p.Value + eps);
                }
            }
            var perturbedJacobian = _ddeSolver.GetJacobianMatrix();
            var sensitivity       = (perturbedJacobian - nominalJacobian) * (1.0 / eps);

            _eigenPair = _evpSolver.Solve(sensitivity, nominalJacobian, "SM");
            var omega = _eigenPair.EigenValue.Real;

            _hasConvergedEigenMode = Math.Abs(omega) < _tol;
            bool?IsNeutral = null;

            if (_hasConvergedEigenMode.Value)
            {
                IsNeutral = IsStable(_eigenPair.RightEigenMatrix.Real());
            }
            else
            {
                potentials[perturbationTarget] -= omega;
            }
            if (IsNeutral.HasValue)
            {
                if (!IsNeutral.Value)
                {
                    potentials[perturbationTarget] *= _dropFactor;
                }
            }
            return(IsNeutral);
        }
Example #3
0
        public Eigen GetEigen(SparseMatrix sparse, int num)
        {
            OutPut(sparse, "L");
            matlab.Execute(@"[EV,ED] = eigs(L," + num.ToString() + ",'sm') ");

            double[,] eigVector = GetMatrix("EV");
            double[,] eigValue = GetMatrix("ED");
             
            int len=eigVector.GetLength(0);
            List<EigenPair> list = new List<EigenPair>();

            for (int i = 0; i < num; i++)
            {
                double realPart = eigValue[i,i];  

                List<double> vector = new List<double>();

                for (int j = 0; j < len; j++)
                {
                    double value = eigVector[j,i];
                    vector.Add(value);
                }

                EigenPair newPair = new EigenPair(realPart, vector);


                list.Add(newPair);
            }

            list.Sort();

            Eigen eigen = new Eigen();
            eigen.SortedEigens = list.ToArray();  
             
            return eigen;

        }
Example #4
0
        public Eigen ReadEigen(string modelName)
        {
            string fileName = Path.GetFileNameWithoutExtension(modelName);
            string path = GetPath() + fileName + ".eigens";

            Eigen eigen = new Eigen();
            List<EigenPair> list = new List<EigenPair>();

            //Read File
            using (StreamReader sr = new StreamReader(path))
            {
                String line = null;
                List<double> currentVector = null;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line != "" && line[0] == '#')
                    {
                        String[] tokens = line.Split(' ');
                        List<double> aVector = new List<double>();
                        currentVector = aVector;

                        EigenPair pair = new EigenPair(double.Parse(tokens[1]), aVector);
                        list.Add(pair);
                    }
                    else if (line != "")
                    {
                        double value = double.Parse(line);
                        currentVector.Add(value);
                    }
                }
            }
            list.Sort();
            eigen.SortedEigens = list.ToArray();
            return eigen;
        }
Example #5
0
        public Eigen ComputeEigensByLib(SparseMatrixDouble sparse, double sigma, int count)
        {
            int[] pCol;
            int[] iRow;
            double[] Values;
            int NNZ;

            int m = sparse.RowCount;

            sparse.ToCCS(out pCol, out iRow, out Values, out NNZ);

            double[] ImagePart = new double[count];
            double[] RealPart = new double[count];
            double[] Vectors = new double[count * m];

            fixed (int* ri = iRow, cp = pCol)
            fixed (double* val = Values, vets = Vectors, imgPart = ImagePart, relPart = RealPart)
            {
                int result = ComputeEigenNoSymmetricShiftModeCRS(ri, cp, val, NNZ, m, count, sigma, relPart, imgPart, vets);
            }

           

            List<EigenPair> list = new List<EigenPair>();

            for (int i = 0; i < count; i++)
            {
                double realPart = RealPart[i];

                 

                List<double> vector = new List<double>();

                int startIndex = i * m;
                int endIndex = i * m + m;

                for (int j = startIndex; j < endIndex; j++)
                {
                    double value = Vectors[j];
                    vector.Add(value);
                }

                EigenPair newPair = new EigenPair(realPart, vector);


                list.Add(newPair);
            }

            list.Sort();

            Eigen eigen = new Eigen();
            eigen.SortedEigens = list.ToArray();
            return eigen;
        }