JDFTx  1.2.0
scaled.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------
2 Copyright 2012 Ravishankar Sundararaman
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx. If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #ifndef JDFTX_CORE_SCALED_H
21 #define JDFTX_CORE_SCALED_H
22 
24 
25 template<typename T> struct scaled
26 { const T& data;
27  double scale;
28  scaled(const T& data, double scale=1.0) : data(data), scale(scale) {}
29  operator T() const { T ret(data); return ret *= scale; }
30  scaled<T>& operator*=(double s) { scale *= s; return *this; }
31 };
32 
33 template<typename T> T& operator+=(T& y, const scaled<T>& x) { if(y) axpy(x.scale, x.data, y); else y = x.scale * x.data; return y; }
34 template<typename T> T& operator-=(T& y, const scaled<T>& x) { if(y) axpy(-x.scale, x.data, y); else y = (-x.scale) * x.data; return y; }
35 template<typename T> T operator+(const scaled<T>& x, const scaled<T>& y) { T ret(x); ret += y; return ret; }
36 template<typename T> T operator-(const scaled<T>& x, const scaled<T>& y) { T ret(x); ret -= y; return ret; }
37 
38 template<typename T> scaled<T> operator-(const scaled<T>& x) { return scaled<T>(x.data, -x.scale); }
39 template<typename T> scaled<T> operator*(double s, const scaled<T>& x) { return scaled<T>(x.data, x.scale * s); }
40 template<typename T> scaled<T> operator*(const scaled<T>& x, double s) { return scaled<T>(x.data, x.scale * s); }
41 
42 #endif
Definition: scaled.h:25
Tptr & operator-=(Tptr &in, const Tptr &other)
Decrement.
Definition: Operators.h:170
Tptr operator+(const Tptr &in1, const Tptr &in2)
Add (preserve inputs)
Definition: Operators.h:171
void axpy(double alpha, const Tptr &X, Tptr &Y)
Generic axpy for complex data types (Note: null pointers are treated as zero)
Definition: Operators.h:158
Tptr & operator+=(Tptr &in, const Tptr &other)
Increment.
Definition: Operators.h:169
Tptr operator-(const Tptr &in1, const Tptr &in2)
Subtract (preserve inputs)
Definition: Operators.h:175