Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ by adding `expline` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:expline, "~> 0.1.0"}]
[{:expline, "~> 0.2"}]
end
```

Expand Down
6 changes: 4 additions & 2 deletions lib/expline/matrix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ defmodule Expline.Matrix do
at(matrix, i, j)
end
|> Enum.all?(fn
0.0 -> true
+0.0 -> true
-0.0 -> true
_ -> false
end)
end
Expand All @@ -125,7 +126,8 @@ defmodule Expline.Matrix do
at(matrix, i, j)
end
|> Enum.all?(fn
0.0 -> true
+0.0 -> true
-0.0 -> true
_ -> false
end)
end
Expand Down
89 changes: 78 additions & 11 deletions lib/expline/spline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,68 @@ defmodule Expline.Spline do
end
end

@doc """
Interpolate a curvature from the spline.

## Examples

iex> with {:ok, spline} <- Expline.Spline.from_points([{0.0, 0.0}, {1.0, 1.0}, {2.0, 2.0}]),
...> do: Expline.Spline.interpolate_curvature(spline, 0.5)
{:ok, 1.0}
"""
@spec interpolate_curvature(t(), independent_value()) ::
{:ok, curvature()}
| {:error, interpolation_error()}
| {:error, :corrupt_spline}
def interpolate_curvature(%__MODULE__{} = spline, x) when is_float(x) do
with :error <- Map.fetch(spline.derivatives, x) do
case :ordsets.filter(fn {x1, x2} -> x1 < x and x < x2 end, spline.ranges) do
[{_x1, _x2} = range] ->
do_interpolate_curvature(spline, range, x)

[] ->
extrapolate_curvature(spline, x)

_ranges ->
{:error, :corrupt_spline}
end
end
end

@spec do_interpolate_curvature(t(), range(), independent_value()) :: {:ok, dependent_value()}
defp do_interpolate_curvature(%__MODULE__{} = spline, {x1, x2}, x) do
y1 = Map.get(spline.points, x1)
y2 = Map.get(spline.points, x2)

k1 = Map.get(spline.derivatives, x1)
k2 = Map.get(spline.derivatives, x2)

# Described by equations (1), (2), (3), and (4) on
# https://en.wikipedia.org/wiki/Spline_interpolation
t = (x - x1) / (x2 - x1)
a = k1 * (x2 - x1) - (y2 - y1)
b = -k2 * (x2 - x1) + (y2 - y1)

dy = (y2 - y1 + (1 - 2 * t) * (a * (1 - t) + b * t) + t * (1 - t) * (b - a)) / (x2 - x1)
{:ok, dy}
end

@spec extrapolate_curvature(t(), independent_value()) ::
{:ok, dependent_value()}
| {:error, :corrupt_extrema}
defp extrapolate_curvature(spline, x) do
cond do
spline.min > x ->
{:ok, Map.get(spline.derivatives, spline.min)}

spline.max < x ->
{:ok, Map.get(spline.derivatives, spline.max)}

true ->
{:error, :corrupt_extrema}
end
end

@spec make_derivatives(%{required(independent_value()) => dependent_value()}) :: %{
required(independent_value()) => curvature()
}
Expand All @@ -357,38 +419,38 @@ defmodule Expline.Spline do
Expline.Matrix.construct(n + 1, n + 2, fn
# first row
0, 0 ->
2 / (x1 - x0)
2 / subtract(x1, x0)

0, 1 ->
1 / (x1 - x0)
1 / subtract(x1, x0)

# =
0, j when j == n + 1 ->
3.0 * ((y1 - y0) / :math.pow(x1 - x0, 2))
3.0 * (subtract(y1, y0) / :math.pow(subtract(x1, x0), 2))

# last row
^n, j when j == n - 1 ->
1 / (xn - xn_1)
1 / subtract(xn, xn_1)

^n, ^n ->
2 / (xn - xn_1)
2 / subtract(xn, xn_1)

# =
^n, j when j == n + 1 ->
3.0 * ((yn - yn_1) / :math.pow(xn - xn_1, 2))
3.0 * (subtract(yn, yn_1) / :math.pow(subtract(xn, xn_1), 2))

# middle rows
i, j when j == i - 1 ->
[xi_1, xi] = Enum.map(-1..0, fn offset -> Enum.at(xs, i + offset) end)
1.0 / (xi - xi_1)
1.0 / subtract(xi, xi_1)

i, i ->
[xi_1, xi, xi1] = Enum.map(-1..1, fn offset -> Enum.at(xs, i + offset) end)
2.0 * (1.0 / (xi - xi_1) + 1.0 / (xi1 - xi))
2.0 * (1.0 / subtract(xi, xi_1) + 1.0 / subtract(xi1, xi))

i, j when j == i + 1 ->
[xi, xi1] = Enum.map(0..1, fn offset -> Enum.at(xs, i + offset) end)
1.0 / (xi1 - xi)
1.0 / subtract(xi1, xi)

# =
i, j when j == n + 1 ->
Expand All @@ -399,8 +461,8 @@ defmodule Expline.Spline do
|> Enum.map(&Map.get(points, &1))

3.0 *
((yi - yi_1) / :math.pow(xi - xi_1, 2) +
(yi1 - yi) / :math.pow(xi1 - xi, 2))
(subtract(yi, yi_1) / :math.pow(subtract(xi, xi_1), 2) +
subtract(yi1, yi) / :math.pow(subtract(xi1, xi), 2))

# empty terms
_i, _j ->
Expand All @@ -414,4 +476,9 @@ defmodule Expline.Spline do
Enum.zip(xs, Vector.to_list(derivative_vector)) |> Map.new()
end
end

@spec subtract(minuend :: nil | number(), subtrahend :: nil | number()) :: number()
defp subtract(nil, _), do: 0.0
defp subtract(_, nil), do: 0.0
defp subtract(minuend, subtrahend), do: minuend - subtrahend
end
33 changes: 8 additions & 25 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule Expline.Mixfile do
use Mix.Project

@app :expline
@version "0.2.0"
@app :explinex
@version "0.2.4"

def project do
[
Expand All @@ -22,7 +22,7 @@ defmodule Expline.Mixfile do

# Generated documentation parameters
name: "Expline",
source_url: "https://github.com/isaacsanders/#{@app}",
source_url: "https://github.com/am-kantox/#{@app}",
docs: docs()
]
end
Expand All @@ -42,34 +42,17 @@ defmodule Expline.Mixfile do

def package do
[
name: :expline,
organization: "kantox",
name: @app,
files: ["lib", "mix.exs", "README*", "LICENSE*"],
licenses: ["MIT"],
maintainers: ["Isaac Sanders"],
links: %{"GitHub" => "https://github.com/isaacsanders/#{@app}"}
maintainers: ["Isaac Sanders", "Aleksei Matiushkin"],
links: %{"GitHub" => "https://github.com/am-kantox/#{@app}"}
]
end

def dialyzer do
[
flags: [
:error_handling,
:no_behaviours,
:no_contracts,
:no_fail_call,
:no_fun_app,
:no_improper_lists,
:no_match,
:no_missing_calls,
:no_opaque,
:no_return,
:no_undefined_callbacks,
:no_unused,
:race_conditions,
:underspecs,
:unknown
]
]
[]
end

def docs do
Expand Down
12 changes: 7 additions & 5 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
%{
"decimal": {:hex, :decimal, "1.3.1", "157b3cedb2bfcb5359372a7766dd7a41091ad34578296e951f58a946fcab49c6", [:mix], []},
"dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"},
"dialyxir": {:hex, :dialyxir, "1.3.0", "fd1672f0922b7648ff9ce7b1b26fcf0ef56dda964a459892ad15f6b4410b5284", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "00b2a4bcd6aa8db9dcb0b38c1225b7277dca9bc370b6438715667071a304696f"},
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"},
"earmark_parser": {:hex, :earmark_parser, "1.4.32", "fa739a0ecfa34493de19426681b23f6814573faee95dfd4b4aafe15a7b5b32c6", [:mix], [], "hexpm", "b8b0dd77d60373e77a3d7e8afa598f325e49e8663a51bcc2b88ef41838cca755"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.21.3", "857ec876b35a587c5d9148a2512e952e24c24345552259464b98bfbb883c7b42", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "0db1ee8d1547ab4877c5b5dffc6604ef9454e189928d5ba8967d4a58a801f161"},
"makeup": {:hex, :makeup, "1.0.1", "82f332e461dc6c79dbd82fbe2a9c10d48ed07146f0a478286e590c83c52010b5", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "49736fe5b66a08d8575bf5321d716bac5da20c8e6b97714fec2bcd6febcfa1f8"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm", "589b5af56f4afca65217a1f3eb3fee7e79b09c40c742fddc1c312b3ac0b3399f"},
"ex_doc": {:hex, :ex_doc, "0.29.4", "6257ecbb20c7396b1fe5accd55b7b0d23f44b6aa18017b415cb4c2b91d997729", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "2c6699a737ae46cb61e4ed012af931b57b699643b24dabe2400a8168414bc4f5"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
"pollution": {:hex, :pollution, "0.9.2", "3f67542631071c99f807d2a8f9da799c07cd983c902f5357b9e1569c20a26e76", [:mix], [], "hexpm", "6399fd8ffd97dcc3d9d277f60542a234d644d7bcc0d48c8fda93d6be4801bac2"},
"quixir": {:hex, :quixir, "0.9.3", "f01c37386b9e1d0526f01a8734a6d7884af294a0ec360f05c24c7171d74632bd", [:mix], [{:pollution, "~> 0.9.2", [hex: :pollution, repo: "hexpm", optional: false]}], "hexpm", "4f3a1fe7c82b767d935b3f7b94cf34b91ef78bb487ef256b303d77417fc7d589"},
}