Пример #1
0
        public static ComplexNumber Parse(SqlString str)
        {
            // Regex pattern to match a single number
            const string NumberPattern = @"(?<Number>[+-]?(?:[ij][0-9eE\.]+|[0-9eE\.]+[ij]?))";

            // Regex pattern to match the operator
            const string OperatorPattern = @"(?<Operator>[+-])";

            // Regex pattern to match the whole complex number
            //
            const string Pattern = @"^" +               // Start of string
                                   @"\s*" +             // Whitespace
                                   NumberPattern +      // Number
                                   @"\s*" +             // Whitespace
                                   "(?:" +              // Start of optional of noncapturing group
                                   OperatorPattern +    // Operator
                                   @"\s*" +             // Whitespace
                                   NumberPattern +      // Number
                                   @"\s*" +             // Whitespace
                                   ")?" +               // End of optional noncapturing group
                                   "$";                 // End of string

            Match match;
            CaptureCollection numberCaptures;
            CaptureCollection operatorCaptures;

            double op = 1.0;
            double real = 0.0D;
            double imaginary = 0.0D;

            // Parses the string as a double by first removing the i or j
            Func<string, double> parse = s => double.Parse(Regex.Replace(s, "[ij]", ""));
            Func<string, bool> isImaginary = s => Regex.IsMatch(s, "[ij]");

            ComplexNumber num;

            if (str.IsNull)
                return Null;

            match = Regex.Match(str.Value, Pattern);

            // String format is invalid if regex does not match
            if (!match.Success)
                throw new FormatException("Input string was not in a correct format.");

            // Get the captures for the Number and Operator groups
            numberCaptures = match.Groups["Number"].Captures;
            operatorCaptures = match.Groups["Operator"].Captures;

            // If the string defines two numbers, ensure that exactly one of them is imaginary
            if (numberCaptures.Count == 2 && !(isImaginary(numberCaptures[0].Value) ^ isImaginary(numberCaptures[1].Value)))
                throw new FormatException("Input string was not in a correct format.");

            // Parse the first capture group to the
            // appropriate part of the complex number
            if (isImaginary(numberCaptures[0].Value))
                imaginary = parse(numberCaptures[0].Value);
            else
                real = parse(numberCaptures[0].Value);

            if (numberCaptures.Count == 2)
            {
                // Determine if the sign needs to be
                // inverted based on the operator
                if (operatorCaptures[0].Value == "-")
                    op = -1.0;

                // Parse the second capture group to the
                // appropriate part of the complex number
                if (isImaginary(numberCaptures[1].Value))
                    imaginary = op * parse(numberCaptures[1].Value);
                else
                    real = op * parse(numberCaptures[1].Value);
            }

            // Return the complex number
            num = new ComplexNumber();
            num.m_real = real;
            num.m_imaginary = imaginary;
            return num;
        }
Пример #2
0
        public static ComplexNumber Parse(SqlString str)
        {
            // Regex pattern to match a single number
            const string NumberPattern = @"(?<Number>[+-]?(?:[ij][0-9eE\.]+|[0-9eE\.]+[ij]?))";

            // Regex pattern to match the operator
            const string OperatorPattern = @"(?<Operator>[+-])";

            // Regex pattern to match the whole complex number
            //
            const string Pattern = @"^" +               // Start of string
                                   @"\s*" +             // Whitespace
                                   NumberPattern +      // Number
                                   @"\s*" +             // Whitespace
                                   "(?:" +              // Start of optional of noncapturing group
                                   OperatorPattern +    // Operator
                                   @"\s*" +             // Whitespace
                                   NumberPattern +      // Number
                                   @"\s*" +             // Whitespace
                                   ")?" +               // End of optional noncapturing group
                                   "$";                 // End of string

            Match             match;
            CaptureCollection numberCaptures;
            CaptureCollection operatorCaptures;

            double op        = 1.0;
            double real      = 0.0D;
            double imaginary = 0.0D;

            // Parses the string as a double by first removing the i or j
            Func <string, double> parse       = s => double.Parse(Regex.Replace(s, "[ij]", ""));
            Func <string, bool>   isImaginary = s => Regex.IsMatch(s, "[ij]");

            ComplexNumber num;

            if (str.IsNull)
            {
                return(Null);
            }

            match = Regex.Match(str.Value, Pattern);

            // String format is invalid if regex does not match
            if (!match.Success)
            {
                throw new FormatException("Input string was not in a correct format.");
            }

            // Get the captures for the Number and Operator groups
            numberCaptures   = match.Groups["Number"].Captures;
            operatorCaptures = match.Groups["Operator"].Captures;

            // If the string defines two numbers, ensure that exactly one of them is imaginary
            if (numberCaptures.Count == 2 && !(isImaginary(numberCaptures[0].Value) ^ isImaginary(numberCaptures[1].Value)))
            {
                throw new FormatException("Input string was not in a correct format.");
            }

            // Parse the first capture group to the
            // appropriate part of the complex number
            if (isImaginary(numberCaptures[0].Value))
            {
                imaginary = parse(numberCaptures[0].Value);
            }
            else
            {
                real = parse(numberCaptures[0].Value);
            }

            if (numberCaptures.Count == 2)
            {
                // Determine if the sign needs to be
                // inverted based on the operator
                if (operatorCaptures[0].Value == "-")
                {
                    op = -1.0;
                }

                // Parse the second capture group to the
                // appropriate part of the complex number
                if (isImaginary(numberCaptures[1].Value))
                {
                    imaginary = op * parse(numberCaptures[1].Value);
                }
                else
                {
                    real = op * parse(numberCaptures[1].Value);
                }
            }

            // Return the complex number
            num             = new ComplexNumber();
            num.m_real      = real;
            num.m_imaginary = imaginary;
            return(num);
        }
Пример #3
0
 public ComplexNumber Divide(ComplexNumber num)
 {
     ComplexNumber result = new ComplexNumber();
     result.SetPhasor(Magnitude / num.Magnitude, Angle - num.Angle);
     return result;
 }
Пример #4
0
 public ComplexNumber Multiply(ComplexNumber num)
 {
     ComplexNumber result = new ComplexNumber();
     result.SetPhasor(Magnitude * num.Magnitude, Angle + num.Angle);
     return result;
 }
Пример #5
0
 public ComplexNumber Subtract(ComplexNumber num)
 {
     ComplexNumber result = new ComplexNumber();
     result.m_real = m_real - num.m_real;
     result.m_imaginary = m_imaginary - num.m_imaginary;
     return result;
 }
Пример #6
0
 public ComplexNumber Add(ComplexNumber num)
 {
     ComplexNumber result = new ComplexNumber();
     result.m_real = m_real + num.m_real;
     result.m_imaginary = m_imaginary + num.m_imaginary;
     return result;
 }
Пример #7
0
 public ComplexNumber Negate()
 {
     ComplexNumber num = new ComplexNumber();
     num.m_real = -m_real;
     num.m_imaginary = -m_imaginary;
     return num;
 }