Skip to content

Instantly share code, notes, and snippets.

@rysk-t
Created February 3, 2015 04:24
Show Gist options
  • Select an option

  • Save rysk-t/8d1aef0fb67abde1d259 to your computer and use it in GitHub Desktop.

Select an option

Save rysk-t/8d1aef0fb67abde1d259 to your computer and use it in GitHub Desktop.
linspace in ruby
def linspace(initVal, last, num)
arr = [*initVal..num]
arr = arr.collect{|i| i.to_f*last/num}
return arr
end
@zhipingx

zhipingx commented May 5, 2016

Copy link
Copy Markdown

made it more like python linspace :)

def linspace(low, high, num)
  [*0..num].collect { |i| low + i.to_f * (high-low)/num }
end

@pelegm

pelegm commented Nov 18, 2016

Copy link
Copy Markdown

made it more like python linspace :)

Note that num here doesn't work like num in Python. The length of the list it returns is longer by one.
This is more similar to numpy's version:

def linspace(low, high, num)                                                                                                                  
  [*0..(num-1)].collect { |i| low + i.to_f * (high-low)/(num-1) }                                                                             
end

@kojix2

kojix2 commented Nov 2, 2019

Copy link
Copy Markdown

Numo::NArray is Ruby alternative to Python numpy

require 'numo/narray'

Numo::Int32.linspace(0, 100, 11)
#=> Numo::Int32#shape=[11]
#[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

@ChildrenofkoRn

Copy link
Copy Markdown
require "benchmark/ips"
require 'numo/narray'

Benchmark.ips do |x|

  def linespace(low, high, num)
    [*0..(num-1)].collect { |i| low + i.to_f * (high-low)/(num-1) }
  end

  def linespace_w_step(low, high, num)
    step = high / (num - 1).to_f
    low.step(by: step, to: high).to_a
  end

  params = [0, 1.5, 149]

  x.report "linespace" do
    linespace(*params)
  end

  x.report "linespace_w_step" do
    linespace_w_step(*params)
  end

  x.report "numo/narray" do
    Numo::DFloat.linspace(*params)
  end

  # object Numo::DFloat#shape to Array
  x.report "numo/narray w .to_a" do
    Numo::DFloat.linspace(*params).to_a
  end

  x.compare!
end
Warming up --------------------------------------
           linespace     3.713k i/100ms
    linespace_w_step    15.084k i/100ms
         numo/narray    61.781k i/100ms
 numo/narray w .to_a    22.587k i/100ms
Calculating -------------------------------------
           linespace     37.499k (± 1.2%) i/s -    189.363k in   5.050482s
    linespace_w_step    151.478k (± 1.2%) i/s -    769.284k in   5.079202s
         numo/narray    606.801k (± 3.0%) i/s -      3.089M in   5.095471s
 numo/narray w .to_a    224.785k (± 2.9%) i/s -      1.129M in   5.028979s

Comparison:
         numo/narray:   606800.9 i/s
 numo/narray w .to_a:   224785.1 i/s - 2.70x  (± 0.00) slower
    linespace_w_step:   151478.4 i/s - 4.01x  (± 0.00) slower
           linespace:    37499.1 i/s - 16.18x  (± 0.00) slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment