📄 doublevector.java
字号:
return Math.sqrt( sum2() );
}
/** Returns ||u-v||^2
* @param v the second vector
*/
public double sum2( DoubleVector v )
{
return minus( v ).sum2();
}
/** Returns a subvector.
* @param i0 the index of the first element
* @param i1 the index of the last element
* @return v[i0:i1]
*/
public DoubleVector subvector( int i0, int i1 )
{
DoubleVector v = new DoubleVector( i1-i0+1 );
v.set(0, i1 - i0, this, i0);
return v;
}
/** Returns a subvector.
* @param index stores the indices of the needed elements
* @return v[index]
*/
public DoubleVector subvector( IntVector index ) {
DoubleVector v = new DoubleVector( index.size() );
for( int i = 0; i < index.size(); i++ )
v.V[i] = V[index.V[i]];
return v;
}
/** Returns a vector from the pivoting indices. Elements not indexed are
* set to zero.
* @param index stores the pivoting indices
* @param length the total number of the potential elements
* @return the subvector */
public DoubleVector unpivoting( IntVector index, int length ) {
if( index.size() > length )
throw new IllegalArgumentException("index.size() > length ");
DoubleVector u = new DoubleVector( length );
for( int i = 0; i < index.size(); i++ ) {
u.V[index.V[i]] = V[i];
}
return u;
}
/** Adds a value to all the elements
* @param x the value
*/
public DoubleVector plus ( double x ) {
return copy().plusEquals( x );
}
/** Adds a value to all the elements in place
* @param x the value
*/
public DoubleVector plusEquals ( double x ) {
for( int i = 0; i < size(); i++ )
V[i] += x;
return this;
}
/**
* Adds another vector element by element
* @param v the second vector
*/
public DoubleVector plus( DoubleVector v ) {
return copy().plusEquals( v );
}
/**
* Adds another vector in place element by element
* @param v the second vector
*/
public DoubleVector plusEquals( DoubleVector v ) {
for(int i = 0; i < size(); i++ )
V[i] += v.V[i];
return this;
}
/**
* Subtracts a value
* @param x the value
*/
public DoubleVector minus( double x ) {
return plus( -x );
}
/**
* Subtracts a value in place
* @param x the value
*/
public DoubleVector minusEquals( double x ) {
plusEquals( -x );
return this;
}
/**
* Subtracts another DoubleVector element by element
* @param v the second DoubleVector
*/
public DoubleVector minus( DoubleVector v ) {
return copy().minusEquals( v );
}
/**
* Subtracts another DoubleVector element by element in place
* @param v the second DoubleVector
*/
public DoubleVector minusEquals( DoubleVector v ) {
for(int i = 0; i < size(); i++ )
V[i] -= v.V[i];
return this;
}
/** Multiplies a scalar
@param s scalar
@return s * v
*/
public DoubleVector times( double s ) {
return copy().timesEquals( s );
}
/** Multiply a vector by a scalar in place, u = s * u
@param s scalar
@return replace u by s * u
*/
public DoubleVector timesEquals( double s ) {
for (int i = 0; i < size(); i++) {
V[i] *= s;
}
return this;
}
/**
* Multiplies another DoubleVector element by element
* @param v the second DoubleVector
*/
public DoubleVector times( DoubleVector v ) {
return copy().timesEquals( v );
}
/**
* Multiplies another DoubleVector element by element in place
* @param v the second DoubleVector
*/
public DoubleVector timesEquals( DoubleVector v ) {
for(int i = 0; i < size(); i++ )
V[i] *= v.V[i];
return this;
}
/**
* Divided by another DoubleVector element by element
* @param v the second DoubleVector
*/
public DoubleVector dividedBy ( DoubleVector v ) {
return copy().dividedByEquals( v );
}
/**
* Divided by another DoubleVector element by element in place
* @param v the second DoubleVector
*/
public DoubleVector dividedByEquals ( DoubleVector v ) {
for( int i = 0; i < size(); i++ ) {
V[i] /= v.V[i];
}
return this;
}
/**
* Checks if it is an empty vector
*/
public boolean isEmpty() {
if( size() == 0 ) return true;
return false;
}
/**
* Returns a vector that stores the cumulated values of the original
* vector */
public DoubleVector cumulate()
{
return copy().cumulateInPlace();
}
/**
* Cumulates the original vector in place
*/
public DoubleVector cumulateInPlace()
{
for (int i = 1; i < size(); i++) {
V[i] += V[i-1];
}
return this;
}
/**
* Returns the index of the maximum. <p>
* If multiple maximums exist, the index of the first is returned.
*/
public int indexOfMax()
{
int index = 0;
double ma = V[0];
for( int i = 1; i < size(); i++ ){
if( ma < V[i] ) {
ma = V[i];
index = i;
}
}
return index;
}
/**
* Returns true if vector not sorted
*/
public boolean unsorted () {
if( size() < 2 ) return false;
for( int i = 1; i < size(); i++ ) {
if( V[i-1] > V[i] )
return true;
}
return false;
}
/**
* Combine two vectors together
* @param v the second vector
*/
public DoubleVector cat( DoubleVector v ) {
DoubleVector w = new DoubleVector( size() + v.size() );
w.set(0, size() - 1, this, 0);
w.set(size(), size() + v.size()-1, v, 0);
return w;
}
/**
* Swaps the values stored at i and j
* @param i the index i
* @param j the index j
*/
public void swap( int i, int j ){
if( i == j ) return;
double t = V[i];
V[i] = V[j];
V[j] = t;
}
/**
* Returns the maximum value of all elements
*/
public double max () {
if( size() < 1 ) throw new IllegalArgumentException("zero size");
double ma = V[0];
if( size() < 2 ) return ma;
for( int i = 1; i < size(); i++ ) {
if( V[i] > ma ) ma = V[i];
}
return ma;
}
/**
* Applies a method to the vector
* @param className the class name
* @param method the method
*/
public DoubleVector map( String className, String method ) {
try {
Class c = Class.forName( className );
Class [] cs = new Class[1];
cs[ 0 ] = Double.TYPE;
Method m = c.getMethod( method, cs );
DoubleVector w = new DoubleVector( size() );
Object [] obj = new Object[1];
for( int i = 0; i < size(); i++ ) {
obj[0] = new Double( V[i] );
w.set( i, Double.parseDouble(m.invoke( null, obj ).toString()) );
}
return w;
}
catch ( Exception e ) {
e.printStackTrace();
System.exit(1);
}
return null;
}
/**
* Returns the reverse vector
*/
public DoubleVector rev() {
int n = size();
DoubleVector w = new DoubleVector( n );
for(int i = 0; i < n; i++ )
w.V[i] = V[n-i-1];
return w;
}
/**
* Returns a random vector of uniform distribution
* @param n the size of the vector
*/
public static DoubleVector random( int n ) {
DoubleVector v = new DoubleVector( n );
for (int i = 0; i < n; i++) {
v.V[i] = Math.random();
}
return v;
}
/** Convert the DoubleVecor to a string
*/
public String toString() {
return toString( 5, false );
}
/** Convert the DoubleVecor to a string
* @param digits the number of digits after decimal point
* @param trailing true if trailing zeros are to be shown
*/
public String toString( int digits, boolean trailing ) {
if( isEmpty() ) return "null vector";
StringBuffer text = new StringBuffer();
FlexibleDecimalFormat nf = new FlexibleDecimalFormat( digits,
trailing );
nf.grouping( true );
for( int i = 0; i < size(); i ++ ) nf.update( V[i] );
int count = 0;
int width = 80;
String number;
for( int i = 0; i < size(); i++ ) {
number = nf.format(V[i]);
count += 1 + number.length();
if( count > width-1 ) {
text.append('\n');
count = 1 + number.length();
}
text.append( " " + number );
}
return text.toString();
}
public static void main( String args[] ) {
DoubleVector u = random(10);
DoubleVector v = random(10);
DoubleVector a = random(10);
DoubleVector w = a;
System.out.println( random(10).plus(v).plus(w) );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -