qpoint.cpp

来自「算断裂的」· C++ 代码 · 共 99 行

CPP
99
字号
// ------------------------------------------------------------------// qpoint.cpp//// This file contains routines for math with points in R^2 and R^3// ------------------------------------------------------------------// Copyright (c) 1999 by Cornell University.  All rights reserved.// // See the accompanying file 'Copyright' for authorship information,// the terms of the license governing this software, and disclaimers// concerning this software.// ------------------------------------------------------------------// This file is part of the QMG software.  // Version 2.0 of QMG, release date September 3, 1999// ------------------------------------------------------------------#include "qpoint.h"// L2 normalization of a vector.void QMG::Point::normalize() {  Real s = 0.0;  {    for (int i = 0; i < di_; ++i)      s += coords_[i] * coords_[i];  }    s = sqrt(s);  {    for (int i = 0; i < di_; ++i)      coords_[i] /= s;  }}QMG::Real QMG::Point::l2norm() const {  Real returnval = 0;  for (int i = 0; i < di_; ++i)    returnval += coords_[i] * coords_[i];  return sqrt(returnval);}QMG::Point QMG::Point::cross_product(const Point& a, const Point& b) {  Point returnval;  returnval[0] = (a.coords_[1] * b.coords_[2] - a.coords_[2] * b.coords_[1]);  returnval[1] = (a.coords_[2] * b.coords_[0] - a.coords_[0] * b.coords_[2]);  returnval[2] = (a.coords_[0] * b.coords_[1] - a.coords_[1] * b.coords_[0]);  return returnval;}QMG::Point QMG::Point::subtract(const Point& a, const Point& b) {  Point returnval;  for (int i = 0; i < di_; ++i)    returnval[i] = a.coords_[i] - b.coords_[i];  return returnval;}QMG::ostream& operator<<(QMG::ostream& os, const QMG::Point& pt) {  for (int i = 0; i < pt.embedded_dim(); ++i) {    os << pt[i] << " ";  }  return os;}QMG::Real QMG::Point::inner_product(const Point& a, const Point& b) {  Real ip = 0;  for (int i = 0; i < di_; ++i)    ip += a.coords_[i] * b.coords_[i];  return ip;}QMG::Real QMG::Point::l1dist(const Point& a, const Point& b) {  Real d = 0;  for (int i = 0; i < di_; ++i)    d += fabs(a.coords_[i] - b.coords_[i]);  return d;}QMG::Real QMG::Point::l2dist(const Point& a, const Point& b, int dim) {  Real d = 0;  Real t;  for (int i = 0; i < dim; ++i) {    t = a.coords_[i] - b.coords_[i];    d += t * t;  }  return sqrt(d);}int QMG::Point::di_;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?