Ian Jauslin
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Jauslin <ian.jauslin@roma1.infn.it>2016-05-20 20:30:15 +0000
committerIan Jauslin <ian.jauslin@roma1.infn.it>2016-05-20 20:30:15 +0000
commit2125f01f97abfe343fc5e0cc078bf1d081b2e441 (patch)
tree932dc60739384224be31f9e894ae63055634435e /doc/libinum-examples
Initial commitv1.0
Diffstat (limited to 'doc/libinum-examples')
-rw-r--r--doc/libinum-examples/Makefile80
-rwxr-xr-xdoc/libinum-examples/build/integral_gauss-legendrebin0 -> 127104 bytes
-rwxr-xr-xdoc/libinum-examples/build/root_newtonbin0 -> 18712 bytes
-rw-r--r--doc/libinum-examples/objs/integral_gauss-legendre.obin0 -> 11072 bytes
-rw-r--r--doc/libinum-examples/objs/root_newton.obin0 -> 9048 bytes
-rw-r--r--doc/libinum-examples/src/integral_gauss-legendre.c92
-rw-r--r--doc/libinum-examples/src/root_newton.c58
7 files changed, 230 insertions, 0 deletions
diff --git a/doc/libinum-examples/Makefile b/doc/libinum-examples/Makefile
new file mode 100644
index 0000000..5c7ccc1
--- /dev/null
+++ b/doc/libinum-examples/Makefile
@@ -0,0 +1,80 @@
+# whether to link dynamically
+# if static=0 then link dynamically
+# if static=2 then link statically
+# if static=1 then link libkondo statically but other libraries dynamically
+STATIC=1
+
+VERSION=0.3.1
+
+# products of the compilation
+PROJECT_BINS= root_newton integral_gauss-legendre
+
+# debug and optimization flags
+#DB= -ggdb
+OPT= -O3
+
+# warning flags
+WARNINGS= -Wall -Wextra -Wno-strict-overflow -std=c99 -pedantic -Wno-unused-parameter
+
+# compiler
+CC=/usr/bin/gcc
+LD=$(CC)
+
+# directories
+INCLUDE =
+LIB =
+#INCLUDE = -I../../include
+#LIB = -L../../build
+
+# flags
+override LDFLAGS +=$(LIB)
+override CFLAGS +=$(INCLUDE) $(DB) $(OPT) $(WARNINGS)
+
+# build directories
+BUILDDIR=./build
+SRCDIR=./src
+OBJDIR=./objs
+
+# objects
+OBJS = $(addprefix $(OBJDIR)/, root_newton.o integral_gauss-legendre.o)
+
+# flags which depend on whether to link statically or dynamically
+# lib flag
+LIBINUM_FLAG=-linum
+# additional library required for static linking
+XTRA_LIBS=
+
+ifeq ($(STATIC),1)
+ # libinum is linked against libm, libmpfr, libgmp and libpthread
+ XTRA_LIBS=-lm -lmpfr -lgmp -lpthread
+ # link binaries using the static library
+ LIBINUM_FLAG=-l:libinum.a
+else ifeq ($(STATIC),2)
+ # libinum is linked against libm, libmpfr, libgmp and libpthread
+ XTRA_LIBS=-lm -lmpfr -lgmp -lpthread
+ # link binaries statically
+ override LDFLAGS += -static
+else
+ # required flag for subsequent dynamic linking
+ override CFLAGS += -fPIC
+endif
+
+
+all: init $(PROJECT_BINS)
+
+# create dirs
+init:
+ @[ -d $(OBJDIR) ] || /bin/mkdir $(OBJDIR)
+ @[ -d $(BUILDDIR) ] || /bin/mkdir $(BUILDDIR)
+
+root_newton:
+ $(CC) -c $(CFLAGS) src/$@.c -o objs/$@.o
+ $(LD) $(LDFLAGS) -o $(BUILDDIR)/$@ objs/$@.o $(LIBINUM_FLAG) $(XTRA_LIBS)
+
+integral_gauss-legendre:
+ $(CC) -c $(CFLAGS) src/$@.c -o objs/$@.o
+ $(LD) $(LDFLAGS) -o $(BUILDDIR)/$@ objs/$@.o $(LIBINUM_FLAG) $(XTRA_LIBS)
+
+clean:
+ @rm -rf $(OBJDIR)
+ @rm -rf $(BUILDDIR)
diff --git a/doc/libinum-examples/build/integral_gauss-legendre b/doc/libinum-examples/build/integral_gauss-legendre
new file mode 100755
index 0000000..b86cfe7
--- /dev/null
+++ b/doc/libinum-examples/build/integral_gauss-legendre
Binary files differ
diff --git a/doc/libinum-examples/build/root_newton b/doc/libinum-examples/build/root_newton
new file mode 100755
index 0000000..68a2e50
--- /dev/null
+++ b/doc/libinum-examples/build/root_newton
Binary files differ
diff --git a/doc/libinum-examples/objs/integral_gauss-legendre.o b/doc/libinum-examples/objs/integral_gauss-legendre.o
new file mode 100644
index 0000000..83841c9
--- /dev/null
+++ b/doc/libinum-examples/objs/integral_gauss-legendre.o
Binary files differ
diff --git a/doc/libinum-examples/objs/root_newton.o b/doc/libinum-examples/objs/root_newton.o
new file mode 100644
index 0000000..1e567f7
--- /dev/null
+++ b/doc/libinum-examples/objs/root_newton.o
Binary files differ
diff --git a/doc/libinum-examples/src/integral_gauss-legendre.c b/doc/libinum-examples/src/integral_gauss-legendre.c
new file mode 100644
index 0000000..f65465f
--- /dev/null
+++ b/doc/libinum-examples/src/integral_gauss-legendre.c
@@ -0,0 +1,92 @@
+#include <stdio.h>
+#include <stdarg.h>
+// define MPFR_USE_VA_LIST to enable the use of mpfr_inits and mpfr_clears
+#define MPFR_USE_VA_LIST
+#include <mpfr.h>
+#include <libinum.h>
+
+int func(mpfr_t* out, mpfr_t in, void* extra_args);
+
+int main(int argc, const char* argv[]){
+ int ret;
+ mpfr_t tolerance, val, lower, upper;
+ array_mpfr abcissa, weights;
+
+ // precision of MPFR floats
+ mpfr_set_default_prec(53);
+
+ // tolerance for computing weights
+ mpfr_init(tolerance);
+ mpfr_set_d(tolerance, 1.e-11, MPFR_RNDN);
+
+ // compute weights
+ ret=gauss_legendre_weights_mpfr(10, tolerance, 10000, &abcissa, &weights);
+ // return codes
+ if(ret==LIBINUM_ERROR_MAXITER){
+ printf("error: maximum number of iterations reached when computing the integration abcissa\n");
+ mpfr_clear(tolerance);
+ return(ret);
+ }
+ else if(ret==LIBINUM_ERROR_NAN){
+ printf("error: infinity encountered when computing the integration abcissa\n");
+ mpfr_clear(tolerance);
+ return(ret);
+ }
+ else{
+ printf("abcissa:\n");
+ array_mpfr_print(abcissa);
+ printf("\nweights:\n");
+ array_mpfr_print(weights);
+ printf("\n");
+ }
+
+ mpfr_clear(tolerance);
+
+ mpfr_inits(val, lower, upper, NULL);
+
+ mpfr_set_ui(lower, 0, MPFR_RNDN);
+ mpfr_set_ui(upper, 2, MPFR_RNDN);
+
+ ret=integrate_gauss_mpfr(&val, &func, lower, upper, abcissa, weights, NULL);
+ // return codes
+ if(ret==LIBINUM_ERROR_SIZE_MISMATCH){
+ printf("error: the mpfr_arrays 'abcissa' and 'weights' must have the same length\n");
+ mpfr_clears(val, upper, lower, NULL);
+ array_mpfr_free(abcissa);
+ array_mpfr_free(weights);
+ return(ret);
+ }
+ else if(ret==LIBINUM_ERROR_NAN){
+ printf("error: the integrand is singular\n");
+ array_mpfr_free(abcissa);
+ array_mpfr_free(weights);
+ mpfr_clears(val, upper, lower, NULL);
+ return(ret);
+ }
+ else{
+ printf("\\int_0^2 x/sin(x) dx = ");
+ fprint_mpfr(stdout, val);
+ printf("\n");
+ }
+
+ mpfr_clears(val, upper, lower, NULL);
+ array_mpfr_free(abcissa);
+ array_mpfr_free(weights);
+
+ return(0);
+}
+
+/*
+// e^x
+int func(mpfr_t* out, mpfr_t in, void* extra_args){
+ mpfr_exp(*out, in, MPFR_RNDN);
+ return(0);
+}
+*/
+
+// x/sin(x)
+int func(mpfr_t* out, mpfr_t in, void* extra_args){
+ mpfr_sin(*out, in, MPFR_RNDN);
+ mpfr_div(*out, in, *out, MPFR_RNDN);
+ return(0);
+}
diff --git a/doc/libinum-examples/src/root_newton.c b/doc/libinum-examples/src/root_newton.c
new file mode 100644
index 0000000..f307c58
--- /dev/null
+++ b/doc/libinum-examples/src/root_newton.c
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <stdarg.h>
+// define MPFR_USE_VA_LIST to enable the use of mpfr_inits and mpfr_clears
+#define MPFR_USE_VA_LIST
+#include <mpfr.h>
+#include <libinum.h>
+
+int func(mpfr_t* out, mpfr_t in, void* extra_args);
+int dfunc(mpfr_t* out, mpfr_t in, void* extra_args);
+
+int main(int argc, const char* argv[]){
+ int ret;
+ mpfr_t init, prec, x;
+ unsigned int maxiter;
+ int extra_arg;
+
+ mpfr_inits(init, prec, x, NULL);
+
+ // start at 2
+ mpfr_set_ui(init, 2, MPFR_RNDN);
+ // precision
+ mpfr_set_d(prec, 1.e-30, MPFR_RNDN);
+ // maximum number of iterations before error
+ maxiter=10000;
+ // extra argument
+ extra_arg=4;
+
+ // compute root
+ ret=root_newton_mpfr(&x, &func, &dfunc, init, prec, maxiter, &extra_arg);
+
+ // return codes
+ if(ret==LIBINUM_ERROR_MAXITER){
+ printf("error: maximum number of iterations reached\n");
+ }
+ else if(ret==LIBINUM_ERROR_NAN){
+ printf("error: infinity encountered\n");
+ }
+ else{
+ mpfr_printf("% 14.7Re\n", x);
+ }
+
+ mpfr_clears(init, prec, x, NULL);
+
+ return(0);
+}
+
+// x^2-1
+int func(mpfr_t* out, mpfr_t in, void* extra_args){
+ mpfr_pow_ui(*out, in, 2, MPFR_RNDN);
+ mpfr_add_d(*out, *out, -1./ *((int*)extra_args), MPFR_RNDN);
+ return(0);
+}
+
+// 2*x
+int dfunc(mpfr_t* out, mpfr_t in, void* extra_args){
+ mpfr_mul_ui(*out, in, 2, MPFR_RNDN);
+ return(0);
+}