#include #include #include #include bool equalFloat(float x, float y){ // return (fabs(x - y) < fabs(1e-8 * min(fabs(x), fabs(y)))); return (fabs(x - y) <= fabs(1e-5 * min(fabs(x), fabs(y)))); // return false; } class myMatrix{ // private: public: int rowN, colN; float *data; myMatrix(int h, int w) :rowN(h), colN(w) { data = (float*)malloc(h * w * sizeof(float)); } ~myMatrix(){ free(data); } void print(){ for(int i = 0;i < rowN;i++){ for(int j = 0;j < colN;j++){ printf("%.3f ", data[i * colN + j]); } printf("\n"); } printf("===========================\n"); } void initializeRandom(){ // srand(0); for(int i = 0;i < rowN * colN;i++){ data[i] = rand() % 0xffff / 1000.0; } } void transpose(){ float *tp = (float*)malloc(rowN * colN * sizeof(float)); for(int i = 0;i < rowN;i++){ for(int j = 0;j < colN;j++){ tp[i + j * rowN] = data[i * colN + j]; } } free(data); data = tp; std::swap(rowN, colN); } bool operator==(const myMatrix &B){ bool flag = true; int diffCount = 0; if(rowN != B.rowN || colN != B.colN) return false; for(int i = 0;i < rowN;i++) { for(int j = 0;j < colN;j++){ if(!equalFloat(data[i * colN + j], B.data[i * colN + j])){ printf("%d %d Left: %f, Right: %f\n", i, j, data[i * colN + j], B.data[i * colN + j]); diffCount++; flag = false; break; } if (!flag) break; } if(!flag) break; } printf("diffCount: %d\n", diffCount); return flag; } };