📄 complex.java
字号:
/**
* DuMP3 version morpheus_0.2.9 - a duplicate/similar file finder in Java<BR>
* Copyright 2005 Alexander Grässer<BR>
* All Rights Reserved, http://dump3.sourceforge.net/<BR>
* <BR>
* This file is part of DuMP3.<BR>
* <BR>
* DuMP3 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later version.<BR>
* <BR>
* DuMP3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.<BR>
* <BR>
* You should have received a copy of the GNU General Public License along with DuMP3; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
* Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.za.grasser.duplicate.util.fft;
/**
* Compilation: javac Complex.java<BR>
* Execution: java Complex<BR>
* <BR>
* Data type for complex numbers.<BR>
* <BR>
* The data type is "immutable" so once you create and initialize a complex object, you cannot change it. The "final" keyword when declaring re and im enforces
* this rule, making it a compile-time error to change the .re or .im fields after they've been initialized.<BR>
* <BR> % java Complex<BR>
* a = a = 5.0 + 6.0i<BR>
* b = -3.0 + 4.0i<BR>
* c = -39.0 + 2.0i<BR>
* d = -39.0 + -2.0i<BR>
* e = 5.0<BR>
*
* @author <a href="http://www.cs.princeton.edu/introcs/32datatype/Complex.java.html">Robert Sedgewick and Kevin Wayne</a>
* @version $Revision: 1.3 $
*/
public final class Complex implements Comparable<Object> {
/**
* <code>re</code> Complex - the real part
*/
private final double re;
/**
* <code>im</code> Complex - the imaginary part
*/
private final double im;
/**
* create a new object with the given real and imaginary parts
*
* @param real
* @param imag
*/
public Complex(final double real, final double imag) {
re = real;
im = imag;
}
/**
* return a string representation of the invoking object
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return re + " + " + im + "i";
}
/**
* return abs/modulus/magnitude
*
* @return abs/modulus/magnitude
*/
public double abs() {
return Math.sqrt(re * re + im * im);
}
/**
* return abs/modulus/magnitude squared
*
* @return abs/modulus/magnitude squared
*/
public double scale() {
return re * re + im * im;
}
/**
* return angle/phase/argument
*
* @return angle/phase/argument
*/
public double phase() {
return Math.atan2(im, re);
}
/**
* return a new object whose value is (this + b)
*
* @param b
* @return a new object whose value is (this + b)
*/
public Complex plus(final Complex b) {
final Complex a = this; // invoking object
final double real = a.re + b.re;
final double imag = a.im + b.im;
final Complex sum = new Complex(real, imag);
return sum;
}
/**
* return a new object whose value is (this - b)
*
* @param b
* @return a new object whose value is (this - b)
*/
public Complex minus(final Complex b) {
final Complex a = this;
final double real = a.re - b.re;
final double imag = a.im - b.im;
final Complex diff = new Complex(real, imag);
return diff;
}
/**
* return a new object whose value is (this * b)
*
* @param b
* @return a new object whose value is (this * b)
*/
public Complex times(final Complex b) {
final Complex a = this;
final double real = a.re * b.re - a.im * b.im;
final double imag = a.re * b.im + a.im * b.re;
final Complex prod = new Complex(real, imag);
return prod;
}
/**
* return a new object whose value is (this * b)
*
* @param b
* @return a new object whose value is (this * b)
*/
public Complex times(final double b) {
return new Complex(re * b, im * b);
}
/**
* return a new object whose value is the conjugate of this
*
* @return a new object whose value is the conjugate of this
*/
public Complex conjugate() {
return new Complex(re, -im);
}
/**
* return a new object whose value is the reciprocal of this
*
* @return a new object whose value is the reciprocal of this
*/
public Complex reciprocal() {
final double scale = scale();
return new Complex(re / scale, -im / scale);
}
/**
* return a / b
*
* @param b
* @return a / b
*/
public Complex divides(final Complex b) {
final Complex a = this;
return a.times(b.reciprocal());
}
/**
* a static version of plus
*
* @param a
* @param b
* @return a+b
*/
public static Complex plus(final Complex a, final Complex b) {
final double real = a.re + b.re;
final double imag = a.im + b.im;
final Complex sum = new Complex(real, imag);
return sum;
}
/**
* return the real part
*
* @return the real part
*/
public double real() {
return re;
}
/**
* return the imaginary part
*
* @return the imaginary part
*/
public double imaginary() {
return im;
}
/**
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(final Object arg0) {
if (arg0 instanceof Complex) {
final Complex c = (Complex)arg0;
if (c.re == re && c.im == im) {
return 0;
}
if (scale() - c.scale() == 0.0) {
return (int)(phase() - c.phase());
}
return (int)(scale() - c.scale());
}
throw new IllegalArgumentException("argument not a Complex");
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object arg0) {
return compareTo(arg0) == 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -