Updated specs. Now working with 0.31.0.

This commit is contained in:
Chris Watson 2019-08-23 13:45:07 -07:00
parent c7bc6f4f5e
commit 57cdb9f6cc
22 changed files with 1146 additions and 1140 deletions

View File

@ -1,6 +1,6 @@
# Apatite
Apatite is meant to be a collecion of mathmatical and scientific computing algorithms for the Crystal programming language. I don't expect it to ever reach the level of completeness as numpy, but hopefully it can save some people the trouble of implementing these methods on their own.
Apatite is a collection of mathematical and scientific algorithms. Currently it implements the API from Ruby's `Matrix` class for both `Matrix` and `Vector`. This API will be added to as needs arise. The goal is for this project to eventually contain everything you could get from SciPy, but in pure Crystal.
## Installation

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="css/style.css" rel="stylesheet" type="text/css">
@ -171,28 +171,28 @@ of Crystal.</p>
<h2>Defined in:</h2>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/error.cr#L1" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/error.cr#L1" target="_blank">
apatite/linear_algebra/error.cr
</a>
<br/>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra.cr#L6" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra.cr#L6" target="_blank">
apatite/linear_algebra.cr
</a>
<br/>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite.cr#L10" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite.cr#L10" target="_blank">
apatite.cr
</a>
<br/>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/version.cr#L1" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/version.cr#L1" target="_blank">
apatite/version.cr
</a>
@ -230,12 +230,12 @@ of Crystal.</p>
<h3>Instance methods inherited from module <code><a href="Apatite/LinearAlgebra.html">Apatite::LinearAlgebra</a></code></h3>
<a href="Apatite/LinearAlgebra.html#sigmoid%28input%3ANumber%29-instance-method" class="tooltip">
<a href="Apatite/LinearAlgebra.html#sigmoid(input:Number)-instance-method" class="tooltip">
<span>sigmoid(input : Number)</span>
sigmoid</a>,
<a href="Apatite/LinearAlgebra.html#sigmoid_d%28input%3ANumber%29-instance-method" class="tooltip">
<a href="Apatite/LinearAlgebra.html#sigmoid_d(input:Number)-instance-method" class="tooltip">
<span>sigmoid_d(input : Number)</span>
sigmoid_d</a>

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="../css/style.css" rel="stylesheet" type="text/css">
@ -163,49 +163,49 @@
<h2>Defined in:</h2>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/error.cr#L2" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/error.cr#L2" target="_blank">
apatite/linear_algebra/error.cr
</a>
<br/>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/vector.cr#L4" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/vector.cr#L5" target="_blank">
apatite/linear_algebra/vector.cr
</a>
<br/>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/matrix/eigenvalue_decomposition.cr#L1" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/matrix/eigenvalue_decomposition.cr#L1" target="_blank">
apatite/linear_algebra/matrix/eigenvalue_decomposition.cr
</a>
<br/>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/matrix/lup_decomposition.cr#L1" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/matrix/lup_decomposition.cr#L1" target="_blank">
apatite/linear_algebra/matrix/lup_decomposition.cr
</a>
<br/>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/matrix.cr#L5" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/matrix.cr#L5" target="_blank">
apatite/linear_algebra/matrix.cr
</a>
<br/>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra.cr#L7" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra.cr#L7" target="_blank">
apatite/linear_algebra.cr
</a>
<br/>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L1" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/ndarray.cr#L1" target="_blank">
apatite/linear_algebra/ndarray.cr
</a>
@ -224,14 +224,14 @@
<ul class="list-summary">
<li class="entry-summary">
<a href="#sigmoid%28input%3ANumber%29-instance-method" class="signature"><strong>#sigmoid</strong>(input : Number)</a>
<a href="#sigmoid(input:Number)-instance-method" class="signature"><strong>#sigmoid</strong>(input : Number)</a>
<div class="summary"><p>Calculates the sigmoid curve for a numeric input.</p></div>
</li>
<li class="entry-summary">
<a href="#sigmoid_d%28input%3ANumber%29-instance-method" class="signature"><strong>#sigmoid_d</strong>(input : Number)</a>
<a href="#sigmoid_d(input:Number)-instance-method" class="signature"><strong>#sigmoid_d</strong>(input : Number)</a>
<div class="summary"><p>Calculates the derivative sigmoid curve for a numeric input.</p></div>
@ -259,7 +259,7 @@
def <strong>sigmoid</strong>(input : Number)
<a class="method-permalink" href="#sigmoid%28input%3ANumber%29-instance-method">#</a>
<a class="method-permalink" href="#sigmoid(input:Number)-instance-method">#</a>
</div>
<div class="doc"><p>Calculates the sigmoid curve for a numeric input.</p>
@ -271,7 +271,7 @@
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra.cr#L15" target="_blank">View source</a>]
[<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra.cr#L15" target="_blank">View source</a>]
</div>
</div>
@ -281,7 +281,7 @@
def <strong>sigmoid_d</strong>(input : Number)
<a class="method-permalink" href="#sigmoid_d%28input%3ANumber%29-instance-method">#</a>
<a class="method-permalink" href="#sigmoid_d(input:Number)-instance-method">#</a>
</div>
<div class="doc"><p>Calculates the derivative sigmoid curve for a numeric input.</p>
@ -291,7 +291,7 @@
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra.cr#L23" target="_blank">View source</a>]
[<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra.cr#L23" target="_blank">View source</a>]
</div>
</div>

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
@ -151,7 +151,7 @@
<h2>Defined in:</h2>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/error.cr#L4" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/error.cr#L4" target="_blank">
apatite/linear_algebra/error.cr
</a>

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
@ -151,7 +151,7 @@
<h2>Defined in:</h2>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/error.cr#L6" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/error.cr#L6" target="_blank">
apatite/linear_algebra/error.cr
</a>

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
@ -151,7 +151,7 @@
<h2>Defined in:</h2>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/error.cr#L8" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/error.cr#L7" target="_blank">
apatite/linear_algebra/error.cr
</a>
@ -162,16 +162,6 @@
<h2>Constructors</h2>
<ul class="list-summary">
<li class="entry-summary">
<a href="#new%28method%2Cthis%2Cother%29-class-method" class="signature"><strong>.new</strong>(method, this, other)</a>
</li>
</ul>
@ -222,25 +212,6 @@
</div>
<h2>Constructor Detail</h2>
<div class="entry-detail" id="new(method,this,other)-class-method">
<div class="signature">
def self.<strong>new</strong>(method, this, other)
<a class="method-permalink" href="#new%28method%2Cthis%2Cother%29-class-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/error.cr#L9" target="_blank">View source</a>]
</div>
</div>

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
@ -164,7 +164,7 @@
<h2>Defined in:</h2>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/error.cr#L3" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/error.cr#L3" target="_blank">
apatite/linear_algebra/error.cr
</a>

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="../../../css/style.css" rel="stylesheet" type="text/css">
@ -163,7 +163,7 @@ equal to the eigenvalues and V is formed by the eigenvectors.</p>
<h2>Defined in:</h2>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/matrix/eigenvalue_decomposition.cr#L12" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/matrix/eigenvalue_decomposition.cr#L12" target="_blank">
apatite/linear_algebra/matrix/eigenvalue_decomposition.cr
</a>
@ -178,7 +178,7 @@ equal to the eigenvalues and V is formed by the eigenvectors.</p>
<ul class="list-summary">
<li class="entry-summary">
<a href="#new%28matrix%29-class-method" class="signature"><strong>.new</strong>(matrix)</a>
<a href="#new(matrix)-class-method" class="signature"><strong>.new</strong>(matrix)</a>
</li>
@ -224,13 +224,13 @@ equal to the eigenvalues and V is formed by the eigenvectors.</p>
def self.<strong>new</strong>(matrix)
<a class="method-permalink" href="#new%28matrix%29-class-method">#</a>
<a class="method-permalink" href="#new(matrix)-class-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/matrix/eigenvalue_decomposition.cr#L13" target="_blank">View source</a>]
[<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/matrix/eigenvalue_decomposition.cr#L13" target="_blank">View source</a>]
</div>
</div>

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="../../../css/style.css" rel="stylesheet" type="text/css">
@ -163,7 +163,7 @@ linear equations. This will fail if singular? returns true.</p>
<h2>Defined in:</h2>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/matrix/lup_decomposition.cr#L12" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/matrix/lup_decomposition.cr#L12" target="_blank">
apatite/linear_algebra/matrix/lup_decomposition.cr
</a>
@ -178,7 +178,7 @@ linear equations. This will fail if singular? returns true.</p>
<ul class="list-summary">
<li class="entry-summary">
<a href="#new%28matrix%29-class-method" class="signature"><strong>.new</strong>(matrix)</a>
<a href="#new(matrix)-class-method" class="signature"><strong>.new</strong>(matrix)</a>
</li>
@ -224,13 +224,13 @@ linear equations. This will fail if singular? returns true.</p>
def self.<strong>new</strong>(matrix)
<a class="method-permalink" href="#new%28matrix%29-class-method">#</a>
<a class="method-permalink" href="#new(matrix)-class-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/matrix/lup_decomposition.cr#L13" target="_blank">View source</a>]
[<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/matrix/lup_decomposition.cr#L13" target="_blank">View source</a>]
</div>
</div>

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
@ -139,17 +139,6 @@
<h2>Included Modules</h2>
<ul class="other-types-list">
<li class="other-type">Comparable(<a href="../../Apatite/LinearAlgebra/NDArray.html">Apatite::LinearAlgebra::NDArray</a>)</li>
<li class="other-type">Enumerable(Float64)</li>
<li class="other-type">Indexable(Float64)</li>
</ul>
@ -162,7 +151,7 @@
<h2>Defined in:</h2>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L2" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/ndarray.cr#L2" target="_blank">
apatite/linear_algebra/ndarray.cr
</a>
@ -173,105 +162,11 @@
<h2>Constructors</h2>
<ul class="list-summary">
<li class="entry-summary">
<a href="#new%28data%3AArray%28Number%29%2Cshape%3AArray%28Int32%29%3F%3Dnil%29-class-method" class="signature"><strong>.new</strong>(data : Array(Number), shape : Array(Int32)? = <span class="n">nil</span>)</a>
</li>
</ul>
<h2>Instance Method Summary</h2>
<ul class="list-summary">
<li class="entry-summary">
<a href="#%5B%5D%28%2Aargs%2C%2A%2Aoptions%29-instance-method" class="signature"><strong>#[]</strong>(*args, **options)</a>
</li>
<li class="entry-summary">
<a href="#%5B%5D%28%2Aargs%2C%2A%2Aoptions%2C%26block%29-instance-method" class="signature"><strong>#[]</strong>(*args, **options, &block)</a>
</li>
<li class="entry-summary">
<a href="#%5B%5D%3D%28%2Aargs%2C%2A%2Aoptions%29-instance-method" class="signature"><strong>#[]=</strong>(*args, **options)</a>
</li>
<li class="entry-summary">
<a href="#%5B%5D%3F%28%2Aargs%2C%2A%2Aoptions%29-instance-method" class="signature"><strong>#[]?</strong>(*args, **options)</a>
</li>
<li class="entry-summary">
<a href="#%5B%5D%3F%28%2Aargs%2C%2A%2Aoptions%2C%26block%29-instance-method" class="signature"><strong>#[]?</strong>(*args, **options, &block)</a>
</li>
<li class="entry-summary">
<a href="#abs-instance-method" class="signature"><strong>#abs</strong></a>
<div class="summary"><p>Returns the absolute value of every item in the array</p></div>
</li>
<li class="entry-summary">
<a href="#acos-instance-method" class="signature"><strong>#acos</strong></a>
<div class="summary"><p>Returns the arccosine of each element in the current array.</p></div>
</li>
<li class="entry-summary">
<a href="#data%3AArray%28Float64%29-instance-method" class="signature"><strong>#data</strong> : Array(Float64)</a>
</li>
<li class="entry-summary">
<a href="#shape%3AArray%28Int32%29-instance-method" class="signature"><strong>#shape</strong> : Array(Int32)</a>
</li>
<li class="entry-summary">
<a href="#size%28%2Aargs%2C%2A%2Aoptions%29-instance-method" class="signature"><strong>#size</strong>(*args, **options)</a>
</li>
<li class="entry-summary">
<a href="#size%28%2Aargs%2C%2A%2Aoptions%2C%26block%29-instance-method" class="signature"><strong>#size</strong>(*args, **options, &block)</a>
</li>
<li class="entry-summary">
<a href="#to_unsafe%28%2Aargs%2C%2A%2Aoptions%29-instance-method" class="signature"><strong>#to_unsafe</strong>(*args, **options)</a>
</li>
<li class="entry-summary">
<a href="#to_unsafe%28%2Aargs%2C%2A%2Aoptions%2C%26block%29-instance-method" class="signature"><strong>#to_unsafe</strong>(*args, **options, &block)</a>
</li>
<li class="entry-summary">
<a href="#unsafe_fetch%28%2Aargs%2C%2A%2Aoptions%29-instance-method" class="signature"><strong>#unsafe_fetch</strong>(*args, **options)</a>
</li>
<li class="entry-summary">
<a href="#unsafe_fetch%28%2Aargs%2C%2A%2Aoptions%2C%26block%29-instance-method" class="signature"><strong>#unsafe_fetch</strong>(*args, **options, &block)</a>
</li>
</ul>
@ -297,75 +192,6 @@
</div>
<h2>Constructor Detail</h2>
<div class="entry-detail" id="new(data:Array(Number),shape:Array(Int32)?=nil)-class-method">
<div class="signature">
def self.<strong>new</strong>(data : <a href="../../Array.html">Array</a>(Number), shape : <a href="../../Array.html">Array</a>(Int32)? = <span class="n">nil</span>)
<a class="method-permalink" href="#new%28data%3AArray%28Number%29%2Cshape%3AArray%28Int32%29%3F%3Dnil%29-class-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L18" target="_blank">View source</a>]
</div>
</div>
@ -373,253 +199,6 @@
<h2>Instance Method Detail</h2>
<div class="entry-detail" id="[](*args,**options)-instance-method">
<div class="signature">
def <strong>[]</strong>(*args, **options)
<a class="method-permalink" href="#%5B%5D%28%2Aargs%2C%2A%2Aoptions%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L11" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="[](*args,**options,&amp;block)-instance-method">
<div class="signature">
def <strong>[]</strong>(*args, **options, &block)
<a class="method-permalink" href="#%5B%5D%28%2Aargs%2C%2A%2Aoptions%2C%26block%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L11" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="[]=(*args,**options)-instance-method">
<div class="signature">
def <strong>[]=</strong>(*args, **options)
<a class="method-permalink" href="#%5B%5D%3D%28%2Aargs%2C%2A%2Aoptions%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L13" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="[]?(*args,**options)-instance-method">
<div class="signature">
def <strong>[]?</strong>(*args, **options)
<a class="method-permalink" href="#%5B%5D%3F%28%2Aargs%2C%2A%2Aoptions%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L12" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="[]?(*args,**options,&amp;block)-instance-method">
<div class="signature">
def <strong>[]?</strong>(*args, **options, &block)
<a class="method-permalink" href="#%5B%5D%3F%28%2Aargs%2C%2A%2Aoptions%2C%26block%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L12" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="abs-instance-method">
<div class="signature">
def <strong>abs</strong>
<a class="method-permalink" href="#abs-instance-method">#</a>
</div>
<div class="doc"><p>Returns the absolute value of every item in the array</p></div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L24" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="acos-instance-method">
<div class="signature">
def <strong>acos</strong>
<a class="method-permalink" href="#acos-instance-method">#</a>
</div>
<div class="doc"><p>Returns the arccosine of each element in the current array.</p></div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L29" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="data:Array(Float64)-instance-method">
<div class="signature">
def <strong>data</strong> : <a href="../../Array.html">Array</a>(Float64)
<a class="method-permalink" href="#data%3AArray%28Float64%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L9" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="shape:Array(Int32)-instance-method">
<div class="signature">
def <strong>shape</strong> : <a href="../../Array.html">Array</a>(Int32)
<a class="method-permalink" href="#shape%3AArray%28Int32%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L11" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="size(*args,**options)-instance-method">
<div class="signature">
def <strong>size</strong>(*args, **options)
<a class="method-permalink" href="#size%28%2Aargs%2C%2A%2Aoptions%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L16" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="size(*args,**options,&amp;block)-instance-method">
<div class="signature">
def <strong>size</strong>(*args, **options, &block)
<a class="method-permalink" href="#size%28%2Aargs%2C%2A%2Aoptions%2C%26block%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L16" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="to_unsafe(*args,**options)-instance-method">
<div class="signature">
def <strong>to_unsafe</strong>(*args, **options)
<a class="method-permalink" href="#to_unsafe%28%2Aargs%2C%2A%2Aoptions%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L15" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="to_unsafe(*args,**options,&amp;block)-instance-method">
<div class="signature">
def <strong>to_unsafe</strong>(*args, **options, &block)
<a class="method-permalink" href="#to_unsafe%28%2Aargs%2C%2A%2Aoptions%2C%26block%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L15" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="unsafe_fetch(*args,**options)-instance-method">
<div class="signature">
def <strong>unsafe_fetch</strong>(*args, **options)
<a class="method-permalink" href="#unsafe_fetch%28%2Aargs%2C%2A%2Aoptions%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L14" target="_blank">View source</a>]
</div>
</div>
<div class="entry-detail" id="unsafe_fetch(*args,**options,&amp;block)-instance-method">
<div class="signature">
def <strong>unsafe_fetch</strong>(*args, **options, &block)
<a class="method-permalink" href="#unsafe_fetch%28%2Aargs%2C%2A%2Aoptions%2C%26block%29-instance-method">#</a>
</div>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/ndarray.cr#L14" target="_blank">View source</a>]
</div>
</div>

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
@ -151,7 +151,7 @@
<h2>Defined in:</h2>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/linear_algebra/error.cr#L5" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/linear_algebra/error.cr#L5" target="_blank">
apatite/linear_algebra/error.cr
</a>

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="css/style.css" rel="stylesheet" type="text/css">
@ -200,7 +200,7 @@ set <span class="o">&lt;&lt;</span> <span class="n">3</span></code></pre>
<h2>Defined in:</h2>
<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/core_ext/array.cr#L1" target="_blank">
<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/core_ext/array.cr#L1" target="_blank">
apatite/core_ext/array.cr
</a>
@ -219,14 +219,14 @@ set <span class="o">&lt;&lt;</span> <span class="n">3</span></code></pre>
<ul class="list-summary">
<li class="entry-summary">
<a href="#all%3F-instance-method" class="signature"><strong>#all?</strong></a>
<a href="#all?-instance-method" class="signature"><strong>#all?</strong></a>
<div class="summary"><p>Tests whether all elements evaluate to true</p></div>
</li>
<li class="entry-summary">
<a href="#any%3F-instance-method" class="signature"><strong>#any?</strong></a>
<a href="#any?-instance-method" class="signature"><strong>#any?</strong></a>
<div class="summary"><p>Tests whether any of the elements evaluate to true</p></div>
@ -326,7 +326,7 @@ set <span class="o">&lt;&lt;</span> <span class="n">3</span></code></pre>
def <strong>all?</strong>
<a class="method-permalink" href="#all%3F-instance-method">#</a>
<a class="method-permalink" href="#all?-instance-method">#</a>
</div>
<div class="doc"><p>Tests whether all elements evaluate to true</p></div>
@ -334,7 +334,7 @@ set <span class="o">&lt;&lt;</span> <span class="n">3</span></code></pre>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/core_ext/array.cr#L3" target="_blank">View source</a>]
[<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/core_ext/array.cr#L3" target="_blank">View source</a>]
</div>
</div>
@ -344,7 +344,7 @@ set <span class="o">&lt;&lt;</span> <span class="n">3</span></code></pre>
def <strong>any?</strong>
<a class="method-permalink" href="#any%3F-instance-method">#</a>
<a class="method-permalink" href="#any?-instance-method">#</a>
</div>
<div class="doc"><p>Tests whether any of the elements evaluate to true</p></div>
@ -352,7 +352,7 @@ set <span class="o">&lt;&lt;</span> <span class="n">3</span></code></pre>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/core_ext/array.cr#L11" target="_blank">View source</a>]
[<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/core_ext/array.cr#L11" target="_blank">View source</a>]
</div>
</div>
@ -370,7 +370,7 @@ set <span class="o">&lt;&lt;</span> <span class="n">3</span></code></pre>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/core_ext/array.cr#L19" target="_blank">View source</a>]
[<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/core_ext/array.cr#L19" target="_blank">View source</a>]
</div>
</div>
@ -386,7 +386,7 @@ set <span class="o">&lt;&lt;</span> <span class="n">3</span></code></pre>
<br/>
<div>
[<a href="https://github.com/watzon/apatite/blob/fd6da99f9d1de3a90a6315a813fcb7206a053d9a/src/apatite/core_ext/array.cr#L24" target="_blank">View source</a>]
[<a href="https://github.com/watzon/apatite/blob/c7bc6f4f5ec3f4b2725b0b6bdf03d9524007e864/src/apatite/core_ext/array.cr#L24" target="_blank">View source</a>]
</div>
</div>

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Crystal Docs 0.29.0">
<meta name="generator" content="Crystal Docs 0.30.1">
<link href="css/style.css" rel="stylesheet" type="text/css">
@ -125,7 +125,7 @@
<div class="main-content">
<h1>Apatite</h1>
<p>Apatite is meant to be a collecion of mathmatical and scientific computing algorithms for the Crystal programming language. I don't expect it to ever reach the level of completeness as numpy, but hopefully it can save some people the trouble of implementing these methods on their own.</p>
<p>Apatite is a collection of mathematical and scientific algorithms. Currently it implements the API from Ruby's <code>Matrix</code> class for both <code>Matrix</code> and <code>Vector</code>. This API will be added to as needs arise. The goal is for this project to eventually contain everything you could get from SciPy, but in pure Crystal.</p>
<h2>Installation</h2>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,161 +1,259 @@
require "../spec_helper"
describe "Apatite::Vector" do
x = Apatite::Vector.new([3, 4])
describe ".[]" do
it "creates a new vector from a list of elements" do
vec = Apatite::Vector[1, 2, 3]
vec.should be_a Apatite::Vector(Int32)
vec[0].should eq 1
vec[2].should eq 3
end
end
describe ".elements" do
it "creates a new vector from an Array" do
arr = [1, 2, 3]
vec = Apatite::Vector.elements(arr)
vec.should be_a Apatite::Vector(Int32)
vec[0].should eq 1
vec[2].should eq 3
end
end
describe ".basis" do
it "creates a standard basis-n vector" do
vec = Apatite::Vector.basis(5, 1)
vec.should eq Apatite::Vector[0, 1, 0, 0, 0]
vec = Apatite::Vector.basis(3, 2)
vec.should eq Apatite::Vector[0, 0, 1]
end
end
describe ".independent?" do
it "returns true if all of vectors are linearly independent" do
Apatite::Vector.independent?(Apatite::Vector[1,0], Apatite::Vector[0,1]).should be_true
Apatite::Vector.independent?(Apatite::Vector[1,2], Apatite::Vector[2,4]).should be_false
end
end
describe ".zero" do
it "creates a new zero vector" do
vec = Apatite::Vector(Int32).zero(5)
vec.should eq Apatite::Vector[0, 0, 0, 0, 0]
end
end
describe "#==" do
it "is true if elements are the same" do
(x == Apatite::Vector.new([3, 4])).should be_true
x = Apatite::Vector.elements([3, 4])
(x == Apatite::Vector.elements([3, 4])).should be_true
end
it "is false if elements are different" do
(x == Apatite::Vector.new([7, 1])).should be_false
x = Apatite::Vector.elements([3, 4])
(x == Apatite::Vector.elements([7, 1])).should be_false
end
end
describe "#e" do
it "returns the `ith` element in the vector" do
x.e(2).should eq(4)
describe "#+" do
it "adds a number to every item in the vector" do
vec = Apatite::Vector[1, 2, 3]
vec2 = vec + 2
vec2.should eq Apatite::Vector[3, 4, 5]
end
it "returns `nil` if `n` is out of range" do
x.e(7).should be_nil
it "adds the items in one vector to each parallel item in another" do
vec1 = Apatite::Vector[1, 2, 3]
vec2 = Apatite::Vector[3, 2, 1]
vec3 = vec1 + vec2
vec3.should be_a Apatite::Vector(Int32)
vec3.should eq Apatite::Vector[4, 4, 4]
end
pending "adds a vector to a matrix"
end
describe "#-" do
it "subtracts a number from every item in the vector" do
vec = Apatite::Vector[3, 4, 5]
vec2 = vec - 2
vec2.should eq Apatite::Vector[1, 2, 3]
end
it "subtracts the items in one vector from each parallel item in another" do
vec1 = Apatite::Vector[1, 2, 3]
vec2 = Apatite::Vector[3, 2, 1]
vec3 = vec1 - vec2
vec3.should be_a Apatite::Vector(Int32)
vec3.should eq Apatite::Vector[-2, 0, 2]
end
pending "subtracts a vector from a matrix"
end
describe "#*" do
it "multiplies every item in the vector by a number" do
vec = Apatite::Vector[2, 3, 4]
vec2 = vec * 2
vec2.should eq Apatite::Vector[4, 6, 8]
end
it "multiplies the items in one vector with each parallel item in another" do
vec1 = Apatite::Vector[1, 2, 3]
vec2 = Apatite::Vector[3, 2, 1]
vec3 = vec1 * vec2
vec3.should be_a Apatite::Vector(Int32)
vec3.should eq Apatite::Vector[3, 4, 3]
end
pending "multiplies a vector with a matrix"
end
describe "#/" do
it "divides every item in the vector by a number" do
vec = Apatite::Vector[2.0, 3.0, 4.0]
vec2 = vec / 2
vec2.should eq Apatite::Vector[1.0, 1.5, 2.0]
end
it "divides the items in one vector by each parallel item in another" do
vec1 = Apatite::Vector[1.0, 2.0, 3.0]
vec2 = Apatite::Vector[3.0, 2.0, 1.0]
vec3 = vec1 / vec2
vec3.should be_a Apatite::Vector(Float64)
vec3.should eq Apatite::Vector[0.3333333333333333, 1.0, 3.0]
end
pending "divides a vector by a matrix"
end
describe "#==" do
it "ensures two vectors are equal" do
vec1 = Apatite::Vector[1, 2, 3]
vec2 = Apatite::Vector[1, 2, 3]
(vec1 == vec2).should be_true
end
it "checks equality between a vector and an array" do
vec = Apatite::Vector[1, 2, 3]
arr = [1, 2, 3]
(vec == arr).should be_true
end
end
describe "#to_unit_vector" do
it "successfully converts a vector to a unit vector" do
res = [0.6, 0.8]
x.to_unit_vector.each_with_index do |e, i|
e.should be_close(res[i], 0.1)
describe "#angle_with" do
it "returns an angle with another vector" do
vec1 = Apatite::Vector[1, 2, 3]
vec2 = Apatite::Vector[3, 2, 1]
vec1.angle_with(vec2).should eq 0.7751933733103613
end
end
describe "#clone" do
it "creates a copy of the vector" do
vec = Apatite::Vector[1, 2, 3]
cpy = vec.clone
vec.should eq cpy
vec.object_id.should_not eq cpy.object_id
end
end
describe "#map" do
it "should map over a vector's elements" do
vec1 = Apatite::Vector[1, 2, 3]
vec2 = vec1.map(&.succ)
vec2.should eq Apatite::Vector[2, 3, 4]
end
it "should map over two vectors simultaniously" do
vec1 = Apatite::Vector[1, 2, 3]
vec2 = Apatite::Vector[4, 5, 6]
vec3 = vec1.map(vec2) { |v1, v2| v1 + v2 }
vec3.should eq Apatite::Vector[5, 7, 9]
end
end
describe "#covector" do
it "should create a single row matrix from the vector" do
vec = Apatite::Vector[1, 2, 3]
mat = vec.covector
mat.should be_a Apatite::Matrix(Int32)
mat.row_count.should eq 1
mat[0].should eq [1, 2, 3]
end
end
describe "#cross_product" do
it "should return the cross product of multiple vectors" do
vec1 = Apatite::Vector[1, 2, 3]
cross = vec1.cross_product(Apatite::Vector[3, 4, 5])
cross.should eq Apatite::Vector[-2, 4, -2]
end
end
describe "#inner_product" do
it "should return the inner product of two vectors" do
vec = Apatite::Vector[1, 2, 3]
prod = vec.inner_product(Apatite::Vector[3, 4, 5])
prod.should eq 26
end
end
describe "#each" do
it "should iterate over each item in the vector" do
vec = Apatite::Vector[1, 2, 3]
index = 0
vec.each do |n|
n.should eq index + 1
index += 1
end
end
end
describe "#dimensions" do
it "gets vector's dimensions" do
x.dimensions.should eq({1, 2})
describe "#magnitude" do
it "should return the modulus of a vector" do
vec = Apatite::Vector[1, 2, 3]
vec.magnitude.should eq 3.7416573867739413
end
end
describe "#rows" do
it "gets vector's rows" do
x.rows.should eq(1)
describe "#normalize" do
it "should return a vector with the same direction, but norm 1" do
vec = Apatite::Vector[1, 2, 3]
vec.normalize.should eq Apatite::Vector[0.2672612419124244, 0.5345224838248488, 0.8017837257372732]
end
end
describe "#cols" do
it "gets vector's columns" do
x.cols.should eq(2)
describe "#round" do
it "should round each entry in the vector" do
vec = Apatite::Vector[0.2672612419124244, 0.5345224838248488, 0.8017837257372732]
vec.round(2).should eq Apatite::Vector[0.27, 0.53, 0.80]
end
end
describe "#product" do
it "computes the product of a vector" do
x.product.should eq(12)
end
describe "#coerce" do
it "should return a new vector with elements of a different type" do
vec = Apatite::Vector[1, 2, 3]
flt = vec.coerce(Float64)
flt.should be_a Apatite::Vector(Float64)
end
describe "#angle_from" do
it "should compute the angle between `y` and `z`" do
y = Apatite::Vector.create([1, 1])
z = Apatite::Vector.create([1, 0])
y.angle_from(z).should be_close(Math::PI / 4, 0.1)
end
it "should work with big rationals" do
vec = Apatite::Vector[1, 2, 3]
rat = vec.coerce(BigRational, 2)
rat.should be_a Apatite::Vector(BigRational)
end
describe "#parallel_to?" do
it "correctly determines if a vector is parallel to another" do
x.parallel_to?(Apatite::Vector.create([6, 8])).should be_true
x.parallel_to?(Apatite::Vector.create([1, 1])).should be_false
it "should work with complex numbers" do
vec = Apatite::Vector[1, 2, 3]
com = vec.coerce(Complex, 1)
com.should be_a Apatite::Vector(Complex)
end
end
describe "#antiparallel_to?" do
it "correctly determines if a vector is antiparallel to another" do
x.antiparallel_to?(Apatite::Vector.create([-3, -4])).should be_true
x.antiparallel_to?(x).should be_false
end
end
describe "#perpendicular_to?" do
it "correctly determines if a vector is antiparallel to another" do
x.perpendicular_to?(Apatite::Vector.create([-4, 3])).should be_true
x.perpendicular_to?(x).should be_false
end
end
describe "#dot" do
it "calculates the dot product of the vector and another vector" do
x.dot(Apatite::Vector.create([2, 3])).should eq(18)
end
end
describe "#add" do
it "adds a number to every item in a vector" do
x.add(2).should eq([5, 6])
end
it "adds an enumerable to a vector" do
x.add([3, 2]).should eq([6, 6])
end
end
describe "#subtract" do
it "subtracts a number from every item in a vector" do
x.subtract(2).should eq([1, 2])
end
it "subtracts an enumerable from a vector" do
x.subtract([3, 2]).should eq([0, 2])
end
end
describe "#multiply" do
it "multiplies a number with every item in a vector" do
x.multiply(2).should eq([6, 8])
end
it "multiplies an enumerable with a vector" do
x.multiply([3, 2]).should eq([9, 8])
end
end
describe "#sum" do
it "sums all items in a vector" do
x.sum.should eq(7)
end
end
describe "#chomp" do
it "returns a new vector with the first `n` items of the old vector" do
x.chomp(1).should eq([4])
end
end
describe "#top" do
it "returns a new vector with the last `n` items of the old vector" do
x.top(1).should eq([3])
end
end
describe "#augment" do
it "creates a new vector with the elements fro vector b appended to those from vector a" do
y = x.clone
y.augment(Apatite::Vector.create([5])).should eq([3, 4, 5])
end
end
describe ".log" do
it "should calculate the log of the vector" do
pp x
x.log.should eq([1.0986122886681098, 1.3862943611198906])
end
end
it "should allow for scalar addition" do
a = Apatite::Vector.create([2, 3, 4])
b = a.add(1)
b.should eq([3, 4, 5])
end
end

View File

@ -4,12 +4,6 @@ module Apatite
class ErrDimensionMismatch < Error; end
class ZeroVectorError < Error; end
class ErrNotRegular < Error; end
class ErrOperationNotDefined < Error
def initialize(method, this, other)
message = "no overload matches '#{this}##{method}' with type #{other}"
super(message)
end
end
class ErrOperationNotDefined < Error; end
end
end

View File

@ -292,6 +292,10 @@ module Apatite::LinearAlgebra
@column_count = column_count || rows[0].try &.size
end
def <=>(other : Matrix)
rows <=> other.rows
end
# Returns row `i` of the matrix as an Array. Raises if the
# index is not found.
def [](i)
@ -668,7 +672,7 @@ module Apatite::LinearAlgebra
raise "laplace_expansion of empty matrix is not defined" if empty?
unless 0 <= num && num < row_count
raise ArgumentError, "invalid num (#{num.inspect} for 0..#{row_count - 1})"
raise ArgumentError.new("invalid num (#{num.inspect} for 0..#{row_count - 1})")
end
if row
@ -676,7 +680,7 @@ module Apatite::LinearAlgebra
e * cofactor(num, k)
end.reduce(&.+)
else
col(num).map_with_index do |e, k|
column(num).map_with_index do |e, k|
e * cofactor(k, num)
end.reduce(&.+)
end

View File

@ -1,32 +1,32 @@
module Apatite::LinearAlgebra
class NDArray
include Enumerable(Float64)
include Indexable(Float64)
include Comparable(NDArray)
# include Enumerable(Float64)
# include Indexable(Float64)
# include Comparable(NDArray)
getter data : Array(Float64)
# getter data : Array(Float64)
getter shape : Array(Int32)
# getter shape : Array(Int32)
delegate :[], to: @data
delegate :[]?, to: @data
delegate :[]=, to: @data
delegate :unsafe_fetch, to: @data
delegate :to_unsafe, to: @data
delegate :size, to: @data
# delegate :[], to: @data
# delegate :[]?, to: @data
# delegate :[]=, to: @data
# delegate :unsafe_fetch, to: @data
# delegate :to_unsafe, to: @data
# delegate :size, to: @data
def initialize(data : Array(Number), shape : Array(Int32)? = nil)
@data = data.is_a?(Array(Float64)) ? data.flatten : data.flatten.map(&.to_f64)
@shape = shape || [@data.size]
end
# def initialize(data : Array(Number), shape : Array(Int32)? = nil)
# @data = data.is_a?(Array(Float64)) ? data.flatten : data.flatten.map(&.to_f64)
# @shape = shape || [@data.size]
# end
# Returns the absolute value of every item in the array
def abs
map { |e| e.abs }
end
# # Returns the absolute value of every item in the array
# def abs
# map { |e| e.abs }
# end
# Returns the arccosine of each element in the current array.
def acos
end
# # Returns the arccosine of each element in the current array.
# def acos
# end
end
end

View File

@ -1,5 +1,6 @@
require "complex"
require "big"
require "json"
require "./matrix"
module Apatite::LinearAlgebra
# Represents a mathematical vector, and also constitutes a row or column
@ -48,7 +49,7 @@ module Apatite::LinearAlgebra
# Vector.independent?(Vector[1,2], Vector[2,4])
# # => false
# ```
def Vector.independent?(*vs)
def self.independent?(*vs)
vs.each do |v|
raise "expected Vector, but got #{v.class}" unless v.is_a?(Vector)
raise ErrDimensionMismatch.new unless v.size == vs.first.size
@ -64,64 +65,82 @@ module Apatite::LinearAlgebra
new(array)
end
# Multiplies the vector by x, where x is a number or a matrix.
def *(x)
case x
when Number
# Alien mothership
def <=>(other : Vector | Indexable)
case other
when Vector
elements <=> other.elements
when Indexable
elements <=> other
end
end
# Multiplies the vector by x, where x is a number.
def *(x : Number)
els = @elements.map { |e| (e * x).as(T) }
self.class.elements(els, false)
when Matrix
Matrix.column_vector(self) * x
when Vector
self.elements.zip(x.elements).map { |(x, y)| x * y }
else
raise ArgumentError.new
end
# Multiplies the vector by x, where x is a matrix.
def *(x : Matrix)
Matrix.column_vector(self) * x
end
# Multiplies the vector by x, where x is another vector.
def *(x : Vector)
els = self.elements.zip(x.elements).map { |(x, y)| x * y }
self.class.elements(els, false)
end
# Vector addition.
def +(x)
case x
when Number
def +(x : Number)
els = @elements.map { |e| (e + x).as(T) }
self.class.elements(els, false)
when Matrix
Matrix.column_vector(self) + x
when Vector
self.elements.zip(x.elements).map { |(x, y)| x + y }
else
raise ArgumentError.new
end
# Vector addition.
def +(x : Matrix)
Matrix.column_vector(self) + x
end
# Vector addition.
def +(x : Vector)
els = self.elements.zip(x.elements).map { |(x, y)| x + y }
self.class.elements(els, false)
end
# Vector subtraction.
def -(x)
case x
when Number
def -(x : Number)
els = @elements.map { |e| (e - x).as(T) }
self.class.elements(els, false)
when Matrix
Matrix.column_vector(self) - x
when Vector
self.elements.zip(x.elements).map { |(x, y)| x - y }
else
raise ArgumentError.new
end
# Vector subtraction.
def -(x : Matrix)
Matrix.column_vector(self) - x
end
# Vector subtraction.
def -(x : Vector)
els = self.elements.zip(x.elements).map { |(x, y)| x - y }
self.class.elements(els, false)
end
# Vector division.
def /(x)
case x
when Number
def /(x : Number)
els = @elements.map { |e| (e / x).as(T) }
self.class.elements(els, false)
when Matrix
Matrix.column_vector(self) / x
when Vector
self.elements.zip(x.elements).map { |(x, y)| x / y }
else
raise ArgumentError.new
end
# Vector division.
def /(x : Matrix)
Matrix.column_vector(self) / x
end
# Vector division.
def /(x : Vector)
els = self.elements.zip(x.elements).map { |(x, y)| x / y }
self.class.elements(els, false)
end
# Equality operator
@ -156,12 +175,15 @@ module Apatite::LinearAlgebra
self.class.elements(@elements)
end
# Maps over a vector, passing each element to the block
def map(&block : T -> _)
els = @elements.map(&block)
self.class.elements(els, false)
end
def map2(v, &block : T, T -> _)
# Maps over the current vector and `v` in conjunction, passing each
# element in each to the block and returning a new vector
def map(v, &block : T, T -> _)
raise ErrDimensionMismatch.new if size != v.size
arr = Array.new(size) do |i|
yield @elements[i], v[i]
@ -192,7 +214,7 @@ module Apatite::LinearAlgebra
v[0]*@elements[2] - v[2]*@elements[0],
v[1]*@elements[0] - v[0]*@elements[1] ]
else
rows = self + vs + Array.new(size) {|i| Vector.basis(size, i) }
rows = [self, vs.to_a, Array.new(size) {|i| Vector.basis(size, i) }].flatten
Matrix.rows(rows).laplace_expansion(row: size - 1)
end
end
@ -205,10 +227,7 @@ module Apatite::LinearAlgebra
# Returns the inner product of this vector with the other.
def inner_product(v)
raise ErrDimensionMismatch.new if size != v.size
p = 0
each2(v) { |v1, v2| p += v1 * v2 }
p
map(v) { |v1, v2| v1 * v2 }.sum
end
# ditto
@ -217,7 +236,7 @@ module Apatite::LinearAlgebra
end
# Iterate over the elements of this vector and `v` in conjunction.
def each2(v, &block)
def each(v, &block)
raise ErrDimensionMismatch.new if size != v.size
size.times do |i|
yield @elements[i], v[i]
@ -239,7 +258,7 @@ module Apatite::LinearAlgebra
def normalize
n = magnitude
raise ZeroVectorError.new("Zero vectors can not be normalized") if n == 0
self / n
self.coerce(Float64) / n
end
# ditto
@ -252,23 +271,31 @@ module Apatite::LinearAlgebra
map{ |e| e.round(ndigits) }
end
# The coerce method allows you to attempt to coerce the elements
# in the matrix to another type. The type
def coerce(klass, *args)
case klass.to_s
when "Complex"
raise "coercing to a Complex requires a second argument" unless args[0]?
els = @elements.map { |e| Complex.new(e, args[0].as(Int32)) }
when "BigInt"
base = args[0]? || 10
els = @elements.map { |e| klass.new(e, base) }
when "BigRational"
raise "coercing to a BigRational requires a second argument to use as a denominator" unless args[0]?
els = @elements.map { |e| klass.new(e, args[0]) }
else
els = @elements.map { |e| klass.new(e) }
# Attempt to coerce the elements in a vector to Complex with
# `imag` as the imaginary number.
def coerce(klass : Complex.class, imag : Number)
els = @elements.map { |e| Complex.new(e, imag) }
Vector.elements(els)
end
# Attempt to coerce the elements in a vector to BigInt with
# an optional `base` value.
def coerce(klass : BigInt.class, base = 10)
els = @elements.map { |e| BigInt.new(e, base) }
Vector.elements(els)
end
# Attempt to coerce the elements in a vector to BigRational
# with the given `denominator`.
def coerce(klass : BigRational.class, denominator : Int)
els = @elements.map { |e| BigRational.new(e, denominator) }
Vector.elements(els)
end
# The coerce method allows you to attempt to coerce the elements
# in the matrix to another type.
def coerce(klass : U.class) : Vector(U) forall U
els = @elements.map { |e| klass.new(e).as(U) }
Vector.elements(els)
end