remove dependencies on function_timer

This commit is contained in:
Krishna Vedala 2020-05-29 12:34:58 -04:00
parent 8f45f7e680
commit 4caa46c10c
6 changed files with 73 additions and 67 deletions

View File

@ -6,7 +6,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include "function_timer.h"
/** /**
* dynamically large number * dynamically large number
@ -102,13 +101,11 @@ int main(int argc, char *argv[])
large_num *result = new_number(); large_num *result = new_number();
function_timer *timer = new_timer();
clock_t start_time = clock(); clock_t start_time = clock();
start_timer(timer);
for (i = 2; i <= number; i++) for (i = 2; i <= number; i++)
/* Multiply every number from 2 thru N */ /* Multiply every number from 2 thru N */
multiply(result, i); 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; // time_taken = (clock() - start_time) / (double) CLOCKS_PER_SEC;
printf("%d! = ", number); printf("%d! = ", number);

View File

@ -7,28 +7,34 @@
* *
* Try the highly unstable Wilkinson's polynomial: * 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 <complex.h>
#include <limits.h>
#include <math.h> #include <math.h>
#include <time.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <limits.h> #include <time.h>
#include <complex.h>
#include "function_timer.h"
#define ACCURACY 1e-10 /**< maximum accuracy limit */ #define ACCURACY 1e-10 /**< maximum accuracy limit */
/** /**
* Evaluate the value of a polynomial with given coefficients * 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 */ long double complex poly_function(double *coeffs, unsigned int degree,
unsigned int degree, /**< degree of polynomial */ long double complex x)
long double complex x /*<< point at which to evaluate the polynomial */
)
{ {
long double complex out = 0.; long double complex out = 0.;
unsigned int n; unsigned int n;
@ -41,6 +47,7 @@ long double complex poly_function(double *coeffs, /**< coefficients of the
/** /**
* create a textual form of complex number * create a textual form of complex number
* \param[in] x point at which to evaluate the polynomial
* \returns pointer to converted string * \returns pointer to converted string
*/ */
const char *complex_str(long double complex x) const char *complex_str(long double complex x)
@ -56,7 +63,9 @@ const char *complex_str(long double complex x)
/** /**
* check for termination condition * 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) char check_termination(long double delta)
{ {
@ -79,13 +88,17 @@ int main(int argc, char **argv)
if (argc < 2) 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; return 0;
} }
degree = argc - 1; /* detected polynomial degree */ degree = argc - 1; /* detected polynomial degree */
coeffs = (double *)malloc(degree * sizeof(double)); /* store all input coefficients */ coeffs = (double *)malloc(
s0 = (long double complex *)malloc((degree - 1) * sizeof(long double complex)); /* number of roots = degree-1 */ 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: */ /* initialize random seed: */
srand(time(NULL)); srand(time(NULL));
@ -126,7 +139,8 @@ int main(int argc, char **argv)
double tmp; double tmp;
if (n > 0) 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 else
{ {
tmp = coeffs[0]; tmp = coeffs[0];
@ -151,11 +165,9 @@ int main(int argc, char **argv)
#endif #endif
double tol_condition = 1; double tol_condition = 1;
double dtime;
unsigned long iter = 0; unsigned long iter = 0;
function_timer *timer = new_timer(); clock_t start_time = clock();
start_timer(timer);
while (!check_termination(tol_condition) && iter < INT_MAX) while (!check_termination(tol_condition) && iter < INT_MAX)
{ {
long double complex delta = 0; long double complex delta = 0;
@ -168,7 +180,8 @@ int main(int argc, char **argv)
for (n = 0; n < degree - 1; n++) 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; long double complex denominator = 1.0;
for (i = 0; i < degree - 1; i++) for (i = 0; i < degree - 1; i++)
if (i != n) if (i != n)
@ -178,7 +191,8 @@ int main(int argc, char **argv)
if (isnan(cabsl(delta)) || isinf(cabsl(delta))) 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; goto end;
} }
@ -206,7 +220,7 @@ int main(int argc, char **argv)
} }
end: end:
dtime = end_timer_delete(timer); clock_t end_time = clock();
#if defined(DEBUG) || !defined(NDEBUG) #if defined(DEBUG) || !defined(NDEBUG)
fclose(log_file); fclose(log_file);
@ -216,7 +230,8 @@ end:
for (n = 0; n < degree - 1; n++) for (n = 0; n < degree - 1; n++)
printf("\t%s\n", complex_str(s0[n])); printf("\t%s\n", complex_str(s0[n]));
printf("absolute average change: %.4g\n", tol_condition); 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(coeffs);
free(s0); free(s0);

View File

@ -1,15 +1,15 @@
/** /**
* @file * @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. * Newton-Raphson interpolation algorithm.
**/ **/
#include <stdio.h> #include <complex.h> /* requires minimum of C99 */
#include <limits.h>
#include <math.h> #include <math.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <limits.h>
#include <complex.h> /* requires minimum of C99 */
#define ACCURACY 1e-10 /**< solution accuracy */ #define ACCURACY 1e-10 /**< solution accuracy */
@ -27,10 +27,7 @@ double complex func(double complex x)
* Return first order derivative of the function. * Return first order derivative of the function.
* \f$f'(x)\f$ * \f$f'(x)\f$
*/ */
double complex d_func(double complex x) double complex d_func(double complex x) { return 2. * x; }
{
return 2. * x;
}
/** /**
* main function * main function
@ -61,8 +58,8 @@ int main(int argc, char **argv)
double r = creal(root); double r = creal(root);
double c = cimag(root); double c = cimag(root);
printf("Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n", counter, r, printf("Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n", counter,
c >= 0 ? '+' : '-', c >= 0 ? c : -c, delta); r, c >= 0 ? '+' : '-', c >= 0 ? c : -c, delta);
} }
#endif #endif
} }

View File

@ -4,11 +4,11 @@
* given matrix. * given matrix.
*/ */
#include "qr_decompose.h"
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include "qr_decompose.h" #include <time.h>
#include <function_timer.h>
/** /**
* main function * main function
@ -56,10 +56,9 @@ int main(void)
} }
} }
function_timer *t1 = new_timer(); clock_t t1 = clock();
start_timer(t1);
qr_decompose(A, Q, R, ROWS, COLUMNS); 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(R, ROWS, COLUMNS);
print_matrix(Q, ROWS, COLUMNS); print_matrix(Q, ROWS, COLUMNS);

View File

@ -1,22 +1,22 @@
/** /**
* @file * @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 <stdio.h> #include "qr_decompose.h"
#include <math.h> #include <math.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include "qr_decompose.h"
#include <function_timer.h>
#define LIMS 9 #define LIMS 9 /**< */
/** /**
* create a square matrix of given size with random elements * 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) */ void create_matrix(double **A, int N)
int N /**< size of matrix to create */
)
{ {
int i, j, tmp, lim2 = LIMS >> 1; int i, j, tmp, lim2 = LIMS >> 1;
srand(time(NULL)); 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. * Perform multiplication of two matrices.
* * R2 must be equal to C1 * * R2 must be equal to C1
* * Resultant matrix size should be R1xC2 * * 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 * \returns pointer to resultant matrix
*/ */
double **mat_mul(double **A, /**< first matrix to multiply */ double **mat_mul(double **A, double **B, double **OUT, int R1, int C1, int R2,
double **B, /**< second matrix to multiply */ int C2)
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 */
)
{ {
if (C1 != R2) if (C1 != R2)
{ {
@ -111,8 +112,7 @@ int main(int argc, char **argv)
int counter = 0, num_eigs = rows - 1; int counter = 0, num_eigs = rows - 1;
double last_eig = 0; double last_eig = 0;
function_timer *t1 = new_timer(); clock_t t1 = clock();
start_timer(t1);
while (num_eigs > 0) /* continue till all eigen values are found */ while (num_eigs > 0) /* continue till all eigen values are found */
{ {
/* iterate with QR decomposition */ /* iterate with QR decomposition */
@ -127,7 +127,8 @@ int main(int argc, char **argv)
print_matrix(A, rows, columns); print_matrix(A, rows, columns);
print_matrix(Q, rows, columns); print_matrix(Q, rows, columns);
print_matrix(R, columns, columns); print_matrix(R, columns, columns);
printf("-------------------- %d ---------------------\n", ++counter); printf("-------------------- %d ---------------------\n",
++counter);
#endif #endif
mat_mul(R, Q, A, columns, columns, rows, columns); mat_mul(R, Q, A, columns, columns, rows, columns);
for (int i = 0; i < rows; i++) for (int i = 0; i < rows; i++)
@ -146,7 +147,7 @@ int main(int argc, char **argv)
columns--; columns--;
} }
eigen_vals[0] = A[0][0]; 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) #if defined(DEBUG) || !defined(NDEBUG)
print_matrix(R, mat_size, mat_size); print_matrix(R, mat_size, mat_size);

View File

@ -4,7 +4,6 @@
#ifdef _OPENMP #ifdef _OPENMP
#include <omp.h> #include <omp.h>
#endif #endif
#include "function_timer.h"
unsigned long MAX_N = 28123; unsigned long MAX_N = 28123;
@ -91,20 +90,19 @@ int main(int argc, char **argv)
printf("Not using parallleization!\n"); printf("Not using parallleization!\n");
#endif #endif
double total_duration = 0; double total_duration = 0.f;
long i; long i;
function_timer *timer = new_timer();
#ifdef _OPENMP #ifdef _OPENMP
#pragma omp parallel for reduction(+ \ #pragma omp parallel for reduction(+ \
: sum) schedule(runtime) : sum) schedule(runtime)
#endif #endif
for (i = 1; i <= MAX_N; i++) for (i = 1; i <= MAX_N; i++)
{ {
start_timer(timer); clock_t start_time = clock();
if (!is_sum_of_abundant(i)) if (!is_sum_of_abundant(i))
sum += i; sum += i;
clock_t end_time = clock(); 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); printf("... %5lu: %8lu\r", i, sum);
if (i % 100 == 0) if (i % 100 == 0)
@ -114,6 +112,5 @@ int main(int argc, char **argv)
printf("Time taken: %.4g s\n", total_duration); 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); printf("Sum of numbers that cannot be represented as sum of two abundant numbers : %lu\n", sum);
delete_timer(timer);
return 0; return 0;
} }