Accord.Math.Optimization.LinearConstraint.parseString C# (CSharp) Method

parseString() private method

private parseString ( IObjectiveFunction function, string constraint, CultureInfo culture ) : void
function IObjectiveFunction
constraint string
culture System.Globalization.CultureInfo
return void
        private void parseString(IObjectiveFunction function, string constraint, CultureInfo culture)
        {
            if (String.IsNullOrEmpty(constraint))
                throw new FormatException("Constraint is empty.");

            string f = constraint.Replace("*", String.Empty).Replace(" ", String.Empty);

            if (f[0] != '-' && f[0] != '+')
                f = f.Insert(0, "+");

            ConstraintType type;

            string lhs, rhs;

            if (f.Contains(">="))
                type = ConstraintType.GreaterThanOrEqualTo;
            else if (f.Contains("<="))
                type = ConstraintType.LesserThanOrEqualTo;
            else if (f.Contains("="))
                type = ConstraintType.EqualTo;
            else
                throw new FormatException("Invalid constraint type.");



            var terms = new Dictionary<string, double>();

            string separator = culture.NumberFormat.NumberDecimalSeparator;

            Regex r = new Regex(@"[\-\+][\s]*(\d*\" + separator + @"{0,1}\d+)?[\s]*([a-zA-Z])*");
            Regex number = new Regex(@"\d*\" + separator + @"{0,1}\d+");
            Regex symbol = new Regex(@"[a-zA-Z]");
            Regex comp = new Regex(@"(>=|<=|=)");


            var sides = comp.Split(f);
            lhs = sides[0];
            rhs = sides[2];

            double value = Double.Parse(rhs, culture);

            MatchCollection matches = r.Matches(lhs, 0);

            foreach (Match m in matches)
            {
                string term = m.Value;

                double scalar = (term[0] == '-') ? -1 : 1;

                // Extract value
                MatchCollection coeff = number.Matches(term);

                foreach (Match c in coeff)
                    scalar *= Double.Parse(c.Value, culture);

                // Extract symbols
                MatchCollection symbols = symbol.Matches(term);

                if (symbols.Count == 1)
                {
                    terms.Add(symbols[0].Value, scalar);
                }
                else
                {
                    if (coeff.Count == 1)
                        value -= scalar;
                    else if (term != "+")
                        throw new FormatException("Unexpected expression.");
                }
            }

            List<int> indices = new List<int>();
            List<double> scalars = new List<double>();

            foreach (var term in terms)
            {
                indices.Add(function.Variables[term.Key]);
                scalars.Add(term.Value);
            }

            NumberOfVariables = indices.Count;
            VariablesAtIndices = indices.ToArray();
            CombinedAs = scalars.ToArray();
            ShouldBe = type;
            Value = value;
        }