/// <summary> /// 多項式が整理できる場合は整理する /// ・分母分子ごとに同類項をまとめる /// ・共通因数があれば求める /// ・共通因数を掛けて約分し、それを分母分子に分けて掛ける /// </summary> private void Initialise() { if (NumMonof.IsZero()) { DenMonof = new List <Monomial> { 1 }; return; } if (NumMonof.IsOne() && DenMonof.IsOne()) { return; } //0の項を削除 NumMonof = NumMonof.Where(x => !x.IsZero()).ToList(); DenMonof = DenMonof.Where(x => !x.IsZero()).ToList(); //同類項をまとめる var SortedNumerator = NumMonof.Grouping(IsNumberKey: false).Select(x => { var res = x[0]; res.Number = x.Select(y => y.Number).Aggregate((now, next) => now + next); return(res); }).ToList(); var SortedDenominator = DenMonof.Grouping(IsNumberKey: false).Select(x => { var res = x[0]; res.Number = x.Select(y => y.Number).Aggregate((now, next) => now + next); return(res); }).ToList(); if (SortedNumerator.IsOne() || SortedDenominator.IsOne()) { NumMonof = SortedNumerator; DenMonof = SortedDenominator; return; } var NumFactor = Factorizations.FactorOut(SortedNumerator); var DenFactor = Factorizations.FactorOut(SortedDenominator); Monomial Factor = NumFactor.Item1 / DenFactor.Item1; NumMonof = NumFactor.Item2.Select(x => x * Factor.GetNumerator()).ToList(); DenMonof = DenFactor.Item2.Select(x => x * Factor.GetDenominator()).ToList(); Reduction(); Trace.WriteLine("Formula Initialized:" + this.GetString()); }
/// <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; } } } }