Dhairya Malhotra 7 yıl önce
ebeveyn
işleme
da52ac5165
4 değiştirilmiş dosya ile 245 ekleme ve 24 silme
  1. 4 0
      include/sctl/matrix.hpp
  2. 14 0
      include/sctl/matrix.txx
  3. 51 3
      include/sctl/vector.hpp
  4. 176 21
      include/sctl/vector.txx

+ 4 - 0
include/sctl/matrix.hpp

@@ -70,6 +70,10 @@ template <class ValueType> class Matrix {
 
   Matrix<ValueType>& operator-=(ValueType s);
 
+  Matrix<ValueType>& operator*=(ValueType s);
+
+  Matrix<ValueType>& operator/=(ValueType s);
+
   Matrix<ValueType> operator+(ValueType s) const;
 
   Matrix<ValueType> operator-(ValueType s) const;

+ 14 - 0
include/sctl/matrix.txx

@@ -299,6 +299,20 @@ template <class ValueType> Matrix<ValueType>& Matrix<ValueType>::operator-=(Valu
   return *this;
 }
 
+template <class ValueType> Matrix<ValueType>& Matrix<ValueType>::operator*=(ValueType s) {
+  Long N = dim[0] * dim[1];
+  for (Long i = 0; i < N; i++) data_ptr[i] *= s;
+  Profile::Add_FLOP(N);
+  return *this;
+}
+
+template <class ValueType> Matrix<ValueType>& Matrix<ValueType>::operator/=(ValueType s) {
+  Long N = dim[0] * dim[1];
+  for (Long i = 0; i < N; i++) data_ptr[i] /= s;
+  Profile::Add_FLOP(N);
+  return *this;
+}
+
 template <class ValueType> Matrix<ValueType> Matrix<ValueType>::operator+(ValueType s) const {
   Long N = dim[0] * dim[1];
   Matrix<ValueType> M_r(dim(0), dim(1));

+ 51 - 3
include/sctl/vector.hpp

@@ -44,14 +44,54 @@ template <class ValueType> class Vector {
 
   void PushBack(const ValueType& x);
 
-  Vector& operator=(const Vector& V);
-
-  Vector& operator=(const std::vector<ValueType>& V);
+  // Element access
 
   ValueType& operator[](Long j);
 
   const ValueType& operator[](Long j) const;
 
+  // Vector-Vector operations
+
+  Vector& operator=(const std::vector<ValueType>& V);
+
+  Vector& operator=(const Vector& V);
+
+  Vector& operator+=(const Vector& V);
+
+  Vector& operator-=(const Vector& V);
+
+  Vector& operator*=(const Vector& V);
+
+  Vector& operator/=(const Vector& V);
+
+  Vector operator+(const Vector& V) const ;
+
+  Vector operator-(const Vector& V) const ;
+
+  Vector operator*(const Vector& V) const ;
+
+  Vector operator/(const Vector& V) const ;
+
+  // Vector-Scalar operations
+
+  Vector& operator=(ValueType s);
+
+  Vector& operator+=(ValueType s);
+
+  Vector& operator-=(ValueType s);
+
+  Vector& operator*=(ValueType s);
+
+  Vector& operator/=(ValueType s);
+
+  Vector operator+(ValueType s) const ;
+
+  Vector operator-(ValueType s) const ;
+
+  Vector operator*(ValueType s) const ;
+
+  Vector operator/(ValueType s) const ;
+
  private:
   Long dim;
   Long capacity;
@@ -59,6 +99,14 @@ template <class ValueType> class Vector {
   bool own_data;
 };
 
+template <class ValueType> Vector<ValueType> operator+(ValueType s, const Vector<ValueType>& V);
+
+template <class ValueType> Vector<ValueType> operator-(ValueType s, const Vector<ValueType>& V);
+
+template <class ValueType> Vector<ValueType> operator*(ValueType s, const Vector<ValueType>& V);
+
+template <class ValueType> Vector<ValueType> operator/(ValueType s, const Vector<ValueType>& V);
+
 template <class ValueType> std::ostream& operator<<(std::ostream& output, const Vector<ValueType>& V);
 
 }  // end namespace

+ 176 - 21
include/sctl/vector.txx

@@ -7,15 +7,6 @@
 
 namespace SCTL_NAMESPACE {
 
-template <class ValueType> std::ostream& operator<<(std::ostream& output, const Vector<ValueType>& V) {
-  std::ios::fmtflags f(std::cout.flags());
-  output << std::fixed << std::setprecision(4) << std::setiosflags(std::ios::left);
-  for (Long i = 0; i < V.Dim(); i++) output << std::setw(10) << V[i] << ' ';
-  output << ";\n";
-  std::cout.flags(f);
-  return output;
-}
-
 template <class ValueType> Vector<ValueType>::Vector() {
   dim = 0;
   capacity = 0;
@@ -159,6 +150,29 @@ template <class ValueType> void Vector<ValueType>::PushBack(const ValueType& x)
   }
 }
 
+// Element access
+
+template <class ValueType> inline ValueType& Vector<ValueType>::operator[](Long j) {
+  assert(j >= 0 && j < dim);
+  return data_ptr[j];
+}
+
+template <class ValueType> inline const ValueType& Vector<ValueType>::operator[](Long j) const {
+  assert(j >= 0 && j < dim);
+  return data_ptr[j];
+}
+
+// Vector-Vector operations
+
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const std::vector<ValueType>& V) {
+  {
+    if (capacity < V.size()) ReInit(V.size());
+    dim = V.size();
+    memcopy(data_ptr, Ptr2ConstItr<ValueType>(&V[0], V.size()), dim);
+  }
+  return *this;
+}
+
 template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const Vector<ValueType>& V) {
   if (this != &V) {
     if (capacity < V.dim) ReInit(V.dim);
@@ -168,23 +182,164 @@ template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const
   return *this;
 }
 
-template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const std::vector<ValueType>& V) {
-  {
-    if (capacity < V.size()) ReInit(V.size());
-    dim = V.size();
-    memcopy(data_ptr, Ptr2ConstItr<ValueType>(&V[0], V.size()), dim);
-  }
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator+=(const Vector<ValueType>& V) {
+  SCTL_ASSERT(V.Dim() == dim);
+  for (Long i = 0; i < dim; i++) data_ptr[i] += V[i];
+  Profile::Add_FLOP(dim);
   return *this;
 }
 
-template <class ValueType> inline ValueType& Vector<ValueType>::operator[](Long j) {
-  assert(j >= 0 && j < dim);
-  return data_ptr[j];
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator-=(const Vector<ValueType>& V) {
+  SCTL_ASSERT(V.Dim() == dim);
+  for (Long i = 0; i < dim; i++) data_ptr[i] -= V[i];
+  Profile::Add_FLOP(dim);
+  return *this;
 }
 
-template <class ValueType> inline const ValueType& Vector<ValueType>::operator[](Long j) const {
-  assert(j >= 0 && j < dim);
-  return data_ptr[j];
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator*=(const Vector<ValueType>& V) {
+  SCTL_ASSERT(V.Dim() == dim);
+  for (Long i = 0; i < dim; i++) data_ptr[i] *= V[i];
+  Profile::Add_FLOP(dim);
+  return *this;
+}
+
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator/=(const Vector<ValueType>& V) {
+  SCTL_ASSERT(V.Dim() == dim);
+  for (Long i = 0; i < dim; i++) data_ptr[i] /= V[i];
+  Profile::Add_FLOP(dim);
+  return *this;
+}
+
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator+(const Vector<ValueType>& V) const {
+  Vector<ValueType> Vr(dim);
+  SCTL_ASSERT(V.Dim() == dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] + V[i];
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator-(const Vector<ValueType>& V) const {
+  Vector<ValueType> Vr(dim);
+  SCTL_ASSERT(V.Dim() == dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] - V[i];
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator*(const Vector<ValueType>& V) const {
+  Vector<ValueType> Vr(dim);
+  SCTL_ASSERT(V.Dim() == dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] * V[i];
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator/(const Vector<ValueType>& V) const {
+  Vector<ValueType> Vr(dim);
+  SCTL_ASSERT(V.Dim() == dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] / V[i];
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+// Vector-Scalar operations
+
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(ValueType s) {
+  for (Long i = 0; i < dim; i++) data_ptr[i] = s;
+  return *this;
+}
+
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator+=(ValueType s) {
+  for (Long i = 0; i < dim; i++) data_ptr[i] += s;
+  Profile::Add_FLOP(dim);
+  return *this;
+}
+
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator-=(ValueType s) {
+  for (Long i = 0; i < dim; i++) data_ptr[i] -= s;
+  Profile::Add_FLOP(dim);
+  return *this;
+}
+
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator*=(ValueType s) {
+  for (Long i = 0; i < dim; i++) data_ptr[i] *= s;
+  Profile::Add_FLOP(dim);
+  return *this;
+}
+
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator/=(ValueType s) {
+  for (Long i = 0; i < dim; i++) data_ptr[i] /= s;
+  Profile::Add_FLOP(dim);
+  return *this;
+}
+
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator+(ValueType s) const {
+  Vector<ValueType> Vr(dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] + s;
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator-(ValueType s) const {
+  Vector<ValueType> Vr(dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] - s;
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator*(ValueType s) const {
+  Vector<ValueType> Vr(dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] * s;
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator/(ValueType s) const {
+  Vector<ValueType> Vr(dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] / s;
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+template <class ValueType> Vector<ValueType> operator+(ValueType s, const Vector<ValueType>& V) {
+  Long dim = V.Dim();
+  Vector<ValueType> Vr(dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = s + V[i];
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+template <class ValueType> Vector<ValueType> operator-(ValueType s, const Vector<ValueType>& V) {
+  Long dim = V.Dim();
+  Vector<ValueType> Vr(dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = s - V[i];
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+template <class ValueType> Vector<ValueType> operator*(ValueType s, const Vector<ValueType>& V) {
+  Long dim = V.Dim();
+  Vector<ValueType> Vr(dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = s * V[i];
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+template <class ValueType> Vector<ValueType> operator/(ValueType s, const Vector<ValueType>& V) {
+  Long dim = V.Dim();
+  Vector<ValueType> Vr(dim);
+  for (Long i = 0; i < dim; i++) Vr[i] = s / V[i];
+  Profile::Add_FLOP(dim);
+  return Vr;
+}
+
+template <class ValueType> std::ostream& operator<<(std::ostream& output, const Vector<ValueType>& V) {
+  std::ios::fmtflags f(std::cout.flags());
+  output << std::fixed << std::setprecision(4) << std::setiosflags(std::ios::left);
+  for (Long i = 0; i < V.Dim(); i++) output << std::setw(10) << V[i] << ' ';
+  output << ";\n";
+  std::cout.flags(f);
+  return output;
 }
 
 }  // end namespace