Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Complex {
- private final double re;
- private final double im;
- public Complex() {
- re = 0;
- im = 0;
- }
- public Complex(double real, double imaginary) {
- re = real;
- im = imaginary;
- }
- public Complex(double real) {
- re = real;
- im = 0;
- }
- public Complex(Complex z) {
- re = z.re;
- im = z.im;
- }
- public String toString() {
- if (im == 0) return re + "";
- if (re == 0) return im + "i";
- if (im < 0) return re + " - " + (-im) + "i";
- return re + " + " + im + "i";
- }
- public double re() {
- return re;
- }
- public double im() {
- return im;
- }
- public boolean isReal() {
- return im == 0;
- }
- public double abs() {
- return Math.hypot(re, im);
- }
- public double phase() {
- return Math.atan2(im, re);
- }
- public boolean equals(Complex z) {
- return (re == z.re) && (im == z.im);
- }
- public Complex add(Complex z) {
- double real = re + z.re;
- double imaginary = im + z.im;
- return new Complex(real, imaginary);
- }
- public Complex substract(Complex z) {
- double real = re - z.re;
- double imaginary = im - z.im;
- return new Complex(real, imaginary);
- }
- public Complex multiply(double x) {
- return new Complex(x * re, x * im);
- }
- public Complex multiply(Complex z) {
- double real = re * z.re - im * z.im;
- double imaginary = re * z.im + im * z.re;
- return new Complex(real, imaginary);
- }
- public Complex conjugate() {
- return new Complex(re, -im);
- }
- public Complex reciprocal() {
- double scale = re * re + im * im;
- return new Complex(re / scale, -im / scale);
- }
- public Complex divide(double x) {
- return new Complex(re / x, im / x);
- }
- public Complex divide(Complex z) {
- return multiply(z.reciprocal());
- }
- public Complex exp() {
- return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
- }
- public Complex sin() {
- return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
- }
- public Complex cos() {
- return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
- }
- public Complex tan() {
- return sin().divide(cos());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement