📄 doublevector.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * DoubleVector.java * Copyright (C) 2002 Yong Wang * */package weka.classifiers.functions.pace;import java.lang.*;import java.util.*;import java.lang.reflect.*;public class DoubleVector implements Cloneable { double[] V; // array for internal storage of elements. private int sizeOfVector; // size of the vector /* ------------------------ Constructors * ------------------------ */ /** Constructs a null vector. */ public DoubleVector() { this( 0 ); } /** Constructs an n-vector of zeros. @param n length. */ public DoubleVector( int n ){ V = new double[ n ]; setSize( n ); } /** Constructs a constant n-vector. @param n length. @param s the scalar value used to fill the vector */ public DoubleVector( int n, double s ){ this( n ); set( s ); } /** Constructs a vector directly from a double array * @param v the array */ public DoubleVector( double v[] ){ if( v == null ) { V = new double[0]; setSize( 0 ); } else { V = v; setSize( v.length ); } } /* ------------------------ * Public Methods * ------------------------ */ /** Set a single element. * @param i Index. * @param s a[i]. */ public void set( int i, double s ) { V[i] = s; } /** Set all elements to a value * @param s the value */ public void set( double s ) { set(0, size()-1, s); } /** Set some elements to a value * @param i0 the index of the first element * @param i1 the index of the second element * @param s the value */ public void set( int i0, int i1, double s ) { for(int i = i0; i <= i1; i++ ) V[i] = s; } /** Set some elements using a 2-D array * @param i0 the index of the first element * @param i1 the index of the second element * @param j0 the index of the starting element in the 2-D array * @param s the value */ public void set( int i0, int i1, double [] v, int j0){ for(int i = i0; i<= i1; i++) V[i] = v[j0 + i - i0]; } /** Set the elements using a DoubleVector * @param v the DoubleVector */ public void set( DoubleVector v ){ set( 0, v.size() - 1, v, 0); } /** Set some elements using a DoubleVector. * @param i0 the index of the first element * @param i1 the index of the second element * @param v the DoubleVector * @param j0 the index of the starting element in the DoubleVector */ public void set( int i0, int i1, DoubleVector v, int j0){ for(int i = i0; i<= i1; i++) V[i] = v.V[j0 + i - i0]; } /** Access the internal one-dimensional array. @return Pointer to the one-dimensional array of vector elements. */ double [] getArray() { return V; } void setArray( double [] a ) { V = a; } /** Returns a copy of the DoubleVector usng a double array. @return the one-dimensional array. */ public double[] getArrayCopy() { double v[] = new double[size()]; for(int i= 0; i < size(); i++ ) v[i] = V[i]; return v; } /** Sorts the array in place */ public void sort() { Arrays.sort( V, 0, size() ); } /** Sorts the array in place with index returned */ public IntVector sortWithIndex() { IntVector index = IntVector.seq( 0, size()-1 ); sortWithIndex( 0, size()-1, index ); return index; } /** Sorts the array in place with index changed * @param xi first index * @param xj last index * @param index array that stores all indices */ public void sortWithIndex( int xi, int xj, IntVector index ) { if( xi < xj ) { double x, f, k; int xm = (int) (xi + xj) / 2; // median index x = Math.min( V[xi], // median of three Math.max( V[xm], V[xj])); int i = xi; int j = xj; while( i < j ) { while( V[i] < x && i < xj ) i++; while( V[j] > x && j > xi ) j--; if( i <= j ){ swap(i, j); index.swap(i, j); i++; j--; } } sortWithIndex(xi, j, index); sortWithIndex(i, xj, index); } } /** Gets the size of the vector. @return the size */ public int size(){ return sizeOfVector; } /** * Sets the size of the vector * @param m the size */ public void setSize( int m ){ if( m > capacity() ) throw new IllegalArgumentException("insufficient capacity"); sizeOfVector = m; } /** Gets the capacity of the vector. * @return the capacity. */ public int capacity() { if( V == null ) return 0; return V.length; } /** Sets the capacity of the vector * @param n the capacity. */ public void setCapacity ( int n ) { if( n == capacity() ) return; double [] oldV = V; int m = Math.min( n, size() ); V = new double[ n ]; setSize( m ); set(0, m-1, oldV, 0); } /** Gets a single element. * @param i Index. * @return the value of the i-th element */ public double get( int i ) { return V[i]; } /** * Adds a value to an element * @param i the index of the element * @param s the value */ public void setPlus( int i, double s ) { V[i] += s; } /** * Multiplies a value to an element * @param i the index of the element * @param s the value */ public void setTimes( int i, double s ) { V[i] *= s; } /** * Adds an element into the vector * @param x the value of the new element */ public void addElement( double x ) { if( capacity() == 0 ) setCapacity( 10 ); if( size() == capacity() ) setCapacity( 2 * capacity() ); V[size()] = x; setSize( size() + 1 ); } /** * Returns the squared vector */ public DoubleVector square() { DoubleVector v = new DoubleVector( size() ); for(int i = 0; i < size(); i++ ) v.V[i] = V[i] * V[i]; return v; } /** * Returns the square-root of all the elements in the vector */ public DoubleVector sqrt() { DoubleVector v = new DoubleVector( size() ); for(int i = 0; i < size(); i++ ) v.V[i] = Math.sqrt(V[i]); return v; } /** Makes a deep copy of the vector */ public DoubleVector copy() { return (DoubleVector) clone(); } /** Clones the DoubleVector object. */ public Object clone() { int n = size(); DoubleVector u = new DoubleVector( n ); for( int i = 0; i < n; i++) u.V[i] = V[i]; return u; } /** * Returns the inner product of two DoubleVectors * @param v the second DoubleVector * @return the product */ public double innerProduct(DoubleVector v) { if(size() != v.size()) throw new IllegalArgumentException("sizes unmatch"); double p = 0; for (int i = 0; i < size(); i++) { p += V[i] * v.V[i]; } return p; } /** * Returns the signs of all elements in terms of -1, 0 and +1. */ public DoubleVector sign() { DoubleVector s = new DoubleVector( size() ); for( int i = 0; i < size(); i++ ) { if( V[i] > 0 ) s.V[i] = 1; else if( V[i] < 0 ) s.V[i] = -1; else s.V[i] = 0; } return s; } /** Returns the sum of all elements in the vector. */ public double sum() { double s = 0; for( int i=0; i< size(); i++) s += V[i]; return s; } /** Returns the squared sum of all elements in the vector. */ public double sum2() { double s2 = 0; for( int i=0; i< size(); i++) s2 += V[i] * V[i]; return s2; } /** Returns the L1-norm of the vector */ public double norm1() { double s = 0; for( int i=0; i< size(); i++) s += Math.abs(V[i]); return s; } /** Returns the L2-norm of the vector */ public double norm2() { return Math.sqrt( sum2() ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -