Changing the API around
This commit is contained in:
parent
1b69ec120e
commit
6c8ee1fded
|
@ -1,4 +1,3 @@
|
||||||
/docs/
|
|
||||||
/lib/
|
/lib/
|
||||||
/bin/
|
/bin/
|
||||||
/.shards/
|
/.shards/
|
||||||
|
|
1139
src/apatite.cr
1139
src/apatite.cr
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,27 @@
|
||||||
|
require "./linear_algebra/ndarray"
|
||||||
|
require "./linear_algebra/matrix"
|
||||||
|
require "./linear_algebra/vector"
|
||||||
|
|
||||||
|
module Apatite
|
||||||
|
module LinearAlgebra
|
||||||
|
extend self
|
||||||
|
|
||||||
|
# Calculates the sigmoid curve for a numeric input.
|
||||||
|
#
|
||||||
|
# f(x) = 1/(1 + e^-x)
|
||||||
|
#
|
||||||
|
# See also: [Sigmoid function [WikiWand]](https://www.wikiwand.com/en/Sigmoid_function)
|
||||||
|
def sigmoid(input : Number)
|
||||||
|
num = input.to_f64
|
||||||
|
1 / (1 + Math.exp(-num))
|
||||||
|
end
|
||||||
|
|
||||||
|
# Calculates the derivative sigmoid curve for a numeric input.
|
||||||
|
#
|
||||||
|
# f'(x) = f(x)(1 - f(x)),
|
||||||
|
def sigmoid_d(input : Number)
|
||||||
|
num = input.to_f64
|
||||||
|
num * (1 - num)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,6 +1,6 @@
|
||||||
require "./vector"
|
require "./vector"
|
||||||
|
|
||||||
module Apatite
|
module Apatite::LinearAlgebra
|
||||||
class Matrix
|
class Matrix
|
||||||
include Enumerable(Vector)
|
include Enumerable(Vector)
|
||||||
include Indexable(Vector)
|
include Indexable(Vector)
|
|
@ -1,4 +1,4 @@
|
||||||
module Apatite
|
module Apatite::LinearAlgebra
|
||||||
class NDArray
|
class NDArray
|
||||||
include Enumerable(Float64)
|
include Enumerable(Float64)
|
||||||
include Indexable(Float64)
|
include Indexable(Float64)
|
|
@ -1,6 +1,4 @@
|
||||||
require "../../apatite"
|
module Apatite::LinearAlgebra
|
||||||
|
|
||||||
module Apatite
|
|
||||||
# Represents a mathematical vector, and also constitutes a row or column
|
# Represents a mathematical vector, and also constitutes a row or column
|
||||||
# of a `Matrix`
|
# of a `Matrix`
|
||||||
class Vector
|
class Vector
|
||||||
|
@ -8,8 +6,13 @@ module Apatite
|
||||||
include Indexable(Float64)
|
include Indexable(Float64)
|
||||||
include Comparable(Vector)
|
include Comparable(Vector)
|
||||||
|
|
||||||
|
# Cartesian unit vector I
|
||||||
I = Vector.create([1.0, 0.0, 0.0])
|
I = Vector.create([1.0, 0.0, 0.0])
|
||||||
|
|
||||||
|
# Cartesian unit vector J
|
||||||
J = Vector.create([0.0, 1.0, 0.0])
|
J = Vector.create([0.0, 1.0, 0.0])
|
||||||
|
|
||||||
|
# Cartesian unit vector K
|
||||||
K = Vector.create([0.0, 0.0, 1.0])
|
K = Vector.create([0.0, 0.0, 1.0])
|
||||||
|
|
||||||
@buffer : Pointer(Float64)
|
@buffer : Pointer(Float64)
|
||||||
|
@ -386,8 +389,6 @@ module Apatite
|
||||||
Matrix[*vs].rank.equal?(vs.count)
|
Matrix[*vs].rank.equal?(vs.count)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Invokes the given block for each element of `self`, replacing the element
|
# Invokes the given block for each element of `self`, replacing the element
|
||||||
# with the value returned by the block. Returns `self`.
|
# with the value returned by the block. Returns `self`.
|
||||||
#
|
#
|
||||||
|
@ -701,6 +702,12 @@ module Apatite
|
||||||
map_with_index { |x, i| q[i - 1] + (q[i - 1] - x) }
|
map_with_index { |x, i| q[i - 1] + (q[i - 1] - x) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Sums the numbers in the vector and returns a sigmoid value
|
||||||
|
# across the whole vector.
|
||||||
|
def sigmoid
|
||||||
|
LinearAlgebra.sigmoid(sum)
|
||||||
|
end
|
||||||
|
|
||||||
# Utility to make sure vectors are 3D. If they are 2D, a zero
|
# Utility to make sure vectors are 3D. If they are 2D, a zero
|
||||||
# z-component is added.
|
# z-component is added.
|
||||||
def to_3d
|
def to_3d
|
||||||
|
@ -782,7 +789,6 @@ module Apatite
|
||||||
all?(&.zero?)
|
all?(&.zero?)
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
|
||||||
protected def size=(size : Int)
|
protected def size=(size : Int)
|
||||||
@size = size.to_i
|
@size = size.to_i
|
||||||
end
|
end
|
|
@ -0,0 +1,3 @@
|
||||||
|
module Apatite
|
||||||
|
VERSION = "0.1.0"
|
||||||
|
end
|
Loading…
Reference in New Issue