0

Complex numbers class from Numerics does not have any Parse method
How to write Parse methods
Is it good idea to place them into separate class as extension method

This is my attempt but I am not sure that it is correct

public static class Extension
{
    public static bool TryParse(this Complex c,String s, out Complex z)
    {
        double re,im;
        bool b1,b2;
        s.Trim();
        if(!s.EndsWith("i") && !s.EndsWith("j"))
        {
            b1= double.TryParse(s,out re);
            z = new Complex(re,0);
            return b1;
        }
        int index = s.IndexOf('+',1);
        if(index < 0)
          index = s.IndexOf('-',1);
        s = s.Substring(0,s.Length-1);
        if(s.Substring(index+1) == "")
            s+="1";
        if(index < 0)
        {
            b2 = double.TryParse(s,out im);
            z = new Complex(0,im);
            return b2;
        }
        b1 = double.TryParse(s.Substring(0,index),out  re);
        b2 = double.TryParse(s.Substring(index),out  im);
        z = new Complex(re,im);
        return b1 && b2;
    }
}

I am not sure that it is correct I have no idea how I can parse complex number in the format z = (a,b)
where a and b are doubles

There is another Parse method

public static Complex Parse(string s)

Maybe it is good idea to write to Microsoft with request
to update Complex class from Numerics

J Doe
  • 1
  • 1
  • In order to _use_ an Extension method, you always need an instance. I am not sure if there is a way to create a static extension. – Fildor May 16 '22 at 10:06
  • See https://stackoverflow.com/a/866932/982149 – Fildor May 16 '22 at 10:07
  • Does this answer your question? [Static extension methods](https://stackoverflow.com/questions/866921/static-extension-methods) – Fildor May 16 '22 at 10:08
  • I created an API suggestion for this in the dotnet/runtime repo: https://github.com/dotnet/runtime/issues/69388 – Martin Costello May 16 '22 at 10:27
  • Yes extension method need instance Is this parse method written correctly How to parse complex numbers in the form (a,b) How would you write method missing in class – J Doe May 16 '22 at 10:35
  • Martin nice but base type should be double unless we want to present pixel position as complex number – J Doe May 16 '22 at 10:50
  • @JDoe I'm not sure what you mean - Complex is a struct, so has no base type (other than `object`). – Martin Costello May 16 '22 at 11:12

1 Answers1

0

I have no idea how I can parse complex number in the format z = (a,b)

This is how you create an extension method to parse your string to Complex number with format "(a,b)"

public static class StringExtensions
{

    /// <summary>
    /// accepts format (a,b)
    /// </summary>
    /// <returns>Boolean to indicate whether parsing was successfull or not</returns>
    public static bool ToComplex(this string complexString, out Complex complexNumber)
    {
        complexString = complexString.Trim();
        if (string.IsNullOrEmpty(complexString) || !(complexString[0] == '(' && complexString[^1] == ')') || !complexString.Contains(','))
        {
            complexNumber = new Complex();
            return false;
        }

        complexString = complexString[1..^1];
        string[] splitNumbers = complexString.Split(',');

        if (double.TryParse(splitNumbers[0], out double real) && double.TryParse(splitNumbers[1], out double imaginary))
        {
            complexNumber = new Complex(real, imaginary);
            return true;
        }

        complexNumber = new Complex();
        return false;
    }
}

To use it simply import the namespace where you put your file in, then you can myString.ToComplex(out Complex myComplexNumber).

Here is the output in my console application:

string someNumber = "(8437821.47328,47321807432)";
string someNumber2 = "(8437,47321)";
string someNumber3 = "(00002.2,555.5)";

someNumber.ToComplex(out Complex complex);
someNumber2.ToComplex(out Complex complex2);
someNumber3.ToComplex(out Complex complex3);

Console.WriteLine($"some number: {someNumber}. Real number is: {complex.Real}, and imaginary is: {complex.Imaginary}");
Console.WriteLine($"some number: {someNumber2}. Real number is: {complex2.Real}, and imaginary is: {complex2.Imaginary}");
Console.WriteLine($"some number: {someNumber3}. Real number is: {complex3.Real}, and imaginary is: {complex3.Imaginary}");

Console.Read();

screenshot

AMunim
  • 992
  • 6
  • 13