public static <E extends DataElement> double computeSquareError(CentroidCluster<E> cluster) {
    List<E> elements = cluster.getDataElements();

    if (elements.isEmpty()) {
      return 0;
    }

    DoubleVector center = cluster.computeCentroid();

    double totalError = 0;
    final int vectorSize = center.size();

    for (DataElement e : elements) {
      DoubleVector elemVector = e.asVector();
      double errorWithinCluster = 0;

      for (int i = 0; i < vectorSize; i++) {
        errorWithinCluster += MathUtil.square(elemVector.get(i) - center.get(i));
      }

      totalError += errorWithinCluster;
    }

    return totalError;
  }