Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.mahout.math;
 
 
The basic interface including numerous convenience functions

NOTE: All implementing classes must have a constructor that takes an int for cardinality and a no-arg constructor that can be used for marshalling the Writable instance

NOTE: Implementations may choose to reuse the Vector.Element in the Iterable methods

 
 public interface Vector extends Cloneable {

  

Returns:
a formatted String suitable for output
 
   String asFormatString();

  
Assign the value to all elements of the receiver

Parameters:
value a double value
Returns:
the modified receiver
 
   Vector assign(double value);

  
Assign the values to the receiver

Parameters:
values a double[] of values
Returns:
the modified receiver
Throws:
CardinalityException if the cardinalities differ
 
   Vector assign(double[] values);

  
Assign the other vector values to the receiver

Parameters:
other a Vector
Returns:
the modified receiver
Throws:
CardinalityException if the cardinalities differ
 
   Vector assign(Vector other);

  
Apply the function to each element of the receiver

Parameters:
function a DoubleFunction to apply
Returns:
the modified receiver
 
   Vector assign(DoubleFunction function);

  
Apply the function to each element of the receiver and the corresponding element of the other argument

Parameters:
other a Vector containing the second arguments to the function
function a DoubleDoubleFunction to apply
Returns:
the modified receiver
Throws:
CardinalityException if the cardinalities differ
 
   Vector assign(Vector otherDoubleDoubleFunction function);

  
Apply the function to each element of the receiver, using the y value as the second argument of the DoubleDoubleFunction

Parameters:
f a DoubleDoubleFunction to be applied
y a double value to be argument to the function
Returns:
the modified receiver
 
   Vector assign(DoubleDoubleFunction fdouble y);

  
Return the cardinality of the recipient (the maximum number of values)

Returns:
an int
 
   int size();

  

Returns:
true iff this implementation should be considered dense -- that it explicitly represents every value
 
   boolean isDense();

  

Returns:
true iff this implementation should be considered to be iterable in index order in an efficient way. In particular this implies that all() and nonZeroes() ()} return elements in ascending order by index.
  boolean isSequentialAccess();

  
Return a copy of the recipient

Returns:
a new Vector
  @SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException")
Return an object of Vector.Element representing an element of this Vector. Useful when designing new iterator types.

Parameters:
index Index of the Vector.Element required
Returns:
The Vector.Element Object
  Element getElement(int index);

  
Merge a set of (index, value) pairs into the vector.

Parameters:
updates an ordered mapping of indices to values to be merged in.
  void mergeUpdates(OrderedIntDoubleMapping updates);

  
A holder for information about a specific item in the Vector.

When using with an Iterator, the implementation may choose to reuse this element, so you may need to make a copy if you want to keep it

  interface Element {

    

Returns:
the value of this vector element.
    double get();

    

Returns:
the index of this vector element.
    int index();

    

Parameters:
value Set the current element to value.
    void set(double value);
  }

  
Return a new vector containing the values of the recipient divided by the argument

Parameters:
x a double value
Returns:
a new Vector
  Vector divide(double x);

  
Return the dot product of the recipient and the argument

Parameters:
x a Vector
Returns:
a new Vector
Throws:
CardinalityException if the cardinalities differ
  double dot(Vector x);

  
Return the value at the given index

Parameters:
index an int index
Returns:
the double at the index
Throws:
IndexException if the index is out of bounds
  double get(int index);

  
Return the value at the given index, without checking bounds

Parameters:
index an int index
Returns:
the double at the index
  double getQuick(int index);

  
Return an empty vector of the same underlying class as the receiver

Returns:
a Vector
  Vector like();

  
Return a new vector containing the element by element difference of the recipient and the argument

Parameters:
x a Vector
Returns:
a new Vector
Throws:
CardinalityException if the cardinalities differ
  Vector minus(Vector x);

  
Return a new vector containing the normalized (L_2 norm) values of the recipient

Returns:
a new Vector
  Vector normalize();

  
Return a new Vector containing the normalized (L_power norm) values of the recipient.

See http://en.wikipedia.org/wiki/Lp_space

Technically, when 0 < power < 1, we don't have a norm, just a metric, but we'll overload this here.

Also supports power == 0 (number of non-zero elements) and power = java.lang.Double.POSITIVE_INFINITY (max element). Again, see the Wikipedia page for more info

Parameters:
power The power to use. Must be >= 0. May also be java.lang.Double.POSITIVE_INFINITY. See the Wikipedia link for more on this.
Returns:
a new Vector x such that norm(x, power) == 1
  Vector normalize(double power);

  
Return a new vector containing the log(1 + entry)/ L_2 norm values of the recipient

Returns:
a new Vector
Return a new Vector with a normalized value calculated as log_power(1 + entry)/ L_power norm.

Parameters:
power The power to use. Must be > 1. Cannot be java.lang.Double.POSITIVE_INFINITY.
Returns:
a new Vector
  Vector logNormalize(double power);

  
Return the k-norm of the vector.

See http://en.wikipedia.org/wiki/Lp_space

Technically, when 0 > power < 1, we don't have a norm, just a metric, but we'll overload this here. Also supports power == 0 (number of non-zero elements) and power = java.lang.Double.POSITIVE_INFINITY (max element). Again, see the Wikipedia page for more info.

Parameters:
power The power to use.
See also:
normalize(double)
  double norm(double power);

  

Returns:
The minimum value in the Vector
  double minValue();

  

Returns:
The index of the minimum value
  int minValueIndex();

  

Returns:
The maximum value in the Vector
  double maxValue();

  

Returns:
The index of the maximum value
  int maxValueIndex();

  
Return a new vector containing the sum of each value of the recipient and the argument

Parameters:
x a double
Returns:
a new Vector
  Vector plus(double x);

  
Return a new vector containing the element by element sum of the recipient and the argument

Parameters:
x a Vector
Returns:
a new Vector
Throws:
CardinalityException if the cardinalities differ
  Vector plus(Vector x);

  
Set the value at the given index

Parameters:
index an int index into the receiver
value a double value to set
Throws:
IndexException if the index is out of bounds
  void set(int indexdouble value);

  
Set the value at the given index, without checking bounds

Parameters:
index an int index into the receiver
value a double value to set
  void setQuick(int indexdouble value);

  
Increment the value at the given index by the given value.

Parameters:
index an int index into the receiver
increment sets the value at the given index to value + increment;
  void incrementQuick(int indexdouble increment);

  
Return the number of values in the recipient which are not the default value. For instance, for a sparse vector, this would be the number of non-zero values.

Returns:
an int
Return the number of non zero elements in the vector.

Returns:
an int
  int getNumNonZeroElements();

  
Return a new vector containing the product of each value of the recipient and the argument

Parameters:
x a double argument
Returns:
a new Vector
  Vector times(double x);

  
Return a new vector containing the element-wise product of the recipient and the argument

Parameters:
x a Vector argument
Returns:
a new Vector
Throws:
CardinalityException if the cardinalities differ
  Vector times(Vector x);

  
Return a new vector containing the subset of the recipient

Parameters:
offset an int offset into the receiver
length the cardinality of the desired result
Returns:
a new Vector
Throws:
CardinalityException if the length is greater than the cardinality of the receiver
IndexException if the offset is negative or the offset+length is outside of the receiver
  Vector viewPart(int offsetint length);

  
Return the sum of all the elements of the receiver

Returns:
a double
  double zSum();

  
Return the cross product of the receiver and the other vector

Parameters:
other another Vector
Returns:
a Matrix
  Matrix cross(Vector other);
  /*
   * Need stories for these but keeping them here for now.
   */
  // void getNonZeros(IntArrayList jx, DoubleArrayList values);
  // void foreachNonZero(IntDoubleFunction f);
  // DoubleDoubleFunction map);
  // NewVector assign(Vector y, DoubleDoubleFunction function, IntArrayList
  // nonZeroIndexes);

  
Examples speak louder than words: aggregate(plus, pow(2)) is another way to say getLengthSquared(), aggregate(max, abs) is norm(Double.POSITIVE_INFINITY). To sum all of the postive values, aggregate(plus, max(0)).

Parameters:
aggregator used to combine the current value of the aggregation with the result of map.apply(nextValue)
map a function to apply to each element of the vector in turn before passing to the aggregator
Returns:
the final aggregation
  double aggregate(DoubleDoubleFunction aggregatorDoubleFunction map);

  

Generalized inner product - take two vectors, iterate over them both, using the combiner to combine together (and possibly map in some way) each pair of values, which are then aggregated with the previous accumulated value in the combiner.

Example: dot(other) could be expressed as aggregate(other, Plus, Times), and kernelized inner products (which are symmetric on the indices) work similarly.

Parameters:
other a vector to aggregate in combination with
aggregator function we're aggregating with; fa
combiner function we're combining with; fc
Returns:
the final aggregation; if r0 = fc(this[0], other[0]), ri = fa(r_{i-1}, fc(this[i], other[i])) for all i > 0
  double aggregate(Vector otherDoubleDoubleFunction aggregatorDoubleDoubleFunction combiner);

  
Return the sum of squares of all elements in the vector. Square root of this value is the length of the vector.
  double getLengthSquared();

  
Get the square of the distance between this vector and the other vector.
  double getDistanceSquared(Vector v);

  
Gets an estimate of the cost (in number of operations) it takes to lookup a random element in this vector.
  double getLookupCost();

  
Gets an estimate of the cost (in number of operations) it takes to advance an iterator through the nonzero elements of this vector.
  double getIteratorAdvanceCost();

  
Return true iff adding a new (nonzero) element takes constant time for this vector.
  boolean isAddConstantTime();
New to GrepCode? Check out our FAQ X