Class Vector

java.lang.Object
de.tu_bs.isbs.util.math.Vector

public class Vector
extends java.lang.Object
Eine Vektor-Klasse basierend auf Jama.Matrix.

Benutzt eine 1xN-Matrix als Basis für einen N-elementigen (Zeilen-) Vektor. Stellt typische Vektor-Operationen zur Verfügung. (Hilft, Programmierfehler zu vermeiden, die leicht auftreten können, wenn man direkt die Matrix-Klasse einsetzt.)

Meistens werden die zu Grunde liegenden Matrix-Funktionen benutzt, außer in Fällen, in denen aus Geschwindigkeitsgründen eine eigene Implementierung benutzt wird (z.B. beim Skalarprodukt, das sonst eine aufwendige Transposition ausführen würde).

Anmerkung: Das Paket no.uib.cipr stellt ebenfalls Matrix- und Vektor-Klassen zur Verfügung. Jama.Matrix wird hier verwendet, weil die Handhabung einfacher und weniger gefährlich ist als bei den no.uib.cipr-Klassen. Dort verändern Operationen die Objekte i.d.R. "in place", sofern nicht explizit geklont wird.

Author:
reiser
  • Constructor Summary

    Constructors 
    Constructor Description
    Vector​(double[] V)
    Constructs a vector based on array V.
    Vector​(int n)
    Constructs a vector of length n.
    Vector​(int n, double s)
    Constructs a vector of length n, initialized with value s.
  • Method Summary

    Modifier and Type Method Description
    Vector arrayRightDivide​(Vector B)
    Elementweise Division: result := this ./ B
    Vector arrayTimesEquals​(Vector B)
    Element-by-element multiplication in place, this := this .* B.
    Vector copy()
    Copies this vector.
    static double dot​(double[] a, double[] b)
    dot product, <a,b> (compensates roundoff by Kahan summation).
    double dot​(Vector B)
    dot product, this dot B
    double get​(int i)
    Gets element at index i.
    double[] getArray()
    Returns the underlying 1-dimensional Array.
    Matrix getMatrix()
    Returns the underlying 1xN-Matrix.
    int length()
    Length of this vector.
    static void main​(java.lang.String[] args)  
    Vector matrixTimesThis​(Matrix M)
    Matrix-Vektor-Produkt.
    double maxAbs()
    Maximum der Absolutwerte aller Elemente.
    Vector minus​(Vector B)
    Subtracts a vector from this vector.
    Vector plus​(double b)
    Adds a constant to every element of this vector.
    Vector plus​(Vector B)
    Adds a vector to this vector.
    Vector plusEquals​(Vector B)
    Vector addition in place, this := this + B.
    void set​(int i, double s)
    Sets element at index i to value s.
    double squareOfEuclidianLength()
    Länge des Vektors zum Quadrat.
    double sumAbs()
    Liefert Summe der Absolutwerte aller Vektorelemente.
    Vector times​(double s)
    scale this vector by s.
    java.lang.String toCSVString​(char fieldSeperator)
    Vektor als CSV-String ausgeben.
    java.lang.String toString()  
    java.lang.String toTSVString()
    als TAB-getrennter String
    Vector uminus()
    Unary minus.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Vector

      public Vector​(int n)
      Constructs a vector of length n.
      Parameters:
      n - - length of the vector.
    • Vector

      public Vector​(int n, double s)
      Constructs a vector of length n, initialized with value s.
      Parameters:
      n - - length of the vector.
      s - - set all elements to value s.
    • Vector

      public Vector​(double[] V)
      Constructs a vector based on array V.
      Parameters:
      V - array containing vector elements. Note: V is not copied but directly used as storage for the vector's elements. Changes to V also change the vector.
      See Also:
      getArray()
  • Method Details

    • getArray

      public double[] getArray()
      Returns the underlying 1-dimensional Array.
      Returns:
      the underlying 1-dimensional Array.
    • getMatrix

      public Matrix getMatrix()
      Returns the underlying 1xN-Matrix.
      Returns:
      the underlying 1xN-Matrix.
    • matrixTimesThis

      public Vector matrixTimesThis​(Matrix M)
      Matrix-Vektor-Produkt. Liefert neuen Vektor R := Matrix * this. (Fasst den Vektor als Spaltenvektor auf.)
      Parameters:
      M - Matrix
      Returns:
      Liefert R := M * this.
    • get

      public double get​(int i)
      Gets element at index i.
      Parameters:
      i - index.
      Returns:
      element at index i.
    • set

      public void set​(int i, double s)
      Sets element at index i to value s.
      Parameters:
      i - index.
      s - value.
    • plus

      public Vector plus​(Vector B)
      Adds a vector to this vector.
      Parameters:
      B - vector to be added.
      Returns:
      new vector R := this + B
    • plus

      public Vector plus​(double b)
      Adds a constant to every element of this vector.
      Parameters:
      b - constant to be added.
      Returns:
      new vector R := A .+ b
    • plusEquals

      public Vector plusEquals​(Vector B)
      Vector addition in place, this := this + B.
      Parameters:
      B - another vector.
      Returns:
      this := this + B.
    • arrayTimesEquals

      public Vector arrayTimesEquals​(Vector B)
      Element-by-element multiplication in place, this := this .* B.
      Parameters:
      B -
      Returns:
      this := this .* B.
    • minus

      public Vector minus​(Vector B)
      Subtracts a vector from this vector.
      Parameters:
      B - vector to be subtracted.
      Returns:
      new vector R := this - B
    • uminus

      public Vector uminus()
      Unary minus.
      Returns:
      new Vector R := -this.
    • dot

      public double dot​(Vector B)
      dot product, this dot B
      Parameters:
      B -
      Returns:
      s := this dot B.
    • dot

      public static double dot​(double[] a, double[] b)
      dot product, <a,b> (compensates roundoff by Kahan summation).
      Parameters:
      a -
      b -
      Returns:
      s = a dot b.
    • times

      public Vector times​(double s)
      scale this vector by s.
      Parameters:
      s -
      Returns:
      new vector R := s * this.
    • sumAbs

      public double sumAbs()
      Liefert Summe der Absolutwerte aller Vektorelemente. NaN-Werte werden nicht berücksichtigt. Liefert NaN genau dann, wenn _alle_ Elemente NaN sind. (Compensates roundoff by Kahan summation.)
      Returns:
      Sum of absolute values of elements. NaN if all elements are NaN; NaN if length() == 0.
    • squareOfEuclidianLength

      public double squareOfEuclidianLength()
      Länge des Vektors zum Quadrat. Entspricht "v.dot(v)".

      Hinweis: Wenn man die Länge eines 2D-Vektors berechnen will, lieber Math.hypot() statt Math.sqrt(v.squareOfEuclidianLength() nehmen, weil dadurch Überläufe vermieden werden.

      Returns:
      v.dot(v). Liefert NaN, wenn mindestens ein Element NaN ist.
    • maxAbs

      public double maxAbs()
      Maximum der Absolutwerte aller Elemente. NaN-Werte werden nicht berücksichtigt. Liefert NaN genau dann, wenn _alle_ Elemente NaN sind.
      Returns:
    • length

      public int length()
      Length of this vector.
      Returns:
      length of this vector.
    • copy

      public Vector copy()
      Copies this vector.
      Returns:
      copy of this vector.
    • arrayRightDivide

      public Vector arrayRightDivide​(Vector B)
      Elementweise Division: result := this ./ B
      Parameters:
      B -
      Returns:
      R := this ./ B.
    • toTSVString

      public java.lang.String toTSVString()
      als TAB-getrennter String
      Returns:
    • toCSVString

      public java.lang.String toCSVString​(char fieldSeperator)
      Vektor als CSV-String ausgeben.
      Parameters:
      fieldSeperator -
      Returns:
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
      Returns:
      Ruft toTSVString() auf.
    • main

      public static void main​(java.lang.String[] args)