Statistiques
| Révision :

root / sollyaIntegration-4.0 / src / taylorModel-01.c @ 34

Historique | Voir | Annoter | Télécharger (6,66 ko)

1
/** @file taylorModel-01.c
2
 * taylorModel-01: check the taylorform function.
3
 *
4
 * @author S.T.
5
 * @date 2012-11-21
6
 *
7
 */
8
/******************************************************************************/
9
/* Headers, applying the "particular to general" convention.*/
10

    
11
#include "pobyso4/pobyso.h"
12

    
13
/* includes of local headers */
14

    
15
/* includes of project headers */
16

    
17
/* includes of system headers */
18
#include <stdlib.h>
19
#include <gmp.h>
20
#include <sollya.h>
21
#include <setjmp.h>
22
#include <string.h>
23
/* Other declarations */
24

    
25
/* Internal prototypes */
26

    
27
/* Types, constants and macros definitions */
28
#define NUM_ARGS 5
29
#define VARIABLE_NAME_STRING "x"
30
#define RADIX 10
31
/* Global variables */
32

    
33
/* Functions */
34
int
35
main(int argc, char** argv)
36
{
37
  //jmp_buf recover;
38
  sollya_obj_t functionObj      = NULL;
39
  char *freeVariableName        = NULL;
40
  char *functionString          = NULL;
41
  char buffer[20];
42
  char* endptr[1];
43
  unsigned int degree           = 0;
44
  double point                  = 0.0;
45
  sollya_obj_t degreeSo;
46
  sollya_obj_t pointSo;
47
  sollya_obj_t tf;
48
  sollya_obj_t *tfAsArray       = NULL;
49
  int numTfElements             = 0;
50
  int tfIsEndElliptic           = 0;
51
  sollya_obj_t polySo;
52

    
53

    
54
  /* check the command line arguments */
55
  if (argc < NUM_ARGS)
56
  {
57
    fprintf(stderr,
58
            "\n\nusage: %s functionString freeVariableName degree point\n\n",
59
            argv[0]);
60
    fprintf(stderr,
61
            "\tfunctionString: the expression to create a node from.\n");
62
    fprintf(stderr,
63
            "\t\tmust be in quotes (e. g. \"cos(x)\");\n");
64
    fprintf(stderr,
65
            "\tfreeVariableName: the name of the free variable;\n");
66
    fprintf(stderr,
67
            "\t\t(e. g. x (no quotes needed));\n");
68
    fprintf(stderr,
69
            "\tdegree: the degree of the polynomial;\n");
70
    fprintf(stderr,
71
            "\tpoint: the point where the Taylor expansion is to be considered.\n");
72
    fprintf(stderr,"\n\n");
73
    return(1);
74
  }
75

    
76
  buffer[0] ='\0';
77

    
78
  functionString    = argv[1];
79
  freeVariableName  = argv[2];
80
  if (*(argv[3]) == '-')
81
    {
82
      fprintf(stderr,
83
              "\n\n%s: the degree (%s) can not be negative.\n",
84
              argv[0],
85
              argv[3]);
86
      fprintf(stderr,
87
              "Aborting the program.\n\n");
88
      return(1);
89
    }
90

    
91
  degree = strtoul(argv[3], endptr, 10);
92
  /* For a completly safe conversion *endptr must point to a 0 char value
93
     (not the '0' but the '\0' char value!). */
94
  if (**endptr != '\0')
95
  {
96
    fprintf(stderr,
97
            "\n\n%s: could not safely convert the \"%s\" string into an int.\n",
98
            argv[0],
99
            argv[3]);
100
    fprintf(stderr,
101
            "Aborting the program!");
102
    return(1);
103
  }
104
  point = strtod(argv[4], endptr);
105
  /* For a completly safe conversion *endptr must point to a 0 char value
106
     (not the '0' but the '\0' char value!). */
107
  if (**endptr != '\0')
108
  {
109
    fprintf(stderr,
110
            "\n\n%s: could not safely convert the \"%s\" string into double.\n",
111
            argv[0],
112
            argv[4]);
113
    fprintf(stderr,
114
            "Aborting the program!");
115
    return(1);
116
  }
117
  /* Debug printing. */
118
  fprintf(stdout,
119
          "%s: function: %s\n", argv[0], functionString);
120
  fprintf(stdout,
121
          "%s: variable name: %s\n", argv[0], freeVariableName);
122
  fprintf(stdout,
123
          "%s: degree: %d\n", argv[0], degree);
124
  fprintf(stdout,
125
          "%s: point : %f\n", argv[0], point);
126
  fprintf(stdout, "\n");
127
  /* Initialize Sollya. */
128
  sollya_lib_init();
129

    
130
  /* Set the free variable name. */
131
  sollya_lib_name_free_variable(freeVariableName);
132
  if (strcmp(sollya_lib_get_free_variable_name(), freeVariableName))
133
  {
134
    fprintf(stderr, "Could not set \"%s\" as the variable name. Aborting!\n",
135
                      freeVariableName);
136
    sollya_lib_close();
137
    return(1);
138
  }
139
  /* Parse the function string. */
140
  functionObj = pobyso_parse_string(functionString);
141
  if (sollya_lib_obj_is_error(functionObj))
142
  {
143
    fprintf(stderr, "%s: could not parse \"%s\". Aborting!\n", argv[0], functionString);
144
    sollya_lib_close();
145
    return(1);
146
  }
147
  sollya_lib_fprintf(stdout, "%s: function: %b\n", argv[0], functionObj );
148
  pobyso_autoprint(functionObj, NULL);
149
  /* Create the degree object. */
150
  degreeSo = sollya_lib_constant_from_uint64(degree);
151
  pobyso_autoprint(degreeSo, NULL);
152
  /* Create the point object. */
153
  pointSo = sollya_lib_constant_from_double(point);
154
  pobyso_autoprint(pointSo, NULL);
155

    
156
  tf = sollya_lib_taylorform(functionObj, degreeSo, pointSo, NULL);
157
  pobyso_autoprint(tf, NULL);
158

    
159
  if (sollya_lib_obj_is_list(tf))
160
  {
161
    fprintf(stdout, "%s: is a list.\n", argv[0]);
162
  }
163
  if (! sollya_lib_get_list_elements(&tfAsArray, &numTfElements, &tfIsEndElliptic, tf))
164
  {
165
    fprintf(stderr, "%s: convertion of tf to array failed. Aborting!\n",
166
            argv[0]);
167
    /* TODO: clean up. */
168
    sollya_lib_close();
169
    return(1);
170
  }
171
  fprintf(stdout, "tfAsArray address: %p\n", tfAsArray);
172
  fprintf(stdout, "tf is end-elliptic: %d\n", tfIsEndElliptic);
173
  polySo = tfAsArray[0];
174
  fprintf(stdout, "PolySo address: %p\n", polySo);
175
  fprintf(stdout, "PolySo is a function: %d\n", sollya_lib_obj_is_function(polySo));
176
  pobyso_autoprint(polySo, NULL);
177
  pobyso_autoprint(tf, NULL);
178

    
179
  polySo = sollya_lib_get_object_list_head(tf);
180
  //fprintf(stdout, "PolySo is a function: %d\n", sollya_lib_obj_is_function(polySo));
181
  sollya_lib_clear_obj(degreeSo);
182
  sollya_lib_clear_obj(functionObj);
183

    
184
#if 0
185
  fprintf(stderr, "Name of variable: %s\n", getNameOfVariable());
186
  if (functionNode->nodeType == VARIABLE)
187
  {
188
    fprintf(stderr, "Node type is VARIABLE.\n");
189
    if (getNameOfVariable != NULL)
190
    {
191
      if (strcmp(freeVariableName, getNameOfVariable()))
192
      {
193
        fprintf(stderr, "Could not parse %s. Aborting!\n", functionString);
194
        finishTool();
195
        return(1);
196
      }
197
    }
198
  }
199

200
  variableNode      = makeVariable();
201

202
  powNode = makePow(variableNode, makeConstantDouble(3.0));
203
  functionDiffNode  = differentiate(functionNode);
204
  fprintf(stderr, "\tfunctionNode tree...\n");
205
  fprintTree(stderr, functionNode);
206
  fprintf(stderr, "\n");
207
  fprintf(stderr, "\t...end functionNode tree.\n");
208
  fprintf(stderr, "\n");
209
  fprintf(stderr, "\tfunctionDiffNode tree...\n");
210
  fprintTree(stderr,functionDiffNode);
211
  fprintf(stderr, "\n");
212
  fprintf(stderr, "\t...end functionDiffNode tree.\n");
213
  fprintf(stderr, "\n\tvariableNode tree...\n");
214
  fprintTree(stderr, variableNode);
215
  fprintf(stderr, "\n");
216
  fprintf(stderr, "\t...end variableNode tree.\n");
217
  fprintf(stderr, "\n\tpowNode tree...\n");
218
  fprintTree(stderr, powNode);
219
  fprintf(stderr, "\n");
220
  fprintf(stderr, "\t...end powNode tree.\n");
221

222
  free_memory(variableNode);
223
  free_memory(functionNode);
224
  free_memory(functionDiffNode);
225
#endif
226

    
227
  sollya_lib_close();
228
  return(0);
229
} /* End main */