Skip to content

Commit bb15181

Browse files
committed
Bugfix rref_vec using reduce_col_with_vec
1 parent d7da9c7 commit bb15181

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/cdomains/affineEquality/sparseImplementation/listMatrix.ml

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,6 @@ module ListMatrix: AbstractMatrix =
113113
sub_scaled_row row pivot_row s)
114114
) m
115115

116-
let reduce_col_with_vec m j v =
117-
let pivot_element = V.nth v j in
118-
if pivot_element = A.zero then m
119-
else List.mapi (fun idx row ->
120-
let row_value = V.nth row j in
121-
if row_value =: A.zero then row
122-
else (let s = row_value /: pivot_element in
123-
V.map2_f_preserves_zero (fun x y -> x -: s *: y) row v)
124-
) m
125-
126116
let del_col m j =
127117
if num_cols m = 1 then empty ()
128118
else
@@ -202,14 +192,6 @@ module ListMatrix: AbstractMatrix =
202192
let m' = main_loop m m 0 0 in
203193
if affeq_rows_are_valid m' then Some m' else None (* TODO: We can check this for each row, using the helper function row_is_invalid *)
204194

205-
(* This function return a tuple of row index and pivot position (column) in m *)
206-
(* TODO: maybe we could use a Hashmap instead of a list? *)
207-
let get_pivot_positions (m : t) : (int * int) list =
208-
List.rev @@ List.fold_lefti (
209-
fun acc i row -> match V.find_first_non_zero row with
210-
| None -> acc
211-
| Some (pivot_col, _) -> (i, pivot_col) :: acc
212-
) [] m
213195

214196
let assert_rref m =
215197
let pivot_l = get_pivot_positions m in
@@ -230,11 +212,33 @@ module ListMatrix: AbstractMatrix =
230212
in if validate_vec pivot_l then validate vs (i+1) else raise (Invalid_argument "Matrix not in rref: pivot column not empty!")
231213
in validate m 0
232214

233-
(* Inserts the vector v with pivot at piv_idx at the correct position in m. m has to be in rref form. *)
215+
(* This function return a tuple of row index and pivot position (column) in m *)
216+
(* TODO: maybe we could use a Hashmap instead of a list? *)
217+
let get_pivot_positions (m : t) : (int * int) list =
218+
List.rev @@ List.fold_lefti (
219+
fun acc i row -> match V.find_first_non_zero row with
220+
| None -> acc
221+
| Some (pivot_col, _) -> (i, pivot_col) :: acc
222+
) [] m
223+
224+
(* Sets the jth column to zero by subtracting multiples of v *)
225+
let reduce_col_with_vec m j v =
226+
let pivot_element = V.nth v j in
227+
if pivot_element = A.zero then m
228+
else List.mapi (fun idx row ->
229+
let row_value = V.nth row j in
230+
if row_value =: A.zero then row
231+
else (let s = row_value /: pivot_element in
232+
V.map2_f_preserves_zero (fun x y -> x -: (s *: y)) row v)
233+
) m
234+
235+
(* Inserts the vector v with pivot at piv_idx at the correct position in m. m has to be in rref form and v is only <>: A.zero on piv_idx or idx not included in piv_positions *)
234236
let insert_v_according_to_piv m v piv_idx pivot_positions =
237+
let reduced_m = reduce_col_with_vec m piv_idx v in
235238
match List.find_opt (fun (row_idx, piv_col) -> piv_col > piv_idx) pivot_positions with
236-
| None -> append_row m v
237-
| Some (row_idx, _) -> let (before, after) = List.split_at row_idx m in (* TODO: Optimize *)
239+
| None -> append_row reduced_m v
240+
| Some (row_idx, _) ->
241+
let (before, after) = List.split_at row_idx reduced_m in (* TODO: Optimize *)
238242
before @ (v :: after)
239243

240244
(* This function yields the same result as appending v to m, normalizing and removing zero rows would. *)

0 commit comments

Comments
 (0)