/* Check that the operations do not throw an exception (cf. MATH-645). */
  @Test
  public void testConcurrentModification() {
    final RealVector u = new OpenMapRealVector(3, 1e-6);
    u.setEntry(0, 1);
    u.setEntry(1, 0);
    u.setEntry(2, 2);

    final RealVector v1 = new OpenMapRealVector(3, 1e-6);
    v1.setEntry(0, 0);
    v1.setEntry(1, 3);
    v1.setEntry(2, 0);

    u.ebeMultiply(v1);
    u.ebeDivide(v1);
  }
 @Override
 public Label predict(Instance instance) {
   Label l = null;
   if (instance.getLabel() instanceof ClassificationLabel || instance.getLabel() == null) {
     // ----------------- declare variables ------------------
     double lambda = 0.0;
     RealVector x_instance = new ArrayRealVector(matrixX.getColumnDimension(), 0);
     double result = 0.0;
     // -------------------------- initialize xi -------------------------
     for (int idx = 0; idx < matrixX.getColumnDimension(); idx++) {
       x_instance.setEntry(idx, instance.getFeatureVector().get(idx + 1));
     }
     // ------------------ get lambda -----------------------
     for (int j = 0; j < alpha.getDimension(); j++) {
       lambda += alpha.getEntry(j) * kernelFunction(matrixX.getRowVector(j), x_instance);
     }
     // ----------------- make prediction -----------------
     Sigmoid g = new Sigmoid(); // helper function
     result = g.value(lambda);
     l = new ClassificationLabel(result < 0.5 ? 0 : 1);
   } else {
     System.out.println("label type error!");
   }
   return l;
 }
  private static Intersection getIntersection(Ray ray, SphereObject obj, Model model) {
    RealMatrix transform = obj.getTransform();
    final RealMatrix transformInverse = obj.getTransformInverse();
    ray = ray.transform(transformInverse);
    Vector3D c = VectorUtils.toVector3D(obj.getCenter());
    Vector3D p0 = VectorUtils.toVector3D(ray.getP0());
    Vector3D p1 = VectorUtils.toVector3D(ray.getP1());
    float a = (float) p1.dotProduct(p1);
    Vector3D p0c = p0.subtract(c);
    float b = (float) p1.dotProduct(p0c) * 2.0f;

    float cc = (float) (p0c.dotProduct(p0c)) - obj.getSize() * obj.getSize();
    Double t = quadraticEquationRoot1(a, b, cc);
    if (t == null || t < EPSILON) {
      return new Intersection(false);
    }
    Intersection result = new Intersection(true);
    result.setObject(obj);
    final Vector3D p = p0.add(p1.scalarMultiply(t));
    RealVector pv = VectorUtils.toRealVector(p);
    pv.setEntry(3, 1.0);
    RealVector pt = transform.preMultiply(pv);
    result.setP(VectorUtils.toVector3D(pt));
    RealVector nv = pv.subtract(obj.getCenter());
    final RealVector nvt = transformInverse.transpose().preMultiply(nv);
    result.setN(VectorUtils.toVector3D(nvt).normalize());
    result.setDistance(t);
    return result;
  }
 @Override
 public void train(List<Instance> instances) {
   // ------------------------ initialize rows and columns ---------------------
   int rows = instances.size();
   int columns = 0;
   // get max columns
   for (Instance i : instances) {
     int localColumns = Collections.max(i.getFeatureVector().getFeatureMap().keySet());
     if (localColumns > columns) columns = localColumns;
   }
   // ------------------------ initialize alpha vector -----------------------
   alpha = new ArrayRealVector(rows, 0);
   // ------------------------ initialize base X and Y for use --------------------------
   double[][] X = new double[rows][columns];
   double[] Y = new double[rows];
   for (int i = 0; i < rows; i++) {
     Y[i] = ((ClassificationLabel) instances.get(i).getLabel()).getLabelValue();
     for (int j = 0; j < columns; j++) {
       X[i][j] = instances.get(i).getFeatureVector().get(j + 1);
     }
   }
   // ---------------------- gram matrix -------------------
   matrixX = new Array2DRowRealMatrix(X);
   RealMatrix gram = new Array2DRowRealMatrix(rows, rows);
   for (int i = 0; i < rows; i++) {
     for (int j = 0; j < rows; j++) {
       gram.setEntry(i, j, kernelFunction(matrixX.getRowVector(i), matrixX.getRowVector(j)));
     }
   }
   // ---------------------- gradient ascent --------------------------
   Sigmoid g = new Sigmoid(); // helper function
   System.out.println("Training start...");
   System.out.println(
       "Learning rate: " + _learning_rate + " Training times: " + _training_iterations);
   for (int idx = 0; idx < _training_iterations; idx++) {
     System.out.println("Training iteration: " + (idx + 1));
     for (int k = 0; k < rows; k++) {
       double gradient_ascent = 0.0;
       RealVector alpha_gram = gram.operate(alpha);
       for (int i = 0; i < rows; i++) {
         double lambda = alpha_gram.getEntry(i);
         double kernel = gram.getEntry(i, k);
         gradient_ascent =
             gradient_ascent
                 + Y[i] * g.value(-lambda) * kernel
                 + (1 - Y[i]) * g.value(lambda) * (-kernel);
       }
       alpha.setEntry(k, alpha.getEntry(k) + _learning_rate * gradient_ascent);
     }
   }
   System.out.println("Training done!");
 }
    /**
     * Solve the linear equation A &times; X = B in least square sense.
     *
     * <p>The m&times;n matrix A may not be square, the solution X is such that ||A &times; X - B||
     * is minimal.
     *
     * @param b right-hand side of the equation A &times; X = B
     * @return a vector X that minimizes the two norm of A &times; X - B
     * @exception IllegalArgumentException if matrices dimensions don't match
     * @exception InvalidMatrixException if decomposed matrix is singular
     */
    public RealVector solve(final RealVector b)
        throws IllegalArgumentException, InvalidMatrixException {

      if (b.getDimension() != uT.getColumnDimension()) {
        throw MathRuntimeException.createIllegalArgumentException(
            "vector length mismatch: got {0} but expected {1}",
            b.getDimension(), uT.getColumnDimension());
      }

      final RealVector w = uT.operate(b);
      for (int i = 0; i < singularValues.length; ++i) {
        final double si = singularValues[i];
        if (si == 0) {
          throw new SingularMatrixException();
        }
        w.setEntry(i, w.getEntry(i) / si);
      }
      return v.operate(w);
    }