Ian Jauslin
summaryrefslogtreecommitdiff
blob: 7703b1e5017626a9884d0aa52f15695a42e8cc9f (plain)
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
/*
Copyright 2015-2022 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);
}
int RCC_mpfr_cpy_noinit(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);
  }
  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);
}