diff --git a/misc/factorial_large_number.c b/misc/factorial_large_number.c index 56e92d39..25583890 100644 --- a/misc/factorial_large_number.c +++ b/misc/factorial_large_number.c @@ -6,7 +6,6 @@ #include #include #include -#include "function_timer.h" /** * dynamically large number @@ -102,13 +101,11 @@ int main(int argc, char *argv[]) large_num *result = new_number(); - function_timer *timer = new_timer(); clock_t start_time = clock(); - start_timer(timer); for (i = 2; i <= number; i++) /* Multiply every number from 2 thru N */ multiply(result, i); - double time_taken = end_timer_delete(timer) * (double)1e3; + double time_taken = (clock() - start_time) * (double)1e3 / CLOCKS_PER_SEC; // time_taken = (clock() - start_time) / (double) CLOCKS_PER_SEC; printf("%d! = ", number); diff --git a/numerical_methods/durand_kerner_roots.c b/numerical_methods/durand_kerner_roots.c index 9b49582c..3da5dc40 100644 --- a/numerical_methods/durand_kerner_roots.c +++ b/numerical_methods/durand_kerner_roots.c @@ -7,28 +7,34 @@ * * Try the highly unstable Wilkinson's polynomial: * ``` - * ./numerical_methods/durand_kerner_roots.c 1 -210 20615 -1256850 53327946 -1672280820 40171771630 -756111184500 11310276995381 -135585182899530 1307535010540395 -10142299865511450 63030812099294896 -311333643161390640 1206647803780373360 -3599979517947607200 8037811822645051776 -12870931245150988800 13803759753640704000 -8752948036761600000 2432902008176640000 + * ./numerical_methods/durand_kerner_roots.c 1 -210 20615 -1256850 53327946 + * -1672280820 40171771630 -756111184500 11310276995381 -135585182899530 + * 1307535010540395 -10142299865511450 63030812099294896 -311333643161390640 + * 1206647803780373360 -3599979517947607200 8037811822645051776 + * -12870931245150988800 13803759753640704000 -8752948036761600000 + * 2432902008176640000 * ``` */ +#include +#include #include -#include #include #include #include -#include -#include -#include "function_timer.h" +#include #define ACCURACY 1e-10 /**< maximum accuracy limit */ /** * Evaluate the value of a polynomial with given coefficients + * \param[in] coeffs coefficients of the polynomial + * \param[in] degree degree of polynomial + * \param[in] x point at which to evaluate the polynomial + * \returns \f$f(x)\f$ **/ -long double complex poly_function(double *coeffs, /**< coefficients of the polynomial */ - unsigned int degree, /**< degree of polynomial */ - long double complex x /*<< point at which to evaluate the polynomial */ -) +long double complex poly_function(double *coeffs, unsigned int degree, + long double complex x) { long double complex out = 0.; unsigned int n; @@ -41,6 +47,7 @@ long double complex poly_function(double *coeffs, /**< coefficients of the /** * create a textual form of complex number + * \param[in] x point at which to evaluate the polynomial * \returns pointer to converted string */ const char *complex_str(long double complex x) @@ -56,7 +63,9 @@ const char *complex_str(long double complex x) /** * check for termination condition - * \returns 0 if termination not reached, 1 otherwise + * \param[in] delta point at which to evaluate the polynomial + * \returns 0 if termination not reached + * \returns 1 if termination reached */ char check_termination(long double delta) { @@ -79,13 +88,17 @@ int main(int argc, char **argv) if (argc < 2) { - printf("Please pass the coefficients of the polynomial as commandline arguments.\n"); + printf("Please pass the coefficients of the polynomial as commandline " + "arguments.\n"); return 0; } - degree = argc - 1; /* detected polynomial degree */ - coeffs = (double *)malloc(degree * sizeof(double)); /* store all input coefficients */ - s0 = (long double complex *)malloc((degree - 1) * sizeof(long double complex)); /* number of roots = degree-1 */ + degree = argc - 1; /* detected polynomial degree */ + coeffs = (double *)malloc( + degree * sizeof(double)); /* store all input coefficients */ + s0 = (long double complex *)malloc( + (degree - 1) * + sizeof(long double complex)); /* number of roots = degree-1 */ /* initialize random seed: */ srand(time(NULL)); @@ -126,7 +139,8 @@ int main(int argc, char **argv) double tmp; if (n > 0) - coeffs[n] /= tmp; /* numerical errors less when the first coefficient is "1" */ + coeffs[n] /= tmp; /* numerical errors less when the first + coefficient is "1" */ else { tmp = coeffs[0]; @@ -151,11 +165,9 @@ int main(int argc, char **argv) #endif double tol_condition = 1; - double dtime; unsigned long iter = 0; - function_timer *timer = new_timer(); - start_timer(timer); + clock_t start_time = clock(); while (!check_termination(tol_condition) && iter < INT_MAX) { long double complex delta = 0; @@ -168,7 +180,8 @@ int main(int argc, char **argv) for (n = 0; n < degree - 1; n++) { - long double complex numerator = poly_function(coeffs, degree, s0[n]); + long double complex numerator = + poly_function(coeffs, degree, s0[n]); long double complex denominator = 1.0; for (i = 0; i < degree - 1; i++) if (i != n) @@ -178,7 +191,8 @@ int main(int argc, char **argv) if (isnan(cabsl(delta)) || isinf(cabsl(delta))) { - printf("\n\nOverflow/underrun error - got value = %Lg", cabsl(delta)); + printf("\n\nOverflow/underrun error - got value = %Lg", + cabsl(delta)); goto end; } @@ -206,7 +220,7 @@ int main(int argc, char **argv) } end: - dtime = end_timer_delete(timer); + clock_t end_time = clock(); #if defined(DEBUG) || !defined(NDEBUG) fclose(log_file); @@ -216,7 +230,8 @@ end: for (n = 0; n < degree - 1; n++) printf("\t%s\n", complex_str(s0[n])); printf("absolute average change: %.4g\n", tol_condition); - printf("Time taken: %.4g sec\n", dtime); + printf("Time taken: %.4g sec\n", + (end_time - start_time) / (double)CLOCKS_PER_SEC); free(coeffs); free(s0); diff --git a/numerical_methods/newton-raphson-root.c b/numerical_methods/newton-raphson-root.c index 649d05df..e699b80b 100644 --- a/numerical_methods/newton-raphson-root.c +++ b/numerical_methods/newton-raphson-root.c @@ -1,15 +1,15 @@ /** * @file - * Find approximate solution for \f$f(x) = 0\f$ using + * \brief Find approximate solution for \f$f(x) = 0\f$ using * Newton-Raphson interpolation algorithm. **/ -#include +#include /* requires minimum of C99 */ +#include #include +#include #include #include -#include -#include /* requires minimum of C99 */ #define ACCURACY 1e-10 /**< solution accuracy */ @@ -27,10 +27,7 @@ double complex func(double complex x) * Return first order derivative of the function. * \f$f'(x)\f$ */ -double complex d_func(double complex x) -{ - return 2. * x; -} +double complex d_func(double complex x) { return 2. * x; } /** * main function @@ -61,8 +58,8 @@ int main(int argc, char **argv) double r = creal(root); double c = cimag(root); - printf("Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n", counter, r, - c >= 0 ? '+' : '-', c >= 0 ? c : -c, delta); + printf("Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n", counter, + r, c >= 0 ? '+' : '-', c >= 0 ? c : -c, delta); } #endif } diff --git a/numerical_methods/qr_decomposition.c b/numerical_methods/qr_decomposition.c index 2b0d1008..0f418023 100644 --- a/numerical_methods/qr_decomposition.c +++ b/numerical_methods/qr_decomposition.c @@ -4,11 +4,11 @@ * given matrix. */ +#include "qr_decompose.h" #include #include #include -#include "qr_decompose.h" -#include +#include /** * main function @@ -56,10 +56,9 @@ int main(void) } } - function_timer *t1 = new_timer(); - start_timer(t1); + clock_t t1 = clock(); qr_decompose(A, Q, R, ROWS, COLUMNS); - double dtime = end_timer_delete(t1); + double dtime = (double)(clock() - t1) / CLOCKS_PER_SEC; print_matrix(R, ROWS, COLUMNS); print_matrix(Q, ROWS, COLUMNS); diff --git a/numerical_methods/qr_eigen_values.c b/numerical_methods/qr_eigen_values.c index 8494d3fb..68414c95 100644 --- a/numerical_methods/qr_eigen_values.c +++ b/numerical_methods/qr_eigen_values.c @@ -1,22 +1,22 @@ /** * @file - * Compute real eigen values and eigen vectors of a symmetric matrix using QR decomposition method. + * Compute real eigen values and eigen vectors of a symmetric matrix using QR + * decomposition method. */ -#include +#include "qr_decompose.h" #include +#include #include #include -#include "qr_decompose.h" -#include -#define LIMS 9 +#define LIMS 9 /**< */ /** * create a square matrix of given size with random elements + * \param[out] A matrix to create (must be pre-allocated in memory) + * \param[in] N matrix size */ -void create_matrix(double **A, /**< matrix to create (must be pre-allocated in memory) */ - int N /**< size of matrix to create */ -) +void create_matrix(double **A, int N) { int i, j, tmp, lim2 = LIMS >> 1; srand(time(NULL)); @@ -37,16 +37,17 @@ void create_matrix(double **A, /**< matrix to create (must be pre-allocated in m * Perform multiplication of two matrices. * * R2 must be equal to C1 * * Resultant matrix size should be R1xC2 + * \param[in] A first matrix to multiply + * \param[in] B second matrix to multiply + * \param[out] OUT output matrix (must be pre-allocated) + * \param[in] R1 number of rows of first matrix + * \param[in] C1 number of columns of first matrix + * \param[in] R2 number of rows of second matrix + * \param[in] C2 number of columns of second matrix * \returns pointer to resultant matrix */ -double **mat_mul(double **A, /**< first matrix to multiply */ - double **B, /**< second matrix to multiply */ - double **OUT, /**< output matrix (must be pre-allocated) */ - int R1, /**< number of rows of first matrix */ - int C1, /**< number of columns of first matrix */ - int R2, /**< number of rows of second matrix */ - int C2 /**< number of columns of second matrix */ -) +double **mat_mul(double **A, double **B, double **OUT, int R1, int C1, int R2, + int C2) { if (C1 != R2) { @@ -111,8 +112,7 @@ int main(int argc, char **argv) int counter = 0, num_eigs = rows - 1; double last_eig = 0; - function_timer *t1 = new_timer(); - start_timer(t1); + clock_t t1 = clock(); while (num_eigs > 0) /* continue till all eigen values are found */ { /* iterate with QR decomposition */ @@ -127,7 +127,8 @@ int main(int argc, char **argv) print_matrix(A, rows, columns); print_matrix(Q, rows, columns); print_matrix(R, columns, columns); - printf("-------------------- %d ---------------------\n", ++counter); + printf("-------------------- %d ---------------------\n", + ++counter); #endif mat_mul(R, Q, A, columns, columns, rows, columns); for (int i = 0; i < rows; i++) @@ -146,7 +147,7 @@ int main(int argc, char **argv) columns--; } eigen_vals[0] = A[0][0]; - double dtime = end_timer_delete(t1); + double dtime = (double)(clock() - t1) / CLOCKS_PER_SEC; #if defined(DEBUG) || !defined(NDEBUG) print_matrix(R, mat_size, mat_size); diff --git a/project_euler/Problem 23/sol1.c b/project_euler/Problem 23/sol1.c index 3ba1ff9c..23e8369a 100644 --- a/project_euler/Problem 23/sol1.c +++ b/project_euler/Problem 23/sol1.c @@ -4,7 +4,6 @@ #ifdef _OPENMP #include #endif -#include "function_timer.h" unsigned long MAX_N = 28123; @@ -91,20 +90,19 @@ int main(int argc, char **argv) printf("Not using parallleization!\n"); #endif - double total_duration = 0; + double total_duration = 0.f; long i; - function_timer *timer = new_timer(); #ifdef _OPENMP #pragma omp parallel for reduction(+ \ : sum) schedule(runtime) #endif for (i = 1; i <= MAX_N; i++) { - start_timer(timer); + clock_t start_time = clock(); if (!is_sum_of_abundant(i)) sum += i; clock_t end_time = clock(); - total_duration += end_timer(timer); + total_duration += (double)(end_time - start_time) / CLOCKS_PER_SEC; printf("... %5lu: %8lu\r", i, sum); if (i % 100 == 0) @@ -114,6 +112,5 @@ int main(int argc, char **argv) printf("Time taken: %.4g s\n", total_duration); printf("Sum of numbers that cannot be represented as sum of two abundant numbers : %lu\n", sum); - delete_timer(timer); return 0; }