Skip to content

Commit

Permalink
Take a range of rows and create matrices from text
Browse files Browse the repository at this point in the history
  • Loading branch information
versilov committed May 12, 2018
1 parent 6b6f3b9 commit 46b65d6
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 7 deletions.
70 changes: 63 additions & 7 deletions lib/matrex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ defmodule Matrex do
when is_integer(key) and key > 0,
do: {:ok, row(matrex, key)}

@impl Access
def fetch(
%Matrex{
data: <<
_rows::unsigned-integer-little-32,
columns::unsigned-integer-little-32,
data::binary
>>
},
a..b
)
when b > a and columns == 1,
do:
{:ok,
%Matrex{
data:
<<b - a::unsigned-integer-little-32, 1::unsigned-integer-little-32,
binary_part(data, a * 4, (b - a) * 4)::binary>>
}}

@impl Access
def fetch(
%Matrex{
Expand Down Expand Up @@ -780,13 +800,6 @@ defmodule Matrex do
:filename.extension(file_name) == ".csv" ->
file_name
|> File.read!()
|> String.split("\n")
|> Enum.reject(&(String.length(&1) == 0))
|> Enum.map(fn line ->
line
|> String.split(",")
|> Enum.map(fn f -> Float.parse(f) |> elem(0) end)
end)
|> new()

:filename.extension(file_name) == ".mtx" ->
Expand Down Expand Up @@ -983,6 +996,49 @@ defmodule Matrex do
new(rows, cols, list_of_lists)
end

@doc """
Creates new matrix from text representation (i.e., from MatLab/Octave output).
## Examples
iex> Matrex.new("1;0;1;0;1")
#Matrex[5×1]
┌ ┐
│ 1.0 │
│ 0.0 │
│ 1.0 │
│ 0.0 │
│ 1.0 │
└ ┘
iex> Matrex.new(\"\"\"
...> 1.00000 0.10000 0.60000 1.10000
...> 1.00000 0.20000 0.70000 1.20000
...> 1.00000 0.30000 0.80000 1.30000
...> 1.00000 0.40000 0.90000 1.40000
...> 1.00000 0.50000 1.00000 1.50000
...> \"\"\")
#Matrex[5×4]
┌ ┐
│ 1.0 0.1 0.6 1.1 │
│ 1.0 0.2 0.7 1.2 │
│ 1.0 0.3 0.8 1.3 │
│ 1.0 0.4 0.9 1.4 │
│ 1.0 0.5 1.0 1.5 │
└ ┘
"""
@spec new(binary) :: matrex
def new(text) when is_binary(text) do
text
|> String.split(["\n", ";"], trim: true)
|> Enum.map(fn line ->
line
|> String.split(["\s", ","], trim: true)
|> Enum.map(fn f -> Float.parse(f) |> elem(0) end)
end)
|> new()
end

defp new_matrix_from_function(0, _, accumulator), do: %Matrex{data: accumulator}

defp new_matrix_from_function(size, function, accumulator),
Expand Down
28 changes: 28 additions & 0 deletions test/matrex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,34 @@ defmodule MatrexTest do
assert Matrex.neg(matrix) == expected
end

test "#new creates matrix from text representation" do
from_text =
Matrex.new("""
1.00000 0.10000 0.60000 1.10000
1.00000 0.20000 0.70000 1.20000
1.00000 0.30000 0.80000 1.30000
1.00000 0.40000 0.90000 1.40000
1.00000 0.50000 1.00000 1.50000
""")

expected =
Matrex.new([
[1.0, 0.1, 0.6, 1.1],
[1.0, 0.2, 0.7, 1.2],
[1.0, 0.3, 0.8, 1.3],
[1.0, 0.4, 0.9, 1.4],
[1.0, 0.5, 1.0, 1.5]
])

assert from_text == expected
end

test "#new creates matirx from one-line text form" do
from_line = Matrex.new("1;0;1;0;1")
expected = Matrex.new([[1], [0], [1], [0], [1]])
assert from_line == expected
end

test "#new creates a new matrix initialized by a function" do
rows = 2
columns = 3
Expand Down

0 comments on commit 46b65d6

Please sign in to comment.