public static DoubleVector copyOf(DoubleVector source) {
        if (source == null)
            return null;
        DoubleVector result = null;

        if (source instanceof SparseDoubleVector) {
            result = new CompactSparseVector(source.length());
            copyFromSparseVector(result, source);
        } else if (source instanceof DenseVector ||
                   source instanceof ScaledDoubleVector) {
            result = new DenseVector(source.length());
            for (int i = 0; i < source.length(); ++i)
                result.set(i, source.get(i));
        } else if (source instanceof AmortizedSparseVector) {
            result = new AmortizedSparseVector(source.length());
            copyFromSparseVector(result, source);
        } else if (source instanceof DoubleVectorView) {
            DoubleVectorView view = (DoubleVectorView) source;
            return copyOf(view.getOriginalVector());
        } else {
            // Create a copy of the given class using reflection.  

            // First check whether we can find a copy contructor that accepts an
            // instance of Vector
            Class<? extends DoubleVector> sourceClazz = source.getClass();
            try {
                Constructor<? extends DoubleVector> constructor =
                    sourceClazz.getConstructor(DoubleVector.class);
                result = (DoubleVector) constructor.newInstance(source);
            } 
            // If there wasn't a copy constructor, see if there's one that
            // accepts the length and then we'll copy the data over manually
            catch (NoSuchMethodException nsme) {
                try {
                    Constructor<? extends DoubleVector> constructor =
                        sourceClazz.getConstructor(Integer.TYPE);
                    int length = source.length();
                    result = (DoubleVector) constructor.newInstance(
                        Integer.valueOf(length));
                    // Copy the data over
                    for (int i = 0; i < length; ++i) 
                        result.set(i, source.get(i));                    
                }
                catch (Exception e) {
                    throw new UnsupportedOperationException(
                        "Could not find applicalble way to copy a vector " +
                        "of type " + source.getClass(), e);
                }
            }
            catch (Exception e) {
                throw new UnsupportedOperationException(
                    "Could not find applicalble way to copy a vector " +
                    "of type " + source.getClass(), e);
            }
        }
        return result;
    }
public static DoubleVector copyOf ( DoubleVector source ) { if ( source == null ) return null ; DoubleVector result = null ; if ( source instanceof SparseDoubleVector ) { result = new CompactSparseVector ( source . length ( ) ) ; copyFromSparseVector ( result , source ) ; } else if ( source instanceof DenseVector || source instanceof ScaledDoubleVector ) { result = new DenseVector ( source . length ( ) ) ; for ( int i = 0 ; i < source . length ( ) ; ++ i ) result . set ( i , source . get ( i ) ) ; } else if ( source instanceof AmortizedSparseVector ) { result = new AmortizedSparseVector ( source . length ( ) ) ; copyFromSparseVector ( result , source ) ; } else if ( source instanceof DoubleVectorView ) { DoubleVectorView view = ( DoubleVectorView ) source ; return copyOf ( view . getOriginalVector ( ) ) ; } else { Class < ? extends DoubleVector > sourceClazz = source . getClass ( ) ; try { Constructor < ? extends DoubleVector > constructor = sourceClazz . getConstructor ( DoubleVector . class ) ; result = ( DoubleVector ) constructor . newInstance ( source ) ; } catch ( NoSuchMethodException nsme ) { try { Constructor < ? extends DoubleVector > constructor = sourceClazz . getConstructor ( Integer . TYPE ) ; int length = source . length ( ) ; result = ( DoubleVector ) constructor . newInstance ( Integer . valueOf ( length ) ) ; for ( int i = 0 ; i < length ; ++ i ) result . set ( i , source . get ( i ) ) ; } catch ( Exception e ) { throw new UnsupportedOperationException ( "Could not find applicalble way to copy a vector " + "of type " + source . getClass ( ) , e ) ; } } catch ( Exception e ) { throw new UnsupportedOperationException ( "Could not find applicalble way to copy a vector " + "of type " + source . getClass ( ) , e ) ; } } return result ; }
Creates a copy of a given {@code DoubleVector} with the same type as the
original.

@param source The {@code Vector} to copy.

@return A copy of {@code source} with the same type.