2
$\begingroup$

I am reading complex number from a file which gives me a list like that

{"(-0.00152709,-0.000650355)", "(7.05005e-06,-4.80275e-07)", "(2.77004e-05,-3.794e-06)",...}

Here, every element in the list is a tuple of two real numbers representing the real and imaginary part of complex number. I want to convert this list to a list of complex numbers. How to do that?

$\endgroup$
2
$\begingroup$
ls = {"(-0.00152709,-0.000650355)", "(7.05005e-06,-4.80275e-07)", "(2.77004e-05,-3.794e-06)"};

Complex@@Interpreter["Number"][StringSplit[#, "(" | ")" | ","]] & /@ ls

(* {-0.00152709 - 0.000650355 I, 7.05005*10^-6 - 4.80275*10^-7 I, 0.0000277004 - 3.794*10^-6 I}*)

$\endgroup$
1
$\begingroup$

Or, First map the string to the form of a valid complex number string, then use the appropriate Interpreter

lst={"(-0.00152709,-0.000650355)","(7.05005e-06,-4.80275e-07)","(2.77004e-05,-3.794e-06)"};

Map[(Interpreter["SemanticComplexNumber"]@StringReplace[#,"("~~a__~~","~~b__~~")":>a<>" + "<>b<>" I"])&,lst]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.