Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Vector.linspace

  • Loading branch information...
commit f346df2bc5f7adb639bb9b2dee8a84babe9e15d4 1 parent bf0d142
Matias N. authored
Showing with 10 additions and 0 deletions.
  1. +6 −0 lib/gslng/vector.rb
  2. +4 −0 test/vector_test.rb
6 lib/gslng/vector.rb
View
@@ -58,6 +58,12 @@ def Vector.from_array(array)
return v
end
+ # Creates a Vector with linearly distributed values between +start+ and +stop+, separated by +delta+.
+ def Vector.linspace(start, stop, delta)
+ if (start > stop || delta <= 0) then raise 'Invalid values' end
+ Vector.new(((stop - start) / delta).floor.to_i + 1) {|i| start + delta * i}
+ end
+
# Creates a Vector from an Array or a Range
# @see Vector::from_array
# @example
4 test/vector_test.rb
View
@@ -16,6 +16,10 @@ def test_initialize
assert_equal(10, Vector.zero(10).size)
assert(Vector.zero(10).zero?)
assert_nothing_raised { Vector.random(10) }
+ assert_equal(Vector[0], Vector.linspace(0, 1, 1.5))
+ assert_equal(Vector[0,1], Vector.linspace(0, 1, 1))
+ assert_equal(Vector[0,0.5,1], Vector.linspace(0, 1, 0.5))
+ assert_equal(Vector[0,0.7], Vector.linspace(0, 1, 0.7))
end
def test_to_s
Please sign in to comment.
Something went wrong with that request. Please try again.