SparseVector< Type > Class Template Reference

Class for a sparse vector. More...

#include <uservector.h>

Inheritance diagram for SparseVector< Type >:
Inheritance graph
[legend]
Collaboration diagram for SparseVector< Type >:
Collaboration graph
[legend]

List of all members.

Classes

struct  VectorElement
 The structure of one element form my vector in my list of elements. More...

Public Member Functions

const VectorElementgethead ()
 SparseVector (int n=0)
 (Standard-)Constructor for optional dimension.
 SparseVector (int n, int i, Type v)
 Constructor for dimension and one element.
 SparseVector (int n, int i, int j, Type vi, Type vj)
 Constructor for dimension and two elements.
 SparseVector (const SparseVector< Type > &v)
 Copy-Constructor for a SparseVector.
 SparseVector (const UserVector< Type > &v)
 Copy-Constructor for a UserVector.
 SparseVector (const UserVector< Type > &v, const int low, const int up)
 Copy-Constructor for a part of a UserVector.
 SparseVector (const UserVector< Type > &v, const UserVector< int > &block)
 Copy-Constructor for a UserVector and a block-description.
 SparseVector (const vector< Pointer< UserVector< Type > > > &v, const vector< DenseVector< int > > &block)
 Copy-Constructor for a vector of UserVectors and a vector of block-describtions.
 SparseVector (const Type *v, const int &n)
 Copy-Constructor for an array of Types.
 SparseVector (const int &n, const int &nz, const int *indices, const double *elements)
Pointer< UserVector< Type > > getemptycopy (int n) const
 Gives an empty UserVector of this type.
Pointer< UserVector< Type > > getemptycopy () const
 Gives an empty UserVector of this type and dimension.
Pointer< UserVector< Type > > getcopy () const
 Gives a copy of this UserVector.
Pointer< UserVector< Type > > getcopy (const UserVector< int > &block) const
 Gives a Pointer to a copy of one block of this UserVector.
 ~SparseVector ()
 Destructor.
int dim () const
 Gives the dimension.
void resize (const int n)
 Resize this vector.
void SetElement (int i, const Type &v, bool test_for_zero)
 Sets one element of this vector.
void SetElement (const int i, const Type &v)
 Sets one element of this UserVector.
void DelElement (int i)
 Deletes one element of this vector.
Type & operator[] (const int i)
 Gives a reference to one component of this UserVector.
Type operator[] (const int i) const
 Gives one element of this UserVector.
Type operator() (const int i) const
 Gives a copy of one component of this UserVector.
UserVector< Type > & operator= (const UserVector< Type > &v)
SparseVector< Type > & operator= (const SparseVector< Type > &v)
 Assign-Operator for a SparseVector.
UserVector< Type > & operator= (const Type &v)
 Assign-Operator for a value from type Type.
void set (const Type *v, int n)
 Assign-Operator for a Type*.
void set (const Type *v)
 Sets this UserVector to the values of v.
UserVector< Type > & AddMult (Type a, const UserVector< Type > &v)
UserVector< Type > & operator+= (const UserVector< Type > &v)
SparseVector< Type > & AddMult (Type a, const SparseVector< Type > &v)
 Adds a UserVector, multiplied by a Type, to this UserVector.
SparseVector< Type > & operator+= (const SparseVector< Type > &v)
 Adds a SparseVector to this one.
UserVector< Type > & operator-= (const UserVector< Type > &v)
SparseVector< Type > & operator-= (const SparseVector< Type > &v)
 Substracts a SparseVector to this one.
UserVector< Type > & operator*= (const Type &v)
 Multiplies all components with a value from type Type.
UserVector< Type > & operator/= (const Type &v)
 Divides all components through a value from type Type.
DenseVector< Type > operator* (const Type &v) const
 Computes the product of this UserVector and a value from type v.
bool operator>= (const Type &v) const
 Compares each element of this UserVector with a value of type Type.
bool operator== (const Type &v) const
 Compares each element of this UserVector with a value of type Type.
bool operator== (const UserVector< Type > &v) const
Type operator* (const UserVector< Type > &v) const
Type operator* (const SparseVector< Type > &v) const
 Computes the scalar product for this and another SparseVector.
SparseVector< Type > operator+ (const SparseVector< Type > &v) const
 Computes the sum of this SparseVector and another one.
SparseVector< Type > operator- (const SparseVector< Type > &v) const
 Computes the difference of this SparseVector and another one.
void diagmult (SparseVector< Type > y, const SparseVector< Type > &v) const
 Computes the diagonal multiplication of this SparseVector and another one.
void diagmult (UserVector< Type > &y, const UserVector< Type > &v) const
void set_block (const UserVector< Type > &v, const UserVector< int > &block)
 operator const Pointer< Type > () const
 Cast-Operator to a const Pointer of Type.
Type sq_norm2 () const
 Square of 2-Norm.
void print (ostream &out) const
 Prints this UserVector.

Static Public Member Functions

static Pointer< SparseVector
< Type > > 
random (const int n, const Type lb, const Type ub)
 Gives a random SparseVector.
static Pointer< SparseVector
< Type > > 
random (const int n, const Type lb, const Type ub, const double sparsity)
 Gives a random SparseVector with specified sparsity.

Protected Member Functions

VectorElementGetElement (const int i, bool create_if_not_exists=true)
 Get an element from this vector.
void clear (VectorElement *v)
 Clears all elements, starting from the next one.

Protected Attributes

VectorElementhead
 A pointer to the head of my list of elements, which is no element itselfe.
VectorElementlast
 The last element from my list.
int dim_
 The dimenion.

Friends

class SparseMatrix
class IntervalVector
SparseVector< Type > operator* (const Type &v1, const SparseVector< Type > &v2)
 Computes the product of a Type and a SparseVector.

Detailed Description

template<class Type>
class SparseVector< Type >

Class for a sparse vector.

The elements are stored in a simple list sorted by the indices.

Definition at line 814 of file uservector.h.


Constructor & Destructor Documentation

template<class Type>
SparseVector< Type >::SparseVector ( int  n = 0  )  [inline]

(Standard-)Constructor for optional dimension.

Parameters:
n The dimension, default is 0.

Definition at line 922 of file uservector.h.

template<class Type>
SparseVector< Type >::SparseVector ( int  n,
int  i,
Type  v 
) [inline]

Constructor for dimension and one element.

Parameters:
n The dimension.
i The index of the element.
v The value of the element.

Definition at line 933 of file uservector.h.

template<class Type>
SparseVector< Type >::SparseVector ( int  n,
int  i,
int  j,
Type  vi,
Type  vj 
) [inline]

Constructor for dimension and two elements.

Parameters:
n The dimension.
i First index.
j Second index.
vi First value.
vj Second value.

Definition at line 947 of file uservector.h.

template<class Type>
SparseVector< Type >::SparseVector ( const SparseVector< Type > &  v  )  [inline]

Copy-Constructor for a SparseVector.

Parameters:
v The SparseVector to copy.

Definition at line 958 of file uservector.h.

template<class Type>
SparseVector< Type >::SparseVector ( const UserVector< Type > &  v  )  [inline]

Copy-Constructor for a UserVector.

Parameters:
v The UserVector to copy.

Definition at line 975 of file uservector.h.

template<class Type>
SparseVector< Type >::SparseVector ( const UserVector< Type > &  v,
const int  low,
const int  up 
) [inline]

Copy-Constructor for a part of a UserVector.

Parameters:
v The UserVector to copy the part [low,up] from.
low The lower index of the range of elements to copy.
up The upper index.

Definition at line 995 of file uservector.h.

template<class Type>
SparseVector< Type >::SparseVector ( const UserVector< Type > &  v,
const UserVector< int > &  block 
) [inline]

Copy-Constructor for a UserVector and a block-description.

Parameters:
v The UserVector to take one the block from.
block The block-describtion.

Definition at line 1014 of file uservector.h.

template<class Type>
SparseVector< Type >::SparseVector ( const vector< Pointer< UserVector< Type > > > &  v,
const vector< DenseVector< int > > &  block 
) [inline]

Copy-Constructor for a vector of UserVectors and a vector of block-describtions.

Parameters:
v The vector of UserVectors.
block The block-describtion.

Definition at line 1024 of file uservector.h.

template<class Type>
SparseVector< Type >::SparseVector ( const Type *  v,
const int &  n 
) [inline]

Copy-Constructor for an array of Types.

Parameters:
v The array of Types.
n The length of v.

Definition at line 1037 of file uservector.h.

template<class Type>
SparseVector< Type >::SparseVector ( const int &  n,
const int &  nz,
const int *  indices,
const double *  elements 
) [inline]

Definition at line 1050 of file uservector.h.

template<class Type>
SparseVector< Type >::~SparseVector (  )  [inline]

Destructor.

Deletes the list by calling clear(head).

See also:
clear(VectorElement*)

Definition at line 1083 of file uservector.h.


Member Function Documentation

template<class Type>
static Pointer<SparseVector<Type> > SparseVector< Type >::random ( const int  n,
const Type  lb,
const Type  ub 
) [inline, static]

Gives a random SparseVector.

Parameters:
n The dimension.
lb The lower bound.
ub The upper bound.

Definition at line 823 of file uservector.h.

template<class Type>
static Pointer<SparseVector<Type> > SparseVector< Type >::random ( const int  n,
const Type  lb,
const Type  ub,
const double  sparsity 
) [inline, static]

Gives a random SparseVector with specified sparsity.

Parameters:
n The random DenseVector.
lb The lower bound.
ub The upper bound.
sparsity The sparsity, between 0 and 1.

Definition at line 835 of file uservector.h.

template<class Type>
VectorElement* SparseVector< Type >::GetElement ( const int  i,
bool  create_if_not_exists = true 
) [inline, protected]

Get an element from this vector.

Checks first, if it's behind the last element. If not, starts to search for the right place at the beginning.

Parameters:
i The index of the element to set.
create_if_not_exists If true (default), a new element will be added, when no element i existed.
Returns:
A pointer to the of the i-th element or NULL if create_if_not_exists is false and the element didn't exist.

Definition at line 874 of file uservector.h.

template<class Type>
void SparseVector< Type >::clear ( VectorElement v  )  [inline, protected]

Clears all elements, starting from the next one.

Deletes v->next and all elements, which comes later in this list. Sets last to v.

Parameters:
v The VectorElement, where to start to delete the next elements.

Definition at line 909 of file uservector.h.

template<class Type>
const VectorElement* SparseVector< Type >::gethead (  )  [inline]

Definition at line 917 of file uservector.h.

template<class Type>
Pointer<UserVector<Type> > SparseVector< Type >::getemptycopy ( int  n  )  const [inline, virtual]

Gives an empty UserVector of this type.

Abstract.

Parameters:
n The size of the new UserVector.
Returns:
A Pointer to a new UserVector with size n.

Implements UserVector< Type >.

Definition at line 1063 of file uservector.h.

template<class Type>
Pointer<UserVector<Type> > SparseVector< Type >::getemptycopy (  )  const [inline, virtual]

Gives an empty UserVector of this type and dimension.

Returns:
A Pointer to a new UserVector with the size of this one.
See also:
getemptycopy(int)

Reimplemented from UserVector< Type >.

Definition at line 1067 of file uservector.h.

template<class Type>
Pointer<UserVector<Type> > SparseVector< Type >::getcopy (  )  const [inline, virtual]

Gives a copy of this UserVector.

Returns:
A Pointer to a new UserVector, which is a copy of this one.

Reimplemented from UserVector< Type >.

Definition at line 1071 of file uservector.h.

template<class Type>
Pointer<UserVector<Type> > SparseVector< Type >::getcopy ( const UserVector< int > &  block  )  const [inline, virtual]

Gives a Pointer to a copy of one block of this UserVector.

Parameters:
block The block-describtion.
Returns:
A Pointer to a copy of the block, described in block.

Reimplemented from UserVector< Type >.

Definition at line 1075 of file uservector.h.

template<class Type>
int SparseVector< Type >::dim (  )  const [inline, virtual]

Gives the dimension.

Returns:
The dimension dim_.
See also:
resize(int)

Implements UserVector< Type >.

Definition at line 1092 of file uservector.h.

template<class Type>
void SparseVector< Type >::resize ( const int  n  )  [inline, virtual]

Resize this vector.

Deletes all elements with index greater equal n.

Parameters:
n The new dimension.
See also:
dim()

Reimplemented from UserVector< Type >.

Definition at line 1099 of file uservector.h.

template<class Type>
void SparseVector< Type >::SetElement ( int  i,
const Type &  v,
bool  test_for_zero 
) [inline]

Sets one element of this vector.

If the element isn't existing, it's created. If the element exists and set to zero and test_for_zero is true, it's deleted.

Parameters:
i The index of the element to set.
v The value to set.
test_for_zero If true, a new element is only created, if it's not zero.

Definition at line 1118 of file uservector.h.

template<class Type>
void SparseVector< Type >::SetElement ( const int  i,
const Type &  v 
) [inline, virtual]

Sets one element of this UserVector.

Parameters:
i The index of the element.
v The value to set.
See also:
operator[](const int i)

Reimplemented from UserVector< Type >.

Definition at line 1126 of file uservector.h.

template<class Type>
void SparseVector< Type >::DelElement ( int  i  )  [inline]

Deletes one element of this vector.

Parameters:
i The index of the element to delete.

Definition at line 1131 of file uservector.h.

template<class Type>
Type& SparseVector< Type >::operator[] ( const int  i  )  [inline, virtual]

Gives a reference to one component of this UserVector.

Abstract. Use operator(int), when you want to read a component only!

Parameters:
i The index of the element.
Returns:
A reference to the component at the i-th index.
See also:
operator[](const int)

Implements UserVector< Type >.

Definition at line 1141 of file uservector.h.

template<class Type>
Type SparseVector< Type >::operator[] ( const int  i  )  const [inline, virtual]

Gives one element of this UserVector.

Parameters:
i The index of the element.
Returns:
The element at index i, not a reference to it.
See also:
operator[](int)

Reimplemented from UserVector< Type >.

Definition at line 1145 of file uservector.h.

template<class Type>
Type SparseVector< Type >::operator() ( const int  i  )  const [inline, virtual]

Gives a copy of one component of this UserVector.

Use this one, when you want to read a component only! Abstract

Parameters:
i The index of the element.
Returns:
A copy of the the component at the i-th index.
See also:
operator[](const int)

Implements UserVector< Type >.

Definition at line 1149 of file uservector.h.

template<class Type>
UserVector<Type>& SparseVector< Type >::operator= ( const UserVector< Type > &  v  )  [inline]

Definition at line 1159 of file uservector.h.

template<class Type>
SparseVector<Type>& SparseVector< Type >::operator= ( const SparseVector< Type > &  v  )  [inline, virtual]

Assign-Operator for a SparseVector.

Parameters:
v The SparseVector to assign.
Returns:
This SparseVector.

Implements UserVector< Type >.

Definition at line 1169 of file uservector.h.

template<class Type>
UserVector<Type>& SparseVector< Type >::operator= ( const Type &  v  )  [inline, virtual]

Assign-Operator for a value from type Type.

Abstract.

Parameters:
v The value to assign to all elements of this vector.
Returns:
This UserVector.

Implements UserVector< Type >.

Definition at line 1209 of file uservector.h.

template<class Type>
void SparseVector< Type >::set ( const Type *  v,
int  n 
) [inline, virtual]

Assign-Operator for a Type*.

Parameters:
v The Type*.
n The length of v.
Returns:
This UserVector.

Reimplemented from UserVector< Type >.

Definition at line 1218 of file uservector.h.

template<class Type>
void SparseVector< Type >::set ( const Type *  v  )  [inline]

Sets this UserVector to the values of v.

Be careful with this one. v needs to have the length dim().

Parameters:
v The Type*.
Returns:
This UserVector.

Reimplemented from UserVector< Type >.

Definition at line 1231 of file uservector.h.

template<class Type>
UserVector<Type>& SparseVector< Type >::AddMult ( Type  a,
const UserVector< Type > &  v 
) [inline]

Definition at line 1233 of file uservector.h.

template<class Type>
UserVector<Type>& SparseVector< Type >::operator+= ( const UserVector< Type > &  v  )  [inline]

Definition at line 1270 of file uservector.h.

template<class Type>
SparseVector<Type>& SparseVector< Type >::AddMult ( Type  a,
const SparseVector< Type > &  v 
) [inline, virtual]

Adds a UserVector, multiplied by a Type, to this UserVector.

Parameters:
a The Type to multiply v with.
v The UserVector to add, after multiplication with a.
Returns:
This UserVector

Reimplemented from UserVector< Type >.

Definition at line 1307 of file uservector.h.

template<class Type>
SparseVector<Type>& SparseVector< Type >::operator+= ( const SparseVector< Type > &  v  )  [inline, virtual]

Adds a SparseVector to this one.

Parameters:
v The SparseVector to add.
Returns:
This SparseVector.

Implements UserVector< Type >.

Definition at line 1340 of file uservector.h.

template<class Type>
UserVector<Type>& SparseVector< Type >::operator-= ( const UserVector< Type > &  v  )  [inline]

Definition at line 1369 of file uservector.h.

template<class Type>
SparseVector<Type>& SparseVector< Type >::operator-= ( const SparseVector< Type > &  v  )  [inline, virtual]

Substracts a SparseVector to this one.

Parameters:
v The SparseVector to substract.

Implements UserVector< Type >.

Definition at line 1409 of file uservector.h.

template<class Type>
UserVector<Type>& SparseVector< Type >::operator*= ( const Type &  v  )  [inline, virtual]

Multiplies all components with a value from type Type.

Abstract.

Parameters:
v The value to multiply with.
Returns:
This UserVector.

Implements UserVector< Type >.

Definition at line 1438 of file uservector.h.

template<class Type>
UserVector<Type>& SparseVector< Type >::operator/= ( const Type &  v  )  [inline, virtual]

Divides all components through a value from type Type.

Abstract.

Parameters:
v The value to divide.
Returns:
This UserVector.

Implements UserVector< Type >.

Definition at line 1448 of file uservector.h.

template<class Type>
DenseVector<Type> SparseVector< Type >::operator* ( const Type &  v  )  const [inline, virtual]

Computes the product of this UserVector and a value from type v.

Parameters:
v The value to multiply with.
Returns:
A new DenseVector, which is this one multiplied by v.

Reimplemented from UserVector< Type >.

Definition at line 1453 of file uservector.h.

template<class Type>
bool SparseVector< Type >::operator>= ( const Type &  v  )  const [inline, virtual]

Compares each element of this UserVector with a value of type Type.

Parameters:
v The value to compare with.
Returns:
True, if all elements are greater-equal v. False else.

Reimplemented from UserVector< Type >.

Definition at line 1459 of file uservector.h.

template<class Type>
bool SparseVector< Type >::operator== ( const Type &  v  )  const [inline, virtual]

Compares each element of this UserVector with a value of type Type.

Parameters:
v The value to compare with.
Returns:
True, if all elements are equal to v. False else.

Reimplemented from UserVector< Type >.

Definition at line 1465 of file uservector.h.

template<class Type>
bool SparseVector< Type >::operator== ( const UserVector< Type > &  v  )  const [inline]

Definition at line 1472 of file uservector.h.

template<class Type>
Type SparseVector< Type >::operator* ( const UserVector< Type > &  v  )  const [inline]

Definition at line 1489 of file uservector.h.

template<class Type>
Type SparseVector< Type >::operator* ( const SparseVector< Type > &  v  )  const [inline, virtual]

Computes the scalar product for this and another SparseVector.

Parameters:
v The sparse vector to multiply with.
Returns:
The scalar product of this vector and the other one.

Reimplemented from UserVector< Type >.

Definition at line 1501 of file uservector.h.

template<class Type>
SparseVector<Type> SparseVector< Type >::operator+ ( const SparseVector< Type > &  v  )  const [inline, virtual]

Computes the sum of this SparseVector and another one.

Parameters:
v The SparseVector to add.
Returns:
A new SparseVector, which is this one plus the given one.

Reimplemented from UserVector< Type >.

Definition at line 1530 of file uservector.h.

template<class Type>
SparseVector<Type> SparseVector< Type >::operator- ( const SparseVector< Type > &  v  )  const [inline, virtual]

Computes the difference of this SparseVector and another one.

Parameters:
v The SparseVector to substract.
Returns:
The difference as SparseVector.

Reimplemented from UserVector< Type >.

Definition at line 1567 of file uservector.h.

template<class Type>
void SparseVector< Type >::diagmult ( SparseVector< Type >  y,
const SparseVector< Type > &  v 
) const [inline]

Computes the diagonal multiplication of this SparseVector and another one.

Parameters:
v The SparseVector to multiply with.
Returns:
The diagonal product.

Definition at line 1610 of file uservector.h.

template<class Type>
void SparseVector< Type >::diagmult ( UserVector< Type > &  y,
const UserVector< Type > &  v 
) const [inline]

Definition at line 1630 of file uservector.h.

template<class Type>
void SparseVector< Type >::set_block ( const UserVector< Type > &  v,
const UserVector< int > &  block 
) [inline]

Definition at line 1639 of file uservector.h.

template<class Type>
SparseVector< Type >::operator const Pointer< Type > (  )  const [inline, virtual]

Cast-Operator to a const Pointer of Type.

Implements UserVector< Type >.

Definition at line 1643 of file uservector.h.

template<class Type>
Type SparseVector< Type >::sq_norm2 (  )  const [inline, virtual]

Square of 2-Norm.

Returns:
The square of the 2-norm of this UserVector.

Reimplemented from UserVector< Type >.

Definition at line 1654 of file uservector.h.

template<class Type>
void SparseVector< Type >::print ( ostream &  out  )  const [inline, virtual]

Prints this UserVector.

Prints all elements of this UserVector.

Parameters:
out The ostream to print to.

Reimplemented from UserVector< Type >.

Definition at line 1660 of file uservector.h.


Friends And Related Function Documentation

template<class Type>
friend class SparseMatrix [friend]

Definition at line 815 of file uservector.h.

template<class Type>
friend class IntervalVector [friend]

Definition at line 816 of file uservector.h.

template<class Type>
SparseVector<Type> operator* ( const Type &  v1,
const SparseVector< Type > &  v2 
) [friend]

Computes the product of a Type and a SparseVector.

Parameters:
v1 The first factor as Type.
v2 The second factor as SparseVector.
Returns:
The product v1*v2.

Reimplemented from UserVector< Type >.

Definition at line 1519 of file uservector.h.


Member Data Documentation

template<class Type>
VectorElement* SparseVector< Type >::head [protected]

A pointer to the head of my list of elements, which is no element itselfe.

Definition at line 858 of file uservector.h.

template<class Type>
VectorElement* SparseVector< Type >::last [protected]

The last element from my list.

Definition at line 861 of file uservector.h.

template<class Type>
int SparseVector< Type >::dim_ [protected]

The dimenion.

Definition at line 865 of file uservector.h.


The documentation for this class was generated from the following file:

Generated on 10 Mar 2013 for LaGO by  doxygen 1.6.1