📄 z.java
字号:
/* File: Complex.java
* -- A Java class for performing complex
* number arithmetic to double precision.
*
* Copyright (c) 1997 - 2001, Alexander Anderson.
*
* This program 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.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package jmathlib.toolbox.jmathlib.matrix._private.Jampack;
import java.io.Serializable;
import java.text.NumberFormat;
/**
* <p>
* @version
* <b>1.0.1</b> <br>
* <tt>
* Last change: ALM 23 Mar 2001 8:56 pm
* </tt>
* <p>
* A Java class for performing complex number arithmetic to <tt>double</tt>
* precision.
*
* <p>
* <center>
* <applet
* name="SeeComplex"
* archive="SeeComplex.jar"
* code="SeeComplex.class"
* codebase="imagery"
* width="85%"
* height="85%"
* align="Middle"
* alt="SeeZ Applet"
* >
* Make yours a Java enabled browser and OS!
* </applet>
* <p>
* This applet has been adapted<br>from a <a
* href="http://www.pa.uky.edu/~phy211/VecArith/index.html">Vector
* Visualization applet</a> by <a
* href="mailto:Vladimir Sorokin <vsoro00@pop.uky.edu>">Vladimir Sorokin</a>.
* </center>
* <hr>
*
* <p>
* @author <a HREF="mailto:Alexander Anderson <sandy@almide.demon.co.uk>">Sandy Anderson</a>
* @author Priyantha Jayanetti
* <p>
* <font color="000080">
* <pre>
* <b>Copyright (c) 1997 - 2001, Alexander Anderson.</b>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the <a href="http://www.gnu.org/">GNU</a> General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public <a href="GNU_GeneralPublicLicence.html">License</a>
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
* </pre>
* </font>
* <p>
* The latest version of this <tt>Complex</tt> class is available from
* the <a href="http://www.netlib.org/">Netlib Repository</a>.
* <p>
* Here's an example of the style the class permits:<br>
*
* <pre>
* <b>import</b> ORG.netlib.math.complex.Complex;<br>
* <b>public</b> <b>class</b> Test {<br>
* <b>public boolean</b> isInMandelbrot (Z c, <b>int</b> maxIter) {
* Z z= <b>new</b> Z(0, 0);<br>
* <b>for</b> (<b>int</b> i= 0; i < maxIter; i++) {
* z= z.Times(z).Plus(c);
* <b>if</b> (z.abs() > 2) <b>return false</b>;
* }<br>
* <b>return true</b>;
* }<br>
* }
* </pre>
* </dd>
* <p>
* <dd>This class was developed by
* <a HREF="http://www.almide.demon.co.uk">Sandy Anderson</a> at the
* School of Electronic Engineering,
* <a HREF="http://www.mdx.ac.uk/">Middlesex University</a>, UK, and
* Priyantha Jayanetti at The Power Systems Program, the
* <a HREF="http://www.eece.maine.edu/">University of Maine</a>, USA.
* </dd>
* <p>
* <dd>And many, many thanks to <a href="mailto:R.D.Hirsch@red-deer.demon.co.uk">Mr. Daniel
* Hirsch</a>, for his constant advice on the mathematics, his exasperating
* ability to uncover bugs blindfold, and for his persistent badgering over
* the exact wording of this documentation.
* </dd>
* <p>
* <dd>For instance, he starts to growl like a badger if you say "infinite set".</dd><br>
* <dd>"Grrr...What's <i>that</i> mean? <i>Countably</i> infinite?"</dd><br>
* <dd>You think for a while.</dd><br>
* <dd>"Grrr..."</dd><br>
* <dd>"Yes."</dd><br>
* <dd>"Ah! Then you mean <i>infinitely many</i>."</dd><br>
* <p>
**/
public class Z implements Cloneable, Serializable {
public static final String VERSION = "1.0.1";
public static final String DATE = "Fri 23-Mar-2001 8:56 pm";
public static final String AUTHOR = "sandy@almide.demon.co.uk";
public static final String REMARK = "Class available from "
+ "http://www.netlib.org/";
/** Z 1. */
public static final Z ONE = new Z(1,0);
/** Z 0. */
public static final Z ZERO = new Z(0,0);
/** Imaginary unit. */
public static final Z I = new Z(0,1);
/**stores the number format for displaying the number*/
private static NumberFormat numFormat = NumberFormat.getInstance();
/**
* Switches on debugging information.
* <p>
**/
// protected static boolean debug = false;
/**
* Whilst debugging: the nesting level when tracing method calls.
* <p>
**/
// private static int trace_nesting = 0;
/**
* Twice <a
* href="http://cad.ucla.edu/repository/useful/PI.txt"><tt><b>PI</b></tt></a>
* radians is the same thing as 360 degrees.
* <p>
**/
protected static final double TWO_PI = 2.0 * Math.PI;
/**
* A constant representing <i><b>i</b></i>, the famous square root of
* <i>-1</i>.
* <p>
* The other square root of <i>-1</i> is - <i><b>i</b></i>.
* <p>
**/
public static final Z i = new Z(0.0, 1.0);
/**The number 1 stored as a complex number
**/
public static final Z One = new Z(1.0, 0);
/**The number 0 stored as a complex number
**/
public static final Z Zero = new Z(0.0, 0);
// private static long objectCount; // !!!
public double re;
public double im;
//---------------------------------//
// CONSTRUCTORS //
//---------------------------------//
/**
Constructs a <tt>Complex</tt> representing the number zero.
<p>
**/
public Z ()
{
this(0.0, 0.0);
}
/**
* Constructs a <tt>Complex</tt> representing a real number.
*
* <p>
* @param re The real number
* <p>
* @see Complex#real(double)
**/
public Z (double re)
{
this(re, 0.0);
}
/**
* Constructs a separate new <tt>Complex</tt> from an existing
* <tt>Complex</tt>.
*
* <p>
* @param z A <tt>Complex</tt> number
* <p>
**/
public Z (Z z)
{
this(z.re, z.im);
}
/**
* Constructs a <tt>Complex</tt> from real and imaginary parts.
*
* <p>
* <i><b>Note:</b><ul> <font color="000080">All methods in class
* <tt>Complex</tt> which deliver a <tt>Complex</tt> are written such that
* no intermediate <tt>Complex</tt> objects get generated. This means that
* you can easily anticipate the likely effects on garbage collection caused
* by your own coding.</font>
* </ul></i>
* <p>
* @param re Real part
* @param im Imaginary part
* <p>
* @see Complex#cart(double, double)
* @see Complex#polar(double, double)
**/
public Z (double re, double im)
{
this.re = re;
this.im = im;
//numFormat.setMaximumFractionDigits(3);
}
//---------------------------------//
// STATIC //
//---------------------------------//
/**
* Returns a <tt>Complex</tt> representing a real number.
*
* <p>
* @param real The real number
* <p>
* @return <tt>Complex</tt> representation of the real
* <p>
* @see Complex#re()
* @see Complex#cart(double, double)
**/
public static Z real(double real)
{
return new Z(real, 0.0);
}
/**
* Returns a <tt>Complex</tt> from real and imaginary parts.
*
* <p>
* @param re Real part
* @param im Imaginary part
* <p>
* @return <tt>Complex</tt> from Cartesian coordinates
* <p>
* @see Complex#re()
* @see Complex#im()
* @see Complex#polar(double, double)
* @see Complex#toString()
**/
public static Z cart (double re, double im)
{
return new Z(re, im);
}
/**
* Returns a <tt>Complex</tt> from a size and direction.
*
* <p>
* @param r Size
* @param theta Direction (in <i>radians</i>)
* <p>
* @return <tt>Complex</tt> from Polar coordinates
* <p>
* @see Complex#abs()
* @see Complex#arg()
* @see Complex#cart(double, double)
**/
public static Z polar (double r, double theta)
{
if (r < 0.0)
{
theta += Math.PI;
r = -r;
}
theta = theta % TWO_PI;
return cart(r * Math.cos(theta), r * Math.sin(theta));
}
/**
* Returns the <tt>Complex</tt> base raised to the power of the exponent.
*
* <p>
* @param base The base "to raise"
* @param exponent The exponent "by which to raise"
* <p>
* @return base "raised to the power of" exponent
* <p>
* @see Complex#pow(double, Complex)
**/
public static Z pow (Z base, double exponent)
{
// return base.log().scale(exponent).exp();
double re = exponent * Math.log(base.abs());
double im = exponent * base.arg();
double scalar = Math.exp(re);
return cart( scalar * Math.cos(im), scalar * Math.sin(im) );
}
/**
* Returns the base raised to the power of the <tt>Complex</tt> exponent.
*
* <p>
* @param base The base "to raise"
* @param exponent The exponent "by which to raise"
* <p>
* @return base "raised to the power of" exponent
* <p>
* @see Complex#pow(Complex, Complex)
* @see Complex#exp()
**/
public static Z pow (double base, Z exponent)
{
// return real(base).log().Times(exponent).exp();
double re = Math.log(Math.abs(base));
double im = Math.atan2(0.0, base);
double re2 = (re*exponent.re) - (im*exponent.im);
double im2 = (re*exponent.im) + (im*exponent.re);
double scalar = Math.exp(re2);
return cart( scalar * Math.cos(im2), scalar * Math.sin(im2) );
}
/**
* Returns the <tt>Complex</tt> base raised to the power of the <tt>Complex</tt> exponent.
*
* <p>
* @param base The base "to raise"
* @param exponent The exponent "by which to raise"
* <p>
* @return base "raised to the power of" exponent
* <p>
* @see Complex#pow(Complex, double)
* @see Complex#pow(Complex)
**/
public static Z pow (Z base, Z exponent)
{
// return base.log().Times(exponent).exp();
double re = Math.log(base.abs());
double im = base.arg();
double re2 = (re*exponent.re) - (im*exponent.im);
double im2 = (re*exponent.im) + (im*exponent.re);
double scalar = Math.exp(re2);
return cart( scalar * Math.cos(im2), scalar * Math.sin(im2) );
}
//---------------------------------//
// PUBLIC //
//---------------------------------//
/**
* Returns <tt>true</tt> if either the real or imaginary component of this
* <tt>Complex</tt> is an infinite value.
*
* <p>
* @return <tt>true</tt> if either component of the <tt>Complex</tt> object is infinite; <tt>false</tt>, otherwise.
* <p>
**/
public boolean isInfinite()
{
return ( Double.isInfinite(re) || Double.isInfinite(im) );
}
/**
* Returns <tt>true</tt> if either the real or imaginary component of this
* <tt>Complex</tt> is a Not-a-Number (<tt>NaN</tt>) value.
*
* <p>
* @return <tt>true</tt> if either component of the <tt>Complex</tt> object is <tt>NaN</tt>; <tt>false</tt>, otherwise.
* <p>
**/
public boolean isNaN()
{
return ( Double.isNaN(re) || Double.isNaN(im) );
}
/**
* Decides if two <tt>Complex</tt> numbers are "sufficiently" alike to be
* considered equal.
*
* <p>
* <tt>tolerance</tt> is the maximum magnitude of the difference between
* them before they are considered <i>not</i> equal.
* <p>
* Checking for equality between two real numbers on computer hardware is a
* tricky business. Try
* <p>
* <pre> System.out.println((1.0/3.0 * 3.0));</pre>
* <p>
* and you'll see the nature of the problem! It's just as tricky with
* <tt>Complex</tt> numbers.
* <p>
* Realize that because of these complications, it's possible to find that
* the magnitude of one <tt>Complex</tt> number <tt>a</tt> is less than
* another, <tt>b</tt>, and yet <tt>a.equals(b, myTolerance)</tt> returns
* <tt>true</tt>. Be aware!
* <p>
* @param z The <tt>Complex</tt> to compare with
* @param tolerance The tolerance for equality
* <p>
* @return <tt>true</tt>, or <tt>false</tt>
* <p>
**/
public boolean equals (Z z, double tolerance)
{
// still true when _equal_ to tolerance? ...
return abs(re - z.re, im - z.im) <= Math.abs(tolerance);
// ...and tolerance is always non-negative
}//end equals(Complex,double)
/**test if the object is equal to this one within a tolerance of
1E-10
@param arg = the object to test against*/
public boolean equals (Object arg)
{
if(arg instanceof Z)
{
// still true when _equal_ to tolerance? ...
return equals( ((Z)arg), 0.0000000001);
// ...and tolerance is always non-negative
}
return false;
}//end equals(Complex,double)
/**
* Overrides the {@link java.lang.Cloneable <tt>Cloneable</tt>} interface.
*
* <p>
* Standard override; no change in semantics.
* <p>
* The following Java code example illustrates how to clone, or <i>copy</i>, a
* <tt>Complex</tt> number:
* <p>
* <pre>
* Z z1 = <b>new</b> Z(0, 1);
* Z z2 = (Complex) z1.clone();
* </pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -