13,346,882 members (34,939 online)

#### Stats

50.7K views
2K downloads
50 bookmarked
Posted 26 Mar 2009

# OpenWPFChart: Assembling Charts from Components. Part II - Controls

Chart controls composed from Chart Parts
 ```// Ported from "Numerical Recipes in C, 2-nd Edition" to C# 3.0. // Chapter 5.4 Complex Arithmetic // file Complex.cs // \$Id: Complex.cs 18093 2009-03-16 04:15:06Z unknown \$ using System; using System.Globalization; // For CultureInfo namespace NumericalRecipes { /// /// Complex Number /// /// public struct Complex { /// /// Zero (0, 0) Complex Number. /// public static readonly Complex Zero = new Complex(); #region Constructors /// /// Initializes a new instance of the struct. /// /// The re. /// The im. /// This method has two overloads public Complex(double re, double im) { this.re = re; this.im = im; } /// /// Initializes a new instance of the struct. /// /// The re. public Complex(double re) : this(re, 0d) { } #endregion Constructors private double re; /// /// Gets or sets the Real part of the Complex value. /// /// public double Real { get { return re; } } private double im; /// /// Gets or sets the Imaginary part of the Complex value. /// /// public double Imaginary { get { return im; } } #region Object overrides /// public override bool Equals(object obj) { if (obj == null || obj.GetType() != typeof(Complex)) return false; Complex c = (Complex)obj; if (re == c.Real && im == c.Imaginary) return true; return false; } /// public override int GetHashCode() { return re.GetHashCode() ^ im.GetHashCode(); } /// public override string ToString() { if (im == 0.0) return re.ToString(CultureInfo.CurrentCulture); if (re == 0.0) return string.Format(CultureInfo.CurrentCulture, "{0}i{1}", im < 0.0 ? "-" : "+", Math.Abs(im)); return string.Format(CultureInfo.CurrentCulture, "{0}{1}i{2}", re, im < 0.0 ? "-" : "+", Math.Abs(im)); } #endregion Object overrides #region Operators /// /// Implements the operator -. /// /// The value. /// The result of the operator. public static Complex operator-(Complex value) { return Negate(value); } /// /// Implements the operator +. /// /// The first. /// The second. /// The result of the operator. public static Complex operator+(Complex first, Complex second) { return Add(first, second); } /// /// Implements the operator -. /// /// The first. /// The second. /// The result of the operator. public static Complex operator-(Complex first, Complex second) { return Subtract(first, second); } /// /// Implements the operator *. /// /// The first. /// The second. /// The result of the operator. public static Complex operator*(Complex first, Complex second) { return Multiply(first, second); } /// /// Implements the operator *. /// /// The first. /// The second. /// The result of the operator. public static Complex operator*(double first, Complex second) { return Multiply(first, second); } /// /// Implements the operator /. /// /// The dividend. /// The divisor. /// The result of the operator. public static Complex operator/(Complex dividend, Complex divisor) { return Divide(dividend, divisor); } /// /// Implements the operator ==. /// /// The first. /// The second. /// The result of the operator. public static bool operator==(Complex first, Complex second) { return Equals(first, second); } /// /// Implements the operator !=. /// /// The first. /// The second. /// The result of the operator. public static bool operator!=(Complex first, Complex second) { return !(first == second); } #endregion Operators /// /// Negates the specified value. /// /// The value. /// public static Complex Negate(Complex value) { return new Complex(-value.Real, -value.Imaginary); } /// /// Sums up the first and the second arguments. /// /// The first item. /// The second item. /// The Sum. public static Complex Add(Complex first, Complex second) { return new Complex(first.Real + second.Real, first.Imaginary + second.Imaginary); } /// /// Subtracts the specified second argument from the first argument. /// /// The first item. /// The second item. /// The Difference. public static Complex Subtract(Complex first, Complex second) { return new Complex(first.Real - second.Real, first.Imaginary - second.Imaginary); } /// /// Multiplies the first and the second arguments. /// /// The first item. /// The second item. /// The Product. public static Complex Multiply(Complex first, Complex second) { return new Complex(first.Real * second.Real - first.Imaginary * second.Imaginary , first.Imaginary * second.Real + first.Real * second.Imaginary); } /// /// Multiplies the first and the second arguments. /// /// The first item. /// The second item. /// The Product. public static Complex Multiply(double first, Complex second) { return new Complex(second.Real * first, second.Imaginary * first); } /// /// Divides the specified dividend by the divisor. /// /// The dividend item. /// The divisor item. /// The Quotient. public static Complex Divide(Complex dividend, Complex divisor) { double div = divisor.Real * divisor.Real + divisor.Imaginary * divisor.Imaginary; return new Complex((dividend.Real * divisor.Real + dividend.Imaginary * divisor.Imaginary) / div, (dividend.Imaginary * divisor.Real - dividend.Real * divisor.Imaginary) / div); } /// /// Tests the specified arguments for equality. /// /// The first item. /// The second item. /// public static bool Equals(Complex first, Complex second) { return first.Equals(second); } /// /// Gets the Conjugate value. /// /// The value. /// public static Complex Conjugate(Complex value) { return new Complex(value.Real, -value.Imaginary); } /// /// Gets the Absolute value. /// /// The value. /// public static double Abs(Complex value) { double x = Math.Abs(value.Real); double y = Math.Abs(value.Imaginary); double ans, temp; if (x == 0.0) ans = y; else if (y == 0.0) ans = x; else if (x > y) { temp = y / x; ans = x * Math.Sqrt(1.0 + temp * temp); } else { temp = x / y; ans = y * Math.Sqrt(1.0 + temp * temp); } return ans; } /// /// Gets the Square root value. /// /// The value. /// public static Complex Sqrt(Complex value) { if (value.Real == 0.0 && value.Imaginary == 0.0) { return new Complex(); } else { double x = Math.Abs(value.Real); double y = Math.Abs(value.Imaginary); double w, r; if (x >= y) { r = y / x; w = Math.Sqrt(x) * Math.Sqrt(0.5 * (1.0 + Math.Sqrt(1.0 + r * r))); } else { r = x / y; w = Math.Sqrt(y) * Math.Sqrt(0.5 * (r + Math.Sqrt(1.0 + r * r))); } if (value.Real >= 0.0) return new Complex(w, value.Imaginary / (2.0 * w)); else { double im = (value.Imaginary >= 0) ? w : -w; return new Complex(value.Imaginary / (2.0 * im), im); } } } } } ```

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

## License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

## About the Author

 Team Leader Russian Federation
No Biography provided

## You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 26 Mar 2009
Article Copyright 2009 by Oleg V. Polikarpotchkin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid