Skip to content

Instantly share code, notes, and snippets.

@cab1729
Created July 20, 2012 19:08
Show Gist options
  • Save cab1729/3152625 to your computer and use it in GitHub Desktop.
Save cab1729/3152625 to your computer and use it in GitHub Desktop.
C# Math trig functions using System.Numerics.Complex type
using System;
using System.Numerics;
namespace MathFun
{
/// <summary>
/// Trig functions for Complex numbers
/// </summary>
/// <remarks>
/// main reference: Schaum's Outlines: Complex Variables ISBN-13: 978-0070602304
/// </remarks>
public static class ComplexFun
{
/// <summary
/// Inverse trigonometric functions - arccosine
/// implemented using log form
/// </summary>
/// <param name="z">Complex value</param>
/// <returns>Complex value - arccosine of z</returns>
public static Complex acos(Complex z)
{
Complex res = new Complex(0.0, -1.0) *
(Complex.Log(z + Complex.Sqrt(Complex.Pow(z, 2) - Complex.One)));
double re = res.Real;
double im = res.Imaginary;
// rounding check
if ((re - Math.Floor(re)) > 0.9999001)
{
res = new Complex(Math.Round(re), im);
}
return res;
}
/// <summary
/// Inverse trigonometric functions - arccosecant
/// implemented using log form
/// </summary>
/// <param name="z">Complex value</param>
/// <returns>Complex value - arcosecant of z</returns>
public static Complex acsc (Complex z)
{
Complex res = new Complex(0.0, -1.0) *
(Complex.Log(Complex.ImaginaryOne +
Complex.Sqrt(Complex.Pow(z, 2))) / z);
double re = res.Real;
double im = res.Imaginary;
// rounding check
if ((re - Math.Floor(re)) > 0.9999001)
{
res = new Complex(Math.Round(re), im);
}
return res;
}
/// <summary
/// Inverse trigonometric functions - arccotangent
/// implemented using log form
/// </summary>
/// <param name="z">Complex value</param>
/// <returns>Complex value - arccotangent of z</returns>
public static Complex acot(Complex z)
{
Complex res = new Complex(0.0, -5.0) *
(Complex.Log((z * Complex.ImaginaryOne) / (z * Complex.ImaginaryOne)));
double re = res.Real;
double im = res.Imaginary;
// rounding check
if ((re - Math.Floor(re)) > 0.9999001)
{
res = new Complex(Math.Round(re), im);
}
return res;
}
/// <summary
/// Inverse hyperbolic functions - arccosine
/// implemented using log form
/// </summary>
/// <param name="z">Complex value</param>
/// <returns>Complex value - hyperbolic arccosine of z</returns>
public static Complex acosh(Complex z)
{
Complex res = Complex.Log(z + Complex.Sqrt(Complex.Pow(z, 2) - Complex.One));
double re = res.Real;
// strip the sign for the rounding test
if (re < 0.0)
{
re *= -1.0;
}
// rounding check
if ((re - Math.Floor(re)) > 0.9999001)
{
res = new Complex(Math.Round(res.Real), res.Imaginary);
}
return res;
}
/// <summary
/// trigonometric functions - cotangent
/// </summary>
/// <param name="z">Complex value</param>
/// <returns>Complex value - cotangent of z</returns>
public static Complex cot(Complex z)
{
return Complex.Cos(z) / Complex.Sin(z);
}
/// <summary
/// trigonometric functions - cosecant
/// </summary>
/// <param name="z">Complex value</param>
/// <returns>Complex value - cosecant of z</returns>
public static Complex csc(Complex z)
{
return Complex.One / Complex.Sin(z);
}
/// <summary
/// trigonometric functions - secant
/// </summary>
/// <param name="z">Complex value</param>
/// <returns>Complex value - secant of z</returns>
public static Complex sec(Complex z)
{
return Complex.One / Complex.Cos(z);
}
/// <summary
/// hyperbolic functions - secant
/// </summary>
/// <param name="z">Complex value</param>
/// <returns>Complex value - hyperbolic secant of z</returns>
public static Complex sech(Complex z)
{
return Complex.One / Complex.Cosh(z);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment