/// <summary> /// Returns the value of a derived function. /// </summary> /// <param name="function">Continuous function delegate</param> /// <param name="x">Argument value</param> /// <param name="h">Step</param> /// <param name="order">Order</param> /// <returns>Complex number</returns> public Complex32 Compute(IComplex function, Complex32 x, Complex32 h, int order) { // exception if (order > this.points) { throw new Exception("The order of the derivative cannot be greater than the number of interpolation points"); } if (order < 0) { throw new Exception("The derivative order cannot be less than 0"); } // Create the interpolation points int length = this.points + 1; float[,] coefficients = Differentation.GetCoefficients(length); Complex32 sum = 0.0; // do job for (int i = 0, center = 0; i < length; i++) { sum += coefficients[order, i] * function(x + center * h); center++; } // result return(sum / Maths.Pow(h, order)); }
/// <summary> /// /// </summary> /// <param name="f"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="n"></param> /// <returns></returns> private static Complex32 simp(IComplex f, Complex32 a, Complex32 b, int n) { if (n < 3) { return(float.NaN); //Need at least 3 points } Complex32 sum = 0.0; Complex32 h = (b - a) / n; if (n % 2 != 0) { for (int i = 0; i < n - 1; i += 2) { sum += h * (f(a + i * h) + 4 * f(a + (i + 1) * h) + f(a + (i + 2) * h)) / 3; } } else { sum = 3 * h * (f(a) + 3 * f(a + h) + 3 * f(a + 2 * h) + f(a + 3 * h)) / 8; for (int i = 3; i < n - 1; i += 2) { sum += h * (f(a + i * h) + 4 * f(a + (i + 1) * h) + f(a + (i + 2) * h)) / 3; } } return(sum); }
/// <summary> /// /// </summary> /// <param name="f"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="eps"></param> /// <returns></returns> private static Complex32 falpo(IComplex f, Complex32 a, Complex32 b, float eps = 1e-8f) { Complex32 x1 = a; Complex32 x2 = b; Complex32 fb = f(b); int n = 0; while (Maths.Abs(x2 - x1) > eps && n < short.MaxValue) { Complex32 xpoint = x2 - (x2 - x1) * f(x2) / (f(x2) - f(x1)); Complex32 fxpoint = f(xpoint); float s = fb.Real * fxpoint.Real; // sign if (s > 0) { x2 = xpoint; } else { x1 = xpoint; } if (Maths.Abs(fxpoint) < eps) { break; } n++; } return(x2 - (x2 - x1) * f(x2) / (f(x2) - f(x1))); }
/// <summary> /// Multiply two complex numbers. /// </summary> /// <param name="c1">the first operand.</param> /// <param name="c2">the second operand.</param> /// <returns>the product.</returns> public static IComplex Multiply(this IComplex c1, IComplex c2) { double re = c1.Real * c2.Real - c1.Imaginary * c2.Imaginary; double im = c1.Real * c2.Imaginary + c2.Real * c1.Imaginary; return(new Complex(re, im)); }
/// <summary> /// Get the reciprocal of a complex number. /// </summary> /// <remarks> /// <para> /// The multiplicative inverse (or reciprocal) of a complex number is a number, /// that when multiplied with that complex number, gives an answer of 1. /// </para> /// </remarks> /// <param name="c1">the complex operand.</param> /// <returns>the complex reciprocal.</returns> public static IComplex Reciprocal(this IComplex c1) { IComplex conjugate = c1.Conjugate(); double mulFactor = 1.0 / Math.Pow(c1.Modulus, 2); return(new Complex(conjugate.Real * mulFactor, conjugate.Imaginary * mulFactor)); }
/// <summary> /// Divide two complex numbers. /// </summary> /// <param name="c1">the first operand.</param> /// <param name="c2">the second operand.</param> /// <returns>the quotient.</returns> public static IComplex Divide(this IComplex c1, IComplex c2) { return(new Complex( ((c1.Real * c2.Real) + (c1.Imaginary * c2.Imaginary)) / (Math.Pow(c2.Real, 2) + Math.Pow(c2.Imaginary, 2)), ((c1.Imaginary * c2.Real) - (c1.Real * c2.Imaginary)) / (Math.Pow(c2.Real, 2) + Math.Pow(c2.Imaginary, 2)) )); }
/// <summary> /// Multiply two complex numbers. /// </summary> /// <param name="c1">the first operand.</param> /// <param name="c2">the second operand.</param> /// <returns>the product.</returns> public static IComplex Multiply(this IComplex c1, IComplex c2) { return(new Complex( (c1.Real * c2.Real) - (c1.Imaginary * c2.Imaginary), (c1.Real * c2.Imaginary) + (c1.Imaginary * c2.Real) )); }
public LogExpression(IExpression argument, IComplex count) { this.Type = FunctionTypes.Log; this.Argument = argument; this.Count = count; UpdateDimensionKey(); }
/// <summary> /// Divide two complex numbers. /// </summary> /// <param name="c1">the first operand.</param> /// <param name="c2">the second operand.</param> /// <returns>the quotient.</returns> public static IComplex Divide(this IComplex c1, IComplex c2) { double inverseOfSquaredModulus = 1 / Math.Pow(c2.Modulus, 2); double realPart = ((c1.Real * c2.Real) + (c1.Imaginary * c2.Imaginary)) / inverseOfSquaredModulus; double imaginaryPart = ((c1.Imaginary * c2.Real) - (c1.Real * c2.Imaginary)) / inverseOfSquaredModulus; return(new Complex(realPart, imaginaryPart)); }
/// <summary> /// Divide two complex numbers. /// </summary> /// <param name="c1">the first operand.</param> /// <param name="c2">the second operand.</param> /// <returns>the quotient.</returns> public static IComplex Divide(this IComplex c1, IComplex c2) { double den = c2.Real * c2.Real + c2.Imaginary * c2.Imaginary; double num1 = c1.Real * c2.Real + c1.Imaginary * c2.Imaginary; double num2 = c2.Real * c1.Imaginary - c1.Real * c2.Imaginary; return(new Complex(num1 / den, num2 / den)); }
/// <inheritdoc cref="IEquatable{T}.Equals(T)"/> public bool Equals(IComplex other) { const double tolerance = 0.000000001; return(other != null && Math.Abs(this.re - other.Real) < tolerance && Math.Abs(this.im - other.Imaginary) < tolerance); }
/// <summary> /// Adds the given element to the collection /// </summary> /// <param name="item">The item to add</param> public override void Add(IModelElement item) { IComplex valuesCasted = item.As <IComplex>(); if ((valuesCasted != null)) { this._parent.Values.Add(valuesCasted); } }
/// <summary> /// Multiply two complex numbers. /// </summary> /// <param name="c1">the first operand.</param> /// <param name="c2">the second operand.</param> /// <returns>the product.</returns> public static IComplex Multiply(this IComplex c1, IComplex c2) { double firstTerm = c1.Real * c2.Real; double secondTerm = c1.Imaginary * c2.Imaginary; double realPart = firstTerm - secondTerm; double imaginaryPart = firstTerm + secondTerm; return(new Complex(realPart, imaginaryPart)); }
/// <summary> /// Multiply two complex numbers. /// </summary> /// <param name="c1">the first operand.</param> /// <param name="c2">the second operand.</param> /// <returns>the product.</returns> public static IComplex Multiply(this IComplex c1, IComplex c2) { double a = c1.Real; double b = c1.Imaginary; double c = c2.Real; double d = c2.Imaginary; return(new Complex(a * c - b * d, a * d + b * c)); }
public void Pow(IComplex z) { if (z.Im.Numerator == 0) { if (this.Im.Numerator == 0) { this.Re.Pow(z.Re); } else { var n = (double)z.Re.ToNumber(); if (n == 0) { this.Re = new Fraction(); this.Im = new Fraction(0, 1); } else if (n == 1) { } if (z.Re.Numerator > 1 && z.Re.Denominator > 1) { //first power using numeratora and this function then root using this function and denominator throw new NotImplementedException("Complex number to power of fraction is not implemnted"); } else if (z.Re.Numerator > 1 && z.Re.Denominator == 1) { var r2 = (double)((Fraction)this.Re * this.Re + (Fraction)this.Im * this.Im).ToNumber(); var r = Math.Pow(r2, 0.5); var y = (double)this.Im.ToNumber(); var rn = Math.Pow(r, n); var phi = Math.Asin(y / r); this.Re = new Fraction((decimal)(rn * Math.Cos(n * phi)), 1); this.Im = new Fraction((decimal)(rn * Math.Sin(n * phi)), 1); } else if (z.Re.Numerator == 1 && z.Re.Denominator > 1) { var r2 = (double)((Fraction)this.Re * this.Re + (Fraction)this.Im * this.Im).ToNumber(); var r = Math.Pow(r2, 0.5); var y = (double)this.Im.ToNumber(); var rn = Math.Pow(r, n); var phi = Math.Asin(y / r); this.Re = new Fraction((decimal)(rn * Math.Cos(n * phi)), 1); this.Im = new Fraction((decimal)(rn * Math.Sin(n * phi)), 1); } } } else { throw new NotImplementedException("Complex number to power of complex number is not implemented"); } }
/// <summary> /// Divide two complex numbers. /// </summary> /// <param name="c1">the first operand.</param> /// <param name="c2">the second operand.</param> /// <returns>the quotient.</returns> public static IComplex Divide(this IComplex c1, IComplex c2) { double a = c1.Real; double b = c1.Imaginary; double c = c2.Real; double d = c2.Imaginary; double den = c * c + d * d; return(new Complex((a * c + b * d) / den, (b * c - a * d) / den)); }
/// <summary> /// /// </summary> /// <param name="f"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="n"></param> /// <returns></returns> private static Complex32 trap(IComplex f, Complex32 a, Complex32 b, int n) { Complex32 sum = 0.0; Complex32 h = (b - a) / n; for (int i = 0; i < n; i++) { sum += 0.5 * h * (f(a + i * h) + f(a + (i + 1) * h)); } return(sum); }
/// <summary> /// /// </summary> /// <param name="f"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="n"></param> /// <returns></returns> private static Complex32 rect(IComplex f, Complex32 a, Complex32 b, int n) { Complex32 sum = 0.0; Complex32 h = (b - a) / n; for (int i = 0; i < n; i++) { sum += h * f(a + i * h); } return(sum); }
/// <summary> /// Removes the given item from the collection /// </summary> /// <returns>True, if the item was removed, otherwise False</returns> /// <param name="item">The item that should be removed</param> public override bool Remove(IModelElement item) { IComplex complexItem = item.As <IComplex>(); if (((complexItem != null) && this._parent.Values.Remove(complexItem))) { return(true); } return(false); }
private static IExpression FromCount(IComplex count) { if (count.Im.Numerator != 0) { return(new ComplexExpression(count.Re, count.Im)); } else //(count.Im.Numerator == 0) { return(new NumberExpression(count.Re)); } }
public void PrintSubDataType(int indnt, StreamWriter writer, IComplex s) { if (s != null) { indent(indnt, writer); writer.Write("<IComplex>\n"); indent(indnt + 1, writer); writer.Write("<Element>\n"); PrintDataType(indnt + 2, writer, s.ComplexElementDataType); indent(indnt + 1, writer); writer.Write("</Element>\n"); indent(indnt, writer); writer.Write("</IComplex>\n"); } }
/// <summary> /// /// </summary> /// <param name="f"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="n"></param> /// <returns></returns> private static Complex32 midp(IComplex f, Complex32 a, Complex32 b, int n) { // Midpoint Complex32 sum = 0.0; Complex32 h = (b - a) / n; for (int i = 0; i < n; i++) { sum += h * f(a + (i + 0.5) * h); } return(sum); }
/// <summary> /// Конструктор /// </summary> public ComplexForm(IComplex complex, ICities cities) { InitializeComponent(); // Инициализировать компоненты формы _complex = complex; // Сохранить комплекс в поле _cities = cities; // Сохранить список городов в поле _cityAfterRelinking = complex.City; // Сохранить город, связанный с комплексом CleanAllData(); // Очистить все данные формы CleanLocData(); CopyDataFromEntity(); // Скопировать данные из сущности в компоненты формы }
/// <summary> /// /// </summary> /// <param name="f"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="eps"></param> /// <returns></returns> private static Complex32 chord(IComplex f, Complex32 a, Complex32 b, float eps = 1e-8f) { int n = 0; Complex32 x0 = (b - a) / 2.0; Complex32 x; while (Maths.Abs(f(x0) / b) > eps && n < short.MaxValue) { x = x0; x0 = x - (f(x) * (a - x)) / (f(a) - f(x)); n++; } return(x0); }
/// <summary> /// Метод. Связывает дом с выбранным комплексом /// </summary> private void relinkComplexButton_Click(object sender, EventArgs e) { ComplexSelectForm complexSelectForm; // Форма выбора комплекса complexSelectForm = new ComplexSelectForm(_complexes); // Создать форму выбора комплекса complexSelectForm.ShowDialog(); // Отобразить форму выбора комплекса if (complexSelectForm.SelectedComplex != null) // Проверить выбранный комплекс { _complexAfterRelinking = complexSelectForm.SelectedComplex; // Сохранить выбранный комплекс в поле } CopyLinkedDataFromEntity(); // Скопировать данные из сущностей, связанных с основной сущностью }
/// <summary> /// Gets the root value of a nonlinear equation. /// </summary> /// <param name="function">Continuous function delegate</param> /// <param name="a">Start of line</param> /// <param name="b">End of line</param> /// <returns>float precision floating point number</returns> public Complex32 Compute(IComplex function, Complex32 a, Complex32 b) { // chose method of nonlinear switch (method) { case Method.Chord: return(Nonlinear.chord(function, a, b, this.eps)); case Method.FalsePosition: return(Nonlinear.falpo(function, a, b, this.eps)); default: return(Nonlinear.secan(function, a, b, this.eps)); } }
/// <summary> /// /// </summary> /// <param name="f"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="eps"></param> /// <returns></returns> private static Complex32 secan(IComplex f, Complex32 a, Complex32 b, float eps = 1e-8f) { Complex32 x1 = a; Complex32 x2 = b; Complex32 fb = f(b); Complex32 mpoint; int n = 0; while (Maths.Abs(f(x2)) > eps && n < short.MaxValue) { mpoint = x2 - (x2 - x1) * fb / (fb - f(x1)); x1 = x2; x2 = mpoint; fb = f(x2); n++; } return(x2); }
/// <summary> /// Returns the value of the integral of a function. /// </summary> /// <param name="function">Continuous function delegate</param> /// <param name="a">Lower limit</param> /// <param name="b">Upper limit</param> /// <param name="n">Number of splits</param> /// <returns>Complex number</returns> public Complex32 Compute(IComplex function, Complex32 a, Complex32 b, int n) { // chose method of integration switch (method) { case Method.Midpoint: return(Integration.midp(function, a, b, n)); case Method.Trapezoidal: return(Integration.trap(function, a, b, n)); case Method.Simpson: return(Integration.simp(function, a, b, n)); case Method.Romberg: return(Integration.romb(function, a, b, n)); default: return(Integration.rect(function, a, b, n)); } }
/// <summary> /// Метод. Выбирает комплекс из списка комплексов, сохраняет в поле и закрывает диалоговое окно /// </summary> private void selectButton_Click(object sender, EventArgs e) { DataGridViewRow selectedRow; // Выделенная строка int id; // Идентификатор выделенного комплекса int rowCount; // Общее количество строк в списке int selectedRowIndex; // Индекс выделенной строки rowCount = entitiesDataGridView.Rows.Count; // Получить общее количество строк в списке if (rowCount > 0) // Проверить общее количество строк { selectedRow = entitiesDataGridView.SelectedRows[0]; // Получить выделенную строку selectedRowIndex = selectedRow.Index; // Получить индекс выделенной строки id = Convert.ToInt32(selectedRow.Cells["id"].Value); // Получить идентификатор комплекса в выделенной строке _selectedComplex = _complexes.GetComplex(id); // Получить выделенный комплекс } CloseForm(); // Закрыть диалоговое окно }
/// <summary> /// Метод. Отвязывает дом от связанного комплекса /// </summary> private void unlinkComplexButton_Click(object sender, EventArgs e) { DialogResult unlinkConfirm; // Результат подтверждения сообщения unlinkConfirm = MessageBox.Show( // Отобразить окно сообщения с подтверждением и сохранить результат подтверждения "Вы действительно хотите отвязать комплекс?", "Подтверждение", MessageBoxButtons.YesNo); if (unlinkConfirm == DialogResult.Yes) // Проверить результат подтверждения сообщения { _complexAfterRelinking = null; // Отвязать дом от связанного комплекса CleanCountryName(); // Очистить название страны CleanRegionName(); // Очистить название региона CleanCityName(); // Очистить название города CleanComplexNumber(); // Очистить название комплекса CopyLinkedDataFromEntity(); // Скопировать данные из сущностей, связанных с основной сущностью } }