class MatrixUtils {

  public static DoubleMatrix2D normalizeSparseColumnL2(
      final DoubleMatrix2D A, final double[] work) {
    final double[] w = prepareWork(A, work);

    A.forEachNonZero(
        new IntIntDoubleFunction() {
          @Override
          public double apply(int row, int column, double value) {
            w[column] += value * value;
            return value;
          }
        });

    // Take the square root
    for (int c = 0; c < A.columns(); c++) {
      w[c] = Math.sqrt(w[c]);
    }

    // Normalize
    A.forEachNonZero(
        new IntIntDoubleFunction() {
          @Override
          public double apply(int row, int column, double value) {
            A.setQuick(row, column, value / w[column]);
            return 0;
          }
        });

    return A;
  }

}