/// <summary> /// Substituteのための辞書を得る /// </summary> /// <returns></returns> public Dictionary <char, double> GetCharDics() { return(NumMonof.Concat(DenMonof).Select(x => x.GetCharDics().Keys.ToList()) .Aggregate((now, next) => now.Union(next).ToList()).Distinct().ToDictionary(x => x, x => double.NaN)); }
/// <summary> /// 約分する /// </summary> private void Reduction() { var factors = Factorizations.FactorOut(this); NumMonof = (factors.Item1.GetNumerator() * factors.Item2.GetNumerator()).NumMonof; DenMonof = (factors.Item1.GetDenominator() * factors.Item2.GetDenominator()).NumMonof; if (DenMonof.Count() == 1)//分母が一つだけであれば数字は上につける { NumMonof = NumMonof.Select(x => x / DenMonof[0].Number.ToDouble()).ToList(); DenMonof[0].Number = 1; return; } else//上下の式がどちらかの因数である場合は、割り切る { var chars = this.GetCharDics().Keys.ToList(); char target = '0'; if (chars.Count() == 0) { return; } if (chars.Count() == 1) { target = chars[0]; } else { var chardic = chars.ToDictionary(x => x, x => NumMonof.Concat(DenMonof).Max(y => y.Character.ContainsKey(x) ? y.Character[x] : 0)); target = chardic.First().Key; foreach (var dic in chardic) { if (dic.Key != target) { if (dic.Value > chardic[target]) { target = dic.Key; } else if (dic.Value == chardic[target]) { if (dic.Key.CompareTo(target) < 0) { target = dic.Key; } } } } } var IsNumBigger = NumMonof.Max(x => x.Character.ContainsKey(target) ? x.Character[target] : 0) > DenMonof.Max(x => x.Character.ContainsKey(target) ? x.Character[target] : 0); var Bigger = new Dictionary <int, List <Monomial> > { }; var Smaller = new Dictionary <int, List <Monomial> > { }; var BiggerF = new List <Monomial> { }; var SmallerF = new List <Monomial> { }; if (IsNumBigger) { BiggerF = NumMonof; SmallerF = DenMonof; Bigger = NumMonof.GroupBy(x => x.Character.ContainsKey(target) ? x.Character[target] : 0).ToDictionary(x => x.Key, x => x.Select(y => y).ToList()); Smaller = DenMonof.GroupBy(x => x.Character.ContainsKey(target) ? x.Character[target] : 0).ToDictionary(x => x.Key, x => x.Select(y => y).ToList()); } else { BiggerF = DenMonof; SmallerF = NumMonof; Bigger = DenMonof.GroupBy(x => x.Character.ContainsKey(target) ? x.Character[target] : 0).ToDictionary(x => x.Key, x => x.Select(y => y).ToList()); Smaller = NumMonof.GroupBy(x => x.Character.ContainsKey(target) ? x.Character[target] : 0).ToDictionary(x => x.Key, x => x.Select(y => y).ToList()); } if (Smaller.Any(x => x.Value.Count() != 1)) { return; } var SUMfactor = new List <Monomial> { }; var BiggerMax = Bigger.Keys.Max(); var SmallerMax = Smaller.Keys.Max(); while (true) { var factor = Bigger[BiggerMax].Select(x => x / Smaller[SmallerMax][0]).ToList(); SUMfactor = SUMfactor.Concat(factor).ToList(); BiggerF = (new Formula(BiggerF) - new Formula(SmallerF) * new Formula(factor)).NumMonof; Bigger = BiggerF.GroupBy(x => x.Character.ContainsKey(target) ? x.Character[target] : 0).ToDictionary(x => x.Key, x => x.Select(y => y).ToList()); BiggerMax = Bigger.Keys.Max(); if (BiggerF.Count() == 0) { var res = new Formula(SUMfactor); NumMonof = res.NumMonof; DenMonof = res.DenMonof; return; } else if (BiggerMax < SmallerMax) { return; } } } }