Ejemplo n.º 1
0
 public void Subtract(TermSum value)
 {
     foreach (var kvp in value)
     {
         Subtract(kvp.Key, kvp.Value);
     }
 }
Ejemplo n.º 2
0
 public void Add(TermSum value)
 {
     foreach (var kvp in value)
     {
         Add(kvp.Key, kvp.Value);
     }
 }
Ejemplo n.º 3
0
        public static TermSum GetProduct(TermSum arg1, double value)
        {
            var result = new TermSum();

            foreach (var x in arg1._ratios)
            {
                var ratio = x.Value * value;
                result.Add(x.Key, ratio);
            }

            return(result);
        }
Ejemplo n.º 4
0
 public static string AsString(this TermSum value)
 {
     var str = string.Concat(value
                             .Where(x => x.Value != 0)
                             .Select((kvp, i) =>
                                     string.Concat(
                                         i > 0 && kvp.Value > 0 ? "+" : "",
                                         kvp.Value switch
     {
         1 => kvp.Key.IsEmpty ? "1" : "",
         -1 => kvp.Key.IsEmpty ? "-1" : "-",
         _ => kvp.Value.ToString(CultureInfo.InvariantCulture)
     },
Ejemplo n.º 5
0
        public static TermSum GetProduct(TermSum arg1, TermSum arg2)
        {
            var result = new TermSum();

            foreach (var x in arg1._ratios)
            {
                foreach (var y in arg2._ratios)
                {
                    var ratio = x.Value * y.Value;
                    var term  = Term.GetProduct(x.Key, y.Key);
                    result.Add(term, ratio);
                }
            }

            return(result);
        }
Ejemplo n.º 6
0
        private TermSum Parse(ReadOnlySpan <char> text)
        {
            var     next = 0;
            TermSum arg  = null;

            while (next < text.Length)
            {
                if (text[next] == ' ')
                {
                    next++;
                    continue;
                }

                if (TryGetGroup(text, ref next, out var arg2))
                {
                    SetOrMultiply(ref arg, arg2);
                    continue;
                }

                // (?<sign>+-)?(?<coef>[\.0-9]*)
                GetSign(text, next, out var sign, out var slen);
                next += slen;
                if (slen == 0 && arg != null)
                {
                    throw new ArgumentException("Unexpected input value");
                }

                GetDouble(text, next, out var coef, out var coefLength);
                coef *= sign;
                next += coefLength;

                //(?<group>\(.*\))
                if (TryGetGroup(text, ref next, out arg2))
                {
                    SetOrAdd(ref arg, TermSum.GetProduct(arg2, coef));
                }
                //(?<x1>[a-zA-Z](^\d)?)(?<x2>[a-zA-Z](^\d)?)...
                else if (TryGetTerm(text, next, out var term, out var tlen))
                {
                    if (arg == null)
                    {
                        arg = new TermSum();
                    }
                    arg.Add(term, coef);
                    next += tlen;
                }