AForge.Math.Complex.Parse C# (CSharp) Method

Parse() public static method

Converts the specified string to its Complex equivalent.
String representation of the complex number is not correctly formatted.
public static Parse ( string s ) : Complex
s string A string representation of a complex number.
return Complex
        public static Complex Parse(string s)
        {
            var r = new Regex(@"\((?<real>.*),(?<imaginary>.*)\)", RegexOptions.None);
            Match m = r.Match(s);

            if (m.Success)
            {
                return new Complex(
                    double.Parse(m.Result("${real}")),
                    double.Parse(m.Result("${imaginary}"))
                    );
            }
            else
            {
                throw new FormatException("String representation of the complex number is not correctly formatted.");
            }
        }

Usage Example

Beispiel #1
0
 /// <summary>
 /// Try to convert the specified string to its <see cref="Complex"/> equivalent.
 /// </summary>
 ///
 /// <param name="s">A string representation of a complex number.</param>
 ///
 /// <param name="result"><see cref="Complex"/> instance to output the result to.</param>
 ///
 /// <returns>Returns boolean value that indicates if the parse was successful or not.</returns>
 ///
 public static bool TryParse(string s, out Complex result)
 {
     try
     {
         Complex newComplex = Complex.Parse(s);
         result = newComplex;
         return(true);
     }
     catch (FormatException)
     {
         result = new Complex( );
         return(false);
     }
 }