Ejemplo n.º 1
0
 /**
  * Copies source to dest.
  *
  * <p>Neither source nor dest can be null.
  *
  * @param source Mean to copy
  * @param dest Mean to copy to
  * @throws NullArgumentException if either source or dest is null
  */
 public static void copy(Mean source, Mean dest) throws NullArgumentException {
   MathUtils.checkNotNull(source);
   MathUtils.checkNotNull(dest);
   dest.setData(source.getDataRef());
   dest.incMoment = source.incMoment;
   dest.moment = source.moment.copy();
 }
Ejemplo n.º 2
0
 /**
  * Copies source to dest.
  *
  * <p>Neither source nor dest can be null.
  *
  * @param source Product to copy
  * @param dest Product to copy to
  * @throws NullArgumentException if either source or dest is null
  */
 public static void copy(Product source, Product dest) throws NullArgumentException {
   MathUtils.checkNotNull(source);
   MathUtils.checkNotNull(dest);
   dest.setData(source.getDataRef());
   dest.n = source.n;
   dest.value = source.value;
 }
Ejemplo n.º 3
0
 /* 116:    */
 /* 117:    */ public static void copy(Kurtosis source, Kurtosis dest)
     /* 118:    */ throws NullArgumentException
       /* 119:    */ {
   /* 120:218 */ MathUtils.checkNotNull(source);
   /* 121:219 */ MathUtils.checkNotNull(dest);
   /* 122:220 */ dest.setData(source.getDataRef());
   /* 123:221 */ dest.moment = source.moment.copy();
   /* 124:222 */ dest.incMoment = source.incMoment;
   /* 125:    */ }
Ejemplo n.º 4
0
 /**
  * Copies source to dest.
  *
  * <p>Neither source nor dest can be null.
  *
  * @param source Percentile to copy
  * @param dest Percentile to copy to
  * @throws NullArgumentException if either source or dest is null
  */
 public static void copy(Percentile source, Percentile dest) throws NullArgumentException {
   MathUtils.checkNotNull(source);
   MathUtils.checkNotNull(dest);
   dest.setData(source.getDataRef());
   if (source.cachedPivots != null) {
     System.arraycopy(source.cachedPivots, 0, dest.cachedPivots, 0, source.cachedPivots.length);
   }
   dest.quantile = source.quantile;
 }
  /** {@inheritDoc} */
  public void setSubMatrix(final double[][] subMatrix, final int row, final int column)
      throws NoDataException, OutOfRangeException, DimensionMismatchException,
          NullArgumentException {
    MathUtils.checkNotNull(subMatrix);
    final int nRows = subMatrix.length;
    if (nRows == 0) {
      throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW);
    }

    final int nCols = subMatrix[0].length;
    if (nCols == 0) {
      throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
    }

    for (int r = 1; r < nRows; ++r) {
      if (subMatrix[r].length != nCols) {
        throw new DimensionMismatchException(nCols, subMatrix[r].length);
      }
    }

    MatrixUtils.checkRowIndex(this, row);
    MatrixUtils.checkColumnIndex(this, column);
    MatrixUtils.checkRowIndex(this, nRows + row - 1);
    MatrixUtils.checkColumnIndex(this, nCols + column - 1);

    for (int i = 0; i < nRows; ++i) {
      for (int j = 0; j < nCols; ++j) {
        setEntry(row + i, column + j, subMatrix[i][j]);
      }
    }
  }
Ejemplo n.º 6
0
  /** {@inheritDoc} */
  @Override
  public void setSubMatrix(final double[][] subMatrix, final int row, final int column)
      throws NoDataException, OutOfRangeException, DimensionMismatchException,
          NullArgumentException {
    if (data == null) {
      if (row > 0) {
        throw new MathIllegalStateException(LocalizedFormats.FIRST_ROWS_NOT_INITIALIZED_YET, row);
      }
      if (column > 0) {
        throw new MathIllegalStateException(
            LocalizedFormats.FIRST_COLUMNS_NOT_INITIALIZED_YET, column);
      }
      MathUtils.checkNotNull(subMatrix);
      final int nRows = subMatrix.length;
      if (nRows == 0) {
        throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW);
      }

      final int nCols = subMatrix[0].length;
      if (nCols == 0) {
        throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
      }
      data = new double[subMatrix.length][nCols];
      for (int i = 0; i < data.length; ++i) {
        if (subMatrix[i].length != nCols) {
          throw new DimensionMismatchException(subMatrix[i].length, nCols);
        }
        System.arraycopy(subMatrix[i], 0, data[i + row], column, nCols);
      }
    } else {
      super.setSubMatrix(subMatrix, row, column);
    }
  }
 /**
  * Performs all dimension checks on the parameters of {@link #solve(RealLinearOperator,
  * RealVector, RealVector) solve} and {@link #solveInPlace(RealLinearOperator, RealVector,
  * RealVector) solveInPlace}, and throws an exception if one of the checks fails.
  *
  * @param a the linear operator A of the system
  * @param b the right-hand side vector
  * @param x0 the initial guess of the solution
  * @throws NullArgumentException if one of the parameters is {@code null}
  * @throws NonSquareOperatorException if {@code a} is not square
  * @throws DimensionMismatchException if {@code b} or {@code x0} have dimensions inconsistent with
  *     {@code a}
  */
 protected static void checkParameters(
     final RealLinearOperator a, final RealVector b, final RealVector x0)
     throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException {
   MathUtils.checkNotNull(a);
   MathUtils.checkNotNull(b);
   MathUtils.checkNotNull(x0);
   if (a.getRowDimension() != a.getColumnDimension()) {
     throw new NonSquareOperatorException(a.getRowDimension(), a.getColumnDimension());
   }
   if (b.getDimension() != a.getRowDimension()) {
     throw new DimensionMismatchException(b.getDimension(), a.getRowDimension());
   }
   if (x0.getDimension() != a.getColumnDimension()) {
     throw new DimensionMismatchException(x0.getDimension(), a.getColumnDimension());
   }
 }
Ejemplo n.º 8
0
  /**
   * Returns a {@code Complex} whose value is {@code (this / divisor)}. 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. <br>
   * {@code Infinite} and {@code NaN} values are handled according to the following rules, applied
   * in the order presented:
   *
   * <ul>
   *   <li>If either {@code this} or {@code divisor} has a {@code NaN} value in either part, {@link
   *       #NaN} is returned.
   *   <li>If {@code divisor} equals {@link #ZERO}, {@link #NaN} is returned.
   *   <li>If {@code this} and {@code divisor} are both infinite, {@link #NaN} is returned.
   *   <li>If {@code this} is finite (i.e., has no {@code Infinite} or {@code NaN} parts) and {@code
   *       divisor} is infinite (one or both parts infinite), {@link #ZERO} is returned.
   *   <li>If {@code this} is infinite and {@code divisor} is finite, {@code NaN} values are
   *       returned in the parts of the result if the {@link java.lang.Double} rules applied to the
   *       definitional formula force {@code NaN} results.
   * </ul>
   *
   * @param divisor Value by which this {@code Complex} is to be divided.
   * @return {@code this / divisor}.
   * @throws NullArgumentException if {@code divisor} is {@code null}.
   */
  public Complex divide(Complex divisor) throws NullArgumentException {
    MathUtils.checkNotNull(divisor);
    if (isNaN || divisor.isNaN) {
      return NaN;
    }

    final double c = divisor.getReal();
    final double d = divisor.getImaginary();
    if (c == 0.0 && d == 0.0) {
      return NaN;
    }

    if (divisor.isInfinite() && !isInfinite()) {
      return ZERO;
    }

    if (FastMath.abs(c) < FastMath.abs(d)) {
      double q = c / d;
      double denominator = c * q + d;
      return createComplex(
          (real * q + imaginary) / denominator, (imaginary * q - real) / denominator);
    } else {
      double q = d / c;
      double denominator = d * q + c;
      return createComplex(
          (imaginary * q + real) / denominator, (imaginary - real * q) / denominator);
    }
  }
 /**
  * Returns an estimate of the solution to the linear system A &middot; x = b.
  *
  * @param a the linear operator A of the system
  * @param b the right-hand side vector
  * @return a new vector containing the solution
  * @throws NullArgumentException if one of the parameters is {@code null}
  * @throws NonSquareOperatorException if {@code a} is not square
  * @throws DimensionMismatchException if {@code b} has dimensions inconsistent with {@code a}
  * @throws MaxCountExceededException at exhaustion of the iteration count, unless a custom {@link
  *     org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback} has been set
  *     at construction of the {@link IterationManager}
  */
 public RealVector solve(final RealLinearOperator a, final RealVector b)
     throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException,
         MaxCountExceededException {
   MathUtils.checkNotNull(a);
   final RealVector x = new ArrayRealVector(a.getColumnDimension());
   x.set(0.);
   return solveInPlace(a, b, x);
 }
Ejemplo n.º 10
0
  /**
   * Returns a {@code Complex} whose value is {@code (this - subtrahend)}. Uses the definitional
   * formula
   *
   * <pre>
   *  <code>
   *   (a + bi) - (c + di) = (a-c) + (b-d)i
   *  </code>
   * </pre>
   *
   * If either {@code this} or {@code subtrahend} has a {@code NaN]} value in either part, {@link
   * #NaN} is returned; otherwise infinite and {@code NaN} values are returned in the parts of the
   * result according to the rules for {@link java.lang.Double} arithmetic.
   *
   * @param subtrahend value to be subtracted from this {@code Complex}.
   * @return {@code this - subtrahend}.
   * @throws NullArgumentException if {@code subtrahend} is {@code null}.
   */
  public Complex subtract(Complex subtrahend) throws NullArgumentException {
    MathUtils.checkNotNull(subtrahend);
    if (isNaN || subtrahend.isNaN) {
      return NaN;
    }

    return createComplex(real - subtrahend.getReal(), imaginary - subtrahend.getImaginary());
  }
Ejemplo n.º 11
0
  /**
   * Returns a {@code Complex} whose value is {@code (this + addend)}. Uses the definitional formula
   *
   * <pre>
   *  <code>
   *   (a + bi) + (c + di) = (a+c) + (b+d)i
   *  </code>
   * </pre>
   *
   * <br>
   * If either {@code this} or {@code addend} has a {@code NaN} value in either part, {@link #NaN}
   * is returned; otherwise {@code Infinite} and {@code NaN} values are returned in the parts of the
   * result according to the rules for {@link java.lang.Double} arithmetic.
   *
   * @param addend Value to be added to this {@code Complex}.
   * @return {@code this + addend}.
   * @throws NullArgumentException if {@code addend} is {@code null}.
   */
  public Complex add(Complex addend) throws NullArgumentException {
    MathUtils.checkNotNull(addend);
    if (isNaN || addend.isNaN) {
      return NaN;
    }

    return createComplex(real + addend.getReal(), imaginary + addend.getImaginary());
  }
Ejemplo n.º 12
0
 /**
  * Returns a {@code Complex} whose value is {@code this * factor}. Implements preliminary checks
  * for {@code NaN} and infinity followed by the definitional formula:
  *
  * <pre>
  *  <code>
  *   (a + bi)(c + di) = (ac - bd) + (ad + bc)i
  *  </code>
  * </pre>
  *
  * Returns {@link #NaN} if either {@code this} or {@code factor} has one or more {@code NaN}
  * parts. <br>
  * Returns {@link #INF} if neither {@code this} nor {@code factor} has one or more {@code NaN}
  * parts and if either {@code this} or {@code factor} has one or more infinite parts (same result
  * is returned regardless of the sign of the components). <br>
  * Returns finite values in components of the result per the definitional formula in all remaining
  * cases.
  *
  * @param factor value to be multiplied by this {@code Complex}.
  * @return {@code this * factor}.
  * @throws NullArgumentException if {@code factor} is {@code null}.
  */
 public Complex multiply(Complex factor) throws NullArgumentException {
   MathUtils.checkNotNull(factor);
   if (isNaN || factor.isNaN) {
     return NaN;
   }
   if (Double.isInfinite(real)
       || Double.isInfinite(imaginary)
       || Double.isInfinite(factor.real)
       || Double.isInfinite(factor.imaginary)) {
     // we don't use isInfinite() to avoid testing for NaN again
     return INF;
   }
   return createComplex(
       real * factor.real - imaginary * factor.imaginary,
       real * factor.imaginary + imaginary * factor.real);
 }
 /**
  * Create a new {@code FieldMatrix<T>} using the input array as the underlying data array.
  *
  * <p>If an array is built specially in order to be embedded in a {@code FieldMatrix<T>} and not
  * used directly, the {@code copyArray} may be set to {@code false}. This will prevent the copying
  * and improve performance as no new array will be built and no data will be copied.
  *
  * @param field Field to which the elements belong.
  * @param d Data for the new matrix.
  * @param copyArray Whether to copy or reference the input array.
  * @throws DimensionMismatchException if {@code d} is not rectangular.
  * @throws NoDataException if there are not at least one row and one column.
  * @throws NullArgumentException if {@code d} is {@code null}.
  * @see #Array2DRowFieldMatrix(FieldElement[][])
  */
 public Array2DRowFieldMatrix(final Field<T> field, final T[][] d, final boolean copyArray)
     throws DimensionMismatchException, NoDataException, NullArgumentException {
   super(field);
   if (copyArray) {
     copyIn(d);
   } else {
     MathUtils.checkNotNull(d);
     final int nRows = d.length;
     if (nRows == 0) {
       throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW);
     }
     final int nCols = d[0].length;
     if (nCols == 0) {
       throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
     }
     for (int r = 1; r < nRows; r++) {
       if (d[r].length != nCols) {
         throw new DimensionMismatchException(nCols, d[r].length);
       }
     }
     data = d;
   }
 }
 /**
  * Creates a new instance of this class, with custom iteration manager.
  *
  * @param manager the custom iteration manager
  * @throws NullArgumentException if {@code manager} is {@code null}
  */
 public IterativeLinearSolver(final IterationManager manager) throws NullArgumentException {
   MathUtils.checkNotNull(manager);
   this.manager = manager;
 }
 /**
  * Returns an estimate of the solution to the linear system A &middot; x = b.
  *
  * @param a the linear operator A of the system
  * @param b the right-hand side vector
  * @param x0 the initial guess of the solution
  * @return a new vector containing the solution
  * @throws NullArgumentException if one of the parameters is {@code null}
  * @throws NonSquareOperatorException if {@code a} is not square
  * @throws DimensionMismatchException if {@code b} or {@code x0} have dimensions inconsistent with
  *     {@code a}
  * @throws MaxCountExceededException at exhaustion of the iteration count, unless a custom {@link
  *     org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback} has been set
  *     at construction of the {@link IterationManager}
  */
 public RealVector solve(RealLinearOperator a, RealVector b, RealVector x0)
     throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException,
         MaxCountExceededException {
   MathUtils.checkNotNull(x0);
   return solveInPlace(a, b, x0.copy());
 }
Ejemplo n.º 16
0
 /**
  * Returns of value of this complex number raised to the power of {@code x}. Implements the
  * formula:
  *
  * <pre>
  *  <code>
  *   y<sup>x</sup> = exp(x&middot;log(y))
  *  </code>
  * </pre>
  *
  * where {@code exp} and {@code log} are {@link #exp} and {@link #log}, respectively. <br>
  * Returns {@link Complex#NaN} if either real or imaginary part of the input argument is {@code
  * NaN} or infinite, or if {@code y} equals {@link Complex#ZERO}.
  *
  * @param x exponent to which this {@code Complex} is to be raised.
  * @return <code> this<sup>{@code x}</sup></code>.
  * @throws NullArgumentException if x is {@code null}.
  * @since 1.2
  */
 public Complex pow(Complex x) throws NullArgumentException {
   MathUtils.checkNotNull(x);
   return this.log().multiply(x).exp();
 }