double reduceRank()

in src/main/java/org/apache/datasketches/vector/decomposition/MatrixOpsImplOjAlgo.java [104:137]


  double reduceRank(final Matrix A) {
    svd(A, true);

    double svAdjustment = 0.0;

    if (sv_.length >= k_) {
      double medianSVSq = sv_[k_ - 1]; // (l_/2)th item, not yet squared
      medianSVSq *= medianSVSq;
      svAdjustment += medianSVSq; // always track, even if not using compensative mode
      for (int i = 0; i < (k_ - 1); ++i) {
        final double val = sv_[i];
        final double adjSqSV = (val * val) - medianSVSq;
        S_.set(i, i, adjSqSV < 0 ? 0.0 : Math.sqrt(adjSqSV)); // just to be safe
      }
      for (int i = k_ - 1; i < S_.countColumns(); ++i) {
        S_.set(i, i, 0.0);
      }
    } else {
      throw new RuntimeException("Running with d < 2k not (yet?) supported");
      /*
      for (int i = 0; i < sv_.length; ++i) {
        S_.set(i, i, sv_[i]);
      }
      for (int i = sv_.length; i < S_.countColumns(); ++i) {
        S_.set(i, i, 0.0);
      }
      */
    }

    // store the result back in A
    S_.multiply(Vt_, (Primitive64Store) A.getRawObject());

    return svAdjustment;
  }