Class: MDArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable, GenericFunctions
Defined in:
lib/mdarray.rb,
lib/mdarray/views.rb,
lib/mdarray/access.rb,
lib/mdarray/counter.rb,
lib/mdarray/section.rb,
lib/mdarray/creation.rb,
lib/mdarray/printing.rb,
lib/mdarray/lazy_mdarray.rb,
lib/mdarray/ruby_generic_functions.rb

Direct Known Subclasses

NonNumericalMDArray, NumericalMDArray

Defined Under Namespace

Classes: Counter, IteratorFast, IteratorFastBoolean, IteratorFastByte, IteratorFastChar, IteratorFastDouble, IteratorFastFloat, IteratorFastInt, IteratorFastLong, IteratorFastShort, Section

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FunctionCreation

#make_binary_op, #make_unary_op

Methods included from RubyFunctions

#make_binary_operator, #make_binary_operators, #make_comparison_operator, #make_unary_operator, #make_unary_operators, #ruby_binary_function, #ruby_unary_function

Constructor Details

#initialize(type, storage, section = false) ⇒ MDArray


Initializes an MDArray


Parameters:

  • type (String)

    the type of the mdarray: boolean, byte, int, short, long, float, double

  • storage (Array)

    a ruby array with the initialization data to the MDArray

  • section (defaults to: false)

    True if this is an mdarray section



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mdarray.rb', line 115

def initialize(type, storage, section = false)
  
  @id = (0..8).map{MDArray.characters.sample}.join
  @type = type
  @nc_array = storage
  @local_index = Counter.new(self)
  @local_iterator = nil
  @section = section
  @coerced = false            # should never be set by the user! For internal use only!
  @binary_operator = nil
  @unary_operator = nil

  # initialize printing defaults
  printing_defaults

end

Class Attribute Details

.binary_operatorObject

Returns the value of attribute binary_operator.



97
98
99
# File 'lib/mdarray.rb', line 97

def binary_operator
  @binary_operator
end

.charactersObject (readonly)

Returns the value of attribute characters.



101
102
103
# File 'lib/mdarray.rb', line 101

def characters
  @characters
end

.function_mapObject

Returns the value of attribute function_map.



94
95
96
# File 'lib/mdarray.rb', line 94

def function_map
  @function_map
end

.functionsObject

Returns the value of attribute functions.



93
94
95
# File 'lib/mdarray.rb', line 93

def functions
  @functions
end

.non_numericalObject (readonly)

Returns the value of attribute non_numerical.



96
97
98
# File 'lib/mdarray.rb', line 96

def non_numerical
  @non_numerical
end

.numericalObject (readonly)

Returns the value of attribute numerical.



95
96
97
# File 'lib/mdarray.rb', line 95

def numerical
  @numerical
end

.previous_binary_operatorObject

Returns the value of attribute previous_binary_operator.



99
100
101
# File 'lib/mdarray.rb', line 99

def previous_binary_operator
  @previous_binary_operator
end

.previous_unary_operatorObject

Returns the value of attribute previous_unary_operator.



100
101
102
# File 'lib/mdarray.rb', line 100

def previous_unary_operator
  @previous_unary_operator
end

.unary_operatorObject

Returns the value of attribute unary_operator.



98
99
100
# File 'lib/mdarray.rb', line 98

def unary_operator
  @unary_operator
end

Instance Attribute Details

#binary_operatorObject

binary_operator and unary_operator are instance variables that allow overwriting the class variables binary_operator and unary_operator



77
78
79
# File 'lib/mdarray.rb', line 77

def binary_operator
  @binary_operator
end

#coercedObject

Returns the value of attribute coerced.



79
80
81
# File 'lib/mdarray.rb', line 79

def coerced
  @coerced
end

#float_output_precision=(value) ⇒ Object (writeonly)

Sets the attribute float_output_precision

Parameters:

  • value

    the value to set the attribute float_output_precision to.



40
41
42
# File 'lib/mdarray/printing.rb', line 40

def float_output_precision=(value)
  @float_output_precision = value
end

#float_output_suppress_small=(value) ⇒ Object (writeonly)

Sets the attribute float_output_suppress_small

Parameters:

  • value

    the value to set the attribute float_output_suppress_small to.



41
42
43
# File 'lib/mdarray/printing.rb', line 41

def float_output_suppress_small=(value)
  @float_output_suppress_small = value
end

#formatter=(value) ⇒ Object (writeonly)

Sets the attribute formatter

Parameters:

  • value

    the value to set the attribute formatter to.



37
38
39
# File 'lib/mdarray/printing.rb', line 37

def formatter=(value)
  @formatter = value
end

#idObject (readonly)

an array identifier



70
71
72
# File 'lib/mdarray.rb', line 70

def id
  @id
end

#inf_str=(value) ⇒ Object (writeonly)

Sets the attribute inf_str

Parameters:

  • value

    the value to set the attribute inf_str to.



30
31
32
# File 'lib/mdarray/printing.rb', line 30

def inf_str=(value)
  @inf_str = value
end

#int_output_size=(value) ⇒ Object (writeonly)

Sets the attribute int_output_size

Parameters:

  • value

    the value to set the attribute int_output_size to.



39
40
41
# File 'lib/mdarray/printing.rb', line 39

def int_output_size=(value)
  @int_output_size = value
end

#local_indexObject (readonly)

internal helper index for this array



73
74
75
# File 'lib/mdarray.rb', line 73

def local_index
  @local_index
end

#local_iteratorObject (readonly)

Returns the value of attribute local_iterator.



74
75
76
# File 'lib/mdarray.rb', line 74

def local_iterator
  @local_iterator
end

#max_line_width=(value) ⇒ Object (writeonly)

Sets the attribute max_line_width

Parameters:

  • value

    the value to set the attribute max_line_width to.



34
35
36
# File 'lib/mdarray/printing.rb', line 34

def max_line_width=(value)
  @max_line_width = value
end

#nan_str=(value) ⇒ Object (writeonly)

attributes necessary for printing



29
30
31
# File 'lib/mdarray/printing.rb', line 29

def nan_str=(value)
  @nan_str = value
end

#nc_arrayObject (readonly)

Returns the value of attribute nc_array.



72
73
74
# File 'lib/mdarray.rb', line 72

def nc_array
  @nc_array
end

#prefix=(value) ⇒ Object (writeonly)

Sets the attribute prefix

Parameters:

  • value

    the value to set the attribute prefix to.



36
37
38
# File 'lib/mdarray/printing.rb', line 36

def prefix=(value)
  @prefix = value
end

#separator=(value) ⇒ Object (writeonly)

Sets the attribute separator

Parameters:

  • value

    the value to set the attribute separator to.



35
36
37
# File 'lib/mdarray/printing.rb', line 35

def separator=(value)
  @separator = value
end

#summary_edge_items=(value) ⇒ Object (writeonly)

Sets the attribute summary_edge_items

Parameters:

  • value

    the value to set the attribute summary_edge_items to.



31
32
33
# File 'lib/mdarray/printing.rb', line 31

def summary_edge_items=(value)
  @summary_edge_items = value
end

#summary_threshold=(value) ⇒ Object (writeonly)

Sets the attribute summary_threshold

Parameters:

  • value

    the value to set the attribute summary_threshold to.



32
33
34
# File 'lib/mdarray/printing.rb', line 32

def summary_threshold=(value)
  @summary_threshold = value
end

#typeObject (readonly)

Returns the value of attribute type.



71
72
73
# File 'lib/mdarray.rb', line 71

def type
  @type
end

#unary_operatorObject

Returns the value of attribute unary_operator.



78
79
80
# File 'lib/mdarray.rb', line 78

def unary_operator
  @unary_operator
end

Class Method Details

.arange(*args) ⇒ Object


Return evenly spaced values within a given interval. Values are generated within the half-open interval [start, stop) (in other words, the interval including start but excluding stop). For integer arguments the function is equivalent to the Python built-in range function, but returns an mdarray rather than a list. When using a non-integer step, such as 0.1, the results will often not be consistent. It is better to use linspace for these cases.


Parameters:

  • start
  • stop
  • step

Returns:

  • int mdarray



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/mdarray/creation.rb', line 313

def self.arange(*args)

  case args.size
  when 1
    start = 0
    last = args[0]
    stride = 1
  when 2
    start = args[0]
    last = args[1]
    stride = 1
  when 3
    start = args[0]
    last = args[1]
    stride = args[2]
  else
    raise "Method arange can have at most 3 arguments"
  end

  arr = Array.new
  (start...last).step(stride) { |val| arr << val }
  self.build("int", [arr.size], arr)

end

.boolean(shape, storage = nil, layout = :row) ⇒ Object


Builds a boolean mdarray


Parameters:

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



141
142
143
# File 'lib/mdarray/creation.rb', line 141

def self.boolean(shape, storage = nil, layout = :row)
  self.build("boolean", shape, storage, layout)
end

.build(type, shape, storage = nil, layout = :row) ⇒ Object


Builds a new MDArray


Parameters:

  • type

    the type of the new mdarray to build, could be boolean, byte, short, int, long, float, double, string, structure

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mdarray/creation.rb', line 63

def self.build(type, shape, storage = nil, layout = :row)

  if (shape.is_a? String)
    # building from csv
    # using shape as filename
    # using storage as flag for headers
    storage = (storage)? storage : false
    parameters = Csv.read_numeric(shape, storage)
    shape=[parameters[0], parameters[1]]
    storage = parameters[2]
  end

  # Java-NetCDF creates an ArrayObject when given type string.  It should create an
  # ArrayString instead.  Some string methods in Java-NetCDF expect an ArrayObject
  # instead of an ArrayString, however, other libraries actually expect an ArrayString,
  # so we know have two type: "string" stores internally the data as an ArrayObject, 
  # "rstring" stores data internally as an ArrayString
  rtype = (type == "rstring")? "string" : type
  dtype = DataType.valueOf(rtype.upcase)

  jshape = shape.to_java :int

  if (storage)
    jstorage = storage.to_java rtype.downcase.to_sym
    if (type == "rstring")
      # circunvent bug in Java-NetCDF.  Type rstring is actually type string but should
      # and should build and ArrayString and not an ObjectString which is currently being
      # build.
      index = Java::UcarMa2.Index.factory(jshape)
      nc_array = Java::UcarMa2.ArrayString.factory(index, jstorage)
    else
      nc_array = Java::UcarMa2.Array.factory(dtype, jshape, jstorage)
    end
  else
    nc_array = Java::UcarMa2.Array.factory(dtype, jshape)
  end

  klass = Object.const_get("#{rtype.capitalize}MDArray")
  return klass.new(rtype, nc_array)

end

.build_from_nc_array(type, nc_array, section = false) ⇒ Object





127
128
129
130
131
132
133
# File 'lib/mdarray/creation.rb', line 127

def self.build_from_nc_array(type, nc_array, section = false)
  if (!type)
    type = MDArray.get_ncarray_type(nc_array)
  end
  klass = Object.const_get("#{type.capitalize}MDArray")
  return klass.new(type, nc_array, section)
end

.byte(shape, storage = nil, layout = :row) ⇒ Object


Builds a byte mdarray


Parameters:

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



151
152
153
# File 'lib/mdarray/creation.rb', line 151

def self.byte(shape, storage = nil, layout = :row)
  self.build("byte", shape, storage, layout)
end

.calc_value(given_type, function_type, match, partial_match, no_match) ⇒ Object





321
322
323
324
325
326
327
328
329
330
331
# File 'lib/mdarray.rb', line 321

def self.calc_value(given_type, function_type, match, partial_match, no_match)

  if (given_type == function_type)
    match
  elsif ((function_type == "*") || (given_type == "*"))
    partial_match
  else
    no_match
  end

end

.char(shape, storage = nil, layout = :row) ⇒ Object


Builds a char mdarray


Parameters:

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



161
162
163
# File 'lib/mdarray/creation.rb', line 161

def self.char(shape, storage = nil, layout = :row)
  self.build("char", shape, storage, layout)
end

.double(shape, storage = nil, layout = :row) ⇒ Object


Builds a double mdarray


Parameters:

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



213
214
215
# File 'lib/mdarray/creation.rb', line 213

def self.double(shape, storage = nil, layout = :row)
  self.build("double", shape, storage, layout)
end

.float(shape, storage = nil, layout = :row) ⇒ Object


Builds a float mdarray


Parameters:

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



203
204
205
# File 'lib/mdarray/creation.rb', line 203

def self.float(shape, storage = nil, layout = :row)
  self.build("float", shape, storage, layout)
end

.from_jstorage(type, shape, jstorage, section = false) ⇒ Object





109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mdarray/creation.rb', line 109

def self.from_jstorage(type, shape, jstorage, section = false)

  if (shape.size == 1 && shape[0] == 0)
    return nil
  end

  dtype = DataType.valueOf(type.upcase)
  jshape = shape.to_java :int
  nc_array = Java::UcarMa2.Array.factory(dtype, jshape, jstorage)
  klass = Object.const_get("#{type.capitalize}MDArray")
  return klass.new(type, nc_array, section)

end

.fromfunction(type, shape, &block) ⇒ Object


Construct an array by executing a function over each coordinate. The resulting array therefore has a value “fn(x, y, z)“ at coordinate “(x, y, z)“. Parameters



Parameters:

  • type

    : data-type, optional Data-type of the coordinate arrays passed to ‘fn`

  • shape

    : (N,) tuple of ints Shape of the output array, which also determines the shape of the coordinate arrays passed to ‘fn`.

  • &block:

    a block to be executed The block is called with N parameters, each of which represents the coordinates of the array varying along a specific axis. For example, if ‘shape` were “(2, 2)“, then the parameters would be two arrays, “[[0, 0], [1, 1]]“ and “[[0, 1], [0, 1]]“. `fn` must be capable of operating on arrays, and should return a scalar value.



270
271
272
273
274
275
276
277
278
# File 'lib/mdarray/creation.rb', line 270

def self.fromfunction(type, shape, &block)

  dtype = DataType.valueOf(type.upcase)
  jshape = shape.to_java :int
  arr = self.build(type, shape)
  arr.dim_set(nil, block)
  return arr

end

.function_map_to_csvObject


Prints a list of all available functions know to MDArray is csv. Should be reimplemented. For debuging only for now.




243
244
245
246
247
248
249
250
251
252
253
# File 'lib/mdarray.rb', line 243

def self.function_map_to_csv

  p "scope, short name, long name, return type, input1 type, input2 type" 
  
  MDArray.function_map.each_pair do |key, value|
    value.each do |func|
      p "#{func.scope}, #{key}, #{func.long_name}, #{func.return_type}, #{func.input1_type}, #{func.input2_type}"
    end
  end
    
end

.init_with(type, shape, value) ⇒ Object


Build mdarray and fills it with the given value


Parameters:

  • type

    type of the mdarray

  • shape
  • value

    the given value to fill in the mdarray



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/mdarray/creation.rb', line 287

def self.init_with(type, shape, value)

  size = 1
  shape.each do |val|
    size = size * val
  end

  storage = Array.new(size, value)    
  self.build(type, shape, storage)

end

.int(shape, storage = nil, layout = :row) ⇒ Object


Builds an int mdarray


Parameters:

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



182
183
184
# File 'lib/mdarray/creation.rb', line 182

def self.int(shape, storage = nil, layout = :row)
  self.build("int", shape, storage, layout)
end

.lazy=(flag) ⇒ Object





309
310
311
# File 'lib/mdarray/lazy_mdarray.rb', line 309

def self.lazy=(flag)
  set_lazy(flag)
end

.linspace(type, start, stop, number) ⇒ Object





380
381
382
383
384
385
# File 'lib/mdarray/creation.rb', line 380

def self.linspace(type, start, stop, number)
  
  arr = (start..stop).step((stop-start).to_f/(number-1)).map{|x| x }
  self.build(type, [arr.size], arr)
  
end

.long(shape, storage = nil, layout = :row) ⇒ Object


Builds a long mdarray


Parameters:

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



193
194
195
# File 'lib/mdarray/creation.rb', line 193

def self.long(shape, storage = nil, layout = :row)
  self.build("long", shape, storage, layout)
end

.make_binary_op(name, exec_type, func, helper_class, force_type = nil, pre_condition = nil, post_condition = nil) ⇒ Object


Makes a new binary operator for this MDArray. All binary operators are created using this method or the one in module FunctionCreation.


Parameters:

  • name (String)

    name of the new binary operator

  • exec_type

    execution type of the binary operator. Existing execution types at present are: :default, :fill, :in_place, :reduce.

  • func

    the function to be applied for this binary operator. For instance, lets say we are build the “add” binary operator. exec_type is :default, func is a ruby proc Proc.new { |val1, val2| val1 + val2 }

  • force_type (defaults to: nil)

    forces the type of the resulting array after executing the binary operator. For instance, if we force type “int”, then even adding two double arrays the resulting array will be of type int

  • pre_condition (defaults to: nil)

    Proc to be executed before the operator’s execution

  • post_condition (defaults to: nil)

    Proc to be executed after the operator’s execution



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/mdarray.rb', line 271

def self.make_binary_op(name, exec_type, func, helper_class, force_type = nil, 
                        pre_condition = nil, post_condition = nil)

  define_method(name) do |op2, requested_type = nil, *args|
    if (@type == "lazy" || ((op2.is_a? MDArray) && op2.type == "lazy"))
      binary_op = LazyBinaryOperator
    else
      binary_op = get_binary_op
    end
    op = binary_op.new(name, exec_type, force_type, pre_condition, post_condition)
    op.exec(self, op2, requested_type, *args)
  end

  MDArray.register_function(name, func, 2, helper_class)

end

.make_unary_op(name, exec_type, func, helper_class, force_type = nil, pre_condition = nil, post_condition = nil) ⇒ Object


Makes a new unary operator for this MDArray. All unary operators are created using this method or the one in module FunctionCreation.


Parameters:

  • name (String)

    name of the new binary operator

  • exec_type

    execution type of the binary operator. Existing execution types at present are: :default, :fill, :in_place, :reduce.

  • func

    the function to be applied for this binary operator. For instance, lets say we are build the “add” binary operator. exec_type is :default, func is a ruby proc Proc.new { |val1, val2| val1 + val2 }

  • force_type (defaults to: nil)

    forces the type of the resulting array after executing the binary operator. For instance, if we force type “int”, then even adding two double arrays the resulting array will be of type int

  • pre_condition (defaults to: nil)

    Proc to be executed before the operator’s execution

  • post_condition (defaults to: nil)

    Proc to be executed after the operator’s execution



304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/mdarray.rb', line 304

def self.make_unary_op(name, exec_type, func, helper_class, force_type = nil, 
                       pre_condition = nil, post_condition = nil)
  
  define_method(name) do |requested_type = nil, *args|
    unary_op = get_unary_op
    op = unary_op.new(name, exec_type, force_type, pre_condition, post_condition)
    op.exec(self, requested_type, *args)
  end

  MDArray.register_function(name, func, 1, helper_class)

end

.ones(type, shape) ⇒ Object





391
392
393
# File 'lib/mdarray/creation.rb', line 391

def self.ones(type, shape)
  init_with(type, shape, 1)
end

Prints a list of all available functions know to MDArray. Should be reimplemented. For debuging only for now.




224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/mdarray.rb', line 224

def self.print_function_map

  MDArray.function_map.each_pair do |key, value|

    value.each do |func|

      p "package: #{func.package}, short name: #{key}, long name: #{func.long_name}, return type: #{func.return_type}, input1 type: #{func.input1_type}, input2 type: #{func.input2_type}"

    end

  end
    
end

.register_function(name, fmap, arity, helper_class) ⇒ Object


fmap is an array with the following data: long_name, scope, function, return_type, input1_type, input2_type




400
401
402
403
404
405
406
407
408
# File 'lib/mdarray/creation.rb', line 400

def self.register_function(name, fmap, arity, helper_class)
  
  if ((list = MDArray.function_map[name]) == nil)
    list = (MDArray.function_map[name] = Array.new)
  end
  list << FunctionMap.new(name, fmap[0], fmap[1], fmap[2], fmap[3], fmap[4], fmap[5],
                          arity, helper_class)

end

.rstring(shape, storage = nil, layout = :row) ⇒ Object


Builds a string mdarray. Really builds an string array. Only exists to fix the Java-NetCDF issue described above.


Parameters:

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



236
237
238
# File 'lib/mdarray/creation.rb', line 236

def self.rstring(shape, storage = nil, layout = :row)
  self.build("rstring", shape, storage, layout)
end

.select_function(name, package = nil, return_type = nil, input1_type = nil, input2_type = nil) ⇒ Object


Selects the best function to use at execution time for a given operation. MDArray allow for many implementations of the same function. For instance, one could implement the add operation as a ruby proc Proc.new { |val1, val2| val1 + val2 } or as a Java method. At execution time the system will select the best function to execute given a set of decision paramenters. At this time, this method needs to be improved.


Parameters:

  • name

    the name of the function

  • scope (String)

    a given scope defined by the user, used as a decision parameter

  • return_type (defaults to: nil)

    the return type of the function

  • input1_type (defaults to: nil)

    the type of the first argument to the function

  • input2_type (defaults to: nil)

    the type of the second argument to the function



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/mdarray.rb', line 347

def self.select_function(name, package = nil, return_type = nil, input1_type = nil, 
                         input2_type = nil)


=begin
  p "selecting function: #{name}"
  p "return_type: #{return_type}"
  p "input1_type: #{input1_type}"
  p "input2_type: #{input2_type}"
=end

  list = MDArray.function_map[name]
  best_value = -1
  func = nil

  list.each do |function|
    value = (package == function.package)? 2 : 1
    # p "package: #{package}; function package: #{function.package}"
    # p value
    value *= calc_value(return_type, function.return_type, 32, 16, 0)
    # p "return_type: #{return_type}; func_ret_type: #{function.return_type}"
    # p value
    value *= calc_value(input1_type, function.input1_type, 8, 4, 0)
    # p "input1_type: #{input1_type}; func_input1_type: #{function.input1_type}"
    # p value
    value *= calc_value(input2_type, function.input2_type, 2, 1, 0)
    # p "input2_type: #{input2_type}; func_input2_type: #{function.input2_type}"
    # p value
    if (value == 0)
      next
    elsif (value > best_value)
      func = function
      best_value = value
    end
  end

=begin
  p "MDArray.select_function"
  p "selected function #{func.function}"
=end

  if (best_value > 0)
    func
  else
    raise "No method to process operator: #{name}"
  end

end

.set_lazy(flag = true) ⇒ Object





289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/mdarray/lazy_mdarray.rb', line 289

def self.set_lazy(flag = true)

  if (flag)
    if (MDArray.binary_operator != LazyBinaryOperator)
      MDArray.previous_binary_operator = MDArray.binary_operator
      MDArray.previous_unary_operator = MDArray.unary_operator
    end
    MDArray.binary_operator = LazyBinaryOperator
    MDArray.unary_operator = LazyUnaryOperator
  else
    MDArray.binary_operator = MDArray.previous_binary_operator if MDArray.previous_binary_operator != nil
    MDArray.unary_operator = MDArray.previous_unary_operator if MDArray.previous_unary_operator != nil
  end

end

.short(shape, storage = nil, layout = :row) ⇒ Object


Builds a byte mdarray


Parameters:

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



172
173
174
# File 'lib/mdarray/creation.rb', line 172

def self.short(shape, storage = nil, layout = :row)
  self.build("short", shape, storage, layout)
end

.string(shape, storage = nil, layout = :row) ⇒ Object


Builds a string mdarray. Java-NetCDF does not actuallly build a string array when type string is passed, it builds an object array. This is an open issue with Java-NetCDF.


Parameters:

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



225
226
227
# File 'lib/mdarray/creation.rb', line 225

def self.string(shape, storage = nil, layout = :row)
  self.build("string", shape, storage, layout)
end

.structure(shape, storage = nil, layout = :row) ⇒ Object


Builds a structure mdarray


Parameters:

  • shape (Array)

    the shape of the mdarray as a ruby array

  • storage (Array) (defaults to: nil)

    a ruby array with the initialization data



246
247
248
# File 'lib/mdarray/creation.rb', line 246

def self.structure(shape, storage = nil, layout = :row)
  self.build("structure", shape, storage, layout)
end

.typed_arange(type, *args) ⇒ Object


Return evenly spaced values within a given interval. Values are generated within the half-open interval [start, stop) (in other words, the interval including start but excluding stop). For integer arguments the function is equivalent to the Python built-in range function, but returns an mdarray rather than a list.


Parameters:

  • type

    the desired type of the new mdarray

  • start
  • stop
  • step


351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/mdarray/creation.rb', line 351

def self.typed_arange(type, *args)

  case args.size
  when 1
    start = 0
    last = args[0]
    stride = 1
  when 2
    start = args[0]
    last = args[1]
    stride = 1
  when 3
    start = args[0]
    last = args[1]
    stride = args[2]
  else
    raise "Method arange can have at most 3 arguments"
  end

  arr = Array.new
  (start...last).step(stride) { |val| arr << val }
  self.build(type, [arr.size], arr)

end

.upcast(type1, type2) ⇒ Object


Given two types returns the upcasted one




32
33
34
35
36
37
38
39
# File 'lib/mdarray/creation.rb', line 32

def self.upcast(type1, type2)

  type1_i = MDArray.numerical.index(type1)
  type2_i = MDArray.numerical.index(type2)
  type_i = (type1_i < type2_i)? type2_i : type1_i
  type = MDArray.numerical.at(type_i)

end

Instance Method Details

#[](*index) ⇒ Object


When get is used to retrieve an element, it is assumed that the index does not need correction, for instance, no negative index is allowed. If one wants to use negative indexes, then method [] should be used. So mat.get([-1, 0, 0]) raises an exception while mat[-1, 0, 0] gets the last value for the first dimension.




53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mdarray/access.rb', line 53

def [](*index)

  if (index.size != 0)
    @local_index[*index]
  elsif (@local_iterator)
    @local_iterator.get_current
  else
    raise "No iterator defined! Cannot get element"
  end

end

#[]=(*index, value) ⇒ Object





118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mdarray/access.rb', line 118

def []=(*index, value)

  if (index.size != 0)
    @local_index[index] = value
  elsif (@local_iterator)
    @local_iterator.set_current(value)
  else
    raise "No iterator defined! Cannot set element value"
  end

end

#apply_over_axes(axes) ⇒ Object





168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mdarray/printing.rb', line 168

def apply_over_axes(axes)

  counter = Counter.new(self)
  # find the axes and sizes 
  slice = Array.new(rank, 0)
  sizes = Array.new(rank, 1)
  (0..rank).each do |i|
    if (axes[i] != i)
      sizes[i] = shape[i]
    end
  end

  counter.each_along_axes(axes) do |ct, axis|
    section(ct, sizes)
  end
  
end

#compatible(array) ⇒ Object


Checks to see if this array is compatible with another array. Two arrays are compatible if they have the same shape and have operatable types.




215
216
217
# File 'lib/mdarray.rb', line 215

def compatible(array)
  (get_shape == array.get_shape)? true : false
end

#copyObject


Create a copy of this Array, copying the data so that physical order is the same as logical order




29
30
31
# File 'lib/mdarray/views.rb', line 29

def copy
  MDArray.build_from_nc_array(@type, @nc_array.copy())
end

#csv1dObject





99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mdarray/printing.rb', line 99

def csv1d
  
  each_with_counter do |elmt, index|
    if (!is_zero?(index))
      Kernel.print @separator
    end
    Kernel.printf @formatter.call(elmt)
  end
  Kernel.print "\n"

end

#dtypeObject


Gets the element type of this array




171
172
173
# File 'lib/mdarray.rb', line 171

def get_element_type
  @nc_array.getElementType().toString()
end

#eachObject





185
186
187
188
189
190
191
192
# File 'lib/mdarray/access.rb', line 185

def each
  
  iterator = get_iterator_fast
  while (iterator.has_next?)
    yield iterator.get_next if block_given?
  end
  
end

#each_contObject


Continues a each from the position the @local_iterator is in. each_cont cannot be called from multiple threads as there is only one @local_iterator.




218
219
220
221
222
223
224
# File 'lib/mdarray/access.rb', line 218

def each_cont

  while (elmt = self.next)
    yield elmt if block_given?
  end

end

#each_slice(axes, reduce = true) ⇒ Object





254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/mdarray/views.rb', line 254

def each_slice(axes, reduce = true)
  
  counter = Counter.new(self)

  sizes = Array.new
  (0..rank - 1).each do |axis|
    if (axes.include?(axis))
      sizes[axis] = 1
    else
      sizes[axis] = shape[axis]
    end
  end

  counter.each_along_axes(axes) do |ct|
    yield section(ct, sizes, reduce) if block_given?
  end

end

#each_with_counterObject


Cycles through the whole list of elements yielding to a block (if given) the next element and its index. The index is a ruby array.




204
205
206
207
208
209
210
211
# File 'lib/mdarray/access.rb', line 204

def each_with_counter

  iterator = get_iterator_fast
  while (iterator.has_next?)
    yield iterator.get_next, iterator.get_current_counter if block_given?
  end
  
end

#flip(dim) ⇒ Object





37
38
39
# File 'lib/mdarray/views.rb', line 37

def flip(dim)
  MDArray.build_from_nc_array(@type, @nc_array.flip(dim))
end

#get(index = nil) ⇒ Object


When get is used to retrieve an element, it is assumed that the index does not need correction, for instance, no negative index is allowed. If one wants to use negative indexes, then method [] should be used. So mat.get([-1, 0, 0]) raises an exception while mat[-1, 0, 0] gets the last value for the first dimension.




72
73
74
# File 'lib/mdarray/access.rb', line 72

def get(index = nil)
  @local_index.get(index)
end

#get_as(type, counter = nil) ⇒ Object





80
81
82
# File 'lib/mdarray/access.rb', line 80

def get_as(type, counter = nil)
  @local_index.get_as(type, counter)
end

#get_binary_opObject





45
46
47
# File 'lib/mdarray/creation.rb', line 45

def get_binary_op
  (@binary_operator)? @binary_operator : MDArray.binary_operator
end

#get_counterObject





169
170
171
# File 'lib/mdarray/access.rb', line 169

def get_counter
  Counter.new(self)
end

#get_current_indexObject





28
29
30
# File 'lib/mdarray/access.rb', line 28

def get_current_index
  @local_iterator.get_current_index
end

#get_element_typeObject


Gets the element type of this array




167
168
169
# File 'lib/mdarray.rb', line 167

def get_element_type
  @nc_array.getElementType().toString()
end

#get_indexObject





33
34
35
# File 'lib/mdarray/counter.rb', line 33

def get_index
  MDArray::Counter.new(self)
end

#get_iterator_fastObject



Returns:

  • IteratorFast a fast iterator onto the this array.



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/mdarray.rb', line 406

def get_iterator_fast
  
  case type
  when "boolean"
    IteratorFastBoolean.new(self)
  when "char"
    IteratorFastChar.new(self)
  when "short"
    IteratorFastShort.new(self)
  when "int"
    IteratorFastInt.new(self)
  when "long"
    IteratorFastLong.new(self)
  when "float"
    IteratorFastFloat.new(self)
  when "double"
    IteratorFastDouble.new(self)
  else
    IteratorFast.new(self)
  end

end

#get_nextObject


Gets the next element of the local iterator




104
105
106
107
108
109
110
111
112
# File 'lib/mdarray/access.rb', line 104

def get_next

  if (@local_iterator)
    @local_iterator.get_next
  else
    raise "No iterator defined! Cannot get next element"
  end

end

#get_rankObject


Get the number of dimensions of the array.



136
137
138
# File 'lib/mdarray.rb', line 136

def get_rank
  @nc_array.getRank()
end

#get_scalarObject


Sets a value for a scalar D0 array




88
89
90
# File 'lib/mdarray/access.rb', line 88

def get_scalar
  @local_index.get_scalar
end

#get_shapeObject


Gets the shape of the array




157
158
159
# File 'lib/mdarray.rb', line 157

def get_shape
  @nc_array.getShape().to_a
end

#get_sizeObject


Gets the size of the array.




147
148
149
# File 'lib/mdarray.rb', line 147

def get_size
  @nc_array.getSize()
end

#get_unary_opObject



49
50
51
# File 'lib/mdarray/creation.rb', line 49

def get_unary_op
  (@unary_operator)? @unary_operator : MDArray.unary_operator
end

#immutableObject


Given an MDArray, makes it immutable. Renjin data cannot be changed as Renjin assumes it can delay processing.




231
232
233
234
235
236
237
238
# File 'lib/mdarray/access.rb', line 231

def immutable

  instance_eval { def set(name, value) raise "Array is immutable" end }
  instance_eval { def set_scalar(name, value) raise "Array is immutable" end }
  instance_eval { def set_next(value) raise "Array is immutable" end }
  instance_eval { def []=(name, value) raise "Array is immutable" end }

end

#jget(index = nil) ⇒ Object





96
97
98
# File 'lib/mdarray/access.rb', line 96

def jget(index = nil)
  @local_index.jget(index)
end

#ndenumerateObject


Method to print all elements of the array for debuging purposes only. Does not need to be very efficient.




200
201
202
203
204
205
206
207
208
# File 'lib/mdarray.rb', line 200

def ndenumerate
  
  reset_traversal
  while (@local_iterator.has_next?) do
    @local_iterator.next
    print "#{get_current_index} #{@local_iterator.get_current}\n"
  end
  
end

#ndimObject


Get the number of dimensions of the array.



140
141
142
# File 'lib/mdarray.rb', line 140

def get_rank
  @nc_array.getRank()
end

#nextObject


Returns the next element of the local_iterator or nil if no next element available




36
37
38
39
40
41
42
43
44
# File 'lib/mdarray/access.rb', line 36

def next

  if (@local_iterator && @local_iterator.has_next?)
    @local_iterator.get_next
  else
    nil
  end

end

#permute(indices) ⇒ Object


Create a new Array using same backing store as this Array, by permuting the indices. Parameters: indices the old index dims becomes the new kth index. Returns: the new Array Throws: IllegalArgumentException: - wrong rank or dim not valid




106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mdarray/views.rb', line 106

def permute(indices)

  ind = indices.to_java :int
  
  begin
    perm = @nc_array.permute(ind)
  rescue # should catch IllegalArgumentException
    raise "Illegal argument"
  end

  MDArray.build_from_nc_array(@type, perm)

end




81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mdarray/printing.rb', line 81

def print

  case rank
  when 0
    print0d
  when 1
    print1d
  else
    printnd
  end
  Kernel.print "\n"

end

#print0dObject





143
144
145
# File 'lib/mdarray/printing.rb', line 143

def print0d
  Kernel.print(@nc_array.get())
end

#print1dObject





151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mdarray/printing.rb', line 151

def print1d
  
  Kernel.print "["
  each_with_counter do |elmt, index|
    if (!is_zero?(index))
      Kernel.print @separator
    end
    Kernel.printf @formatter.call(elmt)
  end
  Kernel.print "]"
  
end

#printing_defaultsObject





47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mdarray/printing.rb', line 47

def printing_defaults

  @nan_str = "nan"
  @inf_str = "inf"
  @summary_edge_items = 3     # repr N leading and trailing items of each dimension
  @summary_threshold = 1000   # total items > triggers array summarization
  
  @max_line_width = 80
  @separator = " "
  @prefix = " "
  
  @int_output_size = 2
  @float_output_precision = 2
  @float_output_suppress_small = false

  case type
  when "int"
    @formatter = method(:integer_formatter)
  when "float"
    @formatter = method(:float_formatter)
  when "double"
    @formatter = method(:float_formatter)
  when "string"
    @formatter = method(:string_formatter)
  else
    @formatter = method(:default_formatter)
  end

end

#rankObject


Get the number of dimensions of the array.



141
142
143
# File 'lib/mdarray.rb', line 141

def get_rank
  @nc_array.getRank()
end

#reduce(dim = nil) ⇒ Object





83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mdarray/views.rb', line 83

def reduce(dim = nil)

  if (dim)
    nc_array = @nc_array.reduce(dim.to_java :int)
  else
    nc_array = @nc_array.reduce
  end
  
  shape = MDArray.get_shape(nc_array)
  MDArray.build_from_nc_array(@type, nc_array)
  
end

#region(*args) ⇒ Object


Gets a region from this array. Region is the same as section but using a different interface. parameters that can be given shape origin size stride range section spec




205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/mdarray/views.rb', line 205

def region(*args)
  
  opts = Map.options(args)
  reduce = opts.getopt(:reduce)
  sec = MDArray::Section.build(*args)

  if (reduce)
    arr = @nc_array.section(sec.netcdf_elmt.getRanges())
  else
    arr = @nc_array.sectionNoReduce(sec.netcdf_elmt.getRanges())
  end

  # Build the new array as a section from the given one.  Last argument to build is
  # "true" indicating this is a section.
  section = MDArray.build_from_nc_array(@type, arr, true)
  copy_print_parameters(section)
  return section

end

#reset_traversalObject





177
178
179
# File 'lib/mdarray/access.rb', line 177

def reset_traversal
  @local_iterator = get_iterator_fast
end

#reshape(shape, copy = false) ⇒ Object





45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mdarray/views.rb', line 45

def reshape(shape, copy = false)

  new_shape = shape.to_java :int

  if (copy)
    nc_array = @nc_array.reshape(new_shape)
  else
    nc_array = @nc_array.reshapeNoCopy(new_shape)
  end

  MDArray.build_from_nc_array(@type, nc_array)

end

#reshape!(shape) ⇒ Object


This method fixes a bug in netcdf-java!!!




63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mdarray/views.rb', line 63

def reshape!(shape)
  new_shape = shape.to_java :int
  # Netcdf-Java bug... reshape of ArrayString becomes an ArrayObject.  In principle
  # should not require comparison with ArrayString here
  if (@nc_array.is_a? ArrayString)
    @nc_array = Java::UcarMa2.ArrayString.factory(@nc_array.getIndex(), 
      @nc_array.getStorage())
  else
    @nc_array = @nc_array.reshapeNoCopy(new_shape)
  end
  # when we reshape an array we need to re-initialize its index and local_iterator
  @local_index = Counter.new(self)
  @local_iterator = nil
  @self
end

#section(origin, shape, reduce = false) ⇒ Object


Create a new Array as a subsection of this Array, without rank reduction. No data is moved, so the new Array references the same backing store as the original. Throws: InvalidRangeException - if ranges is invalid


Parameters:

  • origin

    ruby array specifying the starting index. Must be same rank as original Array.

  • shape

    ruby array specifying the extents in each dimension. This becomes the shape of the returned Array. Must be same rank as original Array.

  • reduce (defaults to: false)

    true if the array should be reduced by removing dimensions of size 1



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/mdarray/views.rb', line 133

def section(origin, shape, reduce = false)

  jorigin = origin.to_java :int
  jshape = shape.to_java :int

  if (reduce)
    arr = @nc_array.section(jorigin, jshape)
  else
    arr = @nc_array.sectionNoReduce(jorigin, jshape, nil)
  end

  # this is an array section, set it to true
  if (arr.rank == 0)
    return arr.get()
  end

  # Build the new array as a section from the given one.  Last argument to build is
  # "true" indicating this is a section.
  section = MDArray.build_from_nc_array(@type, arr, true)
  copy_print_parameters(section)
  return section

end

#section?Boolean



Returns:

  • (Boolean)


229
230
231
# File 'lib/mdarray/views.rb', line 229

def section?
  @section
end

#section_with_stride(origin, shape, stride, reduce = false) ⇒ Object


Create a new Array as a subsection of this Array, without rank reduction. No data is moved, so the new Array references the same backing store as the original. Throws: InvalidRangeException - if ranges is invalid


Parameters:

  • origin

    ruby array specifying the starting index. Must be same rank as original Array.

  • shape

    ruby array specifying the extents in each dimension. This becomes the shape of the returned Array. Must be same rank as original Array.

  • stride

    array specifying the strides in each dimension. If null, assume all ones.

  • reduce (defaults to: false)

    true if the array should be reduced by removing dimensions of size 1



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mdarray/views.rb', line 172

def section_with_stride(origin, shape, stride, reduce = false)

  jorigin = origin.to_java :int
  jshape = shape.to_java :int
  jstride = stride.to_java :int

  if (reduce)
    arr = @nc_array.section(jorigin, jshape, jstride)
  else
    arr = @nc_array.sectionNoReduce(jorigin, jshape, jstride)
  end

  # Build the new array as a section from the given one.  Last argument to build is
  # "true" indicating this is a section.
  section = MDArray.build_from_nc_array(@type, arr, true)
  copy_print_parameters(section)
  return section

end

#set(index, value) ⇒ Object


When set is used to assign to an element, it is assumed that the index does not need correction, for instance, no negative index is allowed. If one wants to use negative indexes, then method [] should be used. So mat.set([-1, 0, 0], 10), raises an exection while mat[-1, 0, 0] = 10 sets the last value for the first dimension. *index: array with the index position *value: value to be set




139
140
141
# File 'lib/mdarray/access.rb', line 139

def set(index, value)
  @local_index.set(index, value)
end

#set_next(value) ⇒ Object





155
156
157
158
159
160
161
162
163
# File 'lib/mdarray/access.rb', line 155

def set_next(value)

  if (@local_iterator)
    @local_iterator.set_next(value)
  else
    raise "No iterator defined! Cannot set element value"
  end
  
end

#set_scalar(value) ⇒ Object


Sets a value for a scalar D0 array




147
148
149
# File 'lib/mdarray/access.rb', line 147

def set_scalar(value)
  @local_index.set_scalar(value)
end

#shapeObject


Gets the shape of the array




161
162
163
# File 'lib/mdarray.rb', line 161

def get_shape
  @nc_array.getShape().to_a
end

#sizeObject


Gets the size of the array.




151
152
153
# File 'lib/mdarray.rb', line 151

def get_size
  @nc_array.getSize()
end

#slice(dim, val) ⇒ Object


Create a new Array using same backing store as this Array, by fixing the specified dimension at the specified index value. This reduces rank by 1.


Parameters:

  • dim

    dimension to fix

  • val

    value in which to fix the dimension

Returns:

  • new array sliced at the given dimension on the specified value



241
242
243
244
245
246
247
248
# File 'lib/mdarray/views.rb', line 241

def slice(dim, val)
  
  arr = @nc_array.slice(dim, val)
  slice = MDArray.build_from_nc_array(@type, arr, true)
  copy_print_parameters(slice)
  return slice

end

#to_csvObject





115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mdarray/printing.rb', line 115

def to_csv

  @separator = ", "

  counter = Counter.new(self)
  # find the axes and sizes 
  axes = Array.new
  sizes = Array.new
  (0..(rank - 2)).each do |val|
    axes << val
    sizes << 1
  end
  sizes << shape[rank - 1]

  counter.each_along_axes(axes) do |ct, axis|
    (0..ct.size - 2).each do |i|
      Kernel.print ct[i] 
      Kernel.print @separator
    end
    section(ct, sizes).csv1d
  end
  
end

#to_sObject


Prints the array




191
192
193
# File 'lib/mdarray.rb', line 191

def to_s
  self.print
end

#to_string(max_size = 3) ⇒ Object


Prints the content of the netcdf_array. Mainly for debugging purposes.


Parameters:

  • max_size (int) (defaults to: 3)


178
179
180
181
182
183
184
185
# File 'lib/mdarray.rb', line 178

def to_string(max_size = 3)
  if (size > max_size * 2)
    @nc_array.toString()
  else
    @nc_array.toString()
  end

end

#transpose(dim1, dim2) ⇒ Object


Create a new Array using same backing store as this Array, by transposing two of the indices.


Parameters:

  • dim1

    first dimension index

  • dim2

    second dimension index

Returns:

  • new array with dimensions transposed



281
282
283
284
285
286
287
288
# File 'lib/mdarray/views.rb', line 281

def transpose(dim1, dim2)

  arr = @nc_array.transpose(dim1, dim2)
  transpose = MDArray.build_from_nc_array(@type, arr, true)
  copy_print_parameters(transpose)
  return transpose

end