Repository: commons-numbers Updated Branches: refs/heads/master c2fadea8b -> d22b6d863
NUMBERS-68: Added parse() method to Complex(). TODO: Add testing. Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/d22b6d86 Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/d22b6d86 Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/d22b6d86 Branch: refs/heads/master Commit: d22b6d86356096afe8d5681c1035a68791470342 Parents: c2fadea Author: Eric Barnhill <[email protected]> Authored: Tue Apr 24 14:51:51 2018 +0200 Committer: Eric Barnhill <[email protected]> Committed: Tue Apr 24 14:51:51 2018 +0200 ---------------------------------------------------------------------- .../apache/commons/numbers/complex/Complex.java | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/d22b6d86/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java index e65bc10..217c109 100644 --- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java +++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java @@ -20,6 +20,7 @@ package org.apache.commons.numbers.complex; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import java.lang.NumberFormatException; import org.apache.commons.numbers.core.Precision; /** * Representation of a Complex number, i.e. a number which has both a @@ -132,6 +133,40 @@ public final class Complex implements Serializable { } /** + * Parses a text expression in a String object to produce + * a Cartesian complex number. Acceptable formats are: + * <p><ul> * <li>Single number (parsed as real). Example: "3.14" + * <li>Single number with appended "i" (parsed as imaginary): "1.42i" + * <li>Pair of numbers, separated by plus sign and with appended i: "3 + 4i" + * </ul> + * @throws{ParseException, NumberFormatException} + */ + public static Complex parse(String s) { + final String[] terms = s.split("+"); + if (terms.length == 2) { + if (terms[1].indexOf("i") == terms[1].length()) { + String imagTerm = (String) terms[1].subSequence(0, terms[1].length()-1); + final double real = Double.parseDouble(terms[0]); + final double imaginary = Double.parseDouble(imagTerm); + return Complex.ofCartesian(real, imaginary); + } else { + throw new NumberFormatException("Expression must end with unique \"i\""); + } + } else if (terms.length == 1){ + if (terms[0].indexOf("i") == terms[0].length()) { + String imagTerm = (String) terms[0].subSequence(0, terms[0].length()-1); + final double imaginary = Double.parseDouble(imagTerm); + return Complex.ofCartesian(0, imaginary); + } else { + final double real = Double.parseDouble(terms[0]); + return Complex.ofReal(real); + } + } else { + throw new NumberFormatException("Invalid expression for Complex. See documentation for further information."); + } + } + + /** * Returns true if either real or imaginary component of the Complex * is NaN *
