Class: Renjin::Vector

Inherits:
RubySexp show all
Includes:
Enumerable, Index
Defined in:
lib/JRubyR/vector.rb

Direct Known Subclasses

ComplexVector, Logical

Instance Attribute Summary collapse

Attributes included from RBSexp

#attr, #rvar, #scope, #sexp

Instance Method Summary collapse

Methods included from Index

#[], #[]=, #_, #each, #method_missing, #parse

Methods inherited from RubySexp

build, #fassign, #to_string

Methods included from RBSexp

#destroy, #pp, #print, #r, #rclass, #sexp?, #typeof, #unbind

Constructor Details

#initialize(sexp) ⇒ Vector





57
58
59
60
61
# File 'lib/JRubyR/vector.rb', line 57

def initialize(sexp)
  super(sexp)
  @mdarray = nil
  @iterator = @sexp.iterator()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Renjin::Index

Instance Attribute Details

#mdarrayObject (readonly)

Returns the value of attribute mdarray.



51
52
53
# File 'lib/JRubyR/vector.rb', line 51

def mdarray
  @mdarray
end

Instance Method Details

#!=(other_vec) ⇒ Object





379
380
381
382
383
384
# File 'lib/JRubyR/vector.rb', line 379

def !=(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} != #{other_vec.r}")
end

#%(other_vec) ⇒ Object


modulus




302
303
304
305
306
307
# File 'lib/JRubyR/vector.rb', line 302

def %(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} %% #{other_vec.r}")
end

#&(other_vec) ⇒ Object





398
399
400
401
402
403
# File 'lib/JRubyR/vector.rb', line 398

def &(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} & #{other_vec.r}")
end

#*(other_vec) ⇒ Object





280
281
282
283
284
285
# File 'lib/JRubyR/vector.rb', line 280

def *(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} * #{other_vec.r}")
end

#**(other_vec) ⇒ Object


exponentiation




324
325
326
327
328
329
# File 'lib/JRubyR/vector.rb', line 324

def **(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} ** #{other_vec.r}")
end

#+(other_vec) ⇒ Object





258
259
260
261
262
263
# File 'lib/JRubyR/vector.rb', line 258

def +(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} + #{other_vec.r}")
end

#+@Object





242
243
244
# File 'lib/JRubyR/vector.rb', line 242

def +@
  R.eval("+#{r}")
end

#-(other_vec) ⇒ Object





269
270
271
272
273
274
# File 'lib/JRubyR/vector.rb', line 269

def -(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} - #{other_vec.r}")
end

#-@Object





250
251
252
# File 'lib/JRubyR/vector.rb', line 250

def -@
  R.eval("-#{r}")
end

#/(other_vec) ⇒ Object





291
292
293
294
295
296
# File 'lib/JRubyR/vector.rb', line 291

def /(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} / #{other_vec.r}")
end

#<(other_vec) ⇒ Object





357
358
359
360
361
362
# File 'lib/JRubyR/vector.rb', line 357

def <(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} < #{other_vec.r}")
end

#<=(other_vec) ⇒ Object





368
369
370
371
372
373
# File 'lib/JRubyR/vector.rb', line 368

def <=(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} <= #{other_vec.r}")
end

#==(other_val) ⇒ Object





123
124
125
126
# File 'lib/JRubyR/vector.rb', line 123

def ==(other_val)
  other_val = (other_val.is_a? Renjin::RubySexp)? other_val.r : other_val
  (other_val == nil)? false : R.eval("#{r} == #{other_val}")
end

#>(other_vec) ⇒ Object





335
336
337
338
339
340
# File 'lib/JRubyR/vector.rb', line 335

def >(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} > #{other_vec.r}")
end

#>=(other_vec) ⇒ Object





346
347
348
349
350
351
# File 'lib/JRubyR/vector.rb', line 346

def >=(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} >= #{other_vec.r}")
end

#as__mdarrayObject





172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/JRubyR/vector.rb', line 172

def as__mdarray

  if (@mdarray)
  elsif (@sexp.java_kind_of? Java::RbScicom::MDDoubleVector)
    @mdarray = MDArray.build_from_nc_array(:double, @sexp.array)
  elsif (@sexp.java_kind_of? Java::OrgRenjinSexp::DoubleArrayVector)
    @mdarray = MDArray.from_jstorage("double", [@sexp.length()], @sexp.toDoubleArrayUnsafe())
  elsif (sexp.instance_of? Java::OrgRenjinSexp::LongArrayVector)
    @mdarray = MDArray.from_jstorage("long", [@sexp.length()], @sexp.values)
  elsif (@sexp.java_kind_of? Java::OrgRenjinSexp::IntArrayVector)
    @mdarray = MDArray.from_jstorage("int", [@sexp.length()], @sexp.toIntArrayUnsafe())
  elsif (@sexp.java_kind_of? Java::OrgRenjinSexp::StringArrayVector)
    @mdarray = MDArray.from_jstorage("string", [@sexp.length()], @sexp.values)
  elsif (@sexp.java_kind_of? Java::OrgRenjinSexp::LogicalArrayVector)
    @mdarray = MDArray.from_jstorage("int", [@sexp.length()], @sexp.values)
  else
    p "as__mdarray: sexp type needs to be specialized: #{@sexp}"
    @mdarray = Renjin::RubySexp.new(@sexp)
  end
  
  raise "Cannot convert Vector to MDArray" if (!@mdarray)
  return @mdarray
  
end

#atomic?Boolean



Returns:

  • (Boolean)


107
108
109
# File 'lib/JRubyR/vector.rb', line 107

def atomic?
  R.is__atomic(R.eval("#{r}")).gt
end

#character?Boolean



Returns:

  • (Boolean)


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

def character?
  R.is__character(R.eval("#{r}")).gt
end

#coerce(scalar) ⇒ Object





453
454
455
# File 'lib/JRubyR/vector.rb', line 453

def coerce(scalar)
  [R.d(scalar), self]
end

#complex?Boolean



Returns:

  • (Boolean)


83
84
85
# File 'lib/JRubyR/vector.rb', line 83

def complex?
  R.is__complex(R.eval("#{r}")).gt
end

#double?Boolean



Returns:

  • (Boolean)


75
76
77
# File 'lib/JRubyR/vector.rb', line 75

def double?
  R.is__double(R.eval("#{r}")).gt
end

#eq(other_val) ⇒ Object





132
133
134
# File 'lib/JRubyR/vector.rb', line 132

def eq(other_val)
  (other_val == nil)? false : R.eval("identical(#{r},#{other_val.r})")
end

#get(index = nil) ⇒ Object





218
219
220
# File 'lib/JRubyR/vector.rb', line 218

def get(index = nil)
  (index)? as__mdarray[index] : as__mdarray
end

#gt(index = 0) ⇒ Object





234
235
236
# File 'lib/JRubyR/vector.rb', line 234

def gt(index = 0)
  (get(index) == 0)? false : true
end

#gzObject





226
227
228
# File 'lib/JRubyR/vector.rb', line 226

def gz
  get(0)
end

#int_div(other_vec) ⇒ Object





313
314
315
316
317
318
# File 'lib/JRubyR/vector.rb', line 313

def int_div(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} %/% #{other_vec.r}")
end

#integer?Boolean



Returns:

  • (Boolean)


67
68
69
# File 'lib/JRubyR/vector.rb', line 67

def integer?
  R.eval("#{r}").is__integer.gt
end

#l_and(other_vec) ⇒ Object


l_and looks at only the first element of the vector




409
410
411
412
413
414
# File 'lib/JRubyR/vector.rb', line 409

def l_and(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} && #{other_vec.r}")
end

#l_or(other_vec) ⇒ Object





431
432
433
434
435
436
# File 'lib/JRubyR/vector.rb', line 431

def l_or(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} || #{other_vec.r}")
end

#logical?Boolean



Returns:

  • (Boolean)


91
92
93
# File 'lib/JRubyR/vector.rb', line 91

def logical?
  R.is__logical(R.eval("#{r}")).gt
end

#numeric?Boolean



Returns:

  • (Boolean)


115
116
117
# File 'lib/JRubyR/vector.rb', line 115

def numeric?
  R.is__numeric(R.eval("#{r}")).gt
end

#ri(*shape) ⇒ Object


Converts an MDArray shape or index onto an equivalent R shape or index




201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/JRubyR/vector.rb', line 201

def ri(*shape)
  
  rshape = shape.clone
  
  if (rshape.size > 2)
    rshape.reverse!
    rshape[0], rshape[1] = rshape[1], rshape[0]
  end
  rshape.map!{ |val| (val + 1) }
  self[*rshape]
  
end

#xor(other_vec) ⇒ Object





442
443
444
445
446
447
# File 'lib/JRubyR/vector.rb', line 442

def xor(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} xor #{other_vec.r}")
end

#|(other_vec) ⇒ Object


or




420
421
422
423
424
425
# File 'lib/JRubyR/vector.rb', line 420

def |(other_vec)
  if (other_vec.is_a? Numeric)
    other_vec = R.d(other_vec)
  end
  R.eval("#{r} | #{other_vec.r}")
end