1 /*************************************************************/
2 /* This file contains some functions used for reading the */
3 /* Cipol-Light common objects from the standard input. */
4 /*************************************************************/
6 /*** Include files ***/
15 #include "../erroridt.h"
16 #include "../System/systypes.h"
17 #include "../Polylib/polytypes.h"
25 /*** Static variables ***/
27 /*** Global variables ***/
29 /***************************************************/
31 /***************************************************/
33 /** Create a cellule of list **/
35 TList
*listCreate(void)
39 l
=(TList
*)cipolMalloc(sizeof(TList
),"listHeadCreate");
43 /** Set the cellule with an atom (a string) **/
45 TList
*listAtom(TList
*l
,char *str
)
53 /** Set the cellule with another list **/
55 TList
*listGeneralInit(int subType
)
62 ll
=(TListList
*)cipolMalloc(sizeof(TListList
),"listListInit");
66 ll
->elements
=(TList
**)cipolMalloc(sizeof(TList
*)*ELEMENTS_NB
,"listListInit");
67 ll
->allocated_nb
=ELEMENTS_NB
;
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
)
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
*),
91 ll
->elements
[ll
->elements_nb
++]=list
;
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
);
108 if(var
==NULL
) linear
->p
[0][nb
]=coeff
;
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
)
118 int nb
=listLength(variables
);
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
);
127 /** Addition of linear forms **/
129 Matrix
*linearAdd(TList
*variables
,Matrix
*l1
,Matrix
*l2
)
132 int nb
=listLength(variables
);
135 if(l1
==NULL
|| l2
==NULL
) return NULL
;
136 result
=linearCreate(variables
,NULL
,0);
138 Value plus
=value_plus(l1
->p
[0][i
],l2
->p
[0][i
]);
139 value_assign(result
->p
[0][i
],plus
);
144 /** Substraction of linear forms **/
146 Matrix
*linearSub(TList
*variables
,Matrix
*l1
,Matrix
*l2
)
149 int nb
=listLength(variables
);
152 if(l1
==NULL
|| l2
==NULL
) return NULL
;
153 result
=linearCreate(variables
,NULL
,0);
155 Value minus
=value_minus(l1
->p
[0][i
],l2
->p
[0][i
]);
156 value_assign(result
->p
[0][i
],minus
);
161 /** Compute the opposite of linear form **/
163 Matrix
*linearInv(TList
*variables
,Matrix
*l
)
166 int nb
=listLength(variables
);
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
]));
175 /** Multiplication of linear form by a scalar **/
177 Matrix
*linearMult(TList
*variables
,Matrix
*l
,int val
)
180 Value vval
=int_to_value(val
);
181 int nb
=listLength(variables
);
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
));
190 /***************************************************/
191 /** To build polyhedra. */
192 /***************************************************/
194 Polyhedron
*Domain_Polyhedron(Polyhedron
*poly
,Polyhedron
*dom
)
196 if(poly
!=NULL
){ poly
->next
=dom
; return(poly
); }