/usr/lib/x86_64-linux-gnu/ruby/vendor_ruby/2.3.0/narray_ext.rb is in ruby-narray 0.6.1.1-2build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | # Numerical Array Extention for Ruby
# (C) Copyright 2000-2008 by Masahiro TANAKA
#
# This program is free software.
# You can distribute/modify this program
# under the same terms as Ruby itself.
# NO WARRANTY.
#
class NArray
def self.cast(array,type=nil)
case array
when NArray
when Array
array = NArray.to_na(array)
else
raise ArgumentError, "1st argument must be NArray or Array"
end
type = array.typecode if type.nil?
shape = array.shape
na = self.new(type,*shape)
na[] = array
na
end
def integer?
self.typecode==NArray::BYTE ||
self.typecode==NArray::SINT ||
self.typecode==NArray::LINT
end
def complex?
self.typecode==NArray::DCOMPLEX ||
self.typecode==NArray::SCOMPLEX
end
def all?
where.size == size
end
def any?
where.size > 0
end
def none?
where.size == 0
end
def ==(other)
other.kind_of?(NArray) &&
shape == other.shape &&
eq(other).all?
end
def eql?(other)
self.class == other.class &&
typecode == other.typecode &&
shape == other.shape &&
case typecode
when NArray::OBJECT
to_a.eql? other.to_a
else
to_s.eql? other.to_s
end
end
def hash
case typecode
when NArray::OBJECT
[self.class, to_a].hash
else
[self.class, typecode, shape, to_s].hash
end
end
def rank_total(*ranks)
if ranks.size>0
idx = []
ranks.each{|i| idx.push(*i)}
# ranks is expected to be, e.g., [1, 3..5, 7]
a = self.shape
n = 1
idx.each{|i| n *= a[i]}
n
else
self.total
end
end
# delete rows/columns
def delete_at(*args)
if args.size > self.rank
raise ArgumentError, "too many arguments"
end
shp = self.shape
ind = []
self.rank.times do |i|
n = shp[i]
case a=args[i]
when Integer
a = n+a if a<0
raise IndexError, "index(%d) out of range"%[a] if a<0
x = [0...a,a+1...n]
when Range
b = a.first
b = n+b if b<0
raise IndexError, "index(%s) out of range"%[a] if b<0
e = a.last
e = n+e if e<0
e -= 1 if a.exclude_end?
raise IndexError, "index(%s) out of range"%[a] if e<0
x = [0...b,e+1...n]
when Array
x = (0...n).to_a
x -= a.map do |j|
raise IndexError, "contains non-integer" unless Integer===j
(j<0) ? n+j : j
end
else
if a
raise ArgumentError, "invalid argument"
else
x = true
end
end
ind << x
end
self[*ind]
end
# Statistics
def mean(*ranks)
if integer?
a = self.to_type(NArray::DFLOAT)
else
a = self
end
a = NArray.ref(a)
a.sum(*ranks) / (rank_total(*ranks))
end
def stddev(*ranks)
if integer?
a = self.to_type(NArray::DFLOAT)
else
a = self
end
a = NArray.ref(a)
n = rank_total(*ranks)
if complex?
NMath::sqrt( (( a-a.accum(*ranks).div!(n) ).abs**2).sum(*ranks)/(n-1) )
else
NMath::sqrt( (( a-a.accum(*ranks).div!(n) )**2).sum(*ranks)/(n-1) )
end
end
def rms(*ranks)
if integer?
a = self.to_type(NArray::DFLOAT)
else
a = self
end
a = NArray.ref(a)
n = rank_total(*ranks)
if complex?
NMath::sqrt( (a.abs**2).sum(*ranks)/n )
else
NMath::sqrt( (a**2).sum(*ranks)/n )
end
end
def rmsdev(*ranks)
if integer?
a = self.to_type(NArray::DFLOAT)
else
a = self
end
a = NArray.ref(a)
n = rank_total(*ranks)
if complex?
NMath::sqrt( (( a-a.accum(*ranks).div!(n) ).abs**2).sum(*ranks)/n )
else
NMath::sqrt( (( a-a.accum(*ranks).div!(n) )**2).sum(*ranks)/n )
end
end
def median(rank=nil)
shape = self.shape
rank = shape.size-1 if rank==nil
s = sort(rank).reshape!(true,*shape[rank+1..-1])
n = s.shape[0]
if n%2==1
s[n/2,false]
else
s[n/2-1..n/2,false].sum(0)/2
end
end
# Normal distributed random number; valid for floating point types
def randomn
size = self.size
case type = self.typecode
when COMPLEX; type=FLOAT
when SCOMPLEX; type=SFLOAT
when FLOAT
when SFLOAT
else
raise TypeError, "NArray type must be (S)FLOAT or (S)COMPLEX."
end
rr = NArray.new(type,size)
xx = NArray.new(type,size)
i = 0
while i < size
n = size-i
m = ((n+Math::sqrt(n))*1.27).to_i
x = NArray.new(type,m).random!(1) * 2 - 1
y = NArray.new(type,m).random!(1) * 2 - 1
r = x**2 + y**2
idx = (r<1).where
idx = idx[0...n] if idx.size > n
if idx.size>0
rr[i] = r[idx]
xx[i] = x[idx]
i += idx.size
end
end
# Box-Muller transform
rr = ( xx * NMath::sqrt( -2 * NMath::log(rr) / rr ) )
# finish
rr.reshape!(*self.shape) if self.rank > 1
rr = rr.to_type(self.typecode) if type!=self.typecode
if RUBY_VERSION < "1.8.0"
self.type.refer(rr)
else
self.class.refer(rr)
end
end
#alias randomn! randomn
def randomn!
self[]= randomn
self
end
def reverse(*ranks)
if self.rank==0
return self.dup
elsif ranks.size==0
idx = (0...self.rank).map{-1..0}
else
idx = [true]*self.rank
ranks.each do |i|
if !i.kind_of?(Integer)
raise ArgumentError, "Argument must be Integer"
end
if i >= self.rank
raise ArgumentError, "dimension(%s) out of range"%[i]
end
idx[i] = -1..0
end
end
self[*idx]
end
def rot90(n_times=1)
if self.rank < 2
raise "must be >= 2 dimensional array"
end
case n_times%4
when 0
self.dup
when 1
self.transpose(1,0).reverse(0)
when 2
self.reverse(0,1)
when 3
self.transpose(1,0).reverse(1)
end
end
#SFloatOne = NArray.sfloat(1).fill!(1)
end
module NMath
PI = Math::PI
E = Math::E
def recip x
1/x.to_f
end
# Trigonometric function
def csc x
1/sin(x)
end
def csch x
1/sinh(x)
end
def acsc x
asin(1/x.to_f)
end
def acsch x
asinh(1/x.to_f)
end
def sec x
1/cos(x)
end
def sech x
1/cosh(x)
end
def asec x
acos(1/x.to_f)
end
def asech x
acosh(1/x.to_f)
end
def cot x
1/tan(x)
end
def coth x
1/tanh(x)
end
def acot x
atan(1/x.to_f)
end
def acoth x
atanh(1/x.to_f)
end
# Statistics
def covariance(x,y,*ranks)
x = NArray.to_na(x) unless x.kind_of?(NArray)
x = x.to_type(NArray::DFLOAT) if x.integer?
y = NArray.to_na(y) unless y.kind_of?(NArray)
y = y.to_type(NArray::DFLOAT) if y.integer?
n = x.rank_total(*ranks)
xm = x.accum(*ranks).div!(n)
ym = y.accum(*ranks).div!(n)
((x-xm)*(y-ym)).sum(*ranks) / (n-1)
end
module_function :recip
module_function :csc,:sec,:cot,:csch,:sech,:coth
module_function :acsc,:asec,:acot,:acsch,:asech,:acoth
module_function :covariance
end
module FFTW
def convol(a1,a2)
n1x,n1y = a1.shape
n2x,n2y = a2.shape
raise "arrays must have same shape" if n1x!=n2x || n1y!=n2y
(FFTW.fftw( FFTW.fftw(a1,-1) * FFTW.fftw(a2,-1), 1).real) / (n1x*n1y)
end
module_function :convol
end
require 'nmatrix'
|