📄 z.java
字号:
* <p>
* @return An <tt>Object</tt> that is a copy of this <tt>Complex</tt> object.
* <p>
* @see java.lang.Cloneable
* @see java.lang.Object#clone()
**/
public Object clone ()
{
try
{
return (Object)(super.clone());
}
catch (java.lang.CloneNotSupportedException e)
{
return null; // This cannot happen: there would have to be a serious internal error in the Java runtime if this codepath happens!
}
}
/**
* Extracts the real part of a <tt>Complex</tt> as a <tt>double</tt>.
*
* <p>
* <pre>
* re(x + <i><b>i</b></i>*y) = x
* </pre>
* <p>
* @return The real part
* <p>
* @see Complex#im()
* @see Complex#cart(double, double)
* @see Complex#real(double)
**/
public double re ()
{
return re;
}
/**
* Extracts the imaginary part of a <tt>Complex</tt> as a <tt>double</tt>.
*
* <p>
* <pre>
* im(x + <i><b>i</b></i>*y) = y
* </pre>
* <p>
* @return The imaginary part
* <p>
* @see Complex#re()
* @see Complex#cart(double, double)
**/
public double im ()
{
return im;
}//end im()
/**
* Returns the square of the "length" of a <tt>Complex</tt> number.
*
* <p>
* <pre>
* norm(x + <i><b>i</b></i>*y) = x*x + y*y
* </pre>
* <p>
* Always non-negative.
* <p>
* @return The norm
* <p>
* @see Complex#abs()
**/
public double norm ()
{
return (re*re) + (im*im);
}
/**
* Returns the magnitude of a <tt>Complex</tt> number.
*
* <p>
* <pre>
* abs(z) = Sqrt(norm(z))
* </pre>
* <p>
* In other words, it's Pythagorean distance from the origin
* (<i>0 + 0<b>i</b></i>, or zero).
* <p>
* The magnitude is also referred to as the "modulus" or "length".
* <p>
* Always non-negative.
* <p>
* @return The magnitude (or "length")
* <p>
* @see Complex#arg()
* @see Complex#polar(double, double)
* @see Complex#norm()
**/
public double abs()
{
return abs(re, im);
}
static public double abs(Z z)
{
return z.abs();
}
static private double abs(double x, double y)
{
// abs(z) = Sqrt(norm(z))
// Adapted from
// "Numerical Recipes in Fortran 77: The Art of Scientific Computing"
// (ISBN 0-521-43064-X)
double absX = Math.abs(x);
double absY = Math.abs(y);
if (absX == 0.0 && absY == 0.0) { // !!! Numerical Recipes, mmm?
return 0.0;
} else if (absX >= absY) {
double d = y / x;
return absX*Math.sqrt(1.0 + d*d);
} else {
double d = x / y;
return absY*Math.sqrt(1.0 + d*d);
}//endif
}//end abs()
/**
* Returns the <i>principal</i> angle of a <tt>Complex</tt> number, in
* radians, measured counter-clockwise from the real axis. (Think of the
* reals as the x-axis, and the imaginaries as the y-axis.)
*
* <p>
* There are infinitely many solutions, besides the principal solution.
* If <b>A</b> is the principal solution of <i>arg(z)</i>, the others are of
* the form:
* <p>
* <pre>
* <b>A</b> + 2*k*<b>PI</b>
* </pre>
* <p>
* where k is any integer.
* <p>
* <tt>arg()</tt> always returns a <tt>double</tt> between
* -<tt><b>PI</b></tt> and +<tt><b>PI</b></tt>.
* <p>
* <i><b>Note:</b><ul> 2*<tt><b>PI</b></tt> radians is the same as 360 degrees.
* </ul></i>
* <p>
* <i><b>Domain Restrictions:</b><ul> There are no restrictions: the
* class defines arg(0) to be 0
* </ul></i>
* <p>
* @return Principal angle (in radians)
* <p>
* @see Complex#abs()
* @see Complex#polar(double, double)
**/
public double arg ()
{
return Math.atan2(im, re);
}
/**
* Returns the "negative" of a <tt>Complex</tt> number.
*
* <p>
* <pre>
* neg(a + <i><b>i</b></i>*b) = -a - <i><b>i</b></i>*b
* </pre>
* <p>
* The magnitude of the negative is the same, but the angle is flipped
* through <tt><b>PI</b></tt> (or 180 degrees).
* <p>
* @return Negative of the <tt>Complex</tt>
* <p>
* @see Complex#scale(double)
**/
public Z neg()
{
return this.scale(-1.0);
}
/**
* Returns the <tt>Complex</tt> "conjugate".
*
* <p>
* <pre>
* conj(x + <i><b>i</b></i>*y) = x - <i><b>i</b></i>*y
* </pre>
* <p>
* The conjugate appears "flipped" across the real axis.
* <p>
* @return The <tt>Complex</tt> conjugate
*<p>
**/
public Z conj()
{
return cart(re, -im);
}//end conj()
public Z Conj(Z a)
{
im = -im;
return this;
}//end conj()
static private void inv (Z z)
{
double zRe, zIm;
double scalar;
if (Math.abs(z.re) >= Math.abs(z.im))
{
scalar = 1.0 / ( z.re + z.im*(z.im/z.re) );
zRe = scalar;
zIm = scalar * (- z.im/z.re);
}
else
{
scalar = 1.0 / ( z.re*(z.re/z.im) + z.im );
zRe = scalar * ( z.re/z.im);
zIm = - scalar;
}
z.re = zRe;
z.im = zIm;
}
/**
* Returns the <tt>Complex</tt> scaled by a real number.
*
* <p>
* <pre>
* scale((x + <i><b>i</b></i>*y), s) = (x*s + <i><b>i</b></i>*y*s)
* </pre>
* <p>
* Scaling by the real number <i>2.0</i>, doubles the magnitude, but leaves
* the <tt>arg()</tt> unchanged. Scaling by <i>-1.0</i> keeps the magnitude
* the same, but flips the <tt>arg()</tt> by <tt><b>PI</b></tt> (180 degrees).
* <p>
* @param scalar A real number scale factor
* <p>
* @return <tt>Complex</tt> scaled by a real number
* <p>
* @see Complex#Times(Complex)
* @see Complex#Div(Complex)
* @see Complex#neg()
**/
public Z scale (double scalar)
{
return cart(scalar*re, scalar*im);
}
/**
* To perform z1 + z2, you write <tt>z1.Plus(z2)</tt> .
*
* <p>
* <pre>
* (a + <i><b>i</b></i>*b) + (c + <i><b>i</b></i>*d) = ((a+c) + <i><b>i</b></i>*(b+d))
* </pre>
* <p>
**/
public Z Plus(Z z)
{
Z temp = cart(re + z.re, im + z.im);
return temp;
}
public Z Plus(Z z1, Z z2)
{
Z temp = cart(z1.re + z2.re, z1.im + z2.im);
return temp;
}
/**
* To perform z1 - z2, you write <tt>z1.Minus(z2)</tt> .
*
* <p>
* <pre>
* (a + <i><b>i</b></i>*b) - (c + <i><b>i</b></i>*d) = ((a-c) + <i><b>i</b></i>*(b-d))
* </pre>
* <p>
**/
public Z Minus(Z z)
{
return cart(re - z.re, im - z.im);
}
public Z Minus(Z z1, Z z2)
{
return cart(z1.re - z2.re, z1.im - z2.im);
}
/**
* To perform z1 * z2, you write <tt>z1.Times(z2)</tt> .
*
* <p>
* <pre>
* (a + <i><b>i</b></i>*b) * (c + <i><b>i</b></i>*d) = ( (a*c) - (b*d) + <i><b>i</b></i>*((a*d) + (b*c)) )
* </pre>
* <p>
* @see Complex#scale(double)
**/
public Z Times(Z z)
{
return cart( (re*z.re) - (im*z.im), (re*z.im) + (im*z.re) );
// return cart( (re*z.re) - (im*z.im), (re + im)*(z.re + z.im) - re*z.re - im*z.im);
}
public Z Times(Z a, Z b)
{
Z result = a.Times(b);
re = result.re;
im = result.im;
return this;
}
/**
* To perform z1 / z2, you write <tt>z1.Div(z2)</tt> .
*
* <p>
* <pre>
* (a + <i><b>i</b></i>*b) / (c + <i><b>i</b></i>*d) = ( (a*c) + (b*d) + <i><b>i</b></i>*((b*c) - (a*d)) ) / norm(c + <i><b>i</b></i>*d)
* </pre>
* <p>
* <i><b>Take care not to divide by zero!</b></i>
* <p>
* <i><b>Note:</b><ul> <tt>Complex</tt> arithmetic in Java never causes
* exceptions. You have to deliberately check for overflow, division by
* zero, and so on, <u>for yourself</u>.
* </ul></i>
* <p>
* <i><b>Domain Restrictions:</b><ul> z1/z2 is undefined if z2 = 0
* </ul></i>
* <p>
* @see Complex#scale(double)
**/
public Z Div(Z z)
{
Z result = new Z(this);
Div(result, z.re, z.im);
return result;
}
public Z Div(Z a, Z b)
{
Z result = a.Div(b);
re = result.re;
im = result.im;
return this;
}
static public Z Div(Z z, double x)
{
return Div(z, x, 0.0);
}
static public Z Div(Z z, double x, double y)
{
// Adapted from
// "Numerical Recipes in Fortran 77: The Art of Scientific Computing"
// (ISBN 0-521-43064-X)
double zRe, zIm;
double scalar;
if (Math.abs(x) >= Math.abs(y))
{
scalar = 1.0 / ( x + y*(y/x) );
zRe = scalar * (z.re + z.im*(y/x));
zIm = scalar * (z.im - z.re*(y/x));
}
else
{
scalar = 1.0 / ( x*(x/y) + y );
zRe = scalar * (z.re*(x/y) + z.im);
zIm = scalar * (z.im*(x/y) - z.re);
}
z.re = zRe;
z.im = zIm;
return z;
}
/**
* Returns a <tt>Complex</tt> representing one of the two square roots.
*
* <p>
* <pre>
* Sqrt(z) = Sqrt(abs(z)) * ( cos(arg(z)/2) + <i><b>i</b></i> * sin(arg(z)/2) )
* </pre>
* <p>
* For any <i>complex</i> number <i>z</i>, <i>Sqrt(z)</i> will return the
* <i>complex</i> root whose <i>arg</i> is <i>arg(z)/2</i>.
* <p>
* <i><b>Note:</b><ul> There are always two square roots for each
* <tt>Complex</tt> number, except for 0 + 0<b>i</b>, or zero. The other
* root is the <tt>neg()</tt> of the first one. Just as the two roots of
* 4 are 2 and -2, the two roots of -1 are <b>i</b> and - <b>i</b>.
* </ul></i>
* <p>
* @return The square root whose <i>arg</i> is <i>arg(z)/2</i>.
* <p>
* @see Complex#pow(Complex, double)
**/
public Z Sqrt ()
{
Z result = new Z(this);
Sqrt(result);
return result;
}//end Sqrt()
static public void Sqrt (Z z)
{
// with thanks to Jim Shapiro <jnshapi@argo.ecte.uswc.uswest.com>
// adapted from "Numerical Recipies in C" (ISBN 0-521-43108-5)
// by William H. Press et al
double mag = z.abs();
if (mag > 0.0)
{
if (z.re > 0.0)
{
double temp = Math.sqrt(0.5 * (mag + z.re));
z.re = temp;
z.im = 0.5 * z.im / temp;
}
else
{
double temp = Math.sqrt(0.5 * (mag - z.re));
if (z.im < 0.0)
{
temp = -temp;
}//endif
z.re = 0.5 * z.im / temp;
z.im = temp;
}//endif
}
else
{
z.re = 0.0;
z.im = 0.0;
}
}
/**
* Returns this <tt>Complex</tt> raised to the power of a <tt>Complex</tt> exponent.
*
* <p>
* @param exponent The exponent "by which to raise"
* <p>
* @return this <tt>Complex</tt> "raised to the power of" the exponent
* <p>
* @see Complex#pow(Complex, Complex)
**/
public Z pow (Z exponent)
{
return Z.pow(this, exponent);
}
/**
* Returns the number <i><b>e</b></i> "raised to" a <tt>Complex</tt> power.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -