autoMatrix Matrix_readAP (MelderFile file) { try { autofile f = Melder_fopen (file, "rb"); int16_t header [256]; for (long i = 0; i < 256; i ++) header [i] = bingeti2LE (f); double samplingFrequency = header [100]; // converting up (from 16 to 54 bytes) Melder_casual (U"Sampling frequency ", samplingFrequency); autoMatrix me = Matrix_create (0.0, (double) header [34], header [34] /* Number of frames. */, 1.0, 0.5, 0.0, (double) header [35], header [35] /* Number of words per frame. */, 1.0, 0.5); /*Mat := MATRIX_create (Buffer.I2 [36], (* Number of words per frame. *) Buffer.I2 [35], (* Number of frames. *) 1.0, Buffer.I2 [111] / (* Samples per frame. *) Buffer.I2 [101]); (* Sampling frequency. *)*/ Melder_casual (U"... Loading ", header [34], U" frames", U" of ", header [35], U" words ..."); for (long i = 1; i <= my nx; i ++) for (long j = 1; j <= my ny; j ++) my z [j] [i] = bingeti2LE (f); // converting up (from 16 to 54 bytes) /* * Get pitch frequencies. */ for (long i = 1; i <= my nx; i ++) if (my z [1] [i] != 0.0) my z [1] [i] = - samplingFrequency / my z [1] [i]; f.close (file); return me; } catch (MelderError) { Melder_throw (U"Matrix object not read from AP file ", file); } }
void Pitch_difference (Pitch me, Pitch thee) { long nuvtov = 0, nvtouv = 0, ndfdown = 0, ndfup = 0; if (my nx != thy nx || my dx != thy dx || my x1 != thy x1) { Melder_flushError ("Pitch_difference: these Pitches are not aligned."); return; } for (long i = 1; i <= my nx; i ++) { double myf = my frame [i]. candidate [1]. frequency, thyf = thy frame [i]. candidate [1]. frequency; int myUnvoiced = myf == 0 || myf > my ceiling; int thyUnvoiced = thyf == 0 || thyf > thy ceiling; double t = Sampled_indexToX (me, i); if (myUnvoiced && ! thyUnvoiced) { Melder_casual ("Frame %ld time %f: unvoiced to voiced.", i, t); nuvtov ++; } else if (! myUnvoiced && thyUnvoiced) { Melder_casual ("Frame %ld time %f: voiced to unvoiced.", i, t); nvtouv ++; } else if (! myUnvoiced && ! thyUnvoiced) { if (myf > thyf) { //Melder_casual ("Frame %ld time %f: downward frequency jump from %.5g Hz to %.5g Hz.", i, t, myf, thyf); ndfdown ++; } else if (myf < thyf) { //Melder_casual ("Frame %ld time %f: upward frequency jump from %.5g Hz to %.5g Hz.", i, t, myf, thyf); ndfup ++; } } } MelderInfo_open (); MelderInfo_writeLine (L"Difference between two Pitches:"); MelderInfo_writeLine (L"Unvoiced to voiced: ", Melder_integer (nuvtov), L" frames."); MelderInfo_writeLine (L"Voiced to unvoiced: ", Melder_integer (nvtouv), L" frames."); MelderInfo_writeLine (L"Downward frequency jump: ", Melder_integer (ndfdown), L" frames."); MelderInfo_writeLine (L"Upward frequency jump: ", Melder_integer (ndfup), L" frames."); MelderInfo_close (); }
autoCochleagram Sound_to_Cochleagram (Sound me, double dt, double df, double dt_window, double forwardMaskingTime) { try { double duration = my nx * my dx; long nFrames = 1 + (long) floor ((duration - dt_window) / dt); long nsamp_window = (long) floor (dt_window / my dx), halfnsamp_window = nsamp_window / 2 - 1; long nf = lround (25.6 / df); double dampingFactor = forwardMaskingTime > 0.0 ? exp (- dt / forwardMaskingTime) : 0.0; // default 30 ms double integrationCorrection = 1.0 - dampingFactor; nsamp_window = halfnsamp_window * 2; if (nFrames < 2) return autoCochleagram (); double t1 = my x1 + 0.5 * (duration - my dx - (nFrames - 1) * dt); // centre of first frame autoCochleagram thee = Cochleagram_create (my xmin, my xmax, nFrames, dt, t1, df, nf); autoSound window = Sound_createSimple (1, nsamp_window * my dx, 1.0 / my dx); for (long iframe = 1; iframe <= nFrames; iframe ++) { double t = Sampled_indexToX (thee.get(), iframe); long leftSample = Sampled_xToLowIndex (me, t); long rightSample = leftSample + 1; long startSample = rightSample - halfnsamp_window; long endSample = rightSample + halfnsamp_window; if (startSample < 1) { Melder_casual (U"Start sample too small: ", startSample, U" instead of 1."); startSample = 1; } if (endSample > my nx) { Melder_casual (U"End sample too small: ", endSample, U" instead of ", my nx, U"."); endSample = my nx; } /* Copy a window to a frame. */ for (long i = 1; i <= nsamp_window; i ++) window -> z [1] [i] = ( my ny == 1 ? my z[1][i+startSample-1] : 0.5 * (my z[1][i+startSample-1] + my z[2][i+startSample-1]) ) * (0.5 - 0.5 * cos (2.0 * NUMpi * i / (nsamp_window + 1))); autoSpectrum spec = Sound_to_Spectrum (window.get(), true); autoExcitation excitation = Spectrum_to_Excitation (spec.get(), df); for (long ifreq = 1; ifreq <= nf; ifreq ++) thy z [ifreq] [iframe] = excitation -> z [1] [ifreq] + ( iframe > 1 ? dampingFactor * thy z [ifreq] [iframe - 1] : 0 ); } for (long iframe = 1; iframe <= nFrames; iframe ++) for (long ifreq = 1; ifreq <= nf; ifreq ++) thy z [ifreq] [iframe] *= integrationCorrection; return thee; } catch (MelderError) { Melder_throw (me, U": not converted to Cochleagram."); } }
static int findOneZero (int ijt, double vcx [], double a, double b, double *zero) { double x = 0.5 * (a + b), fa = 0.0, fb = 0.0, fx = 0.0; long k; for (k = ijt; k >= 0; k --) { fa = vcx [k] + a * fa; fb = vcx [k] + b * fb; } if (fa * fb >= 0.0) { // there should be a zero between a and b Melder_casual ( U"There is no zero between ,", Melder_single (a), U" and ", Melder_single (b), U".\n The function values are ", Melder_single (fa), U" and ", Melder_single (fb), U", respectively." ); return 0; } do { fx = 0.0; /*x = fa == fb ? 0.5 * (a + b) : a + fa * (a - b) / (fb - fa);*/ x = 0.5 * (a + b); // simple bisection for (k = ijt; k >= 0; k --) fx = vcx [k] + x * fx; if (fa * fx > 0.0) { a = x; fa = fx; } else { b = x; fb = fx; } } while (fabs (fx) > 1e-5); *zero = x; return 1; // OK }
void Minimizer_minimize (Minimizer me, long maxNumOfIterations, double tolerance, int monitor) { try { my tolerance = tolerance; if (maxNumOfIterations <= 0) { return; } if (my iteration + maxNumOfIterations > my maxNumOfIterations) { my maxNumOfIterations += maxNumOfIterations; if (my history) { // clumsy because vector must have been allocated before one can append NUMvector_append<double> (& my history, 1, & my maxNumOfIterations); } else { my history = NUMvector<double> (1, my maxNumOfIterations); } } if (monitor) { my gmonitor = (Graphics) Melder_monitor (0.0, U"Starting..."); } my start = 1; /* for my after() */ my v_minimize (); if (monitor) { monitor_off (me); } if (my success) Melder_casual (U"Minimizer_minimize:", U" minimum ", my minimum, U" reached \nafter ", my iteration, U" iterations and ", my funcCalls, U" function calls."); } catch (MelderError) { if (monitor) { monitor_off (me); // temporarily until better monitor facilities } Melder_clearError(); // memory error in history mechanism is not fatal } }
void Minimizer_minimizeManyTimes (Minimizer me, long numberOfTimes, long maxIterationsPerTime, double tolerance) { double fopt = my minimum; int monitorSingle = numberOfTimes == 1; autoNUMvector<double> popt (NUMvector_copy<double> (my p, 1, my nParameters), 1); if (! monitorSingle) { Melder_progress (0.0, L"Minimize many times"); } /* on first iteration start with current parameters 27/11/97 */ for (long i = 1; i <= numberOfTimes; i++) { Minimizer_minimize (me, maxIterationsPerTime, tolerance, monitorSingle); Melder_casual ("Current %ld: minimum = %.17g", i, my minimum); if (my minimum < fopt) { NUMvector_copyElements (my p, popt.peek(), 1, my nParameters); fopt = my minimum; } Minimizer_reset (me, 0); if (! monitorSingle) { try { Melder_progress ( (double) i / numberOfTimes, Melder_integer (i), L" from ", Melder_integer (numberOfTimes)); therror } catch (MelderError) { Melder_clearError (); // interrurpt, no error break; } } } if (! monitorSingle) { Melder_progress (1.0, 0); } Minimizer_reset (me, popt.peek()); }
void _Melder_free (void **ptr) { if (*ptr == NULL) return; free (*ptr); if (Melder_debug == 34) { Melder_casual ("Melder_free\t%ld\t?\t?", *ptr); } *ptr = NULL; totalNumberOfDeallocations += 1; }
void _Melder_free (void **ptr) { if (*ptr == NULL) return; if (Melder_debug == 34) { Melder_casual (U"Melder_free\t", Melder_pointer (*ptr), U"\t?\t?"); } free (*ptr); *ptr = NULL; totalNumberOfDeallocations += 1; }
void structSteepestDescentMinimizer :: v_minimize () { autoNUMvector<double> dp (1, nParameters); autoNUMvector<double> dpp (1, nParameters); double fret = func (object, p); while (iteration < maxNumOfIterations) { dfunc (object, p, dp.peek()); for (long i = 1; i <= nParameters; i++) { dpp[i] = - eta * dp[i] + momentum * dpp[i]; p[i] += dpp[i]; } history[++iteration] = minimum = func (object, p); success = 2.0 * fabs (fret - minimum) < tolerance * (fabs (fret) + fabs (minimum)); if (after) { try { after (this, aclosure); } catch (MelderError) { Melder_casual ("Interrupted after %ld iterations.", iteration); Melder_clearError (); break; } } if (success) { break; } fret = minimum; } }
void _Thing_forget_nozero (Thing me) { if (! me) return; if (Melder_debug == 40) Melder_casual (U"destroying ", my classInfo -> className); //Melder_casual (U"_Thing_forget_nozero before"); my v_destroy (); //Melder_casual (U"_Thing_forget_nozero after"); theTotalNumberOfThings -= 1; }
void _Thing_forget (Thing *pme) { Thing me = *pme; if (! me) return; if (Melder_debug == 39) Melder_casual ("destroying %ls", my methods -> _className); our destroy (me); Melder_free (me); theTotalNumberOfThings -= 1; *pme = NULL; }
void _Thing_forget (Thing me) { if (! me) return; if (Melder_debug == 40) Melder_casual (U"destroying ", my classInfo -> className); my v_destroy (); trace (U"destroying ", my classInfo -> className); //Melder_free (me); delete me; theTotalNumberOfThings -= 1; }
autoThing Thing_newFromClass (ClassInfo classInfo) { autoThing me (classInfo -> _new ()); trace (U"created ", classInfo -> className); theTotalNumberOfThings += 1; my classInfo = classInfo; Melder_assert (my name == nullptr); // confirm that _new called calloc, so that we see null pointers if (Melder_debug == 40) Melder_casual (U"created ", classInfo -> className, U" (", Melder_pointer (classInfo), U", ", me.get(), U")"); return me; }
void _CollectionOfDaata_v_readBinary (_CollectionOfDaata* me, FILE *f, int formatVersion) { if (formatVersion < 0) { int32 l_size = bingeti4 (f); if (l_size < 0) Melder_throw (U"Empty collection."); my _grow (l_size); for (int32_t i = 1; i <= l_size; i ++) { char klas [200], name [2000]; if (fscanf (f, "%s%s", klas, name) < 2) // BUG Melder_throw (U"Cannot read class and name."); my at [i] = (Daata) Thing_newFromClassName (Melder_peek8to32 (klas), nullptr).releaseToAmbiguousOwner(); my size ++; if (! Thing_isa (my at [i], classDaata)) Melder_throw (U"Cannot read item of class ", Thing_className (my at [i]), U"."); if (fgetc (f) != ' ') Melder_throw (U"Cannot read space."); Data_readBinary (my at [i], f, -1); if (strcmp (name, "?")) Thing_setName (my at [i], Melder_peek8to32 (name)); } } else { int32_t l_size = bingeti4 (f); if (Melder_debug == 44) Melder_casual (U"structCollection :: v_readBinary: Reading ", l_size, U" objects"); my _grow (l_size); for (int32_t i = 1; i <= l_size; i ++) { autostring8 klas = bingets1 (f); if (Melder_debug == 44) Melder_casual (U"structCollection :: v_readBinary: Reading object of type ", Melder_peek8to32 (klas.peek())); int elementFormatVersion; my at [i] = (Daata) Thing_newFromClassName (Melder_peek8to32 (klas.peek()), & elementFormatVersion).releaseToAmbiguousOwner(); my size ++; if (! Thing_isa (my at [i], classDaata) || ! Data_canReadBinary (my at [i])) Melder_throw (U"Objects of class ", Thing_className (my at [i]), U" cannot be read."); autostring32 name = bingetw2 (f); if (Melder_debug == 44) Melder_casual (U"structCollection :: v_readBinary: Reading object with name ", name.peek()); Thing_setName (my at [i], name.peek()); Data_readBinary (my at [i], f, elementFormatVersion); } } }
void FFNet_Eigen_drawIntersection (FFNet me, Eigen eigen, Graphics g, long pcx, long pcy, double xmin, double xmax, double ymin, double ymax) { long ix = labs (pcx), iy = labs (pcy); long numberOfEigenvalues = eigen -> numberOfEigenvalues; long dimension = eigen -> dimension; if (ix > numberOfEigenvalues || iy > numberOfEigenvalues || my nInputs != dimension) { return; } Melder_assert (ix > 0 && iy > 0); double x1, x2, y1, y2; if (xmax <= xmin || ymax <= ymin) { Graphics_inqWindow (g, & x1, & x2, & y1, & y2); } if (xmax <= xmin) { xmin = x1; xmax = x2; } if (ymax <= ymin) { ymin = y1; ymax = y2; } Graphics_setInner (g); Graphics_setWindow (g, xmin, xmax, ymin, ymax); for (long i = 1; i <= my nUnitsInLayer[1]; i++) { long unitOffset = my nInputs + 1; double c1 = 0.0, c2 = 0.0, bias = my w[ my wLast[unitOffset + i] ]; double x[6], y[6], xs[3], ys[3]; int ns = 0; for (long j = 1; j <= my nInputs; j++) { c1 += my w[ my wFirst[unitOffset + i] + j - 1 ] * eigen->eigenvectors[ix][j]; c2 += my w[ my wFirst[unitOffset + i] + j - 1 ] * eigen->eigenvectors[iy][j]; } x[1] = x[2] = x[5] = xmin; x[3] = x[4] = xmax; y[1] = y[4] = y[5] = ymin; y[2] = y[3] = ymax; for (long j = 1; j <= 4; j++) { double p1 = c1 * x[j ] + c2 * y[j ] + bias; double p2 = c1 * x[j + 1] + c2 * y[j + 1] + bias; double r = fabs (p1) / (fabs (p1) + fabs (p2)); if (p1 *p2 > 0 || r == 0.0) { continue; } if (++ns > 2) { break; } xs[ns] = x[j] + (x[j + 1] - x[j]) * r; ys[ns] = y[j] + (y[j + 1] - y[j]) * r; } if (ns < 2) { Melder_casual (U"Intersection for unit ", i, U" outside range"); } else { Graphics_line (g, xs[1], ys[1], xs[2], ys[2]); } } Graphics_unsetInner (g); }
void * _Melder_malloc (unsigned long size) { if (size <= 0) Melder_throw ("Can never allocate ", size, " bytes."); void *result = malloc (size); if (result == NULL) Melder_throw ("Out of memory: there is not enough room for another ", size, " bytes."); if (Melder_debug == 34) { Melder_casual ("Melder_malloc\t%ld\t%ld\t1", result, size); } totalNumberOfAllocations += 1; totalAllocationSize += size; return result; }
static bool IntervalTier_check (IntervalTier me) { for (long iinterval = 1; iinterval <= my intervals.size; iinterval ++) { TextInterval interval = my intervals.at [iinterval]; if (interval -> xmin >= interval -> xmax) { Melder_casual (U"Interval ", iinterval, U" starts at ", interval -> xmin, U" but ends at ", interval -> xmax, U" seconds."); return false; } } if (my intervals.size < 2) return true; for (long iinterval = 1; iinterval < my intervals.size; iinterval ++) { TextInterval thisInterval = my intervals.at [iinterval]; TextInterval nextInterval = my intervals.at [iinterval + 1]; if (thisInterval -> xmax != nextInterval -> xmin) { Melder_casual (U"Interval ", iinterval, U" ends at ", thisInterval -> xmax, U" but the next interval starts at ", nextInterval -> xmin, U" seconds."); return false; } } return true; }
char * Melder_strdup (const char *string) { if (! string) return NULL; long size = strlen (string) + 1; char *result = (char *) malloc (size * sizeof (char)); if (result == NULL) Melder_throw ("Out of memory: there is not enough room to duplicate a text of ", size - 1, " characters."); strcpy (result, string); if (Melder_debug == 34) { Melder_casual ("Melder_strdup\t%ld\t%ld\t1", result, size); } totalNumberOfAllocations += 1; totalAllocationSize += size; return result; }
wchar_t * Melder_wcsdup (const wchar_t *string) { if (! string) return NULL; long size = wcslen (string) + 1; wchar_t *result = (wchar_t *) malloc (size * sizeof (wchar_t)); if (result == NULL) Melder_throw ("Out of memory: there is not enough room to duplicate a text of ", size - 1, " characters."); wcscpy (result, string); if (Melder_debug == 34) { Melder_casual ("Melder_wcsdup\t%ld\t%ld\t4", result, size); } totalNumberOfAllocations += 1; totalAllocationSize += size * sizeof (wchar_t); return result; }
Any _Thing_new (void *table) { Thing_Table us = (structThing_Table*)table; Thing me = (Thing) _Melder_calloc_e (1, us -> _size); if (! me) return Melder_errorp ("(Thing_new:) Out of memory."); theTotalNumberOfThings += 1; my methods = us; my name = NULL; if (! us -> destroy) /* Table not initialized? */ us -> _initialize (us); if (Melder_debug == 39) Melder_casual ("created %ls", my methods -> _className); return me; }
void * _Melder_malloc (int64 size) { if (size <= 0) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes."); if (sizeof (size_t) < 8 && size > SIZE_MAX) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?"); void *result = malloc ((size_t) size); // guarded cast if (result == NULL) Melder_throw (U"Out of memory: there is not enough room for another ", Melder_bigInteger (size), U" bytes."); if (Melder_debug == 34) { Melder_casual (U"Melder_malloc\t", Melder_pointer (result), U"\t", Melder_bigInteger (size), U"\t1"); } totalNumberOfAllocations += 1; totalAllocationSize += size; return result; }
void * _Melder_calloc (long nelem, long elsize) { void *result; if (nelem <= 0) Melder_throw ("Can never allocate ", nelem, " elements."); if (elsize <= 0) Melder_throw ("Can never allocate elements whose size is ", elsize, " bytes."); result = calloc (nelem, elsize); if (result == NULL) Melder_throw ("Out of memory: there is not enough room for ", nelem, " more elements whose sizes are ", elsize, " bytes each."); if (Melder_debug == 34) { Melder_casual ("Melder_calloc\t%ld\t%ld\t%ld", result, nelem, elsize); } totalNumberOfAllocations += 1; totalAllocationSize += nelem * elsize; return result; }
char * Melder_strdup (const char *string) { if (! string) return NULL; int64 size = (int64) strlen (string) + 1; if (sizeof (size_t) < 8 && size > SIZE_MAX) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?"); char *result = (char *) malloc ((size_t) size); if (result == NULL) Melder_throw (U"Out of memory: there is not enough room to duplicate a text of ", Melder_bigInteger (size - 1), U" characters."); strcpy (result, string); if (Melder_debug == 34) { Melder_casual (U"Melder_strdup\t", Melder_pointer (result), U"\t", Melder_bigInteger (size), U"\t1"); } totalNumberOfAllocations += 1; totalAllocationSize += size; return result; }
char32 * Melder_dup (const char32 *string /* cattable */) { if (! string) return NULL; int64 size = (int64) str32len (string) + 1; // guaranteed to be positive if (sizeof (size_t) < 8 && size > SIZE_MAX / sizeof (char32)) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" characters. Use a 64-bit edition of Praat instead?"); char32 *result = (char32 *) malloc ((size_t) size * sizeof (char32)); // guarded conversion if (result == NULL) Melder_throw (U"Out of memory: there is not enough room to duplicate a text of ", Melder_bigInteger (size - 1), U" characters."); str32cpy (result, string); if (Melder_debug == 34) { Melder_casual (U"Melder_dup\t", Melder_pointer (result), U"\t", Melder_bigInteger (size), U"\t4"); } totalNumberOfAllocations += 1; totalAllocationSize += size * (int64) sizeof (char32); return result; }
void * _Melder_calloc (int64 nelem, int64 elsize) { if (nelem <= 0) Melder_throw (U"Can never allocate ", Melder_bigInteger (nelem), U" elements."); if (elsize <= 0) Melder_throw (U"Can never allocate elements whose size is ", Melder_bigInteger (elsize), U" bytes."); if ((uint64_t) nelem > SIZE_MAX / (uint64_t) elsize) // guarded casts to unsigned Melder_throw (U"Can never allocate ", Melder_bigInteger (nelem), U" elements whose sizes are ", Melder_bigInteger (elsize), U" bytes each.", sizeof (size_t) < 8 ? U" Use a 64-bit edition of Praat instead?" : NULL); void *result = calloc ((size_t) nelem, (size_t) elsize); if (result == NULL) Melder_throw (U"Out of memory: there is not enough room for ", Melder_bigInteger (nelem), U" more elements whose sizes are ", elsize, U" bytes each."); if (Melder_debug == 34) { Melder_casual (U"Melder_calloc\t", Melder_pointer (result), U"\t", Melder_bigInteger (nelem), U"\t", Melder_bigInteger (elsize)); } totalNumberOfAllocations += 1; totalAllocationSize += nelem * elsize; return result; }
void Polygon_salesperson (Polygon me, long numberOfIterations) { try { long numberOfShortest = 1, totalDistance, shortestDistance = 0; int numberOfCities = my numberOfPoints; if (numberOfCities < 1) Melder_throw (U"No points."); autoNUMmatrix <int> distance (1, numberOfCities, 1, numberOfCities); computeDistanceTable (me, distance.peek()); autoNUMvector <int> path (0L, numberOfCities); for (int i = 1; i <= numberOfCities; i ++) path [i] = i; path [0] = numberOfCities; // close path autoNUMvector <int> shortestPath (NUMvector_copy (path.peek(), 0, numberOfCities), 0); for (long iteration = 1; iteration <= numberOfIterations; iteration ++) { if (iteration > 1) shuffle (path.peek(), numberOfCities); totalDistance = computeTotalDistance (distance.peek(), path.peek(), numberOfCities); if (iteration == 1) shortestDistance = totalDistance; do { do { } while (tryExchange (distance.peek(), path.peek(), numberOfCities, & totalDistance)); } while (tryAdoption (distance.peek(), path.peek(), numberOfCities, & totalDistance)); if (totalDistance < shortestDistance) { // new shortest path numberOfShortest = 1; for (int i = 0; i <= numberOfCities; i ++) shortestPath [i] = path [i]; shortestDistance = totalDistance; } else if (totalDistance == shortestDistance) // shortest path confirmed numberOfShortest ++; } if (numberOfIterations > 1) Melder_casual (U"Polygon_salesperson:" U" found ", numberOfShortest, U" times the same shortest path."); /* Change me: I will follow the shortest path found. */ autoPolygon help = Data_copy (me); for (long i = 1; i <= numberOfCities; i ++) { my x [i] = help -> x [shortestPath [i]]; my y [i] = help -> y [shortestPath [i]]; } } catch (MelderError) { Melder_throw (me, U": shortest path not found."); } }
void * Melder_realloc (void *ptr, long size) { if (size <= 0) Melder_throw ("Can never allocate ", size, " bytes."); void *result = realloc (ptr, size); // will not show in the statistics... if (result == NULL) Melder_throw ("Out of memory. Could not extend room to ", size, " bytes."); if (ptr == NULL) { // is it like malloc? if (Melder_debug == 34) { Melder_casual ("Melder_realloc\t%ld\t%ld\t1", result, size); } totalNumberOfAllocations += 1; totalAllocationSize += size; } else if (result != ptr) { // did realloc do a malloc-and-free? totalNumberOfAllocations += 1; totalAllocationSize += size; totalNumberOfDeallocations += 1; totalNumberOfMovingReallocs += 1; } else { totalNumberOfReallocsInSitu += 1; } return result; }
void * Melder_realloc (void *ptr, int64 size) { if (size <= 0) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes."); if (sizeof (size_t) < 8 && size > SIZE_MAX) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?"); void *result = realloc (ptr, (size_t) size); // will not show in the statistics... if (result == NULL) Melder_throw (U"Out of memory. Could not extend room to ", Melder_bigInteger (size), U" bytes."); if (ptr == NULL) { // is it like malloc? if (Melder_debug == 34) { Melder_casual (U"Melder_realloc\t", Melder_pointer (result), U"\t", Melder_bigInteger (size), U"\t1"); } totalNumberOfAllocations += 1; totalAllocationSize += size; } else if (result != ptr) { // did realloc do a malloc-and-free? totalNumberOfAllocations += 1; totalAllocationSize += size; totalNumberOfDeallocations += 1; totalNumberOfMovingReallocs += 1; } else { totalNumberOfReallocsInSitu += 1; } return result; }
static int findNewZeroes (int ijt, double ppORIG [], int degree, double zeroes []) // In / out { static double cosa [7] [7] = { { 1, 0, 0, 0, 0, 0, 0 }, { 0, 2, 0, 0, 0, 0, 0 }, { -2, 0, 4, 0, 0, 0, 0 }, { 0, -6, 0, 8, 0, 0, 0 }, { 2, 0, -16, 0, 16, 0, 0 }, { 0, 10, 0, -40, 0, 32, 0 }, { -2, 0, 36, 0, -96, 0, 64 } }; double pp [33], newZeroes [33], px [33]; int pt, vt, i, half_degree = (degree + 1) / 2; for (vt = 0; vt <= half_degree; vt ++) pp [vt] = ppORIG [vt]; if (! (degree & 1)) for (vt = 1; vt <= half_degree; vt ++) pp [vt] -= pp [vt - 1]; for (i = 0; i <= half_degree; i ++) px [i] = cosa [half_degree] [i]; for (pt = half_degree - 1; pt >= 0; pt --) for (vt = 0; vt <= half_degree; vt ++) px [vt] += pp [half_degree - pt] * cosa [pt] [vt]; /* Fill an array with the new zeroes, which lie between the old zeroes. */ newZeroes [0] = 1.0; for (i = 1; i <= half_degree; i ++) { if (! findOneZero (ijt, px, zeroes [i - 1], zeroes [i], & newZeroes [i])) { Melder_casual ( U"Degree ", degree, U" not completed." ); return 0; } } newZeroes [half_degree + 1] = -1.0; /* Grow older. */ for (i = 0; i <= half_degree + 1; i ++) zeroes [i] = newZeroes [i]; return 1; }
void Minimizer_minimize (Minimizer me, long maxNumOfIterations, double tolerance, int monitor) { try { my tolerance = tolerance; if (maxNumOfIterations <= 0) { return; } if (my iteration + maxNumOfIterations > my maxNumOfIterations) { double *history; my maxNumOfIterations += maxNumOfIterations; if (my history) { my history++; /* arrays start at 1 !! */ } history = (double *) Melder_realloc (my history, my maxNumOfIterations * sizeof (double)); my history = --history; /* arrays start at 1 !! */ } if (monitor) { my gmonitor = (Graphics) Melder_monitor (0.0, L"Starting..."); } my start = 1; /* for my after() */ my v_minimize (); if (monitor) { monitor_off (me); } if (my success) Melder_casual ("Minimizer_minimize: minimum %f reached \n" "after %ld iterations and %ld function calls.", my minimum, my iteration, my funcCalls); } catch (MelderError) { if (monitor) { monitor_off (me); // temporarily until better monitor facilities } Melder_clearError(); // memory error in history mechanism is not fatal } }