Repository navigation
Expand file tree
/
Copy pathLagrangeInterpolator.fs
More file actions
36 lines (32 loc) · 2.07 KB
/
Copy pathLagrangeInterpolator.fs
File metadata and controls
36 lines (32 loc) · 2.07 KB
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
module FP.Task03.LagrangeInterpolator
open System
let private finite value = not (Double.IsNaN value || Double.IsInfinity value)
open FP.Task03.FunctionValuesGenerator
let lagrange points x0 =
let points = Seq.toArray points
if points.Length = 0 || points.Length > 64 then invalidArg "points" "Нужно от 1 до 64 точек."
if not (finite x0) || (points |> Array.exists (fun (x,y) -> not (finite x && finite y))) then
invalidArg "points" "Координаты должны быть конечными."
if points |> Array.map fst |> Array.distinct |> Array.length <> points.Length then
invalidArg "points" "Координаты x не должны повторяться."
points |> Array.sumBy (fun (ax,ay) ->
let factor = points |> Array.filter (fun (bx,_) -> bx <> ax)
|> Array.fold (fun acc (bx,_) -> acc * (x0-bx)/(ax-bx)) 1.
ay * factor)
let trySkip offset values =
if offset < 0 then invalidArg "offset" "Отступ должен быть неотрицательным."
match Seq.tryItem offset values with None -> Seq.empty | Some _ -> Seq.skip offset values
let chunkWithStep chunkSize step points =
if chunkSize < 1 || step < 1 then invalidArg "step" "Размер окна и шаг должны быть положительными."
Seq.initInfinite ((*) step)
|> Seq.map (fun offset -> points |> trySkip offset |> Seq.truncate chunkSize)
|> Seq.takeWhile (not << Seq.isEmpty)
let interpolate chunkSize cutoff points step =
if chunkSize < 2 || chunkSize > 64 then invalidArg "chunkSize" "Размер окна: от 2 до 64."
if not (finite cutoff) || cutoff < 0. || cutoff >= 0.5 then
invalidArg "cutoff" "Доля отсечения должна быть от 0 до 0.5, не включая 0.5."
points |> chunkWithStep chunkSize (chunkSize/2)
|> Seq.collect (fun chunk ->
let a = chunk |> Seq.head |> fst
let b = chunk |> Seq.last |> fst
gen (lagrange chunk) (a+cutoff*(b-a)) (b-cutoff*(b-a)) step)