Lagrange (Help please)

So, I need help trasforming these thee functions to just one using List.fold_left

let rec poly lst a x_j= match lst with

|[] → 1.

| (x_k,y_k)::ys ->if x_k == x_j then poly ys a x_j

else ((a -. x_k) /. (x_j -. x_k)) *. poly ys a x_j;;

let rec lagrange_helf lst l a = match lst with

|[] → 0.

| (x_j,y_j)::xs → (poly l a x_j) *. y_j +. lagrange_helf xs l a;;

let lagrange arr a = lagrange_helf arr arr a;;

This is as fas as I got, but I don’t think it’s rigt

let rec lagrange lst a = match lst with [] → 0.

|(x_j,y_j)::xs → List.fold_left (fun lst l a →

                List.fold_left (fun lst a x_j -> a -. x_k) /. (x_j -. x_k )x_k x_j) xs

;;

Is this a homework problem?

You might take a look at the other thread: Lagrange Algorithm Implementation for Ocaml (help pls).

1 Like