bool GaussElimination::testCorrectnessEndformByBigFloat(vector<vector<BigFloat>>& matrix) { // überprüfung, ob auch wirklich nur in matrix[indexDiagonal][indexDiagonal] == 1 und alles andere 0 ausser letzte spalte for(unsigned int i = 0; i < row; ++i) { if( matrix[i][i].compare( &BigFloat(1.0) ) != 0) return false; for(unsigned int j = 0; j < col; ++j) // zusätzliche spalte (col+1) sollte nicht 0 sein if(matrix[i][j].compare( &BigFloat(0.0), 100 ) != 0 && i !=j ) return false; } return true; }
void GaussElimination::eliminateValuesByZeroInColByBigFloat(vector<vector<BigFloat>>& matrix, int indexCol) { for(unsigned int i = 0; i < row; ++i) { // aktuelle zeile nicht if(i == indexCol) continue; // wenn in entsprechender spalte 0 steht, nicht notwendig if(matrix[i][indexCol].compare( &BigFloat(0.0) ) == 0 ) continue; BigFloat multiplyer = matrix[i][indexCol]; //cout << "Multiplyer Row Number " << i << " = " << multiplyer.getdouble() << endl; for(unsigned int j = 0; j <= col; ++j) //zusätzliche spalte angefügt durch vector { //cout << "Matrix[" << i << "][" << j << "] = " << matrix[i][j].getdouble() << " (values of current row)" << endl; //cout << "Matrix[" << indexCol << "][" << j << "] = " << matrix[indexCol][j].getdouble() << endl; BigFloat temp = multiplyer * matrix[indexCol][j]; //cout << matrix[indexCol][j].getdouble() << " * " << multiplyer.getdouble() << " = " << temp.getdouble() << endl; BigFloat res = matrix[i][j] - temp; //cout << matrix[i][j].getdouble() << " - " << temp.getdouble() << " = " << res.getdouble() << endl; matrix[i][j] = res; } } }
inline BigFloat SafeMin( const BigFloat& alpha ) { // TODO: Decide how to only recompute this when the precision changes const BigFloat one = BigFloat(1,alpha.Precision()); const BigFloat eps = Epsilon(alpha); const BigFloat minVal = Min(alpha); const BigFloat invMax = one/Max(alpha); return ( invMax>minVal ? invMax*(one+eps) : minVal ); }
bool GaussElimination::changeValuesInRowByBigFloat(vector<vector<BigFloat>>& matrix, int indexRow) { if( matrix[indexRow][indexRow].compare( &BigFloat(0.0) ) != 0 ) return true; // wenn i.zeile zahl in spalte i = 0, tausche mit zeile darunter!!, wo zahl in i.spalte != 0 -> falls nicht da, gs nicht lösbar for(unsigned int rowCounter = indexRow+1; rowCounter < row; ++rowCounter) { BigFloat value = matrix[rowCounter][indexRow]; if(value.compare( &BigFloat(0.0) ) != 0 ) { // tausche reihen for(unsigned int i = 0; i <= col; ++i) //zusätzliche spalte angefügt durch vector { BigFloat currowValue = matrix[indexRow][i]; BigFloat changeValue = matrix[rowCounter][i]; matrix[indexRow][i] = changeValue; matrix[rowCounter][i] = currowValue; } return true; } } return false; }
ILfloat GetFloat(DICOMHEAD *Header, ILushort GroupNum) { ILfloat Num; iread(&Num, 1, 4); // The 0x02 group is always little endian. if (GroupNum == 0x02) { Float(&Num); return Num; } // Now we have to swizzle it if it is not 0x02. if (Header->BigEndian) BigFloat(&Num); else Float(&Num); return Num; }
void LoadTriangleList (char *filename, triangle_t **pptri, int *numtriangles) { QFile *input; char name[256], tex[256]; float start, exitpattern, t; int count, iLevel, magic, i; tf_triangle tri; triangle_t *ptri; t = -FLOAT_START; *((unsigned char *) &exitpattern + 0) = *((unsigned char *) &t + 3); *((unsigned char *) &exitpattern + 1) = *((unsigned char *) &t + 2); *((unsigned char *) &exitpattern + 2) = *((unsigned char *) &t + 1); *((unsigned char *) &exitpattern + 3) = *((unsigned char *) &t + 0); if ((input = Qopen(filename, "rb")) == 0) { fprintf (stderr,"reader: could not open file '%s'\n", filename); exit (0); } iLevel = 0; Qread(input, &magic, sizeof(int)); if (BigLong (magic) != MAGIC) { fprintf (stderr,"File is not a Alias object separated triangle file, " "magic number is wrong.\n"); exit (0); } ptri = malloc (MAXTRIANGLES * sizeof (triangle_t)); *pptri = ptri; while (Qeof(input) == 0) { Qread(input, &start, sizeof (float)); start = BigFloat (start); if (start != exitpattern) { if (start == FLOAT_START) { // Start of an object or group of objects. i = -1; do { // There are probably better ways to read a string from // a file, but this does allow you to do error checking // (which I'm not doing) on a per character basis. i++; Qread(input, &(name[i]), sizeof (char)); } while (name[i] != '\0'); // indent (); // fprintf(stdout,"OBJECT START: %s\n",name); Qread (input, &count, sizeof (int)); count = BigLong (count); ++iLevel; if (count != 0) { // indent(); // fprintf (stdout, "NUMBER OF TRIANGLES: %d\n", count); i = -1; do { i++; Qread (input, &(tex[i]), sizeof (char)); } while (tex[i] != '\0'); // indent(); // fprintf(stdout," Object texture name: '%s'\n",tex); } /* Else (count == 0) this is the start of a group, and */ /* no texture name is present. */ } else if (start == FLOAT_END) { /* End of an object or group. Yes, the name should be */ /* obvious from context, but it is in here just to be */ /* safe and to provide a little extra information for */ /* those who do not wish to write a recursive reader. */ /* Mia culpa. */ iLevel--; i = -1; do { i++; Qread (input, &(name[i]), sizeof (char)); } while (name[i] != '\0'); // indent(); // fprintf(stdout,"OBJECT END: %s\n",name); continue; } } // read the triangles for (i = 0; i < count; ++i) { int j; Qread (input, &tri, sizeof (tf_triangle)); ByteSwapTri (&tri); for (j = 0; j < 3; j++) { int k; for (k = 0; k < 3; k++) { ptri->verts[j][k] = tri.pt[j].p.v[k]; } } ptri++; if ((ptri - *pptri) >= MAXTRIANGLES) Sys_Error ("Error: too many triangles; increase MAXTRIANGLES\n"); } } *numtriangles = ptri - *pptri; Qclose (input); }
inline BigFloat Base<BigFloat>() { return BigFloat(2); }
//----------------------------------------------------------------------------- // rc_MapInfo // // Sent from application with bug dialog info //----------------------------------------------------------------------------- int rc_MapInfo( char* commandPtr ) { char* cmdToken; int errCode; int infoAddr; int retVal; int retAddr; char buff[128]; // success errCode = 0; // get address of data cmdToken = GetToken( &commandPtr ); if ( !cmdToken[0] ) goto cleanUp; sscanf( cmdToken, "%x", &infoAddr ); // get retAddr cmdToken = GetToken( &commandPtr ); if ( !cmdToken[0] ) goto cleanUp; sscanf( cmdToken, "%x", &retAddr ); // get the caller's info data DmGetMemory( ( void* )infoAddr, sizeof( xrMapInfo_t ), &g_bug_mapInfo, NULL ); // swap the structure BigFloat( &g_bug_mapInfo.position[0], &g_bug_mapInfo.position[0] ); BigFloat( &g_bug_mapInfo.position[1], &g_bug_mapInfo.position[1] ); BigFloat( &g_bug_mapInfo.position[2], &g_bug_mapInfo.position[2] ); BigFloat( &g_bug_mapInfo.angle[0], &g_bug_mapInfo.angle[0] ); BigFloat( &g_bug_mapInfo.angle[1], &g_bug_mapInfo.angle[1] ); BigFloat( &g_bug_mapInfo.angle[2], &g_bug_mapInfo.angle[2] ); g_bug_mapInfo.build = BigDWord( g_bug_mapInfo.build ); g_bug_mapInfo.skill = BigDWord( g_bug_mapInfo.skill ); Sys_NormalizePath( g_bug_mapInfo.savePath, false ); Sys_NormalizePath( g_bug_mapInfo.mapPath, false ); if ( g_bug_hWnd ) { if ( g_bug_mapInfo.mapPath[0] ) { Sys_StripPath( g_bug_mapInfo.mapPath, buff, sizeof( buff ) ); SetDlgItemText( g_bug_hWnd, IDC_BUG_MAP_LABEL, buff ); if ( !g_Games[g_bug_GameType].bUsesSystem1 ) { char *pExtension = V_stristr( buff, ".bsp" ); if ( pExtension ) { *pExtension = '\0'; } pExtension = V_stristr( buff, ".360" ); if ( pExtension ) { *pExtension = '\0'; } V_strncpy( g_bug_szMapNumber, buff, sizeof( g_bug_szMapNumber ) ); int curSel = SendDlgItemMessage( g_bug_hWnd, IDC_BUG_MAPNUMBER, CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)(LPCSTR)g_bug_szMapNumber ); if ( curSel == CB_ERR ) curSel = 0; SendDlgItemMessage( g_bug_hWnd, IDC_BUG_MAPNUMBER, CB_SETCURSEL, curSel, 0 ); } sprintf( buff, "%.2f %.2f %.2f", g_bug_mapInfo.position[0], g_bug_mapInfo.position[1], g_bug_mapInfo.position[2] ); SetDlgItemText( g_bug_hWnd, IDC_BUG_POSITION_LABEL, buff ); sprintf( buff, "%.2f %.2f %.2f", g_bug_mapInfo.angle[0], g_bug_mapInfo.angle[1], g_bug_mapInfo.angle[2] ); SetDlgItemText( g_bug_hWnd, IDC_BUG_ORIENTATION_LABEL, buff ); } else { SetDlgItemText( g_bug_hWnd, IDC_BUG_MAP_LABEL, "" ); SetDlgItemText( g_bug_hWnd, IDC_BUG_POSITION_LABEL, "" ); SetDlgItemText( g_bug_hWnd, IDC_BUG_ORIENTATION_LABEL, "" ); } sprintf( buff, "%d", g_bug_mapInfo.build ); SetDlgItemText( g_bug_hWnd, IDC_BUG_BUILD_LABEL, buff ); EnableWindow( GetDlgItem( g_bug_hWnd, IDC_BUG_SAVEGAME ), g_bug_mapInfo.savePath[0] != '\0' && g_bug_mapInfo.mapPath[0] != '\0' ); EnableWindow( GetDlgItem( g_bug_hWnd, IDC_BUG_INCLUDEBSP ), g_bug_mapInfo.mapPath[0] != '\0' ); } // return the result retVal = 0; int xboxRetVal = BigDWord( retVal ); DmSetMemory( ( void* )retAddr, sizeof( int ), &xboxRetVal, NULL ); DebugCommand( "0x%8.8x = MapInfo( 0x%8.8x )\n", retVal, infoAddr ); cleanUp: return errCode; }
inline BigInt FindFactor ( const BigInt& n, DynamicSieve<SieveUnsigned>& sieve, const PollardPMinusOneCtrl<SieveUnsigned>& ctrl ) { const double twoLog = Log( 2. ); const double nLog = double( Log( BigFloat(n) ) ); const BigInt zero(0), one(1); // Keep all of the generated primes sieve.SetStorage( true ); SieveUnsigned smooth1 = ctrl.smooth1; SieveUnsigned smooth2 = ctrl.smooth2; SieveUnsigned smooth1Bound = Max( Pow(10ULL,9ULL), 8ULL*smooth1 ); SieveUnsigned smooth2Bound = Max( Pow(10ULL,10ULL), 8ULL*smooth2 ); // Ensure that we do not have a GCD of n appear too many times despite // separately checking the powers of two bool separateOdd=false; Int maxGCDFailures=10; Int numGCDFailures=0; BigInt smallPrime, gcd, tmp, diffPower; while( true ) { // Ensure that we have sieved at least up until smooth1 bool neededStage1Sieving = ( sieve.oddPrimes.back() < smooth1 ); if( ctrl.progress && neededStage1Sieving ) Output ("Updating sieve from ",sieve.oddPrimes.back()," to ",smooth1); sieve.Generate( smooth1 ); if( ctrl.progress && neededStage1Sieving ) Output("Done sieving for stage 1"); // Uniformly select a in (Z/(n))* // (alternatively, we could set a=2) BigInt a = SampleUniform( zero, n ); while( GCD( a, n ) != one ) { a = SampleUniform( zero, n ); } if( !separateOdd ) { // Handle 2 separately unsigned smallPrimeExponent = unsigned(nLog/twoLog); for( Int i=0; i<smallPrimeExponent; ++i ) { // a = a^2 (mod n) a *= a; a %= n; } } auto smooth1End = std::upper_bound ( sieve.oddPrimes.begin(), sieve.oddPrimes.end(), smooth1 ); for( auto iter=sieve.oddPrimes.begin(); iter<smooth1End; ++iter ) { auto smallPrime = *iter; double smallPrimeLog = double(Log(double(smallPrime))); unsigned smallPrimeExponent = unsigned(nLog/smallPrimeLog); for( Int i=0; i<smallPrimeExponent; ++i ) { // a = a^smallPrime (mod n) PowMod( a, smallPrime, n, a ); } } // gcd := GCD( a-1, n ) tmp = a; tmp -= 1; GCD( tmp, n, gcd ); if( gcd > one && gcd < n ) { if( ctrl.progress ) Output("Found stage-1 factor of ",gcd); return gcd; } if( separateOdd ) { unsigned twoExponent = unsigned(nLog/twoLog); for( Int i=0; i<twoExponent; ++i ) { // a = a*a (mod n) a *= a; a %= n; //gcd = GCD( a-1, n ); tmp = a; tmp -= 1; GCD( tmp, n, gcd ); if( gcd > one && gcd < n ) { if( ctrl.progress ) Output("Found separate stage-1 factor of ",gcd); return gcd; } } } if( gcd == n ) { if( separateOdd ) { ++numGCDFailures; if( numGCDFailures >= maxGCDFailures ) RuntimeError ("Too many GCD failures despite separately checking " "powers of two"); } else { ++numGCDFailures; separateOdd = true; if( ctrl.progress ) Output("GCD was n; will separately check powers of two"); } continue; } else // gcd == one { if( ctrl.progress ) Output("Proceeding to stage-2"); } // Run stage-2 // ----------- // Store all powers of a^{d_i}, where d_i is the difference between // primes p_{i+1} and p_i, where smooth1 < p_{i+1} <= smooth2 bool neededStage2Sieving = ( sieve.oddPrimes.back() < smooth2 ); if( ctrl.progress && neededStage2Sieving ) Output ("Updating sieve from ",sieve.oddPrimes.back()," to ",smooth2); sieve.Generate( smooth2 ); if( ctrl.progress && neededStage2Sieving ) Output("Done sieving for stage 2"); // NOTE: stage1End has potentially been invalidated due to reallocation auto stage2Beg = std::lower_bound ( sieve.oddPrimes.begin(), sieve.oddPrimes.end(), smooth1 ); auto stage2End = std::upper_bound ( stage2Beg, sieve.oddPrimes.end(), smooth2 ); std::map<SieveUnsigned,BigInt> diffPowers; for( auto iter=stage2Beg; iter<stage2End; ++iter ) { SieveUnsigned diff = *iter - *(iter-1); auto search = diffPowers.find( diff ); const size_t whichPrime = (iter-sieve.oddPrimes.begin())+1; Output(" ",whichPrime,": ",*iter," - ",*(iter-1)," = ",diff); if( search == diffPowers.end() ) { PowMod( a, diff, n, diffPower ); diffPowers.insert( std::make_pair(diff,diffPower) ); Output(" Stored ",a,"^",diff,"=",diffPower); } else Output(" diff=",diff," was redundant"); } // Test each stage two candidate for( auto iter=stage2Beg; iter<stage2End; ++iter ) { SieveUnsigned diff = *iter - *(iter-1); a *= diffPowers[diff]; a %= n; //gcd = GCD( a-1, n ); tmp = a; tmp -= 1; GCD( tmp, n, gcd ); if( gcd > one && gcd < n ) { if( ctrl.progress ) Output("Found stage-2 factor of ",gcd); return gcd; } } if( gcd == n ) { if( separateOdd ) { if( ctrl.progress ) Output("GCD failure ",numGCDFailures); ++numGCDFailures; if( numGCDFailures >= maxGCDFailures ) RuntimeError ("Too many GCD failures despite separately checking " "powers of two"); } else { ++numGCDFailures; separateOdd = true; if( ctrl.progress ) Output("GCD was n; will separately check powers of two"); } continue; } else // gcd == one { if( smooth1 >= smooth1Bound ) { RuntimeError ("Stage-1 smoothness bound of ",smooth1Bound," exceeded"); } if( smooth2 >= smooth2Bound ) { RuntimeError ("Stage-2 smoothness bound of ",smooth2Bound," exceeded"); } smooth1 *= 2; smooth2 *= 2; if( ctrl.progress ) Output ("Increased stage-1 smoothness to ",smooth1," and stage-2 " "smoothness to ",smooth2); } } }