Complex.cs :  » 2.6.4-mono-.net-core » System.Numerics » System » Numerics » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » 2.6.4 mono .net core » System.Numerics 
System.Numerics » System » Numerics » Complex.cs
//
// Complex.cs: Complex number support
//
// Author:
//   Miguel de Icaza (miguel@gnome.org)
//   Marek Safar (marek.safar@gmail.com)
//
// Copyright 2009, 2010 Novell, Inc.
//
//
//
// TODO:
// string ToString (string format, IFormatProvider)
// string ToString (string format)
// string ToString (IFormatProvider)
//
// Acos, Asin, Atan, Exp, Log, Log10, Pow, Sqrt

using System;

namespace System.Numerics{

  public struct Complex : IEquatable<Complex>
  {
    double real, imaginary;

    public static readonly Complex ImaginaryOne = new Complex (0, 1);
    public static readonly Complex One = new Complex (1, 0);
    public static readonly Complex Zero = new Complex (0, 0);
    
    public Complex (double real, double imaginary)
    {
      this.imaginary = imaginary;
      this.real = real;
    }

    public static Complex FromPolarCoordinates (double magnitude, double phase)
    {
      return new Complex (magnitude * Math.Cos (phase), magnitude * Math.Sin (phase));
    }

    public static Complex operator + (Complex left, Complex right)
    {
      return new Complex (left.real + right.real, left.imaginary + right.imaginary);
    }

    public static Complex Add (Complex left, Complex right)
    {
      return new Complex (left.real + right.real, left.imaginary + right.imaginary);
    }
    
    public static Complex operator - (Complex left, Complex right)
    {
      return new Complex (left.real - right.real, left.imaginary - right.imaginary);
    }

    public static Complex Subtract (Complex left, Complex right)
    {
      return new Complex (left.real - right.real, left.imaginary - right.imaginary);
    }
    
    public static Complex operator * (Complex left, Complex right)
    {
      return new Complex (
        left.real * right.real - left.imaginary * right.imaginary,
        left.real * right.imaginary + left.imaginary * right.real);
    }

    public static Complex Multiply (Complex left, Complex right)
    {
      return new Complex (
        left.real * right.real - left.imaginary * right.imaginary,
        left.real * right.imaginary + left.imaginary * right.real);
    }

    public static Complex operator / (Complex left, Complex right)
    {
      double rsri = right.real * right.real + right.imaginary * right.imaginary;
      return new Complex (
        (left.real * right.real + left.imaginary * right.imaginary) / rsri,

        (left.imaginary * right.real - left.real * right.imaginary) / rsri);
    }

    public static Complex Divide (Complex left, Complex right)
    {
      double rsri = right.real * right.real + right.imaginary * right.imaginary;
      return new Complex (
        (left.real * right.real + left.imaginary * right.imaginary) / rsri,

        (left.imaginary * right.real - left.real * right.imaginary) / rsri);
    }

    public static bool operator == (Complex left, Complex right)
    {
      return left.real == right.real && left.imaginary == right.imaginary;
    }

    public bool Equals (Complex value)
    {
      return real == value.real && imaginary == value.imaginary;
    }

    public override bool Equals (object value)
    {
      if (value == null || !(value is Complex))
        return false;

      Complex r = (Complex) value;
      return real == r.real && imaginary == r.imaginary;
    }
    
    public static bool operator != (Complex left, Complex right)
    {
      return left.real != right.real || left.imaginary != right.imaginary;
    }
    
    public static Complex operator - (Complex value)
    {
      return new Complex (-value.real, -value.imaginary);
    }

    public static implicit operator Complex (byte value)
    {
      return new Complex (value, 0);
    }

    public static implicit operator Complex (double value)
    {
      return new Complex (value, 0);
    }
    
    public static implicit operator Complex (short value)
    {
      return new Complex (value, 0);
    }
    
    public static implicit operator Complex (int value)
    {
      return new Complex (value, 0);
    }
    
    public static implicit operator Complex (long value)
    {
      return new Complex (value, 0);
    }

    [CLSCompliant (false)]
    public static implicit operator Complex (sbyte value)
    {
      return new Complex (value, 0);
    }

    public static implicit operator Complex (float value)
    {
      return new Complex (value, 0);
    }

    [CLSCompliant (false)]
    public static implicit operator Complex (ushort value)
    {
      return new Complex (value, 0);
    }

    [CLSCompliant (false)]
    public static implicit operator Complex (uint value)
    {
      return new Complex (value, 0);
    }

    [CLSCompliant (false)]
    public static implicit operator Complex (ulong value)
    {
      return new Complex (value, 0);
    }

    public static explicit operator Complex (decimal value)
    {
      return new Complex ((double) value, 0);
    }

    public static explicit operator Complex (BigInteger value)
    {
      return new Complex ((double) value, 0);
    }

    public override string ToString ()
    {
      return String.Format ("({0:G}, {1:G})", real, imaginary);
    }

    public static double Abs (Complex value)
    {
      return Math.Sqrt (value.imaginary * value.imaginary + value.real * value.real);
    }
    
    public static Complex Conjugate (Complex value)
    {
      return new Complex (value.real, -value.imaginary);
    }

    public static Complex Cos (Complex value)
    {
      return new Complex (Math.Cos (value.real) * Math.Cosh (value.imaginary),
              -Math.Sin (value.real)  * Math.Sinh (value.imaginary));
    }

    public static Complex Cosh (Complex value)
    {
      return new Complex (Math.Cosh (value.real) * Math.Cos (value.imaginary),
              -Math.Sinh (value.real)  * Math.Sin (value.imaginary));
    }
    
    public static Complex Negate (Complex value)
    {
      return -value;
    }

    public static Complex Sin (Complex value)
    {
      return new Complex (Math.Sin (value.real) * Math.Cosh (value.imaginary),
              Math.Cos (value.real)  * Math.Sinh (value.imaginary));
    }
    
    public static Complex Sinh (Complex value)
    {
      return new Complex (Math.Sinh (value.real) * Math.Cos (value.imaginary),
              Math.Cosh (value.real)  * Math.Sin (value.imaginary));
    }
    
    public static Complex Reciprocal (Complex value)
    {
      if (value == Zero)
        return value;
        
      return One / value;
    }
    
    public static Complex Tan (Complex value)
    {
      return Sin (value) / Cos (value);
    }
    
    public static Complex Tanh (Complex value)
    {
      return Sinh (value) / Cosh (value);
    }
    
    public override int GetHashCode ()
    {
      return real.GetHashCode () ^ imaginary.GetHashCode ();
    }
    
    public double Imaginary { get { return imaginary; } }
    public double Real { get { return real; } }

    public double Magnitude {
      get {
        return Math.Sqrt (imaginary * imaginary + real * real);
      }
    }

    public double Phase {
      get {
        return Math.Atan (imaginary / real);
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.