From 3d8f334b2af82a903dea3419b79924bbb9ff3d42 Mon Sep 17 00:00:00 2001 From: Daniel Lurcock Date: Wed, 1 Feb 2023 17:24:56 +0000 Subject: [PATCH 1/2] Update ComplexNumber.java Update to include parsing of values in scientific notation. Also allows leading '+'. --- .../jcomplexnumber/ComplexNumber.java | 104 ++++++++++++------ 1 file changed, 69 insertions(+), 35 deletions(-) diff --git a/com/abdulfatir/jcomplexnumber/ComplexNumber.java b/com/abdulfatir/jcomplexnumber/ComplexNumber.java index b5e887d..40617a6 100644 --- a/com/abdulfatir/jcomplexnumber/ComplexNumber.java +++ b/com/abdulfatir/jcomplexnumber/ComplexNumber.java @@ -11,14 +11,19 @@ *
  • Arithmetic Operations (addition, subtraction, multiplication, division)
  • *
  • Complex Specific Operations - Conjugate, Inverse, Absolute/Magnitude, Argument/Phase
  • *
  • Trigonometric Operations - sin, cos, tan, cot, sec, cosec
  • - *
  • Mathematical Functions - exp
  • - *
  • Complex Parsing of type x+yi
  • + *
  • Mathematical Functions - exp, square, sqrt
  • + *
  • Complex Parsing of type x+yi, in standard or scientific notation
  • * * * @author Abdul Fatir - * @version 1.2 + * @version 1.3 * */ + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.ArrayList; + public class ComplexNumber { /** @@ -334,40 +339,69 @@ public double getArg() public static ComplexNumber parseComplex(String s) { s = s.replaceAll(" ",""); + s = s.replaceAll("E","e"); + s = s.replaceAll("I","i"); ComplexNumber parsed = null; - if(s.contains(String.valueOf("+")) || (s.contains(String.valueOf("-")) && s.lastIndexOf('-') > 0)) - { - String re = ""; - String im = ""; - s = s.replaceAll("i",""); - s = s.replaceAll("I",""); - if(s.indexOf('+') > 0) - { - re = s.substring(0,s.indexOf('+')); - im = s.substring(s.indexOf('+')+1,s.length()); - parsed = new ComplexNumber(Double.parseDouble(re),Double.parseDouble(im)); - } - else if(s.lastIndexOf('-') > 0) - { - re = s.substring(0,s.lastIndexOf('-')); - im = s.substring(s.lastIndexOf('-')+1,s.length()); - parsed = new ComplexNumber(Double.parseDouble(re),-Double.parseDouble(im)); - } + + //first determine if either of the numbers might be in scientific notation + ArrayList allMatches = new ArrayList(); + Matcher m = Pattern.compile("[+-]?[0-9.]+e[+-]?[0-9]+").matcher(s); + while (m.find()) { + allMatches.add(m.group()); } - else - { - // Pure imaginary number - if(s.endsWith("i") || s.endsWith("I")) - { - s = s.replaceAll("i",""); - s = s.replaceAll("I",""); - parsed = new ComplexNumber(0, Double.parseDouble(s)); - } - // Pure real number - else - { - parsed = new ComplexNumber(Double.parseDouble(s),0); - } + + String re; + String im; + + switch(allMatches.size()){ + case 2: //both numbers are scientific notation + re=allMatches.get(0); + im=allMatches.get(1); + parsed = new ComplexNumber(Double.parseDouble(re),Double.parseDouble(im)); + break; + case 1: //one number has scientific notation + String[] s2=s.split(allMatches.get(0).replaceAll("\\+","\\\\+"));//replacement required because the match pattern could be something like "+1.234e-06" and the "+" needs to be escaped for Regex to work + if(s.matches(".*" + allMatches.get(0).replaceAll("\\+","\\\\+") + "i" )){//the scientific number must be imaginary + im=allMatches.get(0); + re=s2[0].isEmpty()?"0":s2[0];//if there is no real number, set it to zero + }else{ //the scientific number must be real + re=allMatches.get(0); + if(!(s2.length==0)){ + im=s2[1]; + im = im.replaceAll("i",""); //replace any trailing i values + }else{//there is no imaginary component, so set it to zero + im="0"; + } + } + parsed = new ComplexNumber(Double.parseDouble(re),Double.parseDouble(im)); + break; + case 0: //no scientific notation, use the original method + if( (s.contains(String.valueOf("+")) && s.lastIndexOf('+') > 0) || (s.contains(String.valueOf("-")) && s.lastIndexOf('-') > 0)){ //has a real and imaginary part + s = s.replaceAll("i",""); + if(s.lastIndexOf('+') > 0) + { + re = s.substring(0,s.lastIndexOf('+')); + im = s.substring(s.lastIndexOf('+')+1,s.length()); + parsed = new ComplexNumber(Double.parseDouble(re),Double.parseDouble(im)); + } + else if(s.lastIndexOf('-') > 0) + { + re = s.substring(0,s.lastIndexOf('-')); + im = s.substring(s.lastIndexOf('-')+1,s.length()); + parsed = new ComplexNumber(Double.parseDouble(re),-Double.parseDouble(im)); + } + }else{ + if(s.endsWith("i"))// Pure imaginary number + { + s = s.replaceAll("i",""); + parsed = new ComplexNumber(0, Double.parseDouble(s)); + } + else // Pure real number + { + parsed = new ComplexNumber(Double.parseDouble(s),0); + } + } + break; } return parsed; } From 8105e672d6d06b6b984c410766ce53cfbc392184 Mon Sep 17 00:00:00 2001 From: Daniel Lurcock Date: Wed, 1 Feb 2023 17:26:53 +0000 Subject: [PATCH 2/2] Update README.md Update to allow parsing of values in scientific notation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b752b0c..ca42d8c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ The features of this library include: * Complex Specific Operations - Conjugate, Inverse, Absolute/Magnitude, Argument/Phase * Trigonometric Operations - sin, cos, tan, cot, sec, cosec * Mathematical Functions - exp, square, sqrt -* **Complex Parsing** of type x+yi +* **Complex Parsing** of type x+yi, in standard or scientific notation ### Example Usage