Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());

}
}
Expand Down
5 changes: 5 additions & 0 deletions TestComplexNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}
}
14 changes: 14 additions & 0 deletions com/abdulfatir/jcomplexnumber/ComplexNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>ComplexNumber</code> 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
*/
Expand Down