From faa7da232afaa109040fbf6ebd8aacf010aa267b Mon Sep 17 00:00:00 2001 From: Aleksandar Bukva Date: Thu, 11 Jun 2015 19:42:29 +0200 Subject: [PATCH 1/4] math: p_exp: Implement exp function using series --- src/math/p_exp.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/math/p_exp.c b/src/math/p_exp.c index aa5fc5b..20c973e 100644 --- a/src/math/p_exp.c +++ b/src/math/p_exp.c @@ -3,7 +3,7 @@ /** * * Calculate exponent (e^a), where e is the base of the natural logarithm - * (2.71828.) + * (2.71828.) Using series expansion of function * * @param a Pointer to input vector * @@ -18,12 +18,51 @@ * @return None * */ -#include +static const int precision = 0.1e-6; + +float inverse(float a) //function to calucalte inverse without division operator using Newton Raphson method +{ + float result = precision; + while(result * a > 1 + precision || result * a < 1 - precision) + { + result *= (2 - a * result); + } + return result; +} + +float single_exp(float b) +{ + float a = b; + if(b < 0) //if exponent is negative then calculate e^b and return 1/e^b + { + a = -a; + } + float result = 1; + float counter = 1; + float next_term = 1; + float old_result; + float error; + do + { + old_result = result; + next_term = next_term * a * inverse(counter); + result += next_term; + counter += 1; + error = (old_result - result) / result; + if(error < 0) + { + error = -error; + } + }while(error > precision); + if(b < 0) + return inverse(result); + return result; +} void p_exp_f32(const float *a, float *c, int n, int p, p_team_t team) { int i; for (i = 0; i < n; i++) { - *(c + i) = expf(*(a + i)); + *(c + i) = single_exp(*(a + i)); } } From 1f4a121268b64a1dfcd83b4dcdd3166a19e8e0a1 Mon Sep 17 00:00:00 2001 From: Aleksandar Bukva Date: Fri, 12 Jun 2015 01:19:29 +0200 Subject: [PATCH 2/4] math: p_exp: Corrected previous version --- src/math/p_exp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/math/p_exp.c b/src/math/p_exp.c index 20c973e..b107995 100644 --- a/src/math/p_exp.c +++ b/src/math/p_exp.c @@ -18,7 +18,7 @@ * @return None * */ -static const int precision = 0.1e-6; +static const float precision = 0.1e-6; float inverse(float a) //function to calucalte inverse without division operator using Newton Raphson method { From 91975c9c4553d9fd40650c8102bf4b21a66aff3e Mon Sep 17 00:00:00 2001 From: Aleksandar Bukva Date: Fri, 12 Jun 2015 11:56:35 +0200 Subject: [PATCH 3/4] Fixing build error --- src/math/p_exp.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/math/p_exp.c b/src/math/p_exp.c index befbbe3..b107995 100644 --- a/src/math/p_exp.c +++ b/src/math/p_exp.c @@ -11,10 +11,13 @@ * * @param n Size of 'a' and 'c' vector. * + * @param p Number of processor to use (task parallelism) + * + * @param team Team to work with + * * @return None * */ -<<<<<<< HEAD static const float precision = 0.1e-6; float inverse(float a) //function to calucalte inverse without division operator using Newton Raphson method @@ -56,10 +59,6 @@ float single_exp(float b) return result; } void p_exp_f32(const float *a, float *c, int n, int p, p_team_t team) -======= -#include -void p_exp_f32(const float *a, float *c, int n) ->>>>>>> upstream/master { int i; From a1fa70f2f0959d0ab392567dbf878644d00bf66a Mon Sep 17 00:00:00 2001 From: Aleksandar Bukva Date: Fri, 12 Jun 2015 12:06:44 +0200 Subject: [PATCH 4/4] changed fucntion declarationt to match with current version without team parameter --- src/math/p_exp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/math/p_exp.c b/src/math/p_exp.c index b107995..6ca5c9c 100644 --- a/src/math/p_exp.c +++ b/src/math/p_exp.c @@ -58,7 +58,7 @@ float single_exp(float b) return inverse(result); return result; } -void p_exp_f32(const float *a, float *c, int n, int p, p_team_t team) +void p_exp_f32(const float *a, float *c, int n) { int i;