⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 complex.java

📁 Apache的common math数学软件包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.math.complex;import java.io.Serializable;import org.apache.commons.math.util.MathUtils;/** * Representation of a Complex number - a number which has both a  * real and imaginary part. * <p> * Implementations of arithmetic operations handle <code>NaN</code> and * infinite values according to the rules for {@link java.lang.Double} * arithmetic, applying definitional formulas and returning <code>NaN</code> or * infinite values in real or imaginary parts as these arise in computation.  * See individual method javadocs for details.</p> * <p> * {@link #equals} identifies all values with <code>NaN</code> in either real  * or imaginary part - e.g., <pre> * <code>1 + NaNi  == NaN + i == NaN + NaNi.</code></pre></p> * * @version $Revision: 620373 $ $Date: 2008-02-10 18:18:39 -0700 (Sun, 10 Feb 2008) $ */public class Complex implements Serializable  {    /** Serializable version identifier */    private static final long serialVersionUID = -6530173849413811929L;        /** The square root of -1. A number representing "0.0 + 1.0i" */        public static final Complex I = new Complex(0.0, 1.0);        /** A complex number representing "NaN + NaNi" */    public static final Complex NaN = new Complex(Double.NaN, Double.NaN);    /** A complex number representing "+INF + INFi" */    public static final Complex INF = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);    /** A complex number representing "1.0 + 0.0i" */        public static final Complex ONE = new Complex(1.0, 0.0);        /** A complex number representing "0.0 + 0.0i" */        public static final Complex ZERO = new Complex(0.0, 0.0);        /**      * The imaginary part      * @deprecated to be made final and private in 2.0     */    protected double imaginary;        /**      * The real part      * @deprecated to be made final and private in 2.0     */    protected double real;        /**     * Create a complex number given the real and imaginary parts.     *     * @param real the real part     * @param imaginary the imaginary part     */    public Complex(double real, double imaginary) {        super();        this.real = real;        this.imaginary = imaginary;    }    /**     * Return the absolute value of this complex number.     * <p>     * Returns <code>NaN</code> if either real or imaginary part is     * <code>NaN</code> and <code>Double.POSITIVE_INFINITY</code> if     * neither part is <code>NaN</code>, but at least one part takes an infinite     * value.</p>     *     * @return the absolute value     */    public double abs() {        if (isNaN()) {            return Double.NaN;        }                if (isInfinite()) {            return Double.POSITIVE_INFINITY;        }                if (Math.abs(real) < Math.abs(imaginary)) {            if (imaginary == 0.0) {                return Math.abs(real);            }            double q = real / imaginary;            return (Math.abs(imaginary) * Math.sqrt(1 + q*q));        } else {            if (real == 0.0) {                return Math.abs(imaginary);            }            double q = imaginary / real;            return (Math.abs(real) * Math.sqrt(1 + q*q));        }    }        /**     * Return the sum of this complex number and the given complex number.     * <p>     * Uses the definitional formula      * <pre>     * (a + bi) + (c + di) = (a+c) + (b+d)i     * </pre></p>     * <p>     * If either this or <code>rhs</code> has a NaN value in either part,     * {@link #NaN} is returned; otherwise Inifinite and NaN values are     * returned in the parts of the result according to the rules for     * {@link java.lang.Double} arithmetic.</p>      *     * @param rhs the other complex number     * @return the complex number sum     * @throws NullPointerException if <code>rhs</code> is null     */    public Complex add(Complex rhs) {           return createComplex(real + rhs.getReal(),            imaginary + rhs.getImaginary());    }        /**     * Return the conjugate of this complex number. The conjugate of     * "A + Bi" is "A - Bi".      * <p>     * {@link #NaN} is returned if either the real or imaginary     * part of this Complex number equals <code>Double.NaN</code>.</p>     * <p>     * If the imaginary part is infinite, and the real part is not NaN,      * the returned value has infinite imaginary part of the opposite     * sign - e.g. the conjugate of <code>1 + POSITIVE_INFINITY i</code>     * is <code>1 - NEGATIVE_INFINITY i</code></p>     *     * @return the conjugate of this Complex object     */    public Complex conjugate() {        if (isNaN()) {            return NaN;        }           return createComplex(real, -imaginary);    }        /**     * Return the quotient of this complex number and the given complex number.     * <p>     * Implements the definitional formula     * <pre><code>     *    a + bi          ac + bd + (bc - ad)i     *    ----------- = -------------------------     *    c + di               c<sup>2</sup> + d<sup>2</sup>     * </code></pre>     * but uses      * <a href="http://doi.acm.org/10.1145/1039813.1039814">     * prescaling of operands</a> to limit the effects of overflows and     * underflows in the computation.</p>     * <p>     * Infinite and NaN values are handled / returned according to the     * following rules, applied in the order presented:     * <ul>     * <li>If either this or <code>rhs</code> has a NaN value in either part,     *  {@link #NaN} is returned.</li>     * <li>If <code>rhs</code> equals {@link #ZERO}, {@link #NaN} is returned.     * </li>     * <li>If this and <code>rhs</code> are both infinite,     * {@link #NaN} is returned.</li>     * <li>If this is finite (i.e., has no infinite or NaN parts) and     *  <code>rhs</code> is infinite (one or both parts infinite),      * {@link #ZERO} is returned.</li>     * <li>If this is infinite and <code>rhs</code> is finite, NaN values are     * returned in the parts of the result if the {@link java.lang.Double}     * rules applied to the definitional formula force NaN results.</li>     * </ul></p>     *      * @param rhs the other complex number     * @return the complex number quotient     * @throws NullPointerException if <code>rhs</code> is null     */    public Complex divide(Complex rhs) {        if (isNaN() || rhs.isNaN()) {            return NaN;        }        double c = rhs.getReal();        double d = rhs.getImaginary();        if (c == 0.0 && d == 0.0) {            return NaN;        }                if (rhs.isInfinite() && !isInfinite()) {            return ZERO;        }        if (Math.abs(c) < Math.abs(d)) {            if (d == 0.0) {                return createComplex(real/c, imaginary/c);            }            double q = c / d;            double denominator = c * q + d;            return createComplex((real * q + imaginary) / denominator,                (imaginary * q - real) / denominator);        } else {            if (c == 0.0) {                return createComplex(imaginary/d, -real/c);            }            double q = d / c;            double denominator = d * q + c;            return createComplex((imaginary * q + real) / denominator,                (imaginary - real * q) / denominator);        }    }        /**     * Test for the equality of two Complex objects.     * <p>     * If both the real and imaginary parts of two Complex numbers     * are exactly the same, and neither is <code>Double.NaN</code>, the two     * Complex objects are considered to be equal.</p>     * <p>     * All <code>NaN</code> values are considered to be equal - i.e, if either     * (or both) real and imaginary parts of the complex number are equal     * to <code>Double.NaN</code>, the complex number is equal to      * <code>Complex.NaN</code>.</p>     *     * @param other Object to test for equality to this     * @return true if two Complex objects are equal, false if     *         object is null, not an instance of Complex, or     *         not equal to this Complex instance     *      */    public boolean equals(Object other) {        boolean ret;                if (this == other) {             ret = true;        } else if (other == null) {            ret = false;        } else  {            try {                Complex rhs = (Complex)other;                if (rhs.isNaN()) {                    ret = this.isNaN();                } else {                ret = (Double.doubleToRawLongBits(real) ==                        Double.doubleToRawLongBits(rhs.getReal())) &&                    (Double.doubleToRawLongBits(imaginary) ==                        Double.doubleToRawLongBits(rhs.getImaginary()));                 }            } catch (ClassCastException ex) {                // ignore exception                ret = false;            }        }              return ret;    }        /**     * Get a hashCode for the complex number.     * <p>     * All NaN values have the same hash code.</p>     *      * @return a hash code value for this object     */    public int hashCode() {        if (isNaN()) {            return 7;        }        return 37 * (17 * MathUtils.hash(imaginary) +             MathUtils.hash(real));    }    /**     * Access the imaginary part.     *     * @return the imaginary part     */    public double getImaginary() {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -