chiark / gitweb /
d5b8d7bb68dd83976aa230f2324de2006dd6afe1
[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
29  public:
30   int      len;       // size of array
31   double*  elements;  // array of values
32
33   RVector() ;
34   RVector(int);       // Constructor
35   RVector(RCRVector); // copy constructor
36   ~RVector() { delete[] elements; elements=0 ; len=0; }
37
38   RCRVector operator=(double) ;
39   RCRVector operator=(RCRVector);
40
41   double & operator () (int i) const {return elements[i] ; }
42   double nrm2() ; // Euclidian norm
43
44   double *raw_data() { return elements; }
45   const double *raw_data_const() const { return elements; }
46
47   friend ostream & operator << (ostream &, const RVector &);
48
49   friend double norm2(RCRVector) ;
50   friend double normInf(RCRVector) ;
51   friend double dot(RCRVector, RCRVector) ;
52   friend void scal(double, RCRVector) ;
53   friend void copy(RCRVector, RCRVector) ;
54   friend void axpy(double, RCRVector, RCRVector) ;
55   friend void gemv(char,double, RCRMatrix, RCRVector, double, RCRVector);
56   friend void ger(double alpha, RCRVector, RCRVector, RCRMatrix);
57
58   int GetLength() const { return len; }; // get vector size
59 };
60
61 /******************* Class RMatrix *************************/
62
63 class RMatrix
64 {
65  protected:
66   double*  Vals; // array of values
67   int       Dim; // dimension
68
69  public:
70    RMatrix() ;
71    RMatrix(int); // dimension
72   ~RMatrix() { delete[] Vals;  Vals=0 ; Dim=0; }
73  
74   RMatrix(RCRMatrix); // copy constructor
75   RCRMatrix operator=(double num) ;
76   RCRMatrix operator=(RCRMatrix) ; // (needed for template stuff)
77
78   double& operator()(int vidx,int hidx) ;
79   friend ostream & operator << (ostream &, const RMatrix &);
80
81   friend void gemv(char,double, RCRMatrix, RCRVector, double, RCRVector);
82   friend void ger(double alpha,RCRVector,RCRVector,RCRMatrix);
83
84   int       GetDim() { return Dim; }; // get dimension
85 };
86
87 #endif