Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

ne.ml 9.15 KB
Newer Older
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
(*-----------------------------------------------------------------------
** Copyright (C) 2002 - Verimag.
** This file may only be copied under the terms of the GNU Library General
** Public License 
**-----------------------------------------------------------------------
**
** File: ne.ml
** Main author: jahier@imag.fr
*)


open Value

module StringMap = struct
  include Map.Make(struct type t = string let compare = compare end)
end


(* exported *)
(** Normal expressions.

  Keys are var names, and the content is the coefficient of the
  monomial. By convention, "" maps the constant value. For instance,
  [("a" -> I(3) ; "b" -> I(-2) ; "" -> I(11))] represents the
  expression [3*a - 2*b + 11].
*)
type t = Value.num StringMap.t

type subst = (string * Value.num) * t


      
(****************************************************************************)
(* exported *)
let (is_a_constant : t -> bool) =
  fun ne -> 
    (StringMap.remove "" ne) = StringMap.empty



(****************************************************************************)

let (add: t -> t -> t) =
  fun ne1 ne2 ->
    ( StringMap.fold
	(fun vn1 val1 acc -> 
	   try
	     let val2 = StringMap.find vn1 acc in
	     let valr = (add_num val1 val2) in
	       if ((not (num_eq_zero valr)) || (vn1 = "") )
	       then StringMap.add vn1 valr acc
	       else StringMap.remove vn1 acc
	   with Not_found -> 
	     StringMap.add vn1 val1 acc
	)
	ne1
	ne2
    )

let _ = assert (
  let ne1 = StringMap.add "" (I(1)) (StringMap.add "toto" (I(2)) StringMap.empty)
  and ne2 = StringMap.add "" (I(2)) (StringMap.add "toto" (I(-3)) StringMap.empty)
  and ne_res = StringMap.add "" (I(3)) (StringMap.add "toto" (I(-1)) StringMap.empty) 
  in
  let ne_cal = add ne1 ne2 in

    ((StringMap.find "toto" ne_res) = (StringMap.find "toto" ne_cal))
    && 
    ((StringMap.find "" ne_res) = (StringMap.find "" ne_cal))
)
    
(****************************************************************************)
let (diff: t -> t -> t) =
  fun ne1 ne2 ->
    ( StringMap.fold
	(fun vn2 val2 acc -> 
	   try
	     let val1 = StringMap.find vn2 acc in
	     let valr = (diff_num val1 val2) in
	       if (not (num_eq_zero valr) || vn2 = "" )
	       then StringMap.add vn2 valr acc
	       else StringMap.remove vn2 acc
	   with Not_found -> 
	     let minus_val2 = 
	       match val2 with
		   I(i) -> I(-i)
		 | F(f) -> F(-. f)
	     in
	       StringMap.add vn2 minus_val2 acc
	)
	ne2
	ne1
    )

let _ = assert (
  let ne1 = StringMap.add "" (I(1)) (StringMap.add "toto" (I(2)) StringMap.empty) 
  and ne2 = StringMap.add "" (I(2)) (StringMap.add "titi" (I(3)) 
				   (StringMap.add "toto" (I(3)) StringMap.empty)) 
  and ne_res = StringMap.add "" (I(-1)) (StringMap.add "toto" (I(-1)) 
				       (StringMap.add "titi" (I(3)) StringMap.empty)) 
  in
  let ne_cal = diff ne1 ne2 
  in
    ((StringMap.find "toto" ne_res) = (StringMap.find "toto" ne_cal))
    && 
    ((StringMap.find "" ne_res) = (StringMap.find "" ne_cal))
)

(****************************************************************************)
let (mult: t -> t -> t) =
  fun ne1 ne2 ->
    if is_a_constant ne1
    then
      let coeff = StringMap.find "" ne1 in
	if num_eq_zero coeff
	then ne1
	else
	  ( StringMap.fold
	      (fun vn value acc -> 
		 StringMap.add vn (mult_num coeff value) acc
	      )
	      ne2
	      StringMap.empty
	  )
    else if is_a_constant ne2
    then
      let coeff = StringMap.find "" ne2 in
	if num_eq_zero coeff
	then ne2
	else
	  ( StringMap.fold
	      (fun vn value acc -> 
		 StringMap.add vn (mult_num coeff value) acc
	      )
	      ne1
	      StringMap.empty
	  )
    else
      failwith "*** Cannot solve non-linear constraints, sorry. \n"

let _ = assert (
  let ne1 = StringMap.add "" (I(1)) (StringMap.add "toto" (I(2)) StringMap.empty) in
  let ne2 = StringMap.add "" (I(2)) StringMap.empty in

  let ne_res = StringMap.add "" (I(2)) (StringMap.add "toto" (I(4)) StringMap.empty) in
  let ne_cal = mult ne1 ne2 in

    ((StringMap.find "toto" ne_res) = (StringMap.find "toto" ne_cal))
    && 
    ((StringMap.find "" ne_res) = (StringMap.find "" ne_cal))
)


(****************************************************************************)
let (quot: t -> t -> t) =
  fun ne1 ne2 ->
    if is_a_constant ne2
    then
      let coeff = StringMap.find "" ne2 in
	( StringMap.fold
	    (fun vn value acc -> 
	       let valr = (quot_num value coeff) in
		 if ((num_eq_zero valr) && (vn <> ""))
		 then acc
		 else StringMap.add vn valr acc
	    )
	    ne1
	    StringMap.empty
	)
    else 
      failwith "*** Cannot solve non-linear constraints, sorry. \n"

let _ = assert (
  let ne1 = StringMap.add "" (I(1)) (StringMap.add "toto" (I(2)) StringMap.empty) in
  let ne2 = StringMap.add "" (I(2)) StringMap.empty in

  let ne_res = StringMap.add "" (I(0)) (StringMap.add "toto" (I(1)) StringMap.empty) in
  let ne_cal = quot ne1 ne2 in

    ((StringMap.find "toto" ne_res) = (StringMap.find "toto" ne_cal))
    && 
    ((StringMap.find "" ne_res) = (StringMap.find "" ne_cal))
)


(****************************************************************************)
let (modulo: t -> t -> t) =
  fun ne1 ne2 ->
    if is_a_constant ne2
    then
      let coeff = StringMap.find "" ne2 in
	( StringMap.fold
	    (fun vn value acc -> 
	       let valr = (modulo_num value coeff) in
		 if ((num_eq_zero valr) && (vn <> ""))
		 then acc
		 else StringMap.add vn (modulo_num value coeff) acc
	    )
	    ne1
	    StringMap.empty
	)
    else 
      failwith "*** Cannot solve non-linear constraints, sorry. \n"

let _ = assert (
  let ne1 = StringMap.add "" (I(1)) (StringMap.add "toto" (I(2)) StringMap.empty) in
  let ne2 = StringMap.add "" (I(2)) StringMap.empty in

  let ne_res = StringMap.add "" (I(1)) (StringMap.add "toto" (I(0)) StringMap.empty) in
  let ne_cal = modulo ne1 ne2 in

    ((StringMap.find "" ne_res) = (StringMap.find "" ne_cal))
)


(****************************************************************************)

(* exported *)
let (fold : (string -> Value.num -> 'acc -> 'acc) -> t -> 'acc -> 'acc) =
  fun f ne acc0 -> 
    StringMap.fold f ne acc0

(* exported *)
let (make : string -> Value.num -> t) =
  fun vn nval -> 
    (StringMap.add vn nval StringMap.empty)

(* exported *)	
229
let (find : string -> t -> Value.num option) =
230
  fun vn ne -> 
231
232
    try Some(StringMap.find vn ne)
    with Not_found -> None
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

(****************************************************************************)


(* exported *)
let (neg_nexpr : t -> t) =
  fun ne -> 
    StringMap.map (fun x -> match x with I(i) -> I(-i) | F(f) -> F(-.f)) ne

(* exported *)
let (split : t -> (string * Value.num) option * t) =
  fun ne -> 
    let f = fun var num_val acc -> 
      match acc with
	  None, ne -> Some(var, num_val), ne
	| Some(v, nv), ne -> Some(v, nv), (StringMap.add var num_val ne)
    in
      StringMap.fold f ne (None, StringMap.empty)


(* exported *)
let (dimension : t -> int) =
  fun ne ->     
    StringMap.fold (fun vn _ cpt -> if vn = "" then cpt else cpt+1) ne 0
    
    
(* exported *)
let (nexpr_add : (Value.num * string) -> t -> t) = 
  fun (nval, vn) ne2 -> 
    StringMap.add vn nval ne2
    
    
(* exported *)
let (apply_subst : t -> subst -> t) =
267
  fun ne2 ((vn, b), ne1) -> 
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    try 
      let a = StringMap.find vn ne2 in
      let new_ne1 = 
	StringMap.map 
	  (fun x -> Value.mult_num x a) 
	  ne1 
      in
      let new_ne2 = 
	StringMap.map 
	  (fun x -> Value.mult_num x b) 
	  (StringMap.remove vn ne2) 
      in
	add new_ne1 new_ne2
    with 
	Not_found -> ne2


(* exported *)
let (apply_substl : subst list -> t -> t) =
  fun sl ne -> 
    List.fold_left (apply_subst) ne sl



(* exported *)
let (apply_simple_subst : t -> string * Value.num -> t) =
  fun  ne (vn, v) -> 
    try 
      let a = StringMap.find vn ne in
      let ne2 = StringMap.remove vn ne in
298
299
300
301
302
303
304
305
	( 
	  try 
	    let b = StringMap.find "" ne in
	      StringMap.add "" ((Value.add_num b (Value.mult_num v a))) ne2
	  with 
	      Not_found -> 
		StringMap.add "" (Value.mult_num v a) ne2
	)
306
307
308
309
310
311
312
313
314
315
316
317
318
    with 
	Not_found -> ne

(* exported *)
exception Not_triangular

(* exported *)
let rec (resolve_triangular_system :((string * Value.num) * t) list -> 
	   (string * Value.num) list) =
  fun sl ->
    match sl with
	[] -> []
      | ((vn, v), ne)::tail -> 
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
	  let s =
	    try
	      let cst = StringMap.find "" ne in
		if 
		  (StringMap.remove "" ne) <> StringMap.empty
		then
		  raise Not_triangular
		else
		  (vn, Value.quot_num cst v) 
	    with Not_found -> 
	      match v with
		  I _ -> (vn, I 0)
		| F _ -> (vn, F 0.0)
	  in
	  let new_tail =      
	    List.map 
	      (fun (x, ne) -> x, apply_simple_subst ne s)
	      tail
	  in
	    s::(resolve_triangular_system new_tail)
339
340
341
342
343
344
345
346
347
348
349

(****************************************************************************)


(* exported *)
let (to_string : t -> string) =
  fun ne -> 
    (StringMap.fold
       (fun vn v acc -> 
	  if vn = ""
	  then ((num_value_to_string v) ^ acc)
350
	  else (" + " ^ (num_value_to_string v ) ^ "*" ^ ((Prevar.format vn) ^ acc)))
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
       ne
       ""
    )

(* exported *)
let (substl_to_string : subst list -> string) = 
  fun sl -> 
    (List.fold_left 
       (fun acc ((vn, a), ne) -> 
	  (acc ^ "   " ^ (Value.num_value_to_string a) ^ "." ^ vn ^ 
	   " -> " ^ to_string ne ^ "\n")
       )
       ""
       sl
    )