Skip to content

Commit

Permalink
Small improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Qqwy committed Oct 9, 2016
1 parent 7e76697 commit bbbfd52
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 13 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [2016] [Wiebe-Marten Wijnja]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 31 additions & 2 deletions lib/matrix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ defmodule Matrix do
@doc false
def inspect(matrix, _opts) do
"""
#Matrix-(#{Tensor.Inspect.dimension_string(matrix)})
#Matrix<(#{Tensor.Inspect.dimension_string(matrix)})
#{inspect_contents(matrix)}
>
"""
end

Expand Down Expand Up @@ -306,8 +307,36 @@ defmodule Matrix do


def product(_a = %Tensor{dimensions: [_,_]}, _b = %Tensor{dimensions: [_,_]}) do
raise Tensor.ArithmeticError, "Cannot compute dot product if the width of matrix `a` does not match the height of matrix `b`!"
raise Tensor.ArithmeticError, "Cannot compute Matrix.product if the width of matrix `a` does not match the height of matrix `b`!"
end

@doc """
Calculates the product of `matrix` with `matrix`, `exponent` times.
If `exponent` == 0, then the result will be the identity matrix with the same dimensions as the given matrix.
This is calculated using the fast [exponentiation by squaring](https://en.wikipedia.org/wiki/Exponentiation_by_squaring) algorithm.
"""
def power(matrix, exponent)

def power(matrix = %Tensor{dimensions: [a,a]}, negative_number) when negative_number < 0 do
product(Matrix.identity_matrix(-1, a), power(matrix, -negative_number))
end

def power(matrix = %Tensor{dimensions: [a,a]}, 0), do: Matrix.identity_matrix(a)
def power(matrix = %Tensor{dimensions: [a,a]}, 1), do: matrix

def power(matrix = %Tensor{dimensions: [a,a]}, exponent) when rem(exponent, 2) == 0 do
power(product(matrix, matrix), Kernel.div(exponent, 2))
end

def power(matrix = %Tensor{dimensions: [a,a]}, exponent) when rem(exponent, 2) == 1 do
product(matrix, power(product(matrix, matrix), Kernel.div(exponent, 2)))
end

def power(matrix = %Tensor{dimensions: [_,_]}) do
raise Tensor.ArithmeticError, "Cannot compute Matrix.power with non-square matrices"
end


end

8 changes: 4 additions & 4 deletions lib/tensor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ defmodule Tensor do
iex> mat = Matrix.new([[1,2],[3,4]], 2,2)
iex> {vector, mat2} = Tensor.pop(mat, 0)
iex> inspect(vector)
"#Vector-(2)[1, 2]"
iex> vector
#Vector<(2)[1, 2]>
iex> inspect(mat2)
"#Matrix-(2×2)
"#Matrix<(2×2)
┌ ┐
│ 0, 0│
│ 3, 4│
└ ┘
>
"
"""
@spec pop(tensor, integer, any) :: { tensor | any, tensor}
def pop(tensor, index, default \\ nil)
Expand Down
3 changes: 2 additions & 1 deletion lib/tensor/inspect.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule Tensor.Inspect do
def inspect(tensor, _opts) do
"""
#Tensor(#{dimension_string(tensor)})
#Tensor<(#{dimension_string(tensor)})
#{inspect_tensor_contents(tensor)}
>
"""
end

Expand Down
2 changes: 1 addition & 1 deletion lib/vector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Vector do
defmodule Inspect do
@doc false
def inspect(vector, _opts) do
"#Vector-(#{Tensor.Inspect.dimension_string(vector)})#{inspect Vector.to_list(vector)}"
"#Vector<(#{Tensor.Inspect.dimension_string(vector)})#{inspect Vector.to_list(vector)}>"
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Tensor.Mixfile do

def project do
[app: :tensor,
version: "0.7.0",
version: "0.7.1",
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand Down
12 changes: 8 additions & 4 deletions test/matrix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ defmodule MatrixTest do
test "Inspect" do
matrix = Matrix.new([[1,2],[3,4]], 2,2)
assert Inspect.inspect(matrix, []) == """
#Matrix-(2×2)
#Matrix<(2×2)
┌ ┐
│ 1, 2│
│ 3, 4│
└ ┘
>
"""
end

test "identity_matrix" do
inspect Matrix.identity_matrix(3) =="""
#Matrix-(3×3)
#Matrix<(3×3)
┌ ┐
│ 1, 0, 0│
│ 0, 1, 0│
│ 0, 0, 1│
└ ┘
>
"""
end

Expand All @@ -50,11 +52,12 @@ defmodule MatrixTest do
m1 = Matrix.new([[2,3,4],[1,0,0]], 2,3)
m2 = Matrix.new([[0,1000],[1,100],[0,10]], 3,2)
assert Matrix.product(m1, m2) |> inspect == """
#Matrix-(2×2)
#Matrix<(2×2)
┌ ┐
│ 3, 2340│
│ 0, 1000│
└ ┘
>
"""
end

Expand Down Expand Up @@ -82,7 +85,7 @@ defmodule MatrixTest do
matrix = Matrix.new(board_as_list, 8,8)
assert inspect(matrix) ==
"""
#Matrix-(8×8)
#Matrix<(8×8)
┌ ┐
│ "♜", "♞", "♝", "♛", "♚", "♝", "♞", "♜"│
│ "♟", "♟", "♟", "♟", "♟", "♟", "♟", "♟"│
Expand All @@ -93,6 +96,7 @@ defmodule MatrixTest do
│ "♙", "♙", "♙", "♙", "♙", "♙", "♙", "♙"│
│ "♖", "♘", "♗", "♕", "♔", "♗", "♘", "♖"│
└ ┘
>
"""

end
Expand Down

0 comments on commit bbbfd52

Please sign in to comment.