1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
#include <sollya.h>
#include <mpfr.h>
#include <mpfi.h>
#include <stdlib.h>
#include <string.h>
typedef struct data_struct_t {
char text[32];
int counter;
} data_t;
int myownlog(mpfi_t result, mpfi_t x, int n, void *ptr) {
data_t *data;
data = (data_t *) ptr;
sollya_lib_printf(">>>>%s<<<<>>>>%d<<<<\n", data->text, data->counter);
(data->counter)++;
if(n==0) {
/* Implementation of the neperian logarithm */
if(mpfi_nan_p(x)) {
mpfr_t a;
mpfr_init2(a,mpfi_get_prec(result));
mpfi_interv_fr(result,a,a);
mpfr_clear(a);
return 0;
}
if(mpfi_has_zero(x)) {
mpfr_t a,b;
mpfr_init2(a,mpfi_get_prec(result));
mpfr_init2(b,mpfi_get_prec(result));
mpfr_set_inf(a,-1);
mpfi_get_right(b,x);
mpfr_log(b,b,GMP_RNDU);
mpfi_interv_fr(result, a, b);
mpfr_clear(a);
mpfr_clear(b);
return 0;
}
else {
mpfr_t a,b;
mpfr_init2(a,mpfi_get_prec(result));
mpfr_init2(b,mpfi_get_prec(result));
mpfi_get_left(a,x);
mpfr_log(a,a, GMP_RNDD);
mpfi_get_right(b,x);
mpfr_log(b,b,GMP_RNDU);
mpfi_interv_fr(result, a, b);
mpfr_clear(a);
mpfr_clear(b);
return 0;
}
}
if(n==1) {
/* Implementation of 1/x */
mpfi_inv(result,x);
return 0;
}
if(n==2) {
/* Implementation of -1/x^2 */
mpfi_t temp;
mpfi_init2(temp, mpfi_get_prec(result));
mpfi_sqr(temp, x);
mpfi_inv(temp, temp);
mpfi_neg(result, temp);
mpfi_clear(temp);
return 0;
}
/* else */
mpfr_t a,b;
mpfr_init2(a, mpfi_get_prec(result));
mpfr_init2(b, mpfi_get_prec(result));
mpfr_set_inf(a,-1);
mpfr_set_inf(b, 1);
mpfi_interv_fr(result,a,b);
mpfr_clear(a);
mpfr_clear(b);
return 0;
}
int myownlog2(mpfi_t result, mpfi_t x, int n, void *ptr) {
data_t *data;
data = (data_t *) ptr;
sollya_lib_printf(">>>>%s<<<<>>>>%d<<<<\n", data->text, data->counter);
(data->counter)++;
if(n==0) {
/* Implementation of the neperian logarithm */
if(mpfi_nan_p(x)) {
mpfr_t a;
mpfr_init2(a,mpfi_get_prec(result));
mpfi_interv_fr(result,a,a);
mpfr_clear(a);
return 0;
}
if(mpfi_has_zero(x)) {
mpfr_t a,b;
mpfr_init2(a,mpfi_get_prec(result));
mpfr_init2(b,mpfi_get_prec(result));
mpfr_set_inf(a,-1);
mpfi_get_right(b,x);
mpfr_log(b,b,GMP_RNDU);
mpfi_interv_fr(result, a, b);
mpfr_clear(a);
mpfr_clear(b);
return 0;
}
else {
mpfr_t a,b;
mpfr_init2(a,mpfi_get_prec(result));
mpfr_init2(b,mpfi_get_prec(result));
mpfi_get_left(a,x);
mpfr_log(a,a, GMP_RNDD);
mpfi_get_right(b,x);
mpfr_log(b,b,GMP_RNDU);
mpfi_interv_fr(result, a, b);
mpfr_clear(a);
mpfr_clear(b);
return 0;
}
}
if(n==1) {
/* Implementation of 1/x */
mpfi_inv(result,x);
return 0;
}
if(n==2) {
/* Implementation of -1/x^2 */
mpfi_t temp;
mpfi_init2(temp, mpfi_get_prec(result));
mpfi_sqr(temp, x);
mpfi_inv(temp, temp);
mpfi_neg(result, temp);
mpfi_clear(temp);
return 0;
}
/* else */
mpfr_t a,b;
mpfr_init2(a, mpfi_get_prec(result));
mpfr_init2(b, mpfi_get_prec(result));
mpfr_set_inf(a,-1);
mpfr_set_inf(b, 1);
mpfi_interv_fr(result,a,b);
mpfr_clear(a);
mpfr_clear(b);
return 0;
}
void dealloc(void *ptr) {
data_t *data;
data = (data_t *) ptr;
sollya_lib_printf("Deallocation function called for the data pointer (%s<<<>>>%d)\n", data->text, data->counter);
return;
}
int main(void) {
sollya_obj_t X, f, g;
mpfr_t x,y;
data_t data = { "Hello world", 0 };
sollya_lib_init();
X = sollya_lib_free_variable();
f = sollya_lib_libraryfunction_with_data(X, "superfunc", myownlog, &data, NULL);
sollya_lib_printf("%b (expecting: superfunc)\n", f);
mpfr_init2(x, 30);
mpfr_init2(y, 50);
mpfr_set_ui(x, 2, GMP_RNDN);
sollya_lib_evaluate_function_at_point(y, f, x, NULL);
sollya_lib_printf("%v (expecting: approximate value of log(2))\n", y);
sollya_lib_clear_obj(f);
sollya_lib_clear_obj(X);
f = sollya_lib_parse_string("diff(superfunc(sin(_x_)))");
sollya_lib_printf("%b (expecting: (diff(superfunc))(sin(_x_)) * cos(_x_))\n", f);
sollya_lib_clear_obj(f);
f = sollya_lib_parse_string("diff(superfunc(_x_))");
sollya_lib_printf("%b (expecting: diff(superfunc))\n", f);
mpfr_set_ui(x, 4, GMP_RNDN);
sollya_lib_evaluate_function_at_point(y, f, x, NULL);
sollya_lib_printf("%v (expecting: approximate value of 1/4)\n", y);
sollya_lib_clear_obj(f);
X = sollya_lib_free_variable();
f = sollya_lib_libraryfunction_with_data(X, "superfunc2", myownlog2, &data, dealloc);
g = SOLLYA_ADD(SOLLYA_CONST(3), sollya_lib_copy_obj(f));
sollya_lib_printf("%b (expecting: superfunc2)\n", f);
mpfr_set_ui(x, 2, GMP_RNDN);
sollya_lib_evaluate_function_at_point(y, f, x, NULL);
sollya_lib_printf("%v (expecting: approximate value of log(2))\n", y);
sollya_lib_clear_obj(f);
sollya_lib_printf("Deallocation function must not yet have been called\n");
sollya_lib_clear_obj(X);
sollya_lib_clear_obj(g);
mpfr_clear(x);
mpfr_clear(y);
sollya_lib_close();
return 0;
}
|