combine .cvsignore files into .gitignore file
[sppoc.git] / CipolCore / Common / comscan.c
blobe7f1a2fa77038ddc055e116035e5462e7cf3eaff
1 /*************************************************************/
2 /* This file contains some functions used for reading the */
3 /* Cipol-Light common objects from the standard input. */
4 /*************************************************************/
6 /*** Include files ***/
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stddef.h>
12 #include "comtypes.h"
13 #include "lists.h"
14 #include "../cipol.h"
15 #include "../erroridt.h"
16 #include "../System/systypes.h"
17 #include "../Polylib/polytypes.h"
19 #ifdef DBMALLOC
20 # include "malloc.h"
21 #endif
23 /*** Constants ***/
25 /*** Static variables ***/
27 /*** Global variables ***/
29 /***************************************************/
30 /** To scan lists. */
31 /***************************************************/
33 /** Create a cellule of list **/
35 TList *listCreate(void)
37 TList *l;
39 l=(TList *)cipolMalloc(sizeof(TList),"listHeadCreate");
40 return(l);
43 /** Set the cellule with an atom (a string) **/
45 TList *listAtom(TList *l,char *str)
47 if(!l) return NULL;
48 l->type=LIST_ATOM;
49 l->body.atom=str;
50 return(l);
53 /** Set the cellule with another list **/
55 TList *listGeneralInit(int subType)
57 TList *l;
58 TListList *ll;
60 l=listCreate();
61 if(!l) return NULL;
62 ll=(TListList *)cipolMalloc(sizeof(TListList),"listListInit");
63 l->type=LIST_GENERAL;
64 l->body.list=ll;
65 ll->subType=subType;
66 ll->elements=(TList **)cipolMalloc(sizeof(TList *)*ELEMENTS_NB,"listListInit");
67 ll->allocated_nb=ELEMENTS_NB;
68 ll->elements_nb=0;
69 return(l);
72 TList *listListInit(void)
73 { return listGeneralInit(LIST_SUBGENERAL); }
75 TList *listVectorInit(void)
76 { return listGeneralInit(LIST_SUBVECTOR); }
78 TList *listListAdd(TList *l,TList *list)
80 TListList *ll;
82 if(!l) return NULL;
83 ll=l->body.list;
84 if(ll->elements_nb+1>ll->allocated_nb){
85 ll->allocated_nb += ELEMENTS_NB;
86 ll->elements=(TList **)
87 cipolRealloc(ll->elements,
88 ll->allocated_nb*sizeof(TList *),
89 "listListAdd");
91 ll->elements[ll->elements_nb++]=list;
93 return(l);
96 /***************************************************************/
97 /* Functions to get linear forms, a linear form is like */
98 /* ((v1 ... vn) expr) */
99 /***************************************************************/
101 /** Creation of linear form **/
103 void storeCoefficient(TList *variables,Matrix *linear,char *var,int coeff)
105 int nb=listLength(variables);
106 int i;
108 if(var==NULL) linear->p[0][nb]=coeff;
109 else {
110 for(i=0;i<nb;i++) if(strcmp(listGet(variables,i)->body.atom,var)==0) break;
111 if(i==nb) errError(UNK_VARIABLE,TERROR,"storeCoefficient",NULL);
112 value_assign(linear->p[0][i],coeff);
115 Matrix *linearCreate(TList *variables,char *var,int coeff)
117 Matrix *linear;
118 int nb=listLength(variables);
119 int i;
121 linear=Matrix_Alloc(1,nb+1);
122 for(i=0;i<nb+1;i++) value_assign(linear->p[0][i],0);
123 storeCoefficient(variables,linear,var,coeff);
124 return linear;
127 /** Addition of linear forms **/
129 Matrix *linearAdd(TList *variables,Matrix *l1,Matrix *l2)
131 Matrix *result;
132 int nb=listLength(variables);
133 int i;
135 if(l1==NULL || l2==NULL) return NULL;
136 result=linearCreate(variables,NULL,0);
137 for(i=0;i<=nb;i++){
138 Value plus=value_plus(l1->p[0][i],l2->p[0][i]);
139 value_assign(result->p[0][i],plus);
141 return result;
144 /** Substraction of linear forms **/
146 Matrix *linearSub(TList *variables,Matrix *l1,Matrix *l2)
148 Matrix *result;
149 int nb=listLength(variables);
150 int i;
152 if(l1==NULL || l2==NULL) return NULL;
153 result=linearCreate(variables,NULL,0);
154 for(i=0;i<=nb;i++){
155 Value minus=value_minus(l1->p[0][i],l2->p[0][i]);
156 value_assign(result->p[0][i],minus);
158 return result;
161 /** Compute the opposite of linear form **/
163 Matrix *linearInv(TList *variables,Matrix *l)
165 Matrix *result;
166 int nb=listLength(variables);
167 int i;
169 if(l==NULL) return NULL;
170 result=linearCreate(variables,NULL,0);
171 for(i=0;i<=nb;i++) value_assign(result->p[0][i],value_uminus(l->p[0][i]));
172 return result;
175 /** Multiplication of linear form by a scalar **/
177 Matrix *linearMult(TList *variables,Matrix *l,int val)
179 Matrix *result;
180 Value vval=int_to_value(val);
181 int nb=listLength(variables);
182 int i;
184 if(l==NULL) return NULL;
185 result=linearCreate(variables,NULL,0);
186 for(i=0;i<=nb;i++) value_assign(result->p[0][i],value_mult(l->p[0][i],vval));
187 return result;
190 /***************************************************/
191 /** To build polyhedra. */
192 /***************************************************/
194 Polyhedron *Domain_Polyhedron(Polyhedron *poly,Polyhedron *dom)
196 if(poly!=NULL){ poly->next=dom; return(poly); }
197 else return(dom);