Class: GSL::Contour

Inherits:
Object
  • Object
show all
Defined in:
lib/gsl_extras.rb

Overview

A class for making contours of a function on a regular two dimensional grid. If contours of scattered data are required, see GSL::ScatterInterp#to_contour.

Constant Summary collapse

VALID_CONNECTIONS =

Edges: 1 __

0 | 4 \ /5  | 2
  | 7 / \ 6 |
    3 __
[[0,4,5,2], [0,4,1], [0,7,3], [0,7,6,2], [0,4,5,6,3],[0,7,6,5,1], [1,4,7,3], [1,5,6,3], [1,5,2], [1,4,7,6,2], [2,6,3],[2,5,4,7,3]]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, grid) ⇒ Contour

Returns a new instance of Contour.

Raises:

  • (ArgumentError)


285
286
287
288
289
290
# File 'lib/gsl_extras.rb', line 285

def initialize(x, y, grid)
  @x = x; @y=y; @grid=grid
#       p @grid, @x, @y
  raise ArgumentError.new("Unmatching data sizes: #{x.size}, #{y.size}, #{grid.shape}") unless [x.size, y.size] == grid.shape
  @adaptive = false      
end

Instance Attribute Details

#keep_path_dataObject

Returns the value of attribute keep_path_data.



283
284
285
# File 'lib/gsl_extras.rb', line 283

def keep_path_data
  @keep_path_data
end

Class Method Details

.alloc(x, y, grid) ⇒ Object

Create a new Contour object. x and y are vectors of coordinates, and grid is a matrix of values on those coordinates.



279
280
281
# File 'lib/gsl_extras.rb', line 279

def self.alloc(x, y, grid)
  new(x, y, grid)
end

Instance Method Details

#contours(*values) ⇒ Object

Create a series of contours at the given values. Returns a hash of => array_of_contours. The array_of_contours is a an array of arrays, where each array is a list of [x, y] coordinates along the contour.



309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/gsl_extras.rb', line 309

def contours(*values)
  (values = (0..values[0]+1).to_a.map{|i| i.to_f * (@grid.max - @grid.min) / ( values[0]+1) + @grid.min}; values.pop; values.shift) if values.size==1 and values[0].kind_of? Integer
  cons = values.inject({}){|hash, val| hash[val] = []; hash}
#       p cons
  for i in 0...((@x.size / 2.0).ceil - 1)
    for j in 0...((@y.size / 2.0).ceil - 1)
      analyse_cell(i*2, j*2, cons)
    end
  end
#       pp cons
  cons.keys.each{|val| cons[val] = connect_contours(cons[val])}
  @last_contours = cons
end

#graphkit(*args) ⇒ Object

Create a GraphKit object of the contours.



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/gsl_extras.rb', line 325

def graphkit(*args)
  if args.size == 0
    conts = @last_contours
  else
    conts = contours(*args)
  end
  graphs = conts.map do |val, cons|
    unless cons[0]
      nil
    else
      (cons.map do |con|
#         p con
        contour = con.transpose
        kit = CodeRunner::GraphKit.autocreate({x: {data: contour[0]}, y: {data: contour[1], title: val.to_s}})
        kit.data[0].with = "l"
        kit
      end).sum
    end
  end
  graphs.compact.reverse.sum
end

#set_adaptive(func, scale, multi_adaptive = false) ⇒ Object

:nodoc:



294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/gsl_extras.rb', line 294

def set_adaptive(func, scale, multi_adaptive=false) # :nodoc:
  @func = func; @adaptive = true
  @multi_adaptive = multi_adaptive
  if @multi_adaptive
    @adaption_scale = 4
    raise "Adaption scale should be a power of two for multi_adaptive contour generation" if scale % 2 == 1 and not scale == 1
  
    @next_adaption_scale = scale / 2
  else
    @adaption_scale = scale*2
  end
end