Statistiques
| Révision :

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

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

1 34 storres
/** @file taylorModel-01.c
2 34 storres
 * taylorModel-01: check the taylorform function.
3 34 storres
 *
4 34 storres
 * @author S.T.
5 34 storres
 * @date 2012-11-21
6 34 storres
 *
7 34 storres
 */
8 34 storres
/******************************************************************************/
9 34 storres
/* Headers, applying the "particular to general" convention.*/
10 34 storres
11 36 storres
#include "pobyso.h"
12 34 storres
13 34 storres
/* includes of local headers */
14 34 storres
15 34 storres
/* includes of project headers */
16 34 storres
17 34 storres
/* includes of system headers */
18 34 storres
#include <stdlib.h>
19 34 storres
#include <gmp.h>
20 34 storres
#include <sollya.h>
21 34 storres
#include <setjmp.h>
22 34 storres
#include <string.h>
23 34 storres
/* Other declarations */
24 34 storres
25 34 storres
/* Internal prototypes */
26 34 storres
27 34 storres
/* Types, constants and macros definitions */
28 34 storres
#define NUM_ARGS 5
29 34 storres
#define VARIABLE_NAME_STRING "x"
30 34 storres
#define RADIX 10
31 34 storres
/* Global variables */
32 34 storres
33 34 storres
/* Functions */
34 34 storres
int
35 34 storres
main(int argc, char** argv)
36 34 storres
{
37 34 storres
  //jmp_buf recover;
38 34 storres
  sollya_obj_t functionObj      = NULL;
39 34 storres
  char *freeVariableName        = NULL;
40 34 storres
  char *functionString          = NULL;
41 34 storres
  char buffer[20];
42 34 storres
  char* endptr[1];
43 34 storres
  unsigned int degree           = 0;
44 34 storres
  double point                  = 0.0;
45 34 storres
  sollya_obj_t degreeSo;
46 34 storres
  sollya_obj_t pointSo;
47 34 storres
  sollya_obj_t tf;
48 34 storres
  sollya_obj_t *tfAsArray       = NULL;
49 34 storres
  int numTfElements             = 0;
50 34 storres
  int tfIsEndElliptic           = 0;
51 34 storres
  sollya_obj_t polySo;
52 34 storres
53 34 storres
54 34 storres
  /* check the command line arguments */
55 34 storres
  if (argc < NUM_ARGS)
56 34 storres
  {
57 34 storres
    fprintf(stderr,
58 34 storres
            "\n\nusage: %s functionString freeVariableName degree point\n\n",
59 34 storres
            argv[0]);
60 34 storres
    fprintf(stderr,
61 34 storres
            "\tfunctionString: the expression to create a node from.\n");
62 34 storres
    fprintf(stderr,
63 34 storres
            "\t\tmust be in quotes (e. g. \"cos(x)\");\n");
64 34 storres
    fprintf(stderr,
65 34 storres
            "\tfreeVariableName: the name of the free variable;\n");
66 34 storres
    fprintf(stderr,
67 34 storres
            "\t\t(e. g. x (no quotes needed));\n");
68 34 storres
    fprintf(stderr,
69 34 storres
            "\tdegree: the degree of the polynomial;\n");
70 34 storres
    fprintf(stderr,
71 34 storres
            "\tpoint: the point where the Taylor expansion is to be considered.\n");
72 34 storres
    fprintf(stderr,"\n\n");
73 34 storres
    return(1);
74 34 storres
  }
75 34 storres
76 34 storres
  buffer[0] ='\0';
77 34 storres
78 34 storres
  functionString    = argv[1];
79 34 storres
  freeVariableName  = argv[2];
80 34 storres
  if (*(argv[3]) == '-')
81 34 storres
    {
82 34 storres
      fprintf(stderr,
83 34 storres
              "\n\n%s: the degree (%s) can not be negative.\n",
84 34 storres
              argv[0],
85 34 storres
              argv[3]);
86 34 storres
      fprintf(stderr,
87 34 storres
              "Aborting the program.\n\n");
88 34 storres
      return(1);
89 34 storres
    }
90 34 storres
91 34 storres
  degree = strtoul(argv[3], endptr, 10);
92 34 storres
  /* For a completly safe conversion *endptr must point to a 0 char value
93 34 storres
     (not the '0' but the '\0' char value!). */
94 34 storres
  if (**endptr != '\0')
95 34 storres
  {
96 34 storres
    fprintf(stderr,
97 34 storres
            "\n\n%s: could not safely convert the \"%s\" string into an int.\n",
98 34 storres
            argv[0],
99 34 storres
            argv[3]);
100 34 storres
    fprintf(stderr,
101 34 storres
            "Aborting the program!");
102 34 storres
    return(1);
103 34 storres
  }
104 34 storres
  point = strtod(argv[4], endptr);
105 34 storres
  /* For a completly safe conversion *endptr must point to a 0 char value
106 34 storres
     (not the '0' but the '\0' char value!). */
107 34 storres
  if (**endptr != '\0')
108 34 storres
  {
109 34 storres
    fprintf(stderr,
110 34 storres
            "\n\n%s: could not safely convert the \"%s\" string into double.\n",
111 34 storres
            argv[0],
112 34 storres
            argv[4]);
113 34 storres
    fprintf(stderr,
114 34 storres
            "Aborting the program!");
115 34 storres
    return(1);
116 34 storres
  }
117 34 storres
  /* Debug printing. */
118 34 storres
  fprintf(stdout,
119 34 storres
          "%s: function: %s\n", argv[0], functionString);
120 34 storres
  fprintf(stdout,
121 34 storres
          "%s: variable name: %s\n", argv[0], freeVariableName);
122 34 storres
  fprintf(stdout,
123 34 storres
          "%s: degree: %d\n", argv[0], degree);
124 34 storres
  fprintf(stdout,
125 34 storres
          "%s: point : %f\n", argv[0], point);
126 34 storres
  fprintf(stdout, "\n");
127 34 storres
  /* Initialize Sollya. */
128 34 storres
  sollya_lib_init();
129 34 storres
130 34 storres
  /* Set the free variable name. */
131 34 storres
  sollya_lib_name_free_variable(freeVariableName);
132 34 storres
  if (strcmp(sollya_lib_get_free_variable_name(), freeVariableName))
133 34 storres
  {
134 34 storres
    fprintf(stderr, "Could not set \"%s\" as the variable name. Aborting!\n",
135 34 storres
                      freeVariableName);
136 34 storres
    sollya_lib_close();
137 34 storres
    return(1);
138 34 storres
  }
139 34 storres
  /* Parse the function string. */
140 34 storres
  functionObj = pobyso_parse_string(functionString);
141 34 storres
  if (sollya_lib_obj_is_error(functionObj))
142 34 storres
  {
143 34 storres
    fprintf(stderr, "%s: could not parse \"%s\". Aborting!\n", argv[0], functionString);
144 34 storres
    sollya_lib_close();
145 34 storres
    return(1);
146 34 storres
  }
147 34 storres
  sollya_lib_fprintf(stdout, "%s: function: %b\n", argv[0], functionObj );
148 34 storres
  pobyso_autoprint(functionObj, NULL);
149 34 storres
  /* Create the degree object. */
150 34 storres
  degreeSo = sollya_lib_constant_from_uint64(degree);
151 34 storres
  pobyso_autoprint(degreeSo, NULL);
152 34 storres
  /* Create the point object. */
153 34 storres
  pointSo = sollya_lib_constant_from_double(point);
154 34 storres
  pobyso_autoprint(pointSo, NULL);
155 34 storres
156 34 storres
  tf = sollya_lib_taylorform(functionObj, degreeSo, pointSo, NULL);
157 34 storres
  pobyso_autoprint(tf, NULL);
158 34 storres
159 34 storres
  if (sollya_lib_obj_is_list(tf))
160 34 storres
  {
161 34 storres
    fprintf(stdout, "%s: is a list.\n", argv[0]);
162 34 storres
  }
163 34 storres
  if (! sollya_lib_get_list_elements(&tfAsArray, &numTfElements, &tfIsEndElliptic, tf))
164 34 storres
  {
165 34 storres
    fprintf(stderr, "%s: convertion of tf to array failed. Aborting!\n",
166 34 storres
            argv[0]);
167 34 storres
    /* TODO: clean up. */
168 34 storres
    sollya_lib_close();
169 34 storres
    return(1);
170 34 storres
  }
171 34 storres
  fprintf(stdout, "tfAsArray address: %p\n", tfAsArray);
172 34 storres
  fprintf(stdout, "tf is end-elliptic: %d\n", tfIsEndElliptic);
173 34 storres
  polySo = tfAsArray[0];
174 34 storres
  fprintf(stdout, "PolySo address: %p\n", polySo);
175 34 storres
  fprintf(stdout, "PolySo is a function: %d\n", sollya_lib_obj_is_function(polySo));
176 34 storres
  pobyso_autoprint(polySo, NULL);
177 34 storres
  pobyso_autoprint(tf, NULL);
178 34 storres
179 34 storres
  polySo = sollya_lib_get_object_list_head(tf);
180 34 storres
  //fprintf(stdout, "PolySo is a function: %d\n", sollya_lib_obj_is_function(polySo));
181 34 storres
  sollya_lib_clear_obj(degreeSo);
182 34 storres
  sollya_lib_clear_obj(functionObj);
183 34 storres
184 34 storres
#if 0
185 34 storres
  fprintf(stderr, "Name of variable: %s\n", getNameOfVariable());
186 34 storres
  if (functionNode->nodeType == VARIABLE)
187 34 storres
  {
188 34 storres
    fprintf(stderr, "Node type is VARIABLE.\n");
189 34 storres
    if (getNameOfVariable != NULL)
190 34 storres
    {
191 34 storres
      if (strcmp(freeVariableName, getNameOfVariable()))
192 34 storres
      {
193 34 storres
        fprintf(stderr, "Could not parse %s. Aborting!\n", functionString);
194 34 storres
        finishTool();
195 34 storres
        return(1);
196 34 storres
      }
197 34 storres
    }
198 34 storres
  }
199 34 storres

200 34 storres
  variableNode      = makeVariable();
201 34 storres

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

222 34 storres
  free_memory(variableNode);
223 34 storres
  free_memory(functionNode);
224 34 storres
  free_memory(functionDiffNode);
225 34 storres
#endif
226 34 storres
227 34 storres
  sollya_lib_close();
228 34 storres
  return(0);
229 34 storres
} /* End main */