You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wrote a compare function in terms of fold2, which seems generally useful. It would probably belong in owl_dense_matrix_generic.ml.
letfold2finitm1m2=let rows, cols as dims = shape m1 inif dims <> shape m2 then failwith "fold2: matrices have different shapes"
;
let last_col = cols -1inletapply_faccij=
f acc (get m1 i j) (get m2 i j)
inletrec loopaccij=if i < rows
then loop (apply_f acc i j) (i +1) j
elseif j < last_col (* don't start on next col if at final col *)then loop acc 0 (j +1) (* start over on next col *)else acc
in loop init 00
It's not pretty, and the style probably shows my inexperience, but it's tail recursive. I didn't succeed in coming up with a more elegant purely functional definition. However, I've noticed that existing fold functions in owl use imperative methods internally. I assume that's more efficient, so perhaps you wouldn't want to a version of fold2 similar to this one. I'm not sure what the tradeoffs are between different iter function variants (iteri is defined in owl_dense_matrix_generic.ml, but iter there is defined by the iter in owl_dense_ndarray_generic.ml, which I haven't sorted out fully).
The text was updated successfully, but these errors were encountered:
Perhaps something like this would be useful, too. (I use it for a special-purpose compare function that I wrote.) It's like fold2, but will abort the loop if the accumulator comes to have a specified stop_val value. Other than the extra parameter, this differs from my fold2 by the addition of a single line of code in the loop function.
(** Fold f over matrices m1 and m2 starting with initial value init, short-circuiting if stop_val is encountered: Folds f through all corresponding pairs of elements of matrices m1 and m2 by repeatedly applying f acc element_from_m1 element_from_m2, where acc is the result of previous applications. init is the initial value for acc. If f ever returns stop_val, this function returns immediately. *)letshort_circuit_fold2stop_valfinitm1m2=let rows, cols as dims = shape m1 inif dims <> shape m2 then failwith "matrices have different shapes"
;
let last_col = cols -1inletapply_faccij=
f acc (get m1 i j) (get m2 i j)
inletrec loopaccij=if acc = stop_val then stop_val
elseif i < rows
then loop (apply_f acc i j) (i +1) j
elseif j < last_col (* don't start on next col if at final col *)then loop acc 0 (j +1) (* start over on next col *)else acc
in loop init 00
I wrote a
compare
function in terms offold2
, which seems generally useful. It would probably belong inowl_dense_matrix_generic.ml
.It's not pretty, and the style probably shows my inexperience, but it's tail recursive. I didn't succeed in coming up with a more elegant purely functional definition. However, I've noticed that existing fold functions in owl use imperative methods internally. I assume that's more efficient, so perhaps you wouldn't want to a version of
fold2
similar to this one. I'm not sure what the tradeoffs are between different iter function variants (iteri
is defined in owl_dense_matrix_generic.ml, butiter
there is defined by theiter
in owl_dense_ndarray_generic.ml, which I haven't sorted out fully).The text was updated successfully, but these errors were encountered: