/// <summary> /// The Square (power 2) of this <c>Complex</c> /// </summary> /// <param name="complex">The <see cref="Complex"/> number to perform this operation on.</param> /// <returns> /// The square of this complex number. /// </returns> public static Complex Square(this Complex complex) { if (complex.IsReal()) { return(new Complex(complex.Real * complex.Real, 0.0)); } return(new Complex((complex.Real * complex.Real) - (complex.Imaginary * complex.Imaginary), 2 * complex.Real * complex.Imaginary)); }
/// <summary> /// Trigonometric Cosine of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The cosine of a complex number.</returns> public static Complex Cos(this Complex value) { if (value.IsReal()) { return(new Complex(Cos(value.Real), 0.0)); } return(new Complex( Cos(value.Real) * Cosh(value.Imaginary), -Sin(value.Real) * Sinh(value.Imaginary))); }
/// <summary> /// Hyperbolic Cotangent of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The hyperbolic cotangent of a complex number.</returns> public static Complex Coth(this Complex value) { if (value.IsReal()) { return(new Complex(Coth(value.Real), 0.0)); } // Coth(z) = 1/tanh(z) return(Complex.One / Tanh(value)); }
/// <summary> /// Trigonometric Sine of a <c>Complex</c> number. /// </summary> /// <param name="value"> /// The complex value. /// </param> /// <returns> /// The sine of the complex number. /// </returns> public static Complex Sine(this Complex value) { if (value.IsReal()) { return(new Complex(Sine(value.Real), 0.0)); } return(new Complex( Sine(value.Real) * HyperbolicCosine(value.Imaginary), Cosine(value.Real) * HyperbolicSine(value.Imaginary))); }
/// <summary> /// Trigonometric Cotangent of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The cotangent of the complex number.</returns> public static Complex Cot(this Complex value) { if (value.IsReal()) { return(new Complex(Cot(value.Real), 0d)); } // cot(z) = - j*coth(-j*z) Complex z = Coth(new Complex(value.Imaginary, -value.Real)); return(new Complex(z.Imaginary, -z.Real)); }
/// <summary> /// Trigonometric Tangent of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The tangent of the complex number.</returns> public static Complex Tan(this Complex value) { if (value.IsReal()) { return(new Complex(Tan(value.Real), 0.0)); } // tan(z) = - j*tanh(j*z) Complex z = Tanh(new Complex(-value.Imaginary, value.Real)); return(new Complex(z.Imaginary, -z.Real)); }
/// <summary> /// Trigonometric Tangent of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The tangent of the complex number.</returns> public static Complex Tan(this Complex value) { if (value.IsReal()) { return(new Complex(Tan(value.Real), 0.0)); } var cosr = Cos(value.Real); var sinhi = Sinh(value.Imaginary); var denom = (cosr * cosr) + (sinhi * sinhi); return(new Complex(Sin(value.Real) * cosr / denom, sinhi * Cosh(value.Imaginary) / denom)); }
/// <summary> /// Trigonometric Cosecant of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The cosecant of a complex number.</returns> public static Complex Csc(this Complex value) { if (value.IsReal()) { return(new Complex(Csc(value.Real), 0d)); } var sinr = Sin(value.Real); var sinhi = Sinh(value.Imaginary); var denom = (sinr * sinr) + (sinhi * sinhi); return(new Complex(sinr * Cosh(value.Imaginary) / denom, -Cos(value.Real) * sinhi / denom)); }
/// <summary> /// Trigonometric Tangent of a <c>Complex</c> number. /// </summary> /// <param name="value"> /// The complex value. /// </param> /// <returns> /// The tangent of the complex number. /// </returns> public static Complex Tangent(this Complex value) { if (value.IsReal()) { return(new Complex(Tangent(value.Real), 0.0)); } var cosr = Cosine(value.Real); var sinhi = HyperbolicSine(value.Imaginary); var denom = (cosr * cosr) + (sinhi * sinhi); return(new Complex(Sine(value.Real) * cosr / denom, sinhi * HyperbolicCosine(value.Imaginary) / denom)); }
/// <summary> /// Trigonometric Secant of a <c>Complex</c> number. /// </summary> /// <param name="value"> /// The complex value. /// </param> /// <returns> /// The secant of the complex number. /// </returns> public static Complex Secant(this Complex value) { if (value.IsReal()) { return(new Complex(Secant(value.Real), 0d)); } var cosr = Cosine(value.Real); var sinhi = HyperbolicSine(value.Imaginary); var denom = (cosr * cosr) + (sinhi * sinhi); return(new Complex(cosr * HyperbolicCosine(value.Imaginary) / denom, Sine(value.Real) * sinhi / denom)); }
/// <summary> /// Trigonometric Cosecant of a <c>Complex</c> number. /// </summary> /// <param name="value"> /// The complex value. /// </param> /// <returns> /// The cosecant of a complex number. /// </returns> public static Complex Cosecant(this Complex value) { if (value.IsReal()) { return(new Complex(Cosecant(value.Real), 0d)); } var sinr = Sine(value.Real); var sinhi = HyperbolicSine(value.Imaginary); var denom = (sinr * sinr) + (sinhi * sinhi); return(new Complex(sinr * HyperbolicCosine(value.Imaginary) / denom, -Cosine(value.Real) * sinhi / denom)); }
/// <summary> /// Trigonometric Secant of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The secant of the complex number.</returns> public static Complex Sec(this Complex value) { if (value.IsReal()) { return(new Complex(Sec(value.Real), 0d)); } var cosr = Cos(value.Real); var sinhi = Sinh(value.Imaginary); var denom = (cosr * cosr) + (sinhi * sinhi); return(new Complex(cosr * Cosh(value.Imaginary) / denom, Sin(value.Real) * sinhi / denom)); }
/// <summary> /// Hyperbolic Cosecant of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The hyperbolic cosecant of a complex number.</returns> public static Complex Csch(this Complex value) { if (value.IsReal()) { return(new Complex(Csch(value.Real), 0.0)); } var exp = value.Exp(); if (exp.IsInfinity()) { return(Complex.Zero); } return(2 * exp / (exp.Square() - 1)); }
/// <summary> /// Trigonometric Hyperbolic Secant of a <c>Complex</c> number. /// </summary> /// <param name="value"> /// The complex value. /// </param> /// <returns> /// The hyperbolic secant of a complex number. /// </returns> public static Complex HyperbolicSecant(this Complex value) { if (value.IsReal()) { return(new Complex(HyperbolicSecant(value.Real), 0.0)); } var exp = value.Exponential(); if (exp.IsInfinity()) { return(Complex.Zero); } return(2 * exp / (exp.Square() + 1)); }
/// <summary> /// Hyperbolic Tangent of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The hyperbolic tangent of a complex number.</returns> public static Complex Tanh(this Complex value) { if (value.IsReal()) { return(new Complex(Tanh(value.Real), 0.0)); } var cosi = Cos(value.Imaginary); var sinhr = Sinh(value.Real); if (double.IsInfinity(sinhr)) { return(new Complex(double.IsPositiveInfinity(sinhr) ? 1 : -1, 0.0)); } var denom = (cosi * cosi) + (sinhr * sinhr); return(new Complex(Cosh(value.Real) * sinhr / denom, cosi * Sin(value.Imaginary) / denom)); }
/// <summary> /// Trigonometric Hyperbolic Tangent of a <c>Complex</c> number. /// </summary> /// <param name="value"> /// The complex value. /// </param> /// <returns> /// The hyperbolic tangent of a complex number. /// </returns> public static Complex HyperbolicTangent(this Complex value) { if (value.IsReal()) { return(new Complex(HyperbolicTangent(value.Real), 0.0)); } var cosi = Cosine(value.Imaginary); var sinhr = HyperbolicSine(value.Real); if (double.IsInfinity(sinhr)) { return(new Complex(double.IsPositiveInfinity(sinhr) ? 1 : -1, 0.0)); } var denom = (cosi * cosi) + (sinhr * sinhr); return(new Complex(HyperbolicCosine(value.Real) * sinhr / denom, cosi * Sine(value.Imaginary) / denom)); }
/// <summary> /// Hyperbolic Cotangent of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The hyperbolic cotangent of a complex number.</returns> public static Complex Coth(this Complex value) { if (value.IsReal()) { return(new Complex(Coth(value.Real), 0.0)); } var sini = Sin(value.Imaginary); var sinhr = Sinh(value.Real); if (double.IsInfinity(sinhr)) { return(new Complex(double.IsPositiveInfinity(sinhr) ? 1 : -1, 0.0)); } var denom = (sini * sini) + (sinhr * sinhr); return(new Complex(sinhr * Cosh(value.Real) / denom, -sini * Cos(value.Imaginary) / denom)); }
/// <summary> /// Hyperbolic Cosine of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The hyperbolic cosine of a complex number.</returns> public static Complex Cosh(this Complex value) { if (value.IsReal()) { return(new Complex(Cosh(value.Real), 0.0)); } // cosh(x + j*y) = cosh(x)*cos(y) + j*sinh(x)*sin(y) // if x > huge, cosh(x + j*y) = exp(|x|)/2*cos(y) + j*sign(x)*exp(|x|)/2*sin(y) if (Math.Abs(value.Real) >= 22.0) // Taken from the msun library in FreeBSD { double h = Math.Exp(Math.Abs(value.Real)) * 0.5; return(new Complex( h * Cos(value.Imaginary), Math.Sign(value.Real) * h * Sin(value.Imaginary))); } return(new Complex( Cosh(value.Real) * Cos(value.Imaginary), Sinh(value.Real) * Sin(value.Imaginary))); }
/// <summary> /// Hyperbolic Cosecant of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The hyperbolic cosecant of a complex number.</returns> public static Complex Csch(this Complex value) { if (value.IsReal()) { return(new Complex(Csch(value.Real), 0.0)); } // csch(x + j*y) = (sinh(x)*cot(y)/sin(y) - j*cosh(x)/sin(y))/(1 + sinh^2(x)/sin^2(y)) // if |x| > huge, csch(z) = 4*sinh(x)*cot(y)*sin(y)*exp(-2*|x|) - j*4*cosh(x)*sin(y)*exp(-2*|x|) // if exp(-|x|) = 0, csch(z) = 0 // if cot(y) = +/- oo or 1/sin^2(x) = 1 + cot^2(x) = oo, csch(z) = sign(cot(y))/sinh(x) // // The algorithm is based on Kahan. double coti = Cot(value.Imaginary); double sini = Sin(value.Imaginary); double beta = 1 + coti * coti; double sinhr = Sinh(value.Real); double coshr = Cosh(value.Real); if (Math.Abs(value.Real) >= 22.0) // Taken from the msun library in FreeBSD { double e = Math.Exp(-Math.Abs(value.Real)); return(e == 0.0 ? new Complex(0, 0) : new Complex(4.0 * sinhr * coti * sini * e * e, -4.0 * coshr * sini * e * e)); } if (double.IsInfinity(coti)) { return(new Complex(Math.Sign(coti) / sinhr, 0.0)); } double denom = 1.0 + beta * sinhr * sinhr; return(new Complex(sinhr * coti / sini / denom, -coshr / sini / denom)); }
/// <summary> /// Hyperbolic Secant of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The hyperbolic secant of a complex number.</returns> public static Complex Sech(this Complex value) { if (value.IsReal()) { return(new Complex(Sech(value.Real), 0.0)); } // sech(x + j*y) = (cosh(x)/cos(y) - j*sinh(x)*tan(y)/cos(y))/(1 + sinh^2(x)/cos^2(y)) // if |x| > huge, sech(z) = 4*cosh(x)*cos(y)*exp(-2*|x|) - j*4*sinh(x)*tan(y)*cos(y)*exp(-2*|x|) // if exp(-|x|) = 0, sech(z) = 0 // if tan(y) = +/- oo or 1/cos^2(y) = 1 + tan^2(y) = oo, sech(z) = -j*sign(tan(y))/sinh(x) // // The algorithm is based on Kahan. double tani = Tan(value.Imaginary); double cosi = Cos(value.Imaginary); double beta = 1.0 + tani * tani; double sinhr = Math.Sinh(value.Real); double coshr = Math.Cosh(value.Real); if (Math.Abs(value.Real) >= 22.0) // Taken from the msun library in FreeBSD { double e = Math.Exp(-Math.Abs(value.Real)); return(e == 0.0 ? new Complex(0, 0) : new Complex(4.0 * coshr * cosi * e * e, -4.0 * sinhr * tani * cosi * e * e)); } if (double.IsInfinity(tani)) { return(new Complex(0.0, -Math.Sign(tani) / sinhr)); } double denom = 1.0 + beta * sinhr * sinhr; return(new Complex(coshr / cosi / denom, -sinhr * tani / cosi / denom)); }
/// <summary> /// Hyperbolic Tangent of a <c>Complex</c> number. /// </summary> /// <param name="value">The complex value.</param> /// <returns>The hyperbolic tangent of a complex number.</returns> public static Complex Tanh(this Complex value) { if (value.IsReal()) { return(new Complex(Tanh(value.Real), 0.0)); } // tanh(x + j*y) = (cosh(x)*sinh(x)/cos^2(y) + j*tan(y))/(1 + sinh^2(x)/cos^2(y)) // if |x| > huge, tanh(z) = sign(x) + j*4*cos(y)*sin(y)*exp(-2*|x|) // if exp(-|x|) = 0, tanh(z) = sign(x) // if tan(y) = +/- oo or 1/cos^2(y) = 1 + tan^2(y) = oo, tanh(z) = cosh(x)/sinh(x) // // The algorithm is based on Kahan. if (Math.Abs(value.Real) >= 22.0) // Taken from the msun library in FreeBSD { double e = Math.Exp(-Math.Abs(value.Real)); return(e == 0.0 ? new Complex(Math.Sign(value.Real), 0.0) : new Complex(Math.Sign(value.Real), 4.0 * Math.Cos(value.Imaginary) * Math.Sin(value.Imaginary) * e * e)); } double tani = Tan(value.Imaginary); double beta = 1 + tani * tani; // beta = 1/cos^2(y) = 1 + t^2 double sinhr = Sinh(value.Real); double coshr = Cosh(value.Real); if (double.IsInfinity(tani)) { return(new Complex(coshr / sinhr, 0.0)); } double denom = 1.0 + beta * sinhr * sinhr; return(new Complex(beta * coshr * sinhr / denom, tani / denom)); }
public void CanDetermineIfRealNonNegativeNumber() { var complex = new Complex(1, 0); Assert.IsTrue(complex.IsReal(), "Is a real non-negative number."); }
public void CanDetermineIfRealNumber() { var complex = new Complex(-1, 0); Assert.IsTrue(complex.IsReal(), "Is a real number."); }