chiark / gitweb /
use nlopt_stopping in StoGO (currently only for maxevals and maxtime)
[nlopt.git] / stogo / linalg.h
1 /*
2    Temporary implementation of vector and matrix classes
3    This is more or less borrowed from Serguei's program
4 */
5
6 #ifndef LINALG_H
7 #define LINALG_H
8
9 #include <iostream>
10 using namespace std;
11 #include <math.h>         // for sqrt()
12 #include <float.h>
13
14 typedef const class RVector CRVector;
15 typedef CRVector& RCRVector;
16 typedef const class RMatrix CRMatrix ;
17 typedef CRMatrix& RCRMatrix;
18
19 double eps() ;
20
21 #define max(A,B)    ((A) > (B) ? (A):(B))
22 #define min(A,B)    ((A) < (B) ? (A):(B))
23
24 /********************* Class RVector *********************/
25
26 class RVector{
27 protected:
28   double*  elements;  // array of values
29   int      len;       // size of array
30
31  public:
32   RVector() ;
33   RVector(int);       // Constructor
34   RVector(RCRVector); // copy constructor
35   ~RVector() { delete[] elements; elements=0 ; len=0; }
36
37   RCRVector operator=(double) ;
38   RCRVector operator=(RCRVector);
39
40   double & operator () (int i) const {return elements[i] ; }
41   double nrm2() ; // Euclidian norm
42
43   double *raw_data() { return elements; }
44   const double *raw_data_const() const { return elements; }
45
46   friend ostream & operator << (ostream &, const RVector &);
47
48   friend double norm2(RCRVector) ;
49   friend double normInf(RCRVector) ;
50   friend double dot(RCRVector, RCRVector) ;
51   friend void scal(double, RCRVector) ;
52   friend void copy(RCRVector, RCRVector) ;
53   friend void axpy(double, RCRVector, RCRVector) ;
54   friend void gemv(char,double, RCRMatrix, RCRVector, double, RCRVector);
55   friend void ger(double alpha, RCRVector, RCRVector, RCRMatrix);
56
57   int GetLength() const { return len; }; // get vector size
58 };
59
60 /******************* Class RMatrix *************************/
61
62 class RMatrix
63 {
64  protected:
65   double*  Vals; // array of values
66   int       Dim; // dimension
67
68  public:
69    RMatrix() ;
70    RMatrix(int); // dimension
71   ~RMatrix() { delete[] Vals;  Vals=0 ; Dim=0; }
72  
73   RMatrix(RCRMatrix); // copy constructor
74   RCRMatrix operator=(double num) ;
75   RCRMatrix operator=(RCRMatrix) ; // (needed for template stuff)
76
77   double& operator()(int vidx,int hidx) ;
78   friend ostream & operator << (ostream &, const RMatrix &);
79
80   friend void gemv(char,double, RCRMatrix, RCRVector, double, RCRVector);
81   friend void ger(double alpha,RCRVector,RCRVector,RCRMatrix);
82
83   int       GetDim() { return Dim; }; // get dimension
84 };
85
86 #endif