public override Function Bind(params Parameter[] x) { if (this.p.Coef.Length == 1) { return(this.p.Coef[0].Bind(x)); } Function f = this.inner.Bind(x); if (f is Constant) { Constant c = f as Constant; return(this.p.Value(c.Value).Bind(x)); } if (this.p.Coef.Length == 2 && this.p.Coef[0].Equals(0)) { if (this.p.Coef[1].Equals(1)) { return(f); } return(this.p.Coef[1].Bind(x) * f); } CoefType[] coef = new CoefType[this.p.Coef.Length]; for (int i = 0; i < coef.Length; ++i) { coef[i] = this.p.Coef[i].Bind(x); } return(new GeneralPolynomial(f, coef)); }
/// <summary> /// 多項式同士の減算。 /// </summary> /// <param name="f">f(x)</param> /// <param name="g">g(x)</param> /// <returns>f(x) - g(x)</returns> public static Polynomial operator-(Polynomial f, Polynomial g) { CoefType[] c; if (f.coef.Length > g.coef.Length) { c = new CoefType[f.coef.Length]; int n = 0; for (; n < g.coef.Length; ++n) { c[n] = f.coef[n] - g.coef[n]; } for (; n < f.coef.Length; ++n) { c[n] = f.coef[n]; } } else { c = new CoefType[g.coef.Length]; int n = 0; for (; n < f.coef.Length; ++n) { c[n] = f.coef[n] - g.coef[n]; } for (; n < g.coef.Length; ++n) { c[n] = -g.coef[n]; } } return(new Polynomial(c)); }
/// <summary> /// -f /// </summary> /// <param name="f">f(x)</param> /// <returns>-f(x)</returns> public static Polynomial operator-(Polynomial f) { CoefType[] c; c = new CoefType[f.coef.Length]; for (int n = 0; n < f.coef.Length; ++n) { c[n] = -f.coef[n]; } return(new Polynomial(c)); }
/// <summary> /// a x^n を返す。 /// </summary> /// <param name="n">指数</param> /// <param name="a">係数</param> /// <returns>x の n 乗</returns> public static Polynomial X(int n, CoefType a) { CoefType[] c = new CoefType[n + 1]; for (int i = 0; i < n; ++i) { c[i] = 0; } c[n] = a; return(new Polynomial(c)); }
/// <summary> /// 多項式のパラメータを定数とみなして、係数のみを微分。 /// </summary> /// <param name="x">微分変数</param> /// <returns>微分結果</returns> Function DifferentiateCoef(Variable x) { CoefType[] coef = this.p.Coef; CoefType[] deriv = new CoefType[coef.Length]; for (int i = 0; i < coef.Length; ++i) { deriv[i] = coef[i].Differentiate(x); } return(new GeneralPolynomial(this.inner, new Poly(deriv))); }
/// <summary> /// Возвращает значение коэф нужного типа из списка /// </summary> /// <param name="type"></param> /// <param name="coefs"></param> /// <returns></returns> protected decimal GetValueForCoefType(CoefType type, List <ChosenCoefModel> coefs, bool value2 = false) { if (coefs == null) { return(1); } var coef = coefs.FirstOrDefault(x => x.CoefType_Code == type); if (coef == null || coef.Id == -1) { return(1); } else { return(value2 ? (coef.Value2 ?? 1) : (coef.Value ?? 1)); } }
/// <summary> /// 係数は定数とみなして、多項式のみを微分。 /// </summary> /// <returns>微分結果</returns> Function Differentiate() { CoefType[] coef = this.p.Coef; if (coef.Length <= 1) { return((Constant)0); } CoefType[] deriv = new CoefType[coef.Length - 1]; for (int i = 1; i < coef.Length; ++i) { deriv[i - 1] = i * coef[i]; } return(new GeneralPolynomial(this.inner, new Poly(deriv))); }
/// <summary> /// 多項式同士の加算。 /// </summary> /// <param name="f">f(x)</param> /// <param name="g">g(x)</param> /// <returns>f(x) + g(x)</returns> public static Polynomial operator+(Polynomial f, Polynomial g) { CoefType[] a, b, c; Select(f.coef, g.coef, out a, out b); c = new CoefType[a.Length]; int n = 0; for (; n < b.Length; ++n) { c[n] = a[n] + b[n]; } for (; n < a.Length; ++n) { c[n] = a[n]; } return(new Polynomial(c)); }
protected override Function Differentiate() { CoefType[] coef = this.p.Coef; if (coef.Length <= 1) { return((Constant)0); } //! ↓ Mathematics.Expression.Polynomial の方にあるべきコードかも。 CoefType[] deriv = new CoefType[coef.Length - 1]; for (int i = 1; i < coef.Length; ++i) { deriv[i - 1] = i * coef[i]; } return(new Polynomial(this.inner, new Poly(deriv))); }
/// <summary> /// 配列の畳込み積を計算する。 /// </summary> /// <param name="x">配列1</param> /// <param name="y">配列2</param> /// <returns>x * y</returns> static CoefType[] Convolute(CoefType[] x, CoefType[] y) { CoefType[] a, b, c; Select(x, y, out a, out b); c = new CoefType[a.Length + b.Length - 1]; for (int k = 0; k < c.Length; ++k) { c[k] = 0; } int i = 0; for (; i < b.Length; ++i) { for (int k = 0, l = i; k <= i; ++k, --l) { c[i] += a[k] * b[l]; } } for (; i < a.Length; ++i) { for (int k = i, l = 0; l < b.Length; --k, ++l) { c[i] += a[k] * b[l]; } } for (; i < c.Length; ++i) { for (int l = b.Length - 1, k = i - l; k < a.Length; ++k, --l) { c[i] += a[k] * b[l]; } } return(c); }
/// <summary> /// 配列の畳込み積を計算する。 /// </summary> /// <param name="x">配列1</param> /// <param name="y">配列2</param> /// <returns>x * y</returns> static CoefType[] Convolute(CoefType[] x, CoefType[] y) { CoefType[] a, b, c; Select(x, y, out a, out b); c = new CoefType[a.Length + b.Length - 1]; for(int k=0; k<c.Length; ++k) c[k] = 0; int i=0; for(; i<b.Length; ++i) for(int k=0, l=i; k<=i; ++k, --l) c[i] += a[k] * b[l]; for(; i<a.Length; ++i) for(int k=i, l=0; l<b.Length; --k, ++l) c[i] += a[k] * b[l]; for(; i<c.Length; ++i) for(int l=b.Length-1, k=i-l; k<a.Length; ++k, --l) c[i] += a[k] * b[l]; return c; }
/// <summary> /// a x^n を返す。 /// </summary> /// <param name="n">指数</param> /// <param name="a">係数</param> /// <returns>x の n 乗</returns> public static Polynomial X(int n, CoefType a) { CoefType[] c = new CoefType[n + 1]; for(int i=0; i<n; ++i) c[i] = 0; c[n] = a; return new Polynomial(c); }
/// <summary> /// x と y のうち、長い方の配列を a に、短い方を b に格納。 /// </summary> static void Select(CoefType[] x, CoefType[] y, out CoefType[] a, out CoefType[] b) { if(x.Length > y.Length) { a = x; b = y; } else { a = y; b = x; } }
/// <summary> /// 多項式同士の減算。 /// </summary> /// <param name="f">f(x)</param> /// <param name="g">g(x)</param> /// <returns>f(x) - g(x)</returns> public static Polynomial operator- (Polynomial f, Polynomial g) { CoefType[] c; if(f.coef.Length > g.coef.Length) { c = new CoefType[f.coef.Length]; int n = 0; for(; n<g.coef.Length; ++n) c[n] = f.coef[n] - g.coef[n]; for(; n<f.coef.Length; ++n) c[n] = f.coef[n]; } else { c = new CoefType[g.coef.Length]; int n = 0; for(; n<f.coef.Length; ++n) c[n] = f.coef[n] - g.coef[n]; for(; n<g.coef.Length; ++n) c[n] = -g.coef[n]; } return new Polynomial(c); }
/// <summary> /// -f /// </summary> /// <param name="f">f(x)</param> /// <returns>-f(x)</returns> public static Polynomial operator-(Polynomial f) { CoefType[] c; c = new CoefType[f.coef.Length]; for(int n=0; n<f.coef.Length; ++n) c[n] = -f.coef[n]; return new Polynomial(c); }
/// <summary> /// 多項式同士の加算。 /// </summary> /// <param name="f">f(x)</param> /// <param name="g">g(x)</param> /// <returns>f(x) + g(x)</returns> public static Polynomial operator+ (Polynomial f, Polynomial g) { CoefType[] a, b, c; Select(f.coef, g.coef, out a, out b); c = new CoefType[a.Length]; int n = 0; for(; n<b.Length; ++n) c[n] = a[n] + b[n]; for(; n<a.Length; ++n) c[n] = a[n]; return new Polynomial(c); }
protected override Function Differentiate() { CoefType[] coef = this.p.Coef; if(coef.Length <= 1) return (Constant)0; //! ↓ Mathematics.Expression.Polynomial の方にあるべきコードかも。 CoefType[] deriv = new CoefType[coef.Length - 1]; for(int i=1; i<coef.Length; ++i) { deriv[i - 1] = i * coef[i]; } return new Polynomial(this.inner, new Poly(deriv)); }
private static void FormatCoefAndGoals( CoefType type, Dictionary<CoefType, ICoefficient> coefStorage, out string countGoals, out string coef) { ICoefficient coefAndGoals; coefStorage.TryGetValue(type, out coefAndGoals); if (coefAndGoals == null) { countGoals = " ----"; coef = " ----"; } else { countGoals = ((CoefAndGoals)coefAndGoals).CountGoals.ToString(CultureInfo.InvariantCulture); if (!countGoals.Contains(".")) { countGoals = countGoals + ".0"; } else if ((countGoals.IndexOf(".", StringComparison.Ordinal) + 2) < countGoals.Length) { countGoals = countGoals + "0"; } coef = ((CoefAndGoals)coefAndGoals).Coef.ToString(CultureInfo.InvariantCulture); if (coef.Length < 4) { coef = coef + "0"; } else if (coef.Length > 4) { coef = coef.Remove(4); } } }
private static string FormatCoefficient(CoefType type, Dictionary<CoefType, ICoefficient> coefStorage) { decimal? coef; if (coefStorage.ContainsKey(type)) { coef = coefStorage[type].Get(); } else { coef = null; } if (coef == null) { return " ----"; } var answer = coef.ToString(); if (coef < 9) { answer = " " + answer; } if (answer.IndexOf(",", StringComparison.Ordinal) < answer.Length - 3) { answer = answer.Remove(answer.Length - 1); } return answer; }
/// <summary> /// Возвращает обозначение коэффициента /// </summary> /// <param name="type"></param> /// <returns></returns> public string GetCoefTypeMark(CoefType type) { return(type.GetShortTitle()); }