GithubHelp home page GithubHelp logo

matrix's Introduction

matrix Build Status

This is a Matrix class for Crystal. There are a few ways to create a Matrix:

# Creates a Matrix of Int32 with 3 rows and 2 columns. A Tuple of rows can also 
# be used instead of an array. Each row must have the same number of elements.
Matrix.rows([[1, 2], [3, 4], [5, 6]]) 
# 1, 2
# 3, 4
# 5, 6

# Creates a Matrix with 2 rows and 3 columns. Like with Matrix.rows, the columns 
# must have the same number of elements.
Matrix.columns([[1, 2], [3, 4], [5, 6]])
# 1, 3, 5
# 2, 4, 6

# A Matrix can also be created by giving its number of columns and rows, just 
# like an Array can be created by giving it a starting size. This constructor 
# will yield the linear index, the current row and the current column.
Matrix.new(2, 2) { |idx, row, col| idx  }
# 0, 1
# 2, 3
Matrix.new(2, 2) { |idx, row, col| row  }
# 0, 0
# 1, 1
Matrix.new(2, 2) { |idx, row, col| col  }
 # 0, 1
 # 0, 1

Most methods are documented in the matrix.cr file itself.

matrix's People

Contributors

dkhofer avatar jacobub avatar mamantoha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

matrix's Issues

Spec Fails in Crystal version 0.34.0

Trying to use the Matrix shard, running spec causes below fail:
root@ROB-MSI:/mnt/d/Workspace/matrix# crystal spec --error-trace
error in line 1
Error: while requiring "./spec/matrix_spec.cr"
In spec/matrix_spec.cr:245:21
245 | m.determinant.should be_close(1.25922e+45, 1e40)
^-----
Error: instantiating '(Float64 | Int32 | Nil)#should(Spec::CloseExpectation(Float64, Float64))'
In /usr/share/crystal/src/spec/expectations.cr:446:26
446 | unless expectation.match self
^----
Error: instantiating 'Spec::CloseExpectation(Float64, Float64)#match(Nil)'

In /usr/share/crystal/src/spec/expectations.cr:127:21
127 | (actual_value - @expected_value).abs <= @delta
^
Error: undefined method '-' for Nil

Nil trace:
/usr/share/crystal/src/spec/expectations.cr:127
(actual_value - @expected_value).abs <= @delta

/usr/share/crystal/src/spec/expectations.cr:126
def match(actual_value)

Also seeing a similar error in the transpose function as well.

Can't run specs, compilation fails

Hi,

I thought I'd try upgrading some code to Crystal 0.16.0, but the compiler failed with a problem in the matrix code (on which my code relies). I tried running the specs for the matrix shard and the compiler doesn't like what it sees:

$ crystal spec
Error in ./spec/matrix_spec.cr:7: instantiating 'Matrix(T):Class#rows(Array(Array(Int32)))'

      m = Matrix.rows([[1, 2], [3, 4], [5, 6]])
                 ^~~~

in ./src/matrix.cr:60: instantiating 'Matrix(T):Class#new(Int32, Int32)'

    Matrix.new(rows.size.to_i32, rows.first.size.to_i32) do |i, r, c|
           ^~~

in ./src/matrix.cr:38: instantiating 'Matrix(Int32):Class#new(Int32, Int32)'

    matrix = Matrix(T).new(rows, columns)
                       ^~~

instantiating 'Matrix(Int32)#initialize(Int32, Int32)'

in ./src/matrix.cr:28: instantiating 'size()'

    @buffer = Pointer(T).malloc(size)
                                ^~~~

in ./src/matrix.cr:454: undefined method '*' for Int+ (compile-time type is (Int+ | Int32))

    @rows * @columns
          ^


$ crystal --version
Crystal 0.16.0 (Sun May  8 12:21:15 UTC 2016)

I almost think this is a problem with Crystal itself, given that I'm not really sure what an Int+ is, but thought I'd check here first. If you think I should file a bug in the main Crystal repo let me know, I'm happy to do that. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.