-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨🔧 feat: Fix bugs, streamline supervisor architecture
- Loading branch information
Showing
9 changed files
with
236 additions
and
644 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,46 @@ | ||
defmodule InterpolationCli.Application do | ||
@moduledoc """ | ||
Главный модуль приложения, запускающий супервизор и инициализирующий процессы | ||
для обработки ввода, интерполяции и вывода данных. | ||
Main application module - inits supervisor and processes for handling i/o and interpolation | ||
""" | ||
|
||
use Application | ||
|
||
def start(_type, _args) do | ||
# Это нужно для соответствия спецификации Application | ||
{:ok, self()} | ||
end | ||
|
||
def start_link(frequency, step) do | ||
# Запускаем супервизор | ||
children = [ | ||
def start_link(algo, frequency, step) do | ||
do_nothing_with_frequency(frequency) | ||
|
||
# Set algorithms in application environment for access in InputHandler | ||
Application.put_env(:interpolation_cli, :algorithms, algo) | ||
|
||
base_children = [ | ||
{InterpolationCli.InputHandler, []}, | ||
{InterpolationCli.LinearInterpolator, [frequency, step]}, | ||
{InterpolationCli.OutputHandler, []} | ||
] | ||
|
||
additional_children = [] | ||
|
||
additional_children = | ||
if :linear in algo, | ||
do: additional_children ++ [{InterpolationCli.LinearInterpolator, step}], | ||
else: additional_children | ||
|
||
additional_children = | ||
if :lagrange in algo, | ||
do: additional_children ++ [{InterpolationCli.LagrangeInterpolator, step}], | ||
else: additional_children | ||
|
||
# Combine all children | ||
children = Enum.concat(base_children, additional_children) | ||
|
||
opts = [strategy: :one_for_one, name: InterpolationCli.Supervisor] | ||
Supervisor.start_link(children, opts) | ||
end | ||
|
||
# Dummy function that does nothing but uses frequency | ||
defp do_nothing_with_frequency(_frequency) do | ||
:ok | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
defmodule InterpolationCli.LagrangeInterpolator do | ||
@moduledoc """ | ||
Module for Lagrange interpolation. Receives multiple points and computes | ||
intermediate values with a given sampling frequency and step. | ||
""" | ||
|
||
use GenServer | ||
|
||
# API | ||
def start_link(step) do | ||
GenServer.start_link(__MODULE__, step, name: __MODULE__) | ||
end | ||
|
||
def interpolate(points) do | ||
GenServer.cast(__MODULE__, {:interpolate, points}) | ||
end | ||
|
||
def interpolate_sync(points) do | ||
GenServer.call(__MODULE__, {:interpolate_sync, points}) | ||
end | ||
|
||
# Lagrange interpolation | ||
defp perform_lagrange_interpolation(points, step) do | ||
points = Enum.take(points, -4) | ||
x_min = points |> hd() |> elem(0) | ||
x_max = points |> List.last() |> elem(0) | ||
|
||
xs = | ||
Stream.iterate(x_min, &(&1 + step)) | ||
|> Enum.take_while(&(&1 <= x_max + step)) | ||
|
||
ys = | ||
Enum.map(xs, fn x -> | ||
lagrange_value(points, x) | ||
end) | ||
|
||
res = Enum.zip(xs, ys) | ||
|
||
descr = """ | ||
Lagrange (from point #{Float.round(x_min, 2)} with step #{step}, covering all input X (#{Float.round(x_max, 2)} < #{Float.round(List.last(xs), 2)})): | ||
""" | ||
|
||
{descr, res} | ||
end | ||
|
||
defp lagrange_value(points, x) do | ||
Enum.reduce(points, 0.0, fn {x_i, y_i}, acc -> | ||
acc + y_i * basis_polynomial(points, x, x_i) | ||
end) | ||
end | ||
|
||
defp basis_polynomial(points, x, x_i) do | ||
Enum.reduce(points, 1.0, fn {x_j, _}, prod -> | ||
if x_i != x_j do | ||
prod * (x - x_j) / (x_i - x_j) | ||
else | ||
prod | ||
end | ||
end) | ||
end | ||
|
||
# Callbacks | ||
@impl true | ||
def init(step) do | ||
{:ok, step} | ||
end | ||
|
||
@impl true | ||
def handle_cast({:interpolate, points}, step) do | ||
{descr, res} = perform_lagrange_interpolation(points, step) | ||
InterpolationCli.OutputHandler.output(descr, res) | ||
{:noreply, step} | ||
end | ||
|
||
@impl true | ||
def handle_call({:interpolate_sync, points}, _from, step) do | ||
{descr, res} = perform_lagrange_interpolation(points, step) | ||
InterpolationCli.OutputHandler.output_sync(descr, res) | ||
{:reply, :ok, step} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.