*ptrData++

本文深入解析了C++中++运算符与*运算符的优先级及结合方式,详细解释了两者组合使用时的执行顺序及实际效果,对于深入理解C++操作符的细节具有重要意义。
由于 ++ 运算符与 * 运算符 是同优先级运算符,并且单目运算符都是自右向左的结合方向,
所以 *ptrData++ 相当于 *(ptrData++),
而由于是后置自增运算,所以先参与运算,再自增,
实际效果为:先取 ptrData 所指的值,然后指针再往后移动
#ifndef MINCO_HPP #define MINCO_HPP #include "gcopter/trajectory.hpp" #include <Eigen/Eigen> #include <cmath> #include <vector> namespace minco { // The banded system class is used for solving // banded linear system Ax=b efficiently. // A is an N*N band matrix with lower band width lowerBw // and upper band width upperBw. // Banded LU factorization has O(N) time complexity. class BandedSystem { public: // The size of A, as well as the lower/upper // banded width p/q are needed inline void create(const int &n, const int &p, const int &q) { // In case of re-creating before destroying destroy(); N = n; lowerBw = p; upperBw = q; int actualSize = N * (lowerBw + upperBw + 1); ptrData = new double[actualSize]; std::fill_n(ptrData, actualSize, 0.0); return; } inline void destroy() { if (ptrData != nullptr) { delete[] ptrData; ptrData = nullptr; } return; } private: int N; int lowerBw; int upperBw; // Compulsory nullptr initialization here double *ptrData = nullptr; public: // Reset the matrix to zero inline void reset(void) { std::fill_n(ptrData, N * (lowerBw + upperBw + 1), 0.0); return; } // The band matrix is stored as suggested in "Matrix Computation" inline const double &operator()(const int &i, const int &j) const { return ptrData[(i - j + upperBw) * N + j]; } inline double &operator()(const int &i, const int &j) { return ptrData[(i - j + upperBw) * N + j]; } // This function conducts banded LU factorization in place // Note that NO PIVOT is applied on the matrix "A" for efficiency!!! inline void factorizeLU() { int iM, jM; double cVl; for (int k = 0; k <= N - 2; k++) { iM = std::min(k + lowerBw, N - 1); cVl = operator()(k, k); for (int i = k + 1; i <= iM; i++) { if (operator()(i, k) != 0.0) { operator()(i, k) /= cVl; } } jM = std::min(k + upperBw, N - 1); for (int j = k + 1; j <= jM; j++) { cVl = operator()(k, j); if (cVl != 0.0) { for (int i = k + 1; i <= iM; i++) { if (operator()(i, k) != 0.0) { operator()(i, j) -= operator()(i, k) * cVl; } } } } } return; } 注释上述代码
09-19
class BandedSystem { public: // The size of A, as well as the lower/upper // banded width p/q are needed inline void create(const int &n, const int &p, const int &q) { // In case of re-creating before destroying destroy(); N = n; lowerBw = p; upperBw = q; int actualSize = N * (lowerBw + upperBw + 1); ptrData = new double[actualSize]; std::fill_n(ptrData, actualSize, 0.0); return; } inline void destroy() { if (ptrData != nullptr) { delete[] ptrData; ptrData = nullptr; } return; } inline void operator=(const BandedSystem &bs) { ptrData = nullptr; create(bs.N, bs.lowerBw, bs.upperBw); memcpy(ptrData, bs.ptrData, N * (lowerBw + upperBw + 1) * sizeof(double)); } private: int N; int lowerBw; int upperBw; // Compulsory nullptr initialization here double *ptrData = nullptr; public: // Reset the matrix to zero inline void reset(void) { std::fill_n(ptrData, N * (lowerBw + upperBw + 1), 0.0); return; } // The band matrix is stored as suggested in "Matrix Computation" inline const double &operator()(const int &i, const int &j) const { return ptrData[(i - j + upperBw) * N + j]; } inline double &operator()(const int &i, const int &j) { return ptrData[(i - j + upperBw) * N + j]; } // This function conducts banded LU factorization in place // Note that NO PIVOT is applied on the matrix "A" for efficiency!!! inline void factorizeLU() { int iM, jM; double cVl; for (int k = 0; k <= N - 2; k++) { iM = std::min(k + lowerBw, N - 1); cVl = operator()(k, k); for (int i = k + 1; i <= iM; i++) { if (operator()(i, k) != 0.0) { operator()(i, k) /= cVl; } } jM = std::min(k + upperBw, N - 1); for (int j = k + 1; j <= jM; j++) { cVl = operator()(k, j); if (cVl != 0.0) { for (int i = k + 1; i <= iM; i++) { if (operator()(i, k) != 0.0) { operator()(i, j) -= operator()(i, k) * cVl; } } } } } return; } // This function solves Ax=b, then stores x in b // The input b is required to be N*m, i.e., // m vectors to be solved. inline void solve(Eigen::MatrixXd &b) const { int iM; for (int j = 0; j <= N - 1; j++) { iM = std::min(j + lowerBw, N - 1); for (int i = j + 1; i <= iM; i++) { if (operator()(i, j) != 0.0) { b.row(i) -= operator()(i, j) * b.row(j); } } } for (int j = N - 1; j >= 0; j--) { b.row(j) /= operator()(j, j); iM = std::max(0, j - upperBw); for (int i = iM; i <= j - 1; i++) { if (operator()(i, j) != 0.0) { b.row(i) -= operator()(i, j) * b.row(j); } } } return; } // This function solves ATx=b, then stores x in b // The input b is required to be N*m, i.e., // m vectors to be solved. inline void solveAdj(Eigen::MatrixXd &b) const { int iM; for (int j = 0; j <= N - 1; j++) { b.row(j) /= operator()(j, j); iM = std::min(j + upperBw, N - 1); for (int i = j + 1; i <= iM; i++) { if (operator()(j, i) != 0.0) { b.row(i) -= operator()(j, i) * b.row(j); } } } for (int j = N - 1; j >= 0; j--) { iM = std::max(0, j - lowerBw); for (int i = iM; i <= j - 1; i++) { if (operator()(j, i) != 0.0) { b.row(i) -= operator()(j, i) * b.row(j); } } } return; } }; 详细注释上述代码
最新发布
11-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值