D3tensor.h 6.11 KB
Newer Older
Francois Gygi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2014 The Regents of the University of California
//
// This file is part of Qbox
//
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
// D3tensor.h
//
// double 3x3 tensor
//
////////////////////////////////////////////////////////////////////////////////

#ifndef D3TENSOR_H
#define D3TENSOR_H
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cassert>
#include "blas.h"
#include "D3vector.h"

using namespace std;

class D3tensor
{
  public:

  double a_[9];

  double* a(void) { return &a_[0]; }
  const double* a(void) const { return &a_[0]; }

  explicit D3tensor(void) { clear(); }

  explicit D3tensor(double xx, double yy, double zz)
  { a_[0]=xx; a_[4]=yy; a_[8]=zz; }

  explicit D3tensor(double xx, double yy, double zz,
                    double xy, double yz, double xz, char& uplo)
  {
    a_[0] = xx;
    a_[4] = yy;
    a_[8] = zz;

    if ( uplo == 'l' )
    {
      a_[1] = xy;
      a_[2] = xz;
      a_[5] = yz;
    }
    else if ( uplo == 'u' )
    {
      a_[3] = xy;
      a_[6] = xz;
      a_[7] = yz;
    }
    else if ( uplo == 's' )
    {
      a_[1] = xy;
      a_[2] = xz;
      a_[5] = yz;
      a_[3] = xy;
      a_[6] = xz;
      a_[7] = yz;
    }
    else
      assert(false);
  }

  explicit D3tensor(const D3vector& diag, const D3vector& offdiag)
  {
    a_[0] = diag[0];
    a_[4] = diag[1];
    a_[8] = diag[2];
    a_[1] = offdiag[0];
    a_[5] = offdiag[1];
    a_[2] = offdiag[2];
    a_[3] = offdiag[0];
    a_[7] = offdiag[1];
    a_[6] = offdiag[2];
  }

  explicit D3tensor(const double* a)
  {
    for ( int i = 0; i < 9; i++ ) a_[i] = a[i];
  }

  double& operator[](int i)
  {
    assert(i>=0 && i < 9);
    return a_[i];
  }

  double operator[](int i) const
  {
    assert(i>=0 && i < 9);
    return a_[i];
  }

  void setdiag(int i, double b)
  {
    assert(i>=0 && i<3);
    a_[i*4] = b;
  }

  void setdiag(const D3vector& b)
  {
    for ( int i = 0; i < 3; i++ )
      a_[i*4] = b[i];
  }

  void setoffdiag(int i, double b)
  {
    assert(i>=0 && i<3);
    if ( i == 0 )
    {
      a_[1] = b;
      a_[3] = b;
    }
    else if ( i == 2 )
    {
      a_[2] = b;
      a_[6] = b;
    }
    else
    {
      a_[5] = b;
      a_[7] = b;
    }
  }

  void setoffdiag(const D3vector& b)
  {
    a_[1] = b[0];
    a_[3] = b[0];
    a_[5] = b[1];
    a_[7] = b[1];
    a_[2] = b[2];
    a_[6] = b[2];
  }

  bool operator==(const D3tensor &rhs) const
  {
    bool eq = true;
    for ( int i = 0; i < 9; i++ )
    {
      if ( rhs[i] != a_[i] )
      {
        eq &= false;
      }
    }
    return eq;
  }

  bool operator!=(const D3tensor &rhs) const
  {
    bool neq = false;
    for ( int i = 0; i < 9; i++ )
    {
      if ( rhs[i] != a_[i] )
      {
        neq |= true;
      }
    }
    return neq;
  }

  D3tensor& operator+=(const D3tensor& rhs)
  {
    for ( int i = 0; i < 9; i++ )
      a_[i] += rhs[i];
    return *this;
  }

  D3tensor& operator-=(const D3tensor& rhs)
  {
    for ( int i = 0; i < 9; i++ )
      a_[i] -= rhs[i];
    return *this;
  }

  D3tensor& operator*=(const double& rhs)
  {
    for ( int i = 0; i < 9; i++ )
      a_[i] *= rhs;
    return *this;
  }

  D3tensor& operator/=(const double& rhs)
  {
    for ( int i = 0; i < 9; i++ )
      a_[i] /= rhs;
    return *this;
  }

  friend const D3tensor operator+(const D3tensor& lhs, const D3tensor& rhs)
  {
    return D3tensor(lhs) += rhs;
  }

  friend const D3tensor operator-(const D3tensor& a, const D3tensor& b)
  {
    return D3tensor(a) -= b;
  }

  friend D3tensor operator*(double a, const D3tensor& b)
  {
    return D3tensor(b) *= a;
  }

  friend D3tensor operator*(const D3tensor& a, double b)
  {
    return D3tensor(a) *= b;
  }

  friend D3tensor operator/(const D3tensor& a, double b)
  {
    return D3tensor(a) /= b;
  }

  friend D3tensor operator*(D3tensor& a, D3tensor& b)
  {
    D3tensor c;
    int ithree = 3;
    double one = 1.0, zero = 0.0;
    char t = 'n';
    dgemm ( &t, &t, &ithree, &ithree, &ithree, &one, &a[0], &ithree,
            &b[0], &ithree, &zero, &c[0], &ithree );
    return c;
  }

  friend D3tensor operator-(const D3tensor& a) // unary minus
  {
    return D3tensor()-a;
  }

245
  double norm2(const D3tensor& a) const
Francois Gygi committed
246 247 248 249 250 251
  {
    return a_[0]*a_[0] + a_[1]*a_[1] + a_[2]*a_[2] +
           a_[3]*a_[3] + a_[4]*a_[4] + a_[5]*a_[5] +
           a_[6]*a_[6] + a_[7]*a_[7] + a_[8]*a_[8];
  }

252
  double norm(const D3tensor& a) const
Francois Gygi committed
253 254 255 256
  {
    return sqrt(norm2(a));
  }

257
  double trace(void) const
Francois Gygi committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  {
    return a_[0]+a_[4]+a_[8];
  }

  void traceless(void)
  {
    double b = trace() / 3;
    a_[0] -= b;
    a_[4] -= b;
    a_[8] -= b;
  }

  void clear(void)
  {
    for ( int i = 0; i < 9; i++ )
      a_[i] = 0.0;
  }

  void identity(void)
  {
    clear();
    a_[0] = 1.0;
    a_[4] = 1.0;
    a_[8] = 1.0;
  }

  friend std::ostream& operator<<(std::ostream& os, const D3tensor& rhs)
  {
    const double * const v  = rhs.a();
    os.setf(ios::fixed,ios::floatfield);
    os.setf(ios::right,ios::adjustfield);
    os.precision(8);
Francois Gygi committed
290 291 292 293 294 295
    os << setw(14) << v[0] << " " << setw(14) << v[3] << " " << setw(14) << v[6]
       << "\n"
       << setw(14) << v[1] << " " << setw(14) << v[4] << " " << setw(14) << v[7]
       << "\n"
       << setw(14) << v[2] << " " << setw(14) << v[5] << " " << setw(14) << v[8]
       << "\n";
Francois Gygi committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    return os;
  }

  void syev(char uplo, D3vector& eigval, D3tensor& eigvec)
  {
    double w[3];

    int info;
    char jobz = 'V';
    int lwork=-1;
    double tmplwork;
    int n = 3;

    dsyev(&jobz, &uplo, &n, eigvec.a(), &n, &w[0], &tmplwork, &lwork, &info);

    lwork = (int) tmplwork + 1;
    double* work = new double[lwork];

    eigvec = *this;
    dsyev(&jobz, &uplo, &n, eigvec.a(), &n, &w[0], work, &lwork, &info);
    delete[] work;

    eigval = D3vector(&w[0]);
  }
};
#endif