Changing the API around
This commit is contained in:
parent
1b69ec120e
commit
6c8ee1fded
|
@ -1,4 +1,3 @@
|
|||
/docs/
|
||||
/lib/
|
||||
/bin/
|
||||
/.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"
|
||||
|
||||
module Apatite
|
||||
module Apatite::LinearAlgebra
|
||||
class Matrix
|
||||
include Enumerable(Vector)
|
||||
include Indexable(Vector)
|
||||
|
@ -467,7 +467,7 @@ module Apatite
|
|||
end
|
||||
|
||||
def row(i, &block)
|
||||
@rows.fetch(i){return self}.each(&block)
|
||||
@rows.fetch(i) { return self }.each(&block)
|
||||
self
|
||||
end
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
module Apatite
|
||||
module Apatite::LinearAlgebra
|
||||
class NDArray
|
||||
include Enumerable(Float64)
|
||||
include Indexable(Float64)
|
|
@ -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`.
|
||||
#
|
||||
|
@ -525,9 +526,9 @@ module Apatite
|
|||
Vector[-@buffer[1], @buffer[0]]
|
||||
when 3
|
||||
v = vs[0]
|
||||
Vector[ v[2] * @buffer[1] - v[1] * @buffer[2],
|
||||
Vector[v[2] * @buffer[1] - v[1] * @buffer[2],
|
||||
v[0] * @buffer[2] - v[2] * @buffer[0],
|
||||
v[1] * @buffer[0] - v[0] * @buffer[1] ]
|
||||
v[1] * @buffer[0] - v[0] * @buffer[1]]
|
||||
else
|
||||
# TODO
|
||||
# rows = [self, *vs, Vector.new(size) {|i| Vector.basis(size: size, index: i) }]
|
||||
|
@ -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
|
|
@ -0,0 +1,3 @@
|
|||
module Apatite
|
||||
VERSION = "0.1.0"
|
||||
end
|
Loading…
Reference in New Issue