diff options
Diffstat (limited to 'src/rcc_mpfr.c')
-rw-r--r-- | src/rcc_mpfr.c | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/rcc_mpfr.c b/src/rcc_mpfr.c new file mode 100644 index 0000000..8a362e3 --- /dev/null +++ b/src/rcc_mpfr.c @@ -0,0 +1,124 @@ +/* +Copyright 2015 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "rcc_mpfr.h" +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +// define MPFR_USE_VA_LIST to enable the use of mpfr_inits and mpfr_clears +#define MPFR_USE_VA_LIST +// define MPFR_USE_FILE to enable the use of mpfr_printf +#define MPFR_USE_FILE +#include <mpfr.h> +#include <math.h> +#include "array.h" + +// init +int init_RCC_mpfr(RCC_mpfr* rcc_mpfr, int size){ + int i; + (*rcc_mpfr).values=calloc(size,sizeof(mpfr_t)); + (*rcc_mpfr).indices=calloc(size,sizeof(int)); + (*rcc_mpfr).length=size; + for(i=0;i<size;i++){ + mpfr_init((*rcc_mpfr).values[i]); + } + + return(0); +} + +int free_RCC_mpfr(RCC_mpfr rcc_mpfr){ + int i; + for(i=0;i<rcc_mpfr.length;i++){ + mpfr_clear(rcc_mpfr.values[i]); + } + free(rcc_mpfr.values); + free(rcc_mpfr.indices); + return(0); +} + +// set a given element of an rcc_mpfr +int RCC_mpfr_set_elem(mpfr_t value, int index, RCC_mpfr* rcc_mpfr, int pos){ + mpfr_set((*rcc_mpfr).values[pos], value, MPFR_RNDN); + (*rcc_mpfr).indices[pos]=index; + return(0); +} + +int RCC_mpfr_cpy(RCC_mpfr input,RCC_mpfr* output){ + int i; + + init_RCC_mpfr(output,input.length); + for(i=0;i<input.length;i++){ + RCC_mpfr_set_elem(input.values[i], input.indices[i], output, i); + } + return(0); +} + +// concatenate rcc_mpfr +int RCC_mpfr_concat(RCC_mpfr rcc_mpfr1, RCC_mpfr rcc_mpfr2, RCC_mpfr* output){ + int i; + + init_RCC_mpfr(output,rcc_mpfr1.length+rcc_mpfr2.length); + + for(i=0;i<rcc_mpfr1.length;i++){ + RCC_mpfr_set_elem(rcc_mpfr1.values[i], rcc_mpfr1.indices[i], output, i); + } + + for(i=0;i<rcc_mpfr2.length;i++){ + RCC_mpfr_set_elem(rcc_mpfr2.values[i], rcc_mpfr2.indices[i], output, i+rcc_mpfr1.length); + } + + return(0); +} + +// append an rcc_mpfr at the end of another +int RCC_mpfr_append(RCC_mpfr input, RCC_mpfr* output){ + int i; + for(i=0;i<input.length;i++){ + RCC_mpfr_set_elem(input.values[i], input.indices[i], output, i+(*output).length); + } + (*output).length+=input.length; + return(0); +} + +// print an rcc_mpfr vector with maximal precision +int RCC_mpfr_print(RCC_mpfr rcc_mpfr){ + int j; + // the printf format + Char_Array printf_format; + // number of digits in output + int size; + + // compute size + // WARNING: assumes mpfr_default_prec is an int + size=mpfr_get_default_prec()*log10(2)-1; + + init_Char_Array(&printf_format,12); + char_array_snprintf(&printf_format,"%%d:%%.%dRe",size); + + for(j=0;j<rcc_mpfr.length;j++){ + mpfr_printf(printf_format.str,rcc_mpfr.indices[j],rcc_mpfr.values[j]); + if(j<rcc_mpfr.length-1){ + printf(",\n"); + } + else{ + printf("\n"); + } + } + + free_Char_Array(printf_format); + + return(0); +} |