From ecab09b82e73f248e0222bb4cd0cbfcc59602f95 Mon Sep 17 00:00:00 2001 From: Bencor29 Date: Sun, 27 Oct 2019 01:13:37 +0200 Subject: [PATCH 1/2] Added sqrt method --- README.md | 9 +++++++-- TestComplexNumber.java | 5 +++++ com/abdulfatir/jcomplexnumber/ComplexNumber.java | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3ed1700..13c59ad 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,10 @@ A library which implements the complex number data type in Java. The features of this library include: -1- Arithmetic Operations (addition, subtraction, multiplication, division) +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 +4- Mathematical Functions - exp, square, sqrt 5- **Complex Parsing** of type x+yi @@ -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 */ From 3df8bc382da3e3c317bd2fd8d2c11f107c45260d Mon Sep 17 00:00:00 2001 From: Benjamin CORNOU Date: Sun, 27 Oct 2019 20:37:54 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 13c59ad..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, square, sqrt -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