Project

vectorops

0.0
No commit activity in last 3 years
No release in over 3 years
Advanced operations for Ruby Vector class
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0
~> 3.0.0.beta2
 Project Readme

vectorops

Advanced Vector Operations for Ruby.

READ THIS

This is work in progress and only very few operations are implemented. Feel free to contribute by adding more vector operations like multiplication and so on.

Basic

When you want to perform vector operations like summation in ruby, you cannot use the array class as it behaves like this:

  v1 = [1,1,1,0,0,0]
  v2 = [1,1,1,1,1,1]

  v1 + v2
  # -> [1,1,1,0,0,0,1,1,1,1,1,1]

For this purpose, there is a Vector class, defined in Ruby's stdlib. With Vector objects it's possible to do the following:

  require 'matrix.rb'

  v1 = Vector[1,1,1,0,0,0]
  v2 = Vector[1,1,1,1,1,1]

  v1 + v2
  # -> Vector[2,2,2,1,1,1]

which is exactly what you want. Unfortunately, as of now, the libary seems not to be quite mature, as neither of the following will work by default:

  v1 = Vector[1,1,1,0,0,0]
  v2 = Vector[1,1,1,1,1,1]

  # assigning values to a vector is impossible 
  # as no []= method is defined in the lib
  v1 += v2

  # adding up subsets of the vectors is not possible, as 
  # the [] accessor returns an array rather than a Vector
  v1[0..3] + v2[0..3]

  # adding a scalar to a vector doesn't work as expected as 
  # the + method doesn't handle this case
  v1 + 2

Using the vectorops Gem, the following operations work like expected

  v1 = Vector[1,1,1,0,0,0]
  v2 = Vector[1,1,1,1,1,1]

  v1[0..3]
  # -> Vector[1,1,1]

  v1 += v2
  # -> v1 == Vector[2,2,2,1,1,1]

  v1[0..3] += v2[0..3]
  # -> v1 == Vector[2,2,2,0,0,0]

  v1 + 2
  # -> Vector[3,3,3,1,1,1]

Usage

Install vectorops using the command

  gem install vectorops

Or include the vectorops gem into your Gemfile

  gem 'vectorops'