我有矩陣求逆演算法和用于測驗的 19x19 矩陣。使用 g 在適用于 Linux 的 Windows 子系統中運行它會產生正確的倒矩陣。
在 Windows 上的 Visual Studio 2017 中,它曾經也能正常作業。但是,在升級到 Visual Studio 2019 后,我得到一個包含全零的矩陣。MSVC 壞了還是這里有什么問題?
到目前為止,我已經測驗了用大于/小于公差值替換==0.0/ !=0.0(在某些情況下由于四舍五入可能不安全),但這也不起作用。
預期的結果(我用 g 得到的)是{5.26315784E-2,-1.25313282E-2, ... -1.25000000E-1}. 我使用 Visual Studio 2019 v142、Windows SDK 10.0.19041.0、Release x64 進行編譯,然后我得到{0,0,...0}. 使用 Debug x64 我得到一個不同的(也是錯誤的)結果:所有矩陣條目都是-1.99839720E18. 我使用 C 17 語言標準,/O2 /Oi /Ot /Qpar /arch:AVX2 /fp:fast /fp:except-。我有一個支持 AVX2 的 i7-8700K。
我還在代碼中標記了它在Windows中錯誤回傳的地方。任何幫助是極大的贊賞。
編輯:我剛剛發現錯誤行為的原因是 /arch:AVX2 與 /fp:fast 結合使用。但我不明白為什么。/arch:SSE、/arch:SSE2 和 /arch:AVX 與 /fp:fast 一起正常作業,但 /arch:AVX2 不能正常作業。這里不同的舍入或操作順序如何觸發完全不同的行為?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef unsigned int uint;
void println(const string& s="") {
cout << s << endl;
}
struct floatNxN {
uint N{}; // matrix size is NxN
vector<float> M; // matrix data
floatNxN(const uint N, const float* M) : N {N}, M(N*N) {
for(uint i=0; i<N*N; i ) this->M[i] = M[i];
}
floatNxN(const uint N, const float x=0.0f) : N {N}, M(N*N, x) { // create matrix filled with zeros
}
floatNxN() = default;
~floatNxN() = default;
floatNxN invert() const { // returns inverse matrix
vector<double> A(2*N*N); // calculating intermediate values as double is strictly necessary
for(uint i=0; i<N; i ) {
for(uint j=0; j< N; j ) A[2*N*i j] = (double)M[N*i j];
for(uint j=N; j<2*N; j ) A[2*N*i j] = (double)(i N==j);
}
for(uint k=0; k<N-1; k ) { // at iteration k==2, the content of A is already different in MSVC and g
if(A[2*N*k k]==0.0) {
for(uint i=k 1; i<N; i ) {
if(A[2*N*i k]!=0.0) {
for(uint j=0; j<2*N; j ) {
const double t = A[2*N*k j];
A[2*N*k j] = A[2*N*i j];
A[2*N*i j] = t;
}
break;
} else if(i 1==N) {
return floatNxN(N);
}
}
}
for(uint i=k 1; i<N; i ) {
const double t = A[2*N*i k]/A[2*N*k k];
for(uint j=k; j<2*N; j ) A[2*N*i j] -= A[2*N*k j]*t;
}
}
double det = 1.0;
for(uint k=0; k<N; k ) det *= A[2*N*k k];
if(det==0.0) {
return floatNxN(N);
}
for(int k=N-1; k>0; k--) {
for(int i=k-1; i>=0; i--) {
const double t = A[2*N*i k]/A[2*N*k k];
for(uint j=k; j<2*N; j ) A[2*N*i j] -= A[2*N*k j]*t;
}
}
floatNxN r = floatNxN(N);
for(uint i=0; i<N; i ) {
const double t = A[2*N*i i];
for(uint j=0; j<N; j ) r.M[N*i j] = (float)(A[2*N*i N j]/t);
}
return r;
}
string stringify() const { // converts matrix into string without spaces or newlines
string s = "{" to_string(M[0]);
for(uint i=1; i<N*N; i ) s = "," to_string(M[i]);
return s "}";
}
};
int main() {
const float Md[19*19] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-30,-11,-11,-11,-11,-11,-11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
12, -4, -4, -4, -4, -4, -4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, -1, 0, 0, 0, 0, 1,-1, 1,-1, 0, 0, 1,-1, 1,-1, 0, 0,
0, -4, 4, 0, 0, 0, 0, 1,-1, 1,-1, 0, 0, 1,-1, 1,-1, 0, 0,
0, 0, 0, 1, -1, 0, 0, 1,-1, 0, 0, 1,-1,-1, 1, 0, 0, 1,-1,
0, 0, 0, -4, 4, 0, 0, 1,-1, 0, 0, 1,-1,-1, 1, 0, 0, 1,-1,
0, 0, 0, 0, 0, 1, -1, 0, 0, 1,-1, 1,-1, 0, 0,-1, 1,-1, 1,
0, 0, 0, 0, 0, -4, 4, 0, 0, 1,-1, 1,-1, 0, 0,-1, 1,-1, 1,
0, 2, 2, -1, -1, -1, -1, 1, 1, 1, 1,-2,-2, 1, 1, 1, 1,-2,-2,
0, -4, -4, 2, 2, 2, 2, 1, 1, 1, 1,-2,-2, 1, 1, 1, 1,-2,-2,
0, 0, 0, 1, 1, -1, -1, 1, 1,-1,-1, 0, 0, 1, 1,-1,-1, 0, 0,
0, 0, 0, -2, -2, 2, 2, 1, 1,-1,-1, 0, 0, 1, 1,-1,-1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,-1,-1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,-1,-1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,-1,-1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1,-1,-1, 1, 0, 0, 1,-1,-1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0,-1, 1, 0, 0, 1,-1, 1,-1, 0, 0, 1,-1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1,-1,-1, 1, 0, 0,-1, 1, 1,-1
};
floatNxN M(19, Md);
floatNxN Mm1 = M.invert();
println(Mm1.stringify());
}
uj5u.com熱心網友回復:
似乎是與文字 0 的比較破壞了優化后的演算法。特別是第一個殺死它,因為正確操作的結果需要正好為 0。
通常,與其將浮點值與文字零進行比較,不如將絕對值與非常小的常量進行比較。
這似乎有效:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef unsigned int uint;
void println(const string& s = "") {
cout << s << endl;
}
struct floatNxN {
const double epsilon = 1E-12;
uint N{}; // matrix size is NxN
vector<float> M; // matrix data
floatNxN(const uint N, const float* M) : N{ N }, M(N* N) {
for (uint i = 0; i < N * N; i ) this->M[i] = M[i];
}
floatNxN(const uint N, const float x = 0.0f) : N{ N }, M(N* N, x) { // create matrix filled with zeros
}
floatNxN() = default;
~floatNxN() = default;
floatNxN invert() const { // returns inverse matrix
vector<double> A(2 * N * N); // calculating intermediate values as double is strictly necessary
for (uint i = 0; i < N; i ) {
for (uint j = 0; j < N; j ) A[2 * N * i j] = (double)M[N * i j];
for (uint j = N; j < 2 * N; j ) A[2 * N * i j] = (double)(i N == j);
}
for (uint k = 0; k < N - 1; k ) { // at iteration k==2, the content of A is already different in MSVC and g
if (fabs(A[2 * N * k k]) < epsilon) { // comparing with 0 was the killer here
for (uint i = k 1; i < N; i ) {
if (fabs(A[2 * N * i k]) > epsilon) {
for (uint j = 0; j < 2 * N; j ) {
const double t = A[2 * N * k j];
A[2 * N * k j] = A[2 * N * i j];
A[2 * N * i j] = t;
}
break;
} else if (i 1 == N) {
return floatNxN(N);
}
}
}
for (uint i = k 1; i < N; i ) {
const double t = A[2 * N * i k] / A[2 * N * k k];
for (uint j = k; j < 2 * N; j ) A[2 * N * i j] -= A[2 * N * k j] * t;
}
}
double det = 1.0;
for (uint k = 0; k < N; k ) det *= A[2 * N * k k];
if (fabs(det) < epsilon) {
return floatNxN(N);
}
for (int k = N - 1; k > 0; k--) {
for (int i = k - 1; i >= 0; i--) {
const double t = A[2 * N * i k] / A[2 * N * k k];
for (uint j = k; j < 2 * N; j ) A[2 * N * i j] -= A[2 * N * k j] * t;
}
}
floatNxN r = floatNxN(N);
for (uint i = 0; i < N; i ) {
const double t = A[2 * N * i i];
for (uint j = 0; j < N; j ) r.M[N * i j] = (float)(A[2 * N * i N j] / t);
}
return r;
}
string stringify() const { // converts matrix into string without spaces or newlines
string s = "{" to_string(M[0]);
for (uint i = 1; i < N * N; i ) s = "," to_string(M[i]);
return s "}";
}
};
int main() {
const float Md[19 * 19] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-30,-11,-11,-11,-11,-11,-11, 8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mm1 = M.invert();
println(Mm1.stringify());
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/313900.html
上一篇:ThirdPersonMovement腳本Unity
下一篇:如何修復我的專案中的所有非空警告
