code: @SuppressWarnings("unchecked") @Override 
      public double magnitude() {
          // Check whether the current magnitude is valid and if not, recompute it
          if (magnitude < 0) {
              double m = 0;
              // Special case if we can iterate in time linear to the number of
              // non-zero values
              if (sparseVector instanceof Iterable) {
                  for (DoubleEntry e : (Iterable<DoubleEntry>)sparseVector) {
                      int idx = e.index();
                      if (idx >= vectorOffset 
                              && idx < vectorOffset + vectorLength) {
                          double d = e.value();
                          m += d * d;
                      }
                  }
              }
              else {
                  for (int nz : sparseVector.getNonZeroIndices()) {
                      if (nz >= vectorOffset 
                              && nz < vectorOffset + vectorLength) {
                          double d = doubleVector.get(nz);
                          m += d * d;
                      }
                  }
              }
              magnitude = Math.sqrt(m);
          }
          return magnitude;
      }
docs: {@inheritDoc}