class Chem::Linalg::Matrix

Overview

A Matrix is a rectangular array of numbers, arranged in rows and columns.

It behaves similar to an Array, but items are accessed by two indexes: row and column. Similar to Array, array indexing starts at 0. A negative index is assumed to be relative to the end of the row/column: -1 indicates the last element, -2 is the second to last element, and so on.

There are several convenience methods to create a Matrix (see below), but using the #[] short-hand method is the most common:

require "chem"
alias M = Chem::Linalg::Matrix # avoid repeating the full name
M[[0, 1, 2], [3, 4, 5]]        # => Matrix[[0, 1, 2], [3, 4, 5]]

This partial implementation is heavily based on the original Matrix code in the standard library, which was then moved to a shard (currently available at github.com/Exilor/matrix).

NOTE numbers are internally saved in double precision floating-point format. NOTE: The implementation of some methods may be too slow for big matrices. In such cases, it is suggested to use a specialized library that leverages low-level optimized linear algebra packages such as BLAS/LAPACK

Defined in:

chem/linalg/matrix.cr

Constructors

Instance Method Summary

Constructor Detail

def self.[](*rows : Indexable(Number)) : self #

[View source]
def self.build(rows : Number, columns : Number, &block : Pointer(Float64) -> ) : self #

[View source]
def self.column(*values : Number) : self #

[View source]
def self.diagonal(size : Int, initial_value : Float64) : self #

[View source]
def self.diagonal(size : Int, &block : Int32 -> Number::Primitive) : self #

[View source]
def self.diagonal(*values : Number) : self #

[View source]
def self.identity(size : Int) : self #

[View source]
def self.new(rows : Int32, columns : Int32, initial_value : Float64 | Nil = nil) #

[View source]
def self.new(rows : Int32, columns : Int32, &) #

[View source]
def self.read(path : Path | String, rows : Number, columns : Number) : self #

[View source]
def self.read(io, rows : Number, columns : Number) : self #

[View source]
def self.square(size : Int, initial_value : Float64 | Nil = nil) : self #

[View source]
def self.square(size : Int, &block : Int32, Int32 -> Number::Primitive) : self #

[View source]

Instance Method Detail

def *(other : Number) : self #

[View source]
def *(other : self) : self #

[View source]
def +(other : Number) : self #

[View source]
def +(other : self) : self #

[View source]
def -(other : Number) : self #

[View source]
def -(other : self) : self #

[View source]
def /(other : Number) : self #

[View source]
def ==(other : self) : Bool #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


[View source]
def [](i : Int, j : Int) : Float64 #

[View source]
def []=(i : Int, j : Int, value : Float64) : Float64 #

[View source]
def []?(i : Int, j : Int) : Float64 | Nil #

[View source]
def columns : Int32 #

[View source]
def det : Float64 #

[View source]
def dim : Tuple(Int32, Int32) #

[View source]
def dup : self #
Description copied from class Reference

Returns a shallow copy of this object.

This allocates a new object and copies the contents of self into it.


[View source]
def each_with_index : Iterator(Tuple(Float64, Int32, Int32)) #

[View source]
def each_with_index(&block : Float64, Int32, Int32 -> ) #

[View source]
def inspect(io : ::IO) #

[View source]
def inv : self #

[View source]
def map(&block : Float64 -> Float64) : self #

[View source]
def map_with_index(&block : Float64, Int32, Int32, Int32 -> Float64) : self #

[View source]
def reshape(rows : Int, columns : Int) : self #

[View source]
def reshape!(rows : Int, columns : Int) : self #

[View source]
def resize(rows : Int, columns : Int, fill_value : Number = 0) : self #

[View source]
def resize!(rows : Int, columns : Int, fill_value : Number = 0) : self #

[View source]
def rows : Int32 #

[View source]
def singular? : Bool #

[View source]
def size : Int32 #

[View source]
def square? : Bool #

[View source]
def swap_rows(i1 : Int, i2 : Int) : self #

[View source]
def to_a : Array(Array(Float64)) #

[View source]
def to_s(io : ::IO) #

[View source]
def to_unsafe : Pointer(Float64) #

[View source]
def to_vector : Spatial::Vector #

[View source]
def unsafe_fetch(i : Int32, j : Int32) : Float64 #

[View source]
def unsafe_fetch(index : Int) : Float64 #

[View source]