commons-math3-3.6.1-类-SparseGradient-中英对照文档及源码赏析

commons-math3-3.6.1-类-SparseGradient-中英对照文档及源码赏析

摘要:中英对照文档、源码赏析、org.apache.commons.math3.analysis.differentiation.SparseGradient、SparseGradient、commons-math3、3.6.1、commons-math3-3.6.1、java

完整中文文档、中英对照文档下载请移步:commons-math3-中文文档、中英对照文档-CSDN下载

1. 开源组件说明

commonsmath.gif

commonslogo.png

jar包名称:commons-math3-3.6.1.jar

Maven 依赖:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.6.1</version>
</dependency>

完整中文文档、中英对照文档下载请移步:commons-math3-中文文档、中英对照文档-CSDN下载

2. 类:org.apache.commons.math3.analysis.differentiation.SparseGradient

2.1. SparseGradient 中英对照文档

org.apache.commons.math3.analysis.differentiation
类 SparseGradient
  • java.lang.Object
    • org.apache.commons.math3.analysis.differentiation.SparseGradient
  • All Implemented Interfaces:
    Serializable, FieldElement< SparseGradient>, RealFieldElement< SparseGradient>

    Serializable,FieldElement ,RealFieldElement



    public class SparseGradient
    extends Object
    implements RealFieldElement<SparseGradient>, Serializable
    First derivative computation with large number of variables.

    This class plays a similar role to DerivativeStructure, with a focus on efficiency when dealing with large number of independent variables and most computation depend only on a few of them, and when only first derivative is desired. When these conditions are met, this class should be much faster than DerivativeStructure and use less memory.


    具有大量变量的第一个衍生计算。此类对衍生体进行类似的角色,在处理大量独立变量时,大多数计算只取决于它们中的少数,并且只需要第一个衍生。满足这些条件时,此类应比衍生体更快,并使用更少的内存。

    从以下版本开始:
    3.3

    3.3

    另请参阅:
    Serialized Form

    序列化形式

    • 方法详细说明
      • createConstant
        public static SparseGradient createConstant(double value)
        Factory method creating a constant.

        工厂方法创建常量。

        参数:
        value - value of the constant

        价值 - 常数的值

        返回:
        a new instance

        一个新的实例

      • createVariable
        public static SparseGradient createVariable(int idx,
                                    double value)
        Factory method creating an independent variable.

        工厂方法创建一个独立变量。

        参数:
        idx - index of the variable

        IDX - 变量的索引

        value - value of the variable

        值 - 变量的值

        返回:
        a new instance

        一个新的实例

      • numVars
        public int numVars()
        Find the number of variables.

        找到变量的数量。

        返回:
        number of variables

        变量数量

      • getDerivative
        public double getDerivative(int index)
        Get the derivative with respect to a particular index variable.

        获取关于特定索引变量的衍生度。

        参数:
        index - index to differentiate with.

        索引 - 分辨率的索引。

        返回:
        derivative with respect to a particular index variable

        关于特定索引变量的衍生物

      • getValue
        public double getValue()
        Get the value of the function.

        获取函数的值。

        返回:
        value of the function.

        函数的值。

      • getReal
        public double getReal()
        Get the real value of the number.

        获得数字的实际值。

        指定者:
        getReal in interface  RealFieldElement<SparseGradient>

        interfile in interfialeLement

        返回:
        real value

        实际价值

      • add
        public SparseGradient add(SparseGradient a)
        Compute this + a.

        计算这个+ a。

        指定者:
        add in interface  FieldElement<SparseGradient>

        添加接口FieldElement

        参数:
        a - element to add

        a - 要添加的元素

        返回:
        a new element representing this + a

        代表这个+ a的新元素

      • addInPlace
        public void addInPlace(SparseGradient a)
        Add in place.

        This method is designed to be faster when used multiple times in a loop.

        The instance is changed here, in order to not change the instance the add(SparseGradient) method should be used.


        添加到位。当在循环中多次使用时,此方法设计得更快。此处更改了实例,以便不更改add(sparsegradient)方法的实例。

        参数:
        a - instance to add

        a - 要添加的实例

      • add
        public SparseGradient add(double c)
        '+' operator.

        '+'运营商。

        指定者:
        add in interface  RealFieldElement<SparseGradient>

        添加接口RealFieldElement

        参数:
        c - right hand side parameter of the operator

        C - 右手侧参数的操作员

        返回:
        this+a

        这个+ A.

      • subtract
        public SparseGradient subtract(SparseGradient a)
        Compute this - a.

        计算这个 - a。

        指定者:
        subtract in interface  FieldElement<SparseGradient>

        在界面中减去fieldElement

        参数:
        a - element to subtract

        a - 要减去的元素

        返回:
        a new element representing this - a

        代表这个的新元素 - a

      • subtract
        public SparseGradient subtract(double c)
        '-' operator.

        '-' 操作员。

        指定者:
        subtract in interface  RealFieldElement<SparseGradient>

        在接口中减去InterfieldElement

        参数:
        c - right hand side parameter of the operator

        C - 右手侧参数的操作员

        返回:
        this-a

        这是

      • multiply
        public SparseGradient multiply(SparseGradient a)
        Compute this × a.

        计算此×a。

        指定者:
        multiply in interface  FieldElement<SparseGradient>

        乘法in interface fieldElement

        参数:
        a - element to multiply

        一个 - 要乘法的元素

        返回:
        a new element representing this × a

        一个代表这个×a的新元素

      • multiplyInPlace
        public void multiplyInPlace(SparseGradient a)
        Multiply in place.

        This method is designed to be faster when used multiple times in a loop.

        The instance is changed here, in order to not change the instance the add(SparseGradient) method should be used.


        乘法速率。当在循环中多次使用时,此方法设计得更快。此处更改了实例,以便不更改add(sparsegradient)方法的实例。

        参数:
        a - instance to multiply

        a - 实例乘以

      • multiply
        public SparseGradient multiply(double c)
        '×' operator.

        '×'算子。

        指定者:
        multiply in interface  RealFieldElement<SparseGradient>

        在接口中乘以RealFieldElement

        参数:
        c - right hand side parameter of the operator

        C - 右手侧参数的操作员

        返回:
        this×a

        这个×A.

      • multiply
        public SparseGradient multiply(int n)
        Compute n × this. Multiplication by an integer number is defined as the following sum
        n × this = ∑ i=1 n this.

        计算n×它。通过整数数乘法定义为以下和n×这个=Σi= 1n。

        指定者:
        multiply in interface  FieldElement<SparseGradient>

        乘法in interface fieldElement

        参数:
        n - Number of times this must be added to itself.

        n - 必须添加到自身的次数。

        返回:
        A new element representing n × this.

        一个表示n×的新元素。

      • divide
        public SparseGradient divide(SparseGradient a)
        Compute this ÷ a.

        计算此÷A。

        指定者:
        divide in interface  FieldElement<SparseGradient>

        划分界面FieldElement

        参数:
        a - element to divide by

        a - 划分的元素

        返回:
        a new element representing this ÷ a

        一个代表这个的新元素

      • divide
        public SparseGradient divide(double c)
        '÷' operator.

        '÷'运营商。

        指定者:
        divide in interface  RealFieldElement<SparseGradient>

        划分在接口中真实的

        参数:
        c - right hand side parameter of the operator

        C - 右手侧参数的操作员

        返回:
        this÷a

        这个÷啊

      • negate
        public SparseGradient negate()
        Returns the additive inverse of this element.

        返回此元素的附加逆。

        指定者:
        negate in interface  FieldElement<SparseGradient>

        在界面中否定FieldElement

        返回:
        the opposite of this.

        与此相反。

      • getField
        public Field<SparseGradient> getField()
        Get the Field to which the instance belongs.

        获取实例所属的字段。

        指定者:
        getField in interface  FieldElement<SparseGradient>

        GetField在界面FieldElement

        返回:
        Field to which the instance belongs

        实例所属的字段

      • remainder
        public SparseGradient remainder(double a)
        IEEE remainder operator.

        IEEE剩余的运营商。

        指定者:
        remainder in interface  RealFieldElement<SparseGradient>

        interface中的剩余部分真实的弹性

        参数:
        a - right hand side parameter of the operator

        操作员的右手侧参数

        返回:
        this - n × a where n is the closest integer to this/a (the even integer is chosen for n if this/a is halfway between two integers)

        这个 - n×a其中n是最接近的整数/ a(如果这个/ a在两个整数之间是半途的,则选择偶数整数)

      • remainder
        public SparseGradient remainder(SparseGradient a)
        IEEE remainder operator.

        IEEE剩余的运营商。

        指定者:
        remainder in interface  RealFieldElement<SparseGradient>

        interface中的剩余部分真实的弹性

        参数:
        a - right hand side parameter of the operator

        操作员的右手侧参数

        返回:
        this - n × a where n is the closest integer to this/a (the even integer is chosen for n if this/a is halfway between two integers)

        这个 - n×a其中n是最接近的整数/ a(如果这个/ a在两个整数之间是半途的,则选择偶数整数)

      • ceil
        public SparseGradient ceil()
        Get the smallest whole number larger than instance.

        获得比实例大的最小整数。

        指定者:
        ceil in interface  RealFieldElement<SparseGradient>

        接口中的CEIL RealFieldElement

        返回:
        ceil(this)

        CEIL(这)

      • floor
        public SparseGradient floor()
        Get the largest whole number smaller than instance.

        获得比实例小的最大数量。

        指定者:
        floor in interface  RealFieldElement<SparseGradient>

        界面的地板RealFieldElement

        返回:
        floor(this)

        地板(这)

      • rint
        public SparseGradient rint()
        Get the whole number that is the nearest to the instance, or the even one if x is exactly half way between two integers.

        获取最接近实例的整个数字,或者如果x在两个整数之间正好一半。

        指定者:
        rint in interface  RealFieldElement<SparseGradient>

        接口中的rint sealfieldelement

        返回:
        a double number r such that r is an integer r - 0.5 ≤ this ≤ r + 0.5

        双数R,使R是整数R - 0.5≤这≤R+ 0.5

      • round
        public long round()
        Get the closest long to instance value.

        获得最接近的时间达实例值。

        指定者:
        round in interface  RealFieldElement<SparseGradient>

        接口中的圆形RealFieldElement

        返回:
        closest long to RealFieldElement.getReal()

        最接近realfieldelement.getReal()

      • signum
        public SparseGradient signum()
        Compute the signum of the instance. The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise

        计算实例的Signum。Signum为-1用于负数,+1为正数,否则为0

        指定者:
        signum in interface  RealFieldElement<SparseGradient>

        接口中的Signum RealFieldElement

        返回:
        -1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a

        -1.0,-0.0,+0.0,+1.0或NaN取决于a的标志

      • copySign
        public SparseGradient copySign(SparseGradient sign)
        Returns the instance with the sign of the argument. A NaN sign argument is treated as positive.

        使用参数的符号返回实例。NAN标志论证被视为积极。

        指定者:
        copySign in interface  RealFieldElement<SparseGradient>

        CopySign在界面中真正的单元格式

        参数:
        sign - the sign for the returned value

        签署 - 返回值的标志

        返回:
        the instance with the same sign as the sign argument

        具有与标志参数相同的符号的实例

      • copySign
        public SparseGradient copySign(double sign)
        Returns the instance with the sign of the argument. A NaN sign argument is treated as positive.

        使用参数的符号返回实例。NAN标志论证被视为积极。

        指定者:
        copySign in interface  RealFieldElement<SparseGradient>

        CopySign在界面中真正的单元格式

        参数:
        sign - the sign for the returned value

        签署 - 返回值的标志

        返回:
        the instance with the same sign as the sign argument

        具有与标志参数相同的符号的实例

      • scalb
        public SparseGradient scalb(int n)
        Multiply the instance by a power of 2.

        将实例乘以2的功率。

        指定者:
        scalb in interface  RealFieldElement<SparseGradient>

        inface infilefield元素

        参数:
        n - power of 2

        n - 力量为2

        返回:
        this × 2 n

        这个×2n.

      • hypot
        public SparseGradient hypot(SparseGradient y)
        Returns the hypotenuse of a triangle with sides this and y - sqrt( this 2 + y 2) avoiding intermediate overflow or underflow.
        • If either argument is infinite, then the result is positive infinity.
        • else, if either argument is NaN then the result is NaN.

        返回三角形的斜边,与侧面和Y - SQRT(This2 + Y2)避免中间溢出或下溢。如果任一参数是无限的,那么结果是正无穷大。否则,如果任一参数是NaN,那么结果是NaN。

        指定者:
        hypot in interface  RealFieldElement<SparseGradient>

        接口中的斜面是真实的

        参数:
        y - a value

        y - 一个值

        返回:
        sqrt( this 2 + y 2)

        SQRT(this2 + y2)

      • hypot
        public static SparseGradient hypot(SparseGradient x,
                           SparseGradient y)
        Returns the hypotenuse of a triangle with sides x and y - sqrt( x 2 + y 2) avoiding intermediate overflow or underflow.
        • If either argument is infinite, then the result is positive infinity.
        • else, if either argument is NaN then the result is NaN.

        返回三角形的斜边X和Y - SQRT(X2 + Y2)避免中间溢出或下溢。如果任一参数是无限的,那么结果是正无穷大。否则,如果任一参数是NaN,那么结果是NaN。

        参数:
        x - a value

        X - 一个值

        y - a value

        y - 一个值

        返回:
        sqrt( x 2 + y 2)

        sqrt(x2 + y2)

      • reciprocal
        public SparseGradient reciprocal()
        Returns the multiplicative inverse of this element.

        返回此元素的乘法倒数。

        指定者:
        reciprocal in interface  FieldElement<SparseGradient>

        互联界面中的互酷性

        指定者:
        reciprocal in interface  RealFieldElement<SparseGradient>

        接口中的互酷性RealFieldElement

        返回:
        the inverse of this.

        这方面的反向。

      • rootN
        public SparseGradient rootN(int n)
        N th root.

        第n根。

        指定者:
        rootN in interface  RealFieldElement<SparseGradient>

        接口中的rootn RealFieldElement

        参数:
        n - order of the root

        n - root的顺序

        返回:
        n th root of the instance

        实例的第n个根

      • pow
        public SparseGradient pow(double p)
        Power operation.

        电源操作。

        指定者:
        pow in interface  RealFieldElement<SparseGradient>

        接口中的POW RealFieldElement

        参数:
        p - power to apply

        P - 申请权力

        返回:
        this p

        这个

      • pow
        public SparseGradient pow(int n)
        Integer power operation.

        整数电源操作。

        指定者:
        pow in interface  RealFieldElement<SparseGradient>

        接口中的POW RealFieldElement

        参数:
        n - power to apply

        n - 申请的力量

        返回:
        this n

        本来

      • pow
        public static SparseGradient pow(double a,
                         SparseGradient x)
        Compute a x where a is a double and x a SparseGradient

        计算ax,其中a是双倍和x的sparsegradient

        参数:
        a - number to exponentiate

        a - 代表的号码

        x - power to apply

        X - 申请权力

        返回:
        a x

        斧头

      • expm1
        public SparseGradient expm1()
        Exponential minus 1.

        指数减去1。

        指定者:
        expm1 in interface  RealFieldElement<SparseGradient>

        expm1在界面中真正的单元

        返回:
        exponential minus one of the instance

        指数减去其中一个实例

      • log10
        public SparseGradient log10()
        Base 10 logarithm.

        基数10对数。

        返回:
        base 10 logarithm of the instance

        基础10实例的对数

      • log1p
        public SparseGradient log1p()
        Shifted natural logarithm.

        移动自然对数。

        指定者:
        log1p in interface  RealFieldElement<SparseGradient>

        log1p在interfile中真实的弹性

        返回:
        logarithm of one plus the instance

        一个加上实例的对数

      • atan2
        public SparseGradient atan2(SparseGradient x)
        Two arguments arc tangent operation.

        两个参数arc切线操作。

        指定者:
        atan2 in interface  RealFieldElement<SparseGradient>

        atan2在interface中真实的弹性

        参数:
        x - second argument of the arc tangent

        X - 弧形切线的第二个论点

        返回:
        atan2(this, x)

        atan2(这个,x)

      • atan2
        public static SparseGradient atan2(SparseGradient y,
                           SparseGradient x)
        Two arguments arc tangent operation.

        两个参数arc切线操作。

        参数:
        y - first argument of the arc tangent

        Y - 弧形切线的第一个论点

        x - second argument of the arc tangent

        X - 弧形切线的第二个论点

        返回:
        atan2(y, x)

        atan2(y,x)

      • cosh
        public SparseGradient cosh()
        Hyperbolic cosine operation.

        双曲线余弦操作。

        指定者:
        cosh in interface  RealFieldElement<SparseGradient>

        COSH在界面中真实的弹性

        返回:
        cosh(this)

        Cosh(这个)

      • acosh
        public SparseGradient acosh()
        Inverse hyperbolic cosine operation.

        反向双曲余弦操作。

        指定者:
        acosh in interface  RealFieldElement<SparseGradient>

        Acosh在界面中真实的弹性

        返回:
        acosh(this)

        Acosh(这个)

      • asinh
        public SparseGradient asinh()
        Inverse hyperbolic sine operation.

        反转双曲正弦操作。

        指定者:
        asinh in interface  RealFieldElement<SparseGradient>

        Asinh在界面中真实的

        返回:
        asin(this)

        asin(这)

      • atanh
        public SparseGradient atanh()
        Inverse hyperbolic tangent operation.

        反向双曲线切线操作。

        指定者:
        atanh in interface  RealFieldElement<SparseGradient>

        atanh在界面中真实的弹性

        返回:
        atanh(this)

        atanh(这)

      • toDegrees
        public SparseGradient toDegrees()
        Convert radians to degrees, with error of less than 0.5 ULP

        将弧度转换为度数,误差小于0.5 ulp

        返回:
        instance converted into degrees

        实例转换为度数

      • toRadians
        public SparseGradient toRadians()
        Convert degrees to radians, with error of less than 0.5 ULP

        将度数转换为弧度,误差小于0.5 ulp

        返回:
        instance converted into radians

        实例转换为弧度

      • taylor
        public double taylor(double... delta)
        Evaluate Taylor expansion of a sparse gradient.

        评估稀疏梯度的泰勒膨胀。

        参数:
        delta - parameters offsets (Δx, Δy, ...)

        Delta - 参数偏移(ΔX,ΔY,......)

        返回:
        value of the Taylor expansion at x + Δx, y + Δy, ...

        X +ΔX,Y +ΔY的泰勒膨胀的值,......

      • compose
        public SparseGradient compose(double f0,
                             double f1)
        Compute composition of the instance by a univariate function.

        通过单变量函数计算实例的组成。

        参数:
        f0 - value of the function at (i.e. f( getValue()))

        F0 - 函数的值(即f(getValue())))

        f1 - first derivative of the function at the current point (i.e. f'( getValue()))

        F1 - 当前点的函数的第一个导数(即f'(getValue())))

        返回:
        f(this)

        F(这)

      • linearCombination
        public SparseGradient linearCombination(double[] a,
                                       SparseGradient[] b)
        Compute a linear combination.

        计算线性组合。

        指定者:
        linearCombination in interface  RealFieldElement<SparseGradient>

        界面中的线性过程RealFieldElement

        参数:
        a - Factors.

        a - 因素。

        b - Factors.

        B - 因素。

        返回:
        Σi ai bi.

        Σiai bi。

      • linearCombination
        public SparseGradient linearCombination(SparseGradient a1,
                                       SparseGradient b1,
                                       SparseGradient a2,
                                       SparseGradient b2)
        Compute a linear combination.

        计算线性组合。

        指定者:
        linearCombination in interface  RealFieldElement<SparseGradient>

        界面中的线性过程RealFieldElement

        参数:
        a1 - first factor of the first term

        A1 - 第一项的第一因素

        b1 - second factor of the first term

        B1 - 第一个术语的第二个因素

        a2 - first factor of the second term

        A2 - 第二项的第一因素

        b2 - second factor of the second term

        B2 - 第二项的第二个因素

        返回:
        a 1×b 1 + a 2×b 2

        A1×B1 + A2×B2

        另请参阅:
        RealFieldElement.linearCombination(Object, Object, Object, Object, Object, Object), RealFieldElement.linearCombination(Object, Object, Object, Object, Object, Object, Object, Object)

        RealFieldElement.LinearCombination(对象,对象,对象,对象,对象),RealFieldElement.LinearCombination(对象,对象,对象,对象,对象,对象,对象,对象)

      • linearCombination
        public SparseGradient linearCombination(double a1,
                                       SparseGradient b1,
                                       double a2,
                                       SparseGradient b2)
        Compute a linear combination.

        计算线性组合。

        指定者:
        linearCombination in interface  RealFieldElement<SparseGradient>

        界面中的线性过程RealFieldElement

        参数:
        a1 - first factor of the first term

        A1 - 第一项的第一因素

        b1 - second factor of the first term

        B1 - 第一个术语的第二个因素

        a2 - first factor of the second term

        A2 - 第二项的第一因素

        b2 - second factor of the second term

        B2 - 第二项的第二个因素

        返回:
        a 1×b 1 + a 2×b 2

        A1×B1 + A2×B2

        另请参阅:
        RealFieldElement.linearCombination(double, Object, double, Object, double, Object), RealFieldElement.linearCombination(double, Object, double, Object, double, Object, double, Object)

        RealFieldElement.LinearCombination(双重,对象,双,对象,双,对象),RealFieldElement.LinearCombination(双重,对象,双,对象,双,对象,双,对象)

      • linearCombination
        public SparseGradient linearCombination(SparseGradient a1,
                                       SparseGradient b1,
                                       SparseGradient a2,
                                       SparseGradient b2,
                                       SparseGradient a3,
                                       SparseGradient b3)
        Compute a linear combination.

        计算线性组合。

        指定者:
        linearCombination in interface  RealFieldElement<SparseGradient>

        界面中的线性过程RealFieldElement

        参数:
        a1 - first factor of the first term

        A1 - 第一项的第一因素

        b1 - second factor of the first term

        B1 - 第一个术语的第二个因素

        a2 - first factor of the second term

        A2 - 第二项的第一因素

        b2 - second factor of the second term

        B2 - 第二项的第二个因素

        a3 - first factor of the third term

        A3 - 第三个术语的第一因素

        b3 - second factor of the third term

        B3 - 第三个术语的第二个因素

        返回:
        a 1×b 1 + a 2×b 2 + a 3×b 3

        A1×B1 + A2×B2 + A3×B3

        另请参阅:
        RealFieldElement.linearCombination(Object, Object, Object, Object), RealFieldElement.linearCombination(Object, Object, Object, Object, Object, Object, Object, Object)

        RealFieldElement.LinearCombination(对象,对象,对象,对象),RealFieldElement.LinearCombination(对象,对象,对象,对象,对象,对象,对象,对象)

      • linearCombination
        public SparseGradient linearCombination(double a1,
                                       SparseGradient b1,
                                       double a2,
                                       SparseGradient b2,
                                       double a3,
                                       SparseGradient b3)
        Compute a linear combination.

        计算线性组合。

        指定者:
        linearCombination in interface  RealFieldElement<SparseGradient>

        界面中的线性过程RealFieldElement

        参数:
        a1 - first factor of the first term

        A1 - 第一项的第一因素

        b1 - second factor of the first term

        B1 - 第一个术语的第二个因素

        a2 - first factor of the second term

        A2 - 第二项的第一因素

        b2 - second factor of the second term

        B2 - 第二项的第二个因素

        a3 - first factor of the third term

        A3 - 第三个术语的第一因素

        b3 - second factor of the third term

        B3 - 第三个术语的第二个因素

        返回:
        a 1×b 1 + a 2×b 2 + a 3×b 3

        A1×B1 + A2×B2 + A3×B3

        另请参阅:
        RealFieldElement.linearCombination(double, Object, double, Object), RealFieldElement.linearCombination(double, Object, double, Object, double, Object, double, Object)

        RealFieldElement.Linearcombination(Double,Object,Double,Object),RealFieldElement.Linearcombination(双重,对象,双,对象,双重,对象,双,对象)

      • linearCombination
        public SparseGradient linearCombination(SparseGradient a1,
                                       SparseGradient b1,
                                       SparseGradient a2,
                                       SparseGradient b2,
                                       SparseGradient a3,
                                       SparseGradient b3,
                                       SparseGradient a4,
                                       SparseGradient b4)
        Compute a linear combination.

        计算线性组合。

        指定者:
        linearCombination in interface  RealFieldElement<SparseGradient>

        界面中的线性过程RealFieldElement

        参数:
        a1 - first factor of the first term

        A1 - 第一项的第一因素

        b1 - second factor of the first term

        B1 - 第一个术语的第二个因素

        a2 - first factor of the second term

        A2 - 第二项的第一因素

        b2 - second factor of the second term

        B2 - 第二项的第二个因素

        a3 - first factor of the third term

        A3 - 第三个术语的第一因素

        b3 - second factor of the third term

        B3 - 第三个术语的第二个因素

        a4 - first factor of the third term

        A4 - 第三个术语的第一因素

        b4 - second factor of the third term

        B4 - 第三个任期的第二个因素

        返回:
        a 1×b 1 + a 2×b 2 + a 3×b 3 + a 4×b 4

        A1×B1 + A2×B2 + A3×B3 + A4×B4

        另请参阅:
        RealFieldElement.linearCombination(Object, Object, Object, Object), RealFieldElement.linearCombination(Object, Object, Object, Object, Object, Object)

        RealFieldElement.LinearCombination(对象,对象,对象,对象),RealFieldElement.LinearCombination(对象,对象,对象,对象,对象,对象)

      • linearCombination
        public SparseGradient linearCombination(double a1,
                                       SparseGradient b1,
                                       double a2,
                                       SparseGradient b2,
                                       double a3,
                                       SparseGradient b3,
                                       double a4,
                                       SparseGradient b4)
        Compute a linear combination.

        计算线性组合。

        指定者:
        linearCombination in interface  RealFieldElement<SparseGradient>

        界面中的线性过程RealFieldElement

        参数:
        a1 - first factor of the first term

        A1 - 第一项的第一因素

        b1 - second factor of the first term

        B1 - 第一个术语的第二个因素

        a2 - first factor of the second term

        A2 - 第二项的第一因素

        b2 - second factor of the second term

        B2 - 第二项的第二个因素

        a3 - first factor of the third term

        A3 - 第三个术语的第一因素

        b3 - second factor of the third term

        B3 - 第三个术语的第二个因素

        a4 - first factor of the third term

        A4 - 第三个术语的第一因素

        b4 - second factor of the third term

        B4 - 第三个任期的第二个因素

        返回:
        a 1×b 1 + a 2×b 2 + a 3×b 3 + a 4×b 4

        A1×B1 + A2×B2 + A3×B3 + A4×B4

        另请参阅:
        RealFieldElement.linearCombination(double, Object, double, Object), RealFieldElement.linearCombination(double, Object, double, Object, double, Object)

        RealFieldElement.linearcombination(双重,对象,双,对象),RealFieldElement.Linearcombination(双重,对象,双,对象,双,对象)

      • equals
        public boolean equals(Object other)
        Test for the equality of two sparse gradients.

        Sparse gradients are considered equal if they have the same value and the same derivatives.


        测试两个稀疏梯度的平等。如果它们具有相同的值和相同的衍生物,则认为稀疏梯度是相等的。

        重写:
        equals in class  Object

        等于类对象

        参数:
        other - Object to test for equality to this

        其他 - 用于测试平等的对象

        返回:
        true if two sparse gradients are equal

        如果两个稀疏梯度相等,则为真

      • hashCode
        public int hashCode()
        Get a hashCode for the derivative structure.

        获取衍生结构结构的哈希码。

        重写:
        hashCode in class  Object

        类对象中的哈希码

        返回:
        a hash code value for this object

        此对象的哈希码值

        从以下版本开始:
        3.2

        3.2

2.2. SparseGradient 源码赏析

/*
 * 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.math3.analysis.differentiation;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.math3.Field;
import org.apache.commons.math3.FieldElement;
import org.apache.commons.math3.RealFieldElement;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.MathArrays;
import org.apache.commons.math3.util.MathUtils;
import org.apache.commons.math3.util.Precision;

/**
 * First derivative computation with large number of variables.
 * <p>
 * This class plays a similar role to {@link DerivativeStructure}, with
 * a focus on efficiency when dealing with large number of independent variables
 * and most computation depend only on a few of them, and when only first derivative
 * is desired. When these conditions are met, this class should be much faster than
 * {@link DerivativeStructure} and use less memory.
 * </p>
 *
 * @since 3.3
 */
public class SparseGradient implements RealFieldElement<SparseGradient>, Serializable {

    /** Serializable UID. */
    private static final long serialVersionUID = 20131025L;

    /** Value of the calculation. */
    private double value;

    /** Stored derivative, each key representing a different independent variable. */
    private final Map<Integer, Double> derivatives;

    /** Internal constructor.
     * @param value value of the function
     * @param derivatives derivatives map, a deep copy will be performed,
     * so the map given here will remain safe from changes in the new instance,
     * may be null to create an empty derivatives map, i.e. a constant value
     */
    private SparseGradient(final double value, final Map<Integer, Double> derivatives) {
        this.value = value;
        this.derivatives = new HashMap<Integer, Double>();
        if (derivatives != null) {
            this.derivatives.putAll(derivatives);
        }
    }

    /** Internal constructor.
     * @param value value of the function
     * @param scale scaling factor to apply to all derivatives
     * @param derivatives derivatives map, a deep copy will be performed,
     * so the map given here will remain safe from changes in the new instance,
     * may be null to create an empty derivatives map, i.e. a constant value
     */
    private SparseGradient(final double value, final double scale,
                             final Map<Integer, Double> derivatives) {
        this.value = value;
        this.derivatives = new HashMap<Integer, Double>();
        if (derivatives != null) {
            for (final Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
                this.derivatives.put(entry.getKey(), scale * entry.getValue());
            }
        }
    }

    /** Factory method creating a constant.
     * @param value value of the constant
     * @return a new instance
     */
    public static SparseGradient createConstant(final double value) {
        return new SparseGradient(value, Collections.<Integer, Double> emptyMap());
    }

    /** Factory method creating an independent variable.
     * @param idx index of the variable
     * @param value value of the variable
     * @return a new instance
     */
    public static SparseGradient createVariable(final int idx, final double value) {
        return new SparseGradient(value, Collections.singletonMap(idx, 1.0));
    }

    /**
     * Find the number of variables.
     * @return number of variables
     */
    public int numVars() {
        return derivatives.size();
    }

    /**
     * Get the derivative with respect to a particular index variable.
     *
     * @param index index to differentiate with.
     * @return derivative with respect to a particular index variable
     */
    public double getDerivative(final int index) {
        final Double out = derivatives.get(index);
        return (out == null) ? 0.0 : out;
    }

    /**
     * Get the value of the function.
     * @return value of the function.
     */
    public double getValue() {
        return value;
    }

    /** {@inheritDoc} */
    public double getReal() {
        return value;
    }

    /** {@inheritDoc} */
    public SparseGradient add(final SparseGradient a) {
        final SparseGradient out = new SparseGradient(value + a.value, derivatives);
        for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
            final int id = entry.getKey();
            final Double old = out.derivatives.get(id);
            if (old == null) {
                out.derivatives.put(id, entry.getValue());
            } else {
                out.derivatives.put(id, old + entry.getValue());
            }
        }

        return out;
    }

    /**
     * Add in place.
     * <p>
     * This method is designed to be faster when used multiple times in a loop.
     * </p>
     * <p>
     * The instance is changed here, in order to not change the
     * instance the {@link #add(SparseGradient)} method should
     * be used.
     * </p>
     * @param a instance to add
     */
    public void addInPlace(final SparseGradient a) {
        value += a.value;
        for (final Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
            final int id = entry.getKey();
            final Double old = derivatives.get(id);
            if (old == null) {
                derivatives.put(id, entry.getValue());
            } else {
                derivatives.put(id, old + entry.getValue());
            }
        }
    }

    /** {@inheritDoc} */
    public SparseGradient add(final double c) {
        final SparseGradient out = new SparseGradient(value + c, derivatives);
        return out;
    }

    /** {@inheritDoc} */
    public SparseGradient subtract(final SparseGradient a) {
        final SparseGradient out = new SparseGradient(value - a.value, derivatives);
        for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
            final int id = entry.getKey();
            final Double old = out.derivatives.get(id);
            if (old == null) {
                out.derivatives.put(id, -entry.getValue());
            } else {
                out.derivatives.put(id, old - entry.getValue());
            }
        }
        return out;
    }

    /** {@inheritDoc} */
    public SparseGradient subtract(double c) {
        return new SparseGradient(value - c, derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient multiply(final SparseGradient a) {
        final SparseGradient out =
            new SparseGradient(value * a.value, Collections.<Integer, Double> emptyMap());

        // Derivatives.
        for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
            out.derivatives.put(entry.getKey(), a.value * entry.getValue());
        }
        for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
            final int id = entry.getKey();
            final Double old = out.derivatives.get(id);
            if (old == null) {
                out.derivatives.put(id, value * entry.getValue());
            } else {
                out.derivatives.put(id, old + value * entry.getValue());
            }
        }
        return out;
    }

    /**
     * Multiply in place.
     * <p>
     * This method is designed to be faster when used multiple times in a loop.
     * </p>
     * <p>
     * The instance is changed here, in order to not change the
     * instance the {@link #add(SparseGradient)} method should
     * be used.
     * </p>
     * @param a instance to multiply
     */
    public void multiplyInPlace(final SparseGradient a) {
        // Derivatives.
        for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
            derivatives.put(entry.getKey(), a.value * entry.getValue());
        }
        for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
            final int id = entry.getKey();
            final Double old = derivatives.get(id);
            if (old == null) {
                derivatives.put(id, value * entry.getValue());
            } else {
                derivatives.put(id, old + value * entry.getValue());
            }
        }
        value *= a.value;
    }

    /** {@inheritDoc} */
    public SparseGradient multiply(final double c) {
        return new SparseGradient(value * c, c, derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient multiply(final int n) {
        return new SparseGradient(value * n, n, derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient divide(final SparseGradient a) {
        final SparseGradient out = new SparseGradient(value / a.value, Collections.<Integer, Double> emptyMap());

        // Derivatives.
        for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
            out.derivatives.put(entry.getKey(), entry.getValue() / a.value);
        }
        for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
            final int id = entry.getKey();
            final Double old = out.derivatives.get(id);
            if (old == null) {
                out.derivatives.put(id, -out.value / a.value * entry.getValue());
            } else {
                out.derivatives.put(id, old - out.value / a.value * entry.getValue());
            }
        }
        return out;
    }

    /** {@inheritDoc} */
    public SparseGradient divide(final double c) {
        return new SparseGradient(value / c, 1.0 / c, derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient negate() {
        return new SparseGradient(-value, -1.0, derivatives);
    }

    /** {@inheritDoc} */
    public Field<SparseGradient> getField() {
        return new Field<SparseGradient>() {

            /** {@inheritDoc} */
            public SparseGradient getZero() {
                return createConstant(0);
            }

            /** {@inheritDoc} */
            public SparseGradient getOne() {
                return createConstant(1);
            }

            /** {@inheritDoc} */
            public Class<? extends FieldElement<SparseGradient>> getRuntimeClass() {
                return SparseGradient.class;
            }

        };
    }

    /** {@inheritDoc} */
    public SparseGradient remainder(final double a) {
        return new SparseGradient(FastMath.IEEEremainder(value, a), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient remainder(final SparseGradient a) {

        // compute k such that lhs % rhs = lhs - k rhs
        final double rem = FastMath.IEEEremainder(value, a.value);
        final double k   = FastMath.rint((value - rem) / a.value);

        return subtract(a.multiply(k));

    }

    /** {@inheritDoc} */
    public SparseGradient abs() {
        if (Double.doubleToLongBits(value) < 0) {
            // we use the bits representation to also handle -0.0
            return negate();
        } else {
            return this;
        }
    }

    /** {@inheritDoc} */
    public SparseGradient ceil() {
        return createConstant(FastMath.ceil(value));
    }

    /** {@inheritDoc} */
    public SparseGradient floor() {
        return createConstant(FastMath.floor(value));
    }

    /** {@inheritDoc} */
    public SparseGradient rint() {
        return createConstant(FastMath.rint(value));
    }

    /** {@inheritDoc} */
    public long round() {
        return FastMath.round(value);
    }

    /** {@inheritDoc} */
    public SparseGradient signum() {
        return createConstant(FastMath.signum(value));
    }

    /** {@inheritDoc} */
    public SparseGradient copySign(final SparseGradient sign) {
        final long m = Double.doubleToLongBits(value);
        final long s = Double.doubleToLongBits(sign.value);
        if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) { // Sign is currently OK
            return this;
        }
        return negate(); // flip sign
    }

    /** {@inheritDoc} */
    public SparseGradient copySign(final double sign) {
        final long m = Double.doubleToLongBits(value);
        final long s = Double.doubleToLongBits(sign);
        if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) { // Sign is currently OK
            return this;
        }
        return negate(); // flip sign
    }

    /** {@inheritDoc} */
    public SparseGradient scalb(final int n) {
        final SparseGradient out = new SparseGradient(FastMath.scalb(value, n), Collections.<Integer, Double> emptyMap());
        for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
            out.derivatives.put(entry.getKey(), FastMath.scalb(entry.getValue(), n));
        }
        return out;
    }

    /** {@inheritDoc} */
    public SparseGradient hypot(final SparseGradient y) {
        if (Double.isInfinite(value) || Double.isInfinite(y.value)) {
            return createConstant(Double.POSITIVE_INFINITY);
        } else if (Double.isNaN(value) || Double.isNaN(y.value)) {
            return createConstant(Double.NaN);
        } else {

            final int expX = FastMath.getExponent(value);
            final int expY = FastMath.getExponent(y.value);
            if (expX > expY + 27) {
                // y is negligible with respect to x
                return abs();
            } else if (expY > expX + 27) {
                // x is negligible with respect to y
                return y.abs();
            } else {

                // find an intermediate scale to avoid both overflow and underflow
                final int middleExp = (expX + expY) / 2;

                // scale parameters without losing precision
                final SparseGradient scaledX = scalb(-middleExp);
                final SparseGradient scaledY = y.scalb(-middleExp);

                // compute scaled hypotenuse
                final SparseGradient scaledH =
                        scaledX.multiply(scaledX).add(scaledY.multiply(scaledY)).sqrt();

                // remove scaling
                return scaledH.scalb(middleExp);

            }

        }
    }

    /**
     * Returns the hypotenuse of a triangle with sides {@code x} and {@code y}
     * - sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
     * avoiding intermediate overflow or underflow.
     *
     * <ul>
     * <li> If either argument is infinite, then the result is positive infinity.</li>
     * <li> else, if either argument is NaN then the result is NaN.</li>
     * </ul>
     *
     * @param x a value
     * @param y a value
     * @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
     */
    public static SparseGradient hypot(final SparseGradient x, final SparseGradient y) {
        return x.hypot(y);
    }

    /** {@inheritDoc} */
    public SparseGradient reciprocal() {
        return new SparseGradient(1.0 / value, -1.0 / (value * value), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient sqrt() {
        final double sqrt = FastMath.sqrt(value);
        return new SparseGradient(sqrt, 0.5 / sqrt, derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient cbrt() {
        final double cbrt = FastMath.cbrt(value);
        return new SparseGradient(cbrt, 1.0 / (3 * cbrt * cbrt), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient rootN(final int n) {
        if (n == 2) {
            return sqrt();
        } else if (n == 3) {
            return cbrt();
        } else {
            final double root = FastMath.pow(value, 1.0 / n);
            return new SparseGradient(root, 1.0 / (n * FastMath.pow(root, n - 1)), derivatives);
        }
    }

    /** {@inheritDoc} */
    public SparseGradient pow(final double p) {
        return new SparseGradient(FastMath.pow(value,  p), p * FastMath.pow(value,  p - 1), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient pow(final int n) {
        if (n == 0) {
            return getField().getOne();
        } else {
            final double valueNm1 = FastMath.pow(value,  n - 1);
            return new SparseGradient(value * valueNm1, n * valueNm1, derivatives);
        }
    }

    /** {@inheritDoc} */
    public SparseGradient pow(final SparseGradient e) {
        return log().multiply(e).exp();
    }

    /** Compute a<sup>x</sup> where a is a double and x a {@link SparseGradient}
     * @param a number to exponentiate
     * @param x power to apply
     * @return a<sup>x</sup>
     */
    public static SparseGradient pow(final double a, final SparseGradient x) {
        if (a == 0) {
            if (x.value == 0) {
                return x.compose(1.0, Double.NEGATIVE_INFINITY);
            } else if (x.value < 0) {
                return x.compose(Double.NaN, Double.NaN);
            } else {
                return x.getField().getZero();
            }
        } else {
            final double ax = FastMath.pow(a, x.value);
            return new SparseGradient(ax, ax * FastMath.log(a), x.derivatives);
        }
    }

    /** {@inheritDoc} */
    public SparseGradient exp() {
        final double e = FastMath.exp(value);
        return new SparseGradient(e, e, derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient expm1() {
        return new SparseGradient(FastMath.expm1(value), FastMath.exp(value), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient log() {
        return new SparseGradient(FastMath.log(value), 1.0 / value, derivatives);
    }

    /** Base 10 logarithm.
     * @return base 10 logarithm of the instance
     */
    public SparseGradient log10() {
        return new SparseGradient(FastMath.log10(value), 1.0 / (FastMath.log(10.0) * value), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient log1p() {
        return new SparseGradient(FastMath.log1p(value), 1.0 / (1.0 + value), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient cos() {
        return new SparseGradient(FastMath.cos(value), -FastMath.sin(value), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient sin() {
        return new SparseGradient(FastMath.sin(value), FastMath.cos(value), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient tan() {
        final double t = FastMath.tan(value);
        return new SparseGradient(t, 1 + t * t, derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient acos() {
        return new SparseGradient(FastMath.acos(value), -1.0 / FastMath.sqrt(1 - value * value), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient asin() {
        return new SparseGradient(FastMath.asin(value), 1.0 / FastMath.sqrt(1 - value * value), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient atan() {
        return new SparseGradient(FastMath.atan(value), 1.0 / (1 + value * value), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient atan2(final SparseGradient x) {

        // compute r = sqrt(x^2+y^2)
        final SparseGradient r = multiply(this).add(x.multiply(x)).sqrt();

        final SparseGradient a;
        if (x.value >= 0) {

            // compute atan2(y, x) = 2 atan(y / (r + x))
            a = divide(r.add(x)).atan().multiply(2);

        } else {

            // compute atan2(y, x) = +/- pi - 2 atan(y / (r - x))
            final SparseGradient tmp = divide(r.subtract(x)).atan().multiply(-2);
            a = tmp.add(tmp.value <= 0 ? -FastMath.PI : FastMath.PI);

        }

        // fix value to take special cases (+0/+0, +0/-0, -0/+0, -0/-0, +/-infinity) correctly
        a.value = FastMath.atan2(value, x.value);

        return a;

    }

    /** Two arguments arc tangent operation.
     * @param y first argument of the arc tangent
     * @param x second argument of the arc tangent
     * @return atan2(y, x)
     */
    public static SparseGradient atan2(final SparseGradient y, final SparseGradient x) {
        return y.atan2(x);
    }

    /** {@inheritDoc} */
    public SparseGradient cosh() {
        return new SparseGradient(FastMath.cosh(value), FastMath.sinh(value), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient sinh() {
        return new SparseGradient(FastMath.sinh(value), FastMath.cosh(value), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient tanh() {
        final double t = FastMath.tanh(value);
        return new SparseGradient(t, 1 - t * t, derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient acosh() {
        return new SparseGradient(FastMath.acosh(value), 1.0 / FastMath.sqrt(value * value - 1.0), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient asinh() {
        return new SparseGradient(FastMath.asinh(value), 1.0 / FastMath.sqrt(value * value + 1.0), derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient atanh() {
        return new SparseGradient(FastMath.atanh(value), 1.0 / (1.0 - value * value), derivatives);
    }

    /** Convert radians to degrees, with error of less than 0.5 ULP
     *  @return instance converted into degrees
     */
    public SparseGradient toDegrees() {
        return new SparseGradient(FastMath.toDegrees(value), FastMath.toDegrees(1.0), derivatives);
    }

    /** Convert degrees to radians, with error of less than 0.5 ULP
     *  @return instance converted into radians
     */
    public SparseGradient toRadians() {
        return new SparseGradient(FastMath.toRadians(value), FastMath.toRadians(1.0), derivatives);
    }

    /** Evaluate Taylor expansion of a sparse gradient.
     * @param delta parameters offsets (&Delta;x, &Delta;y, ...)
     * @return value of the Taylor expansion at x + &Delta;x, y + &Delta;y, ...
     */
    public double taylor(final double ... delta) {
        double y = value;
        for (int i = 0; i < delta.length; ++i) {
            y += delta[i] * getDerivative(i);
        }
        return y;
    }

    /** Compute composition of the instance by a univariate function.
     * @param f0 value of the function at (i.e. f({@link #getValue()}))
     * @param f1 first derivative of the function at
     * the current point (i.e. f'({@link #getValue()}))
     * @return f(this)
    */
    public SparseGradient compose(final double f0, final double f1) {
        return new SparseGradient(f0, f1, derivatives);
    }

    /** {@inheritDoc} */
    public SparseGradient linearCombination(final SparseGradient[] a,
                                              final SparseGradient[] b)
        throws DimensionMismatchException {

        // compute a simple value, with all partial derivatives
        SparseGradient out = a[0].getField().getZero();
        for (int i = 0; i < a.length; ++i) {
            out = out.add(a[i].multiply(b[i]));
        }

        // recompute an accurate value, taking care of cancellations
        final double[] aDouble = new double[a.length];
        for (int i = 0; i < a.length; ++i) {
            aDouble[i] = a[i].getValue();
        }
        final double[] bDouble = new double[b.length];
        for (int i = 0; i < b.length; ++i) {
            bDouble[i] = b[i].getValue();
        }
        out.value = MathArrays.linearCombination(aDouble, bDouble);

        return out;

    }

    /** {@inheritDoc} */
    public SparseGradient linearCombination(final double[] a, final SparseGradient[] b) {

        // compute a simple value, with all partial derivatives
        SparseGradient out = b[0].getField().getZero();
        for (int i = 0; i < a.length; ++i) {
            out = out.add(b[i].multiply(a[i]));
        }

        // recompute an accurate value, taking care of cancellations
        final double[] bDouble = new double[b.length];
        for (int i = 0; i < b.length; ++i) {
            bDouble[i] = b[i].getValue();
        }
        out.value = MathArrays.linearCombination(a, bDouble);

        return out;

    }

    /** {@inheritDoc} */
    public SparseGradient linearCombination(final SparseGradient a1, final SparseGradient b1,
                                              final SparseGradient a2, final SparseGradient b2) {

        // compute a simple value, with all partial derivatives
        SparseGradient out = a1.multiply(b1).add(a2.multiply(b2));

        // recompute an accurate value, taking care of cancellations
        out.value = MathArrays.linearCombination(a1.value, b1.value, a2.value, b2.value);

        return out;

    }

    /** {@inheritDoc} */
    public SparseGradient linearCombination(final double a1, final SparseGradient b1,
                                              final double a2, final SparseGradient b2) {

        // compute a simple value, with all partial derivatives
        SparseGradient out = b1.multiply(a1).add(b2.multiply(a2));

        // recompute an accurate value, taking care of cancellations
        out.value = MathArrays.linearCombination(a1, b1.value, a2, b2.value);

        return out;

    }

    /** {@inheritDoc} */
    public SparseGradient linearCombination(final SparseGradient a1, final SparseGradient b1,
                                              final SparseGradient a2, final SparseGradient b2,
                                              final SparseGradient a3, final SparseGradient b3) {

        // compute a simple value, with all partial derivatives
        SparseGradient out = a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3));

        // recompute an accurate value, taking care of cancellations
        out.value = MathArrays.linearCombination(a1.value, b1.value,
                                                 a2.value, b2.value,
                                                 a3.value, b3.value);

        return out;

    }

    /** {@inheritDoc} */
    public SparseGradient linearCombination(final double a1, final SparseGradient b1,
                                              final double a2, final SparseGradient b2,
                                              final double a3, final SparseGradient b3) {

        // compute a simple value, with all partial derivatives
        SparseGradient out = b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3));

        // recompute an accurate value, taking care of cancellations
        out.value = MathArrays.linearCombination(a1, b1.value,
                                                 a2, b2.value,
                                                 a3, b3.value);

        return out;

    }

    /** {@inheritDoc} */
    public SparseGradient linearCombination(final SparseGradient a1, final SparseGradient b1,
                                              final SparseGradient a2, final SparseGradient b2,
                                              final SparseGradient a3, final SparseGradient b3,
                                              final SparseGradient a4, final SparseGradient b4) {

        // compute a simple value, with all partial derivatives
        SparseGradient out = a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3)).add(a4.multiply(b4));

        // recompute an accurate value, taking care of cancellations
        out.value = MathArrays.linearCombination(a1.value, b1.value,
                                                 a2.value, b2.value,
                                                 a3.value, b3.value,
                                                 a4.value, b4.value);

        return out;

    }

    /** {@inheritDoc} */
    public SparseGradient linearCombination(final double a1, final SparseGradient b1,
                                              final double a2, final SparseGradient b2,
                                              final double a3, final SparseGradient b3,
                                              final double a4, final SparseGradient b4) {

        // compute a simple value, with all partial derivatives
        SparseGradient out = b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3)).add(b4.multiply(a4));

        // recompute an accurate value, taking care of cancellations
        out.value = MathArrays.linearCombination(a1, b1.value,
                                                 a2, b2.value,
                                                 a3, b3.value,
                                                 a4, b4.value);

        return out;

    }

    /**
     * Test for the equality of two sparse gradients.
     * <p>
     * Sparse gradients are considered equal if they have the same value
     * and the same derivatives.
     * </p>
     * @param other Object to test for equality to this
     * @return true if two sparse gradients are equal
     */
    @Override
    public boolean equals(Object other) {

        if (this == other) {
            return true;
        }

        if (other instanceof SparseGradient) {
            final SparseGradient rhs = (SparseGradient)other;
            if (!Precision.equals(value, rhs.value, 1)) {
                return false;
            }
            if (derivatives.size() != rhs.derivatives.size()) {
                return false;
            }
            for (final Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
                if (!rhs.derivatives.containsKey(entry.getKey())) {
                    return false;
                }
                if (!Precision.equals(entry.getValue(), rhs.derivatives.get(entry.getKey()), 1)) {
                    return false;
                }
            }
            return true;
        }

        return false;

    }

    /**
     * Get a hashCode for the derivative structure.
     * @return a hash code value for this object
     * @since 3.2
     */
    @Override
    public int hashCode() {
        return 743 + 809 * MathUtils.hash(value) + 167 * derivatives.hashCode();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒水馨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值