diff --git a/README.md b/README.md index 3ed1700..b752b0c 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ A library which implements the complex number data type in Java. The features of this library include: -1- Arithmetic Operations (addition, subtraction, multiplication, division) -2- Complex Specific Operations - Conjugate, Inverse, Absolute/Magnitude, Argument/Phase -3- Trigonometric Operations - sin, cos, tan, cot, sec, cosec -4- Mathematical Functions - exp -5- **Complex Parsing** of type x+yi +* 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, square, sqrt +* **Complex Parsing** of type x+yi ### Example Usage @@ -55,6 +55,11 @@ public class TestComplexNumber // sin, cos System.out.println(ComplexNumber.sin(c3).toString()); System.out.println(ComplexNumber.cos(c3).toString()); + + // square and square root + ComplexNumber c4 = new ComplexNumber(-5, 12); + System.out.println(c4.square()); + System.out.println(c4.sqrt()); } } diff --git a/TestComplexNumber.java b/TestComplexNumber.java index ac1878e..156670f 100644 --- a/TestComplexNumber.java +++ b/TestComplexNumber.java @@ -39,6 +39,11 @@ public static void main(String args[]) // sin, cos System.out.println(ComplexNumber.sin(c3).toString()); System.out.println(ComplexNumber.cos(c3).toString()); + + // square and square root + ComplexNumber c4 = new ComplexNumber(-5, 12); + System.out.println(c4.square()); + System.out.println(c4.sqrt()); } } diff --git a/com/abdulfatir/jcomplexnumber/ComplexNumber.java b/com/abdulfatir/jcomplexnumber/ComplexNumber.java index f761c1d..b5e887d 100644 --- a/com/abdulfatir/jcomplexnumber/ComplexNumber.java +++ b/com/abdulfatir/jcomplexnumber/ComplexNumber.java @@ -181,6 +181,20 @@ public ComplexNumber square() double _imaginary = 2*this.real*this.imaginary; return new ComplexNumber(_real,_imaginary); } + + /** + * The square root of the current complex number. + * @return a ComplexNumber which is the square root of the current complex number. + */ + + public ComplexNumber sqrt() + { + double r = Math.sqrt(this.real*this.real + this.imaginary*this.imaginary); + double real = Math.sqrt((r + this.real) / 2); + double imaginary = Math.sqrt((r - this.real) / 2); + return new ComplexNumber(real, imaginary); + } + /** * @return the complex number in x + yi format */