Changing the API around

This commit is contained in:
Chris Watson 2019-06-12 19:23:30 -07:00
parent 1b69ec120e
commit 6c8ee1fded
No known key found for this signature in database
GPG Key ID: 37DAEF5F446370A4
7 changed files with 83 additions and 1115 deletions

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
/docs/
/lib/
/bin/
/.shards/

File diff suppressed because it is too large Load Diff

View File

@ -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

View File

@ -1,6 +1,6 @@
require "./vector"
module Apatite
module Apatite::LinearAlgebra
class Matrix
include Enumerable(Vector)
include Indexable(Vector)

View File

@ -1,4 +1,4 @@
module Apatite
module Apatite::LinearAlgebra
class NDArray
include Enumerable(Float64)
include Indexable(Float64)

View File

@ -1,6 +1,4 @@
require "../../apatite"
module Apatite
module Apatite::LinearAlgebra
# Represents a mathematical vector, and also constitutes a row or column
# of a `Matrix`
class Vector
@ -8,8 +6,13 @@ module Apatite
include Indexable(Float64)
include Comparable(Vector)
# Cartesian unit vector I
I = Vector.create([1.0, 0.0, 0.0])
# Cartesian unit vector J
J = Vector.create([0.0, 1.0, 0.0])
# Cartesian unit vector K
K = Vector.create([0.0, 0.0, 1.0])
@buffer : Pointer(Float64)
@ -386,8 +389,6 @@ module Apatite
Matrix[*vs].rank.equal?(vs.count)
end
# Invokes the given block for each element of `self`, replacing the element
# 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) }
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
# z-component is added.
def to_3d
@ -782,7 +789,6 @@ module Apatite
all?(&.zero?)
end
# :nodoc:
protected def size=(size : Int)
@size = size.to_i
end

3
src/apatite/version.cr Normal file
View File

@ -0,0 +1,3 @@
module Apatite
VERSION = "0.1.0"
end