narray 0.5.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/src/ChangeLog +614 -0
  2. data/src/MANIFEST +82 -0
  3. data/src/README.en +54 -0
  4. data/src/README.ja +63 -0
  5. data/src/SPEC.en +300 -0
  6. data/src/SPEC.ja +284 -0
  7. data/src/depend +14 -0
  8. data/src/extconf.rb +111 -0
  9. data/src/lib/narray_ext.rb +211 -0
  10. data/src/lib/nmatrix.rb +244 -0
  11. data/src/mkmath.rb +780 -0
  12. data/src/mknafunc.rb +190 -0
  13. data/src/mkop.rb +638 -0
  14. data/src/na_array.c +644 -0
  15. data/src/na_func.c +1624 -0
  16. data/src/na_index.c +988 -0
  17. data/src/na_linalg.c +616 -0
  18. data/src/na_random.c +409 -0
  19. data/src/narray.c +1308 -0
  20. data/src/narray.def +29 -0
  21. data/src/narray.h +170 -0
  22. data/src/narray_local.h +210 -0
  23. data/src/nimage/README.en +38 -0
  24. data/src/nimage/demo/fits.rb +97 -0
  25. data/src/nimage/demo/fits_convol.rb +28 -0
  26. data/src/nimage/demo/fits_fftdemo.rb +27 -0
  27. data/src/nimage/demo/fitsdemo1.rb +13 -0
  28. data/src/nimage/demo/fitsdemo2.rb +30 -0
  29. data/src/nimage/demo/fitsdemo3.rb +26 -0
  30. data/src/nimage/demo/fitsmorph.rb +39 -0
  31. data/src/nimage/demo/life_na.rb +57 -0
  32. data/src/nimage/demo/mandel.rb +41 -0
  33. data/src/nimage/extconf.rb +12 -0
  34. data/src/nimage/lib/nimage.rb +51 -0
  35. data/src/nimage/nimage.c +328 -0
  36. data/src/speed/add.py +12 -0
  37. data/src/speed/add.rb +8 -0
  38. data/src/speed/add_int.py +12 -0
  39. data/src/speed/add_int.rb +9 -0
  40. data/src/speed/lu.m +14 -0
  41. data/src/speed/lu.rb +22 -0
  42. data/src/speed/mat.m +23 -0
  43. data/src/speed/mat.rb +28 -0
  44. data/src/speed/mul.py +12 -0
  45. data/src/speed/mul.rb +9 -0
  46. data/src/speed/mul2.py +15 -0
  47. data/src/speed/mul2.rb +13 -0
  48. data/src/speed/mul_comp.py +12 -0
  49. data/src/speed/mul_comp.rb +9 -0
  50. data/src/speed/mul_int.py +12 -0
  51. data/src/speed/mul_int.rb +9 -0
  52. data/src/speed/mybench.py +15 -0
  53. data/src/speed/mybench.rb +31 -0
  54. data/src/speed/solve.m +18 -0
  55. data/src/speed/solve.py +16 -0
  56. data/src/speed/solve.rb +21 -0
  57. data/src/test/statistics.rb +22 -0
  58. data/src/test/testarray.rb +20 -0
  59. data/src/test/testbit.rb +27 -0
  60. data/src/test/testcast.rb +14 -0
  61. data/src/test/testcomplex.rb +35 -0
  62. data/src/test/testfftw.rb +16 -0
  63. data/src/test/testindex.rb +11 -0
  64. data/src/test/testindexary.rb +26 -0
  65. data/src/test/testindexset.rb +55 -0
  66. data/src/test/testmask.rb +40 -0
  67. data/src/test/testmath.rb +48 -0
  68. data/src/test/testmath2.rb +46 -0
  69. data/src/test/testmatrix.rb +13 -0
  70. data/src/test/testmatrix2.rb +33 -0
  71. data/src/test/testmatrix3.rb +19 -0
  72. data/src/test/testminmax.rb +46 -0
  73. data/src/test/testobject.rb +29 -0
  74. data/src/test/testpow.rb +19 -0
  75. data/src/test/testrandom.rb +23 -0
  76. data/src/test/testround.rb +11 -0
  77. data/src/test/testsort.rb +37 -0
  78. data/src/test/teststr.rb +13 -0
  79. data/src/test/testtrans.rb +18 -0
  80. data/src/test/testwhere.rb +27 -0
  81. metadata +127 -0
@@ -0,0 +1,12 @@
1
+ from mybench import *
2
+
3
+ a = arrayrange(ARRSZ) # % 1000
4
+ b = arrayrange(ARRSZ) # / 1000
5
+
6
+ print "a.typecode:",a.typecode(),", a.shape:",a.shape
7
+ print "b.typecode:",b.typecode(),", b.shape:",b.shape
8
+ print "calculating c = a*b ..."
9
+
10
+ def bench_body(a=a,b=b): c=a*b
11
+
12
+ bench_time(bench_body)
@@ -0,0 +1,9 @@
1
+ require 'mybench'
2
+
3
+ a, b = bench_int
4
+
5
+ print "a = "; p a;
6
+ print "b = "; p b;
7
+
8
+ print "calculating c = a*b ...\n"
9
+ bench_time{ c=a*b }
@@ -0,0 +1,15 @@
1
+ from Numeric import *
2
+ import time
3
+
4
+ REPEAT = 100
5
+ ARRSZ = 1000000
6
+
7
+ def bench_array(type=Float64):
8
+ return arrayrange(ARRSZ).astype(type)
9
+
10
+ def bench_time(func,repeat=REPEAT):
11
+ start = time.clock()
12
+ for i in range(repeat):
13
+ func()
14
+ stop = time.clock()
15
+ print " Time: %7.3f sec" % (stop-start)
@@ -0,0 +1,31 @@
1
+ require 'narray'
2
+
3
+ REPEAT = 100
4
+ ARRSZ = 1_000_0000
5
+ T = (RUBY_VERSION<"1.8.0") ? Time : Process
6
+
7
+ def bench_array(type=Float)
8
+ [ NArray.new(type,ARRSZ).indgen!,
9
+ NArray.new(type,ARRSZ).indgen! ]
10
+ end
11
+
12
+ def bench_float
13
+ bench_array(Float)
14
+ end
15
+
16
+ def bench_int
17
+ bench_array(Integer)
18
+ end
19
+
20
+ def bench_complex
21
+ bench_array(Complex)
22
+ end
23
+
24
+ def bench_time(n=REPEAT)
25
+ t1 = T.times.utime
26
+ for i in 1..n
27
+ yield
28
+ end
29
+ t2 = T.times.utime
30
+ puts " Time: %.2f sec\n\n" % [t2-t1]
31
+ end
data/src/speed/solve.m ADDED
@@ -0,0 +1,18 @@
1
+ # mesuring performance of Octave
2
+ n = 500;
3
+ m = linspace(0,n*n-1,n*n);
4
+ m = rem(m, n+1) + 1;
5
+ m = reshape(m,n,n);
6
+ x = reshape(linspace(1,n*n,n*n),n,n);
7
+
8
+ printf ("solving %ix%i matrix...\n",n,n);
9
+ [t1, u1, s1] = cputime ();
10
+
11
+ y = m \ x;
12
+
13
+ [t2, u2, s2] = cputime ();
14
+ printf (" Time: %5.2f sec\n", u2 - u1);
15
+
16
+ # Time: 6.87 sec, GNU Octave, version 2.0.16 (sparc-sun-solaris2.7).
17
+ #y
18
+ exit
@@ -0,0 +1,16 @@
1
+ from mybench import *
2
+ from LinearAlgebra import *
3
+
4
+ n = 500
5
+
6
+ a = reshape(arrayrange(n*n)%(n+1)+1,(n,n)).astype(Float64)
7
+
8
+ b = reshape(arrayrange(n*n)+1,(n,n)).astype(Float64)
9
+ #b = arrayrange(n).astype(Float64)+1
10
+
11
+ print "LU factorize & solve %ix%i matrix ..."%(n,n)
12
+
13
+ def bench_body(a=a,b=b): c=solve_linear_equations(a,b)
14
+ bench_time(bench_body,1)
15
+
16
+ # Time: 8.120 sec
@@ -0,0 +1,21 @@
1
+ require 'mybench'
2
+
3
+ n = 500
4
+ m = NArray.float(n,n).indgen!
5
+ m = m % (n+1) + 1
6
+ m = NMatrix.ref(m).transpose
7
+ x = NMatrix.float(n,n).indgen!(1).transpose
8
+ #x = NVector.float(n).indgen!(1)
9
+ y = 0
10
+
11
+ puts 'm ='
12
+ p m
13
+ puts 'x ='
14
+ p x
15
+
16
+ printf "solving y=x/m ...\n",n,n
17
+ bench_time(1){ y = x/m }
18
+ # Time: 5.87 sec
19
+
20
+ puts 'y='
21
+ p y
@@ -0,0 +1,22 @@
1
+ require "narray"
2
+ include NMath
3
+
4
+ x = NArray[65, 63, 67, 64, 68, 62, 70, 66, 68, 67, 69, 71]
5
+ y = NArray[68, 66, 68, 65, 69, 66, 68, 65, 71, 67, 68, 70]
6
+
7
+ def test str, x, y=nil
8
+ print str," #=> "
9
+ p eval(str)
10
+ end
11
+
12
+ test "x",x,y
13
+ test "y",x,y
14
+ test "covariance(x,y)",x,y
15
+
16
+ a = covariance(x,y)
17
+
18
+ test "x.stddev",x
19
+
20
+ test "x.sort",x
21
+ test "x.median",x
22
+ test "(x+y.newrank!(0)).median(0)",x,y
@@ -0,0 +1,20 @@
1
+ require 'narray'
2
+
3
+ a = NArray.float(3,3).indgen
4
+
5
+ p NArray[a,[100,101]]
6
+
7
+ b = NArray[ [ [ 0.0, 1.0, 2.0 ],
8
+ [ 3.0, 4.0, 5.0 ],
9
+ [ 6.0, 7.0, 8.0 ] ],
10
+ [100,101] ]
11
+ p b
12
+
13
+ a = NArray.float(2,2).indgen
14
+
15
+ b = NArray[ a,[a] ]
16
+ p b
17
+
18
+ b = NArray[ [ 0.0, 1.0 ],
19
+ [ [ 0.0, 1.0 ] ] ]
20
+ p b
@@ -0,0 +1,27 @@
1
+ require "narray"
2
+
3
+ a = NArray.byte(10).indgen!
4
+
5
+ def test a, str
6
+ print str," #=>\n"
7
+ p eval(str)
8
+ end
9
+
10
+ test a, "a"
11
+ test a, "a & 1"
12
+ test a, "a & 2"
13
+ test a, "a & -1"
14
+
15
+ test a, "a | 1"
16
+ test a, "a | 2"
17
+ test a, "a | -1"
18
+
19
+ test a, "a ^ 1"
20
+ test a, "a ^ 2"
21
+ test a, "a ^ -1"
22
+
23
+ test a, "~a"
24
+
25
+ a = NArray.int(10).indgen!
26
+ test a, "a"
27
+ test a, "~a"
@@ -0,0 +1,14 @@
1
+ require 'narray'
2
+
3
+ def test a,b
4
+ print a," #=> "
5
+ p b
6
+ end
7
+
8
+ a = NArray.int(3,3).indgen!
9
+
10
+ test "a",a
11
+ test "a+1.5",a+1.5
12
+ test "1.5+a",1.5+a
13
+ test "a+NArray[1.2,3.4,5.6]",a+NArray[1.2,3.4,5.6]
14
+ test "a+NArray[Complex(0.5,1.5)]",a+NArray[Complex(0.5,1.5)]
@@ -0,0 +1,35 @@
1
+ require 'narray'
2
+
3
+ def testop(a,b)
4
+ print "a = "; p a
5
+ print "b = "; p b
6
+ print "a+b = "; p a+b
7
+ print "a-b = "; p a-b
8
+ print "a*b = "; p a*b
9
+ print "a/b = "; p a/b
10
+ print "a**b = "; p a**b
11
+ end
12
+
13
+ a = NArray.complex(4,1).indgen!.sbt!(-1) + 0.25.im
14
+ b = NArray.complex(1,3).indgen!.add!(-0.5).mul!(0.5.im)
15
+ testop(a,b)
16
+
17
+ # compare a/b with real-number operation
18
+ # a = NArray(4,1).indgen!.sbt!(-1)
19
+ # b = NArray(1,3).fill!(1)
20
+ # c = b*b + b*b
21
+ # p ( (a*b + 0*b)/c )
22
+ # p ( (0*b - a*b)/c )
23
+
24
+ def testimag(a)
25
+ print "a.real = "
26
+ p a.real
27
+ print "a.imag = "
28
+ p a.imag
29
+ print "a.angle = "
30
+ p a.angle
31
+ print "a.conj = "
32
+ p a.conj
33
+ end
34
+
35
+ testimag a
@@ -0,0 +1,16 @@
1
+ require 'narray'
2
+
3
+ x = NArray.complex(1024,1024).indgen!
4
+
5
+ p x
6
+ puts 'executing fftw ...'
7
+ t1 = Time.times.utime
8
+ y = FFTW.fftw( x, 1 )
9
+ t2 = Time.times.utime
10
+ print "time: ",t2 - t1,"\n"
11
+ p y
12
+
13
+ exit
14
+
15
+ x = NArray.complex(128,128).indgen!
16
+ 10000.times{ x = FFTW.fftw( x, 1 ) }
@@ -0,0 +1,11 @@
1
+ require 'narray'
2
+
3
+ a = NArray.float(3,3).indgen!
4
+
5
+ print "a #=> "; p a
6
+ print "a[1, -1] #=> "; p a[1, -1]
7
+ print "a[1, 2..0] #=> "; p a[1, 2..0]
8
+ print "a[1...2, 0..1] #=> "; p a[1...2, 0..1] # without rank-reduce
9
+ print "a[true, 0..1] #=> "; p a[true, 0..1]
10
+ print "a[0..5] #=> "; p a[0..5]
11
+
@@ -0,0 +1,26 @@
1
+ require 'narray'
2
+
3
+ a = NArray.new(NArray::DFLOAT,5,5).indgen!
4
+ print "a #=> "; p a
5
+
6
+ idx = [2,0,1]
7
+ print "\nidx #=> "
8
+ p idx
9
+
10
+ print "\na[idx,idx] # Index Array\n #=> "
11
+ p a[idx,idx]
12
+
13
+ idx = NArray.to_na(idx)
14
+ idx += 10
15
+ print "\nidx #=> "
16
+ p idx
17
+
18
+ print "\na[idx] #=> "
19
+ p a[idx]
20
+
21
+ print "\na[1,[]] # Empty Array\n #=> "
22
+ p a[1,[]]
23
+
24
+ print "\nFollowing will fail...\n\n"
25
+ print "a[idx,0] #=> \n"
26
+ p a[idx,0]
@@ -0,0 +1,55 @@
1
+ require 'narray'
2
+
3
+ a = NArray.float(3,3).indgen!
4
+
5
+ print "a #=> "; p a
6
+
7
+ print "\na[[]] = [] # Do nothing \na #=> ";
8
+ a[[]] = []; p a
9
+
10
+ print "\na[1,1] = 0 # 1-element replace\na #=> ";
11
+ b=a.dup; b[1,1] = 0
12
+ p b
13
+
14
+ print "\na[0..-1, 1] = 0 # Range replace\na #=> ";
15
+ b=a.dup; b[0..-1, 1] = 0
16
+ p b
17
+
18
+ print "\na[1,2..0] = [100,101,102] # Array replace\na #=> ";
19
+ b=a.dup; b[1,2..0] = [100,101,102]
20
+ p b
21
+
22
+ print "\na[0,1] = [100,101,102] # Specifing Starting point \na #=> ";
23
+ b=a.dup; b[0,1] = [100,101,102]
24
+ p b
25
+
26
+ print "\na[1,0] = [[100],[101],[102]] # Specifing Starting point \na #=> ";
27
+ b=a.dup; b[1,0] = [[100],[101],[102]]
28
+ p b
29
+
30
+ print "\na[true,1] = [100,101,102] # `true' means entire range\na #=> ";
31
+ b=a.dup; b[true,1] = [100,101,102]
32
+ p b
33
+
34
+ print "\na[true,true] = [[100,101,102]] \na #=> ";
35
+ b=a.dup; b[true,true] = [[100,101,102]]
36
+ p b
37
+
38
+
39
+ print "\nFollowing will fail ...\n"
40
+
41
+ print "\na[true,1] = [[100,101,102]] \na #=> ";
42
+ b=a.dup; b[true,1] = [[100,101,102]]
43
+ p b
44
+
45
+ print "\na[true,1] = [[100],[101],[102]] \na #=> ";
46
+ b=a.dup; b[true,1] = [[100],[101],[102]]
47
+ p b
48
+
49
+ print "\na[true,true] = [100,101,102] \na #=> ";
50
+ b=a.dup; b[true,true] = [100,101,102]
51
+ p b
52
+
53
+ print "\na[1,0] = [100,101,102] \na #=> ";
54
+ b=a.dup; b[1,0] = [100,101,102]
55
+ p b
@@ -0,0 +1,40 @@
1
+ require "narray"
2
+
3
+ a = NArray.byte(10)
4
+ a[2..4] = 1
5
+ p a.type, a.count_true, a.count_false
6
+
7
+ begin
8
+ a = NArray.float(10)
9
+ a[2..4] = 1
10
+ p a.type, a.count_true, a.count_false
11
+ rescue
12
+ print a.type," -- Exception raised as expected. The message was: ", $!,"\n"
13
+ end
14
+
15
+ #-------------------
16
+ print "\n--- test masking (float) ---\n"
17
+
18
+ a = NArray.float(5,3).indgen!
19
+ b = (a-2)*2
20
+ c = a.lt(b)
21
+ p c, c.typecode
22
+
23
+ p a, b, a.mask( c ), a[c]
24
+
25
+ #a[c] = ( NArray.int(c.length).indgen!+100 )
26
+ #p a
27
+
28
+ a[c] = 10000
29
+ p a
30
+
31
+ #-------------------
32
+ print "\n--- test masking (complex) ---\n"
33
+ p a = NArray.complex(5).indgen! + Complex::I
34
+
35
+ m = NArray.byte(5)
36
+ m[true] = [0,0,1,1,0]
37
+
38
+ p a[m]
39
+ a[m] = 100.0
40
+ p a
@@ -0,0 +1,48 @@
1
+ require 'narray'
2
+ include NMath
3
+
4
+ def pr x
5
+ x.each{|i|
6
+ if i.kind_of?(Complex)
7
+ printf("%.3f%+.3fi ",i.real,i.image)
8
+ else
9
+ printf("%.3f ",i)
10
+ end
11
+ }
12
+ print "\n"
13
+ end
14
+
15
+ def testmath(x)
16
+ print "x = "
17
+ pr x
18
+ print "sqrt(x) = "
19
+ pr sqrt(x)
20
+ print "sin(x) = "
21
+ pr sin(x)
22
+ print "cos(x) = "
23
+ pr cos(x)
24
+ print "tan(x) = "
25
+ pr tan(x)
26
+ print "sinh(x) = "
27
+ pr sinh(x)
28
+ print "cosh(x) = "
29
+ pr cosh(x)
30
+ print "tanh(x) = "
31
+ pr tanh(x)
32
+ print "exp(x) = "
33
+ pr exp(x)
34
+ print "log(x) = "
35
+ pr log(x)
36
+ print "log10(x) = "
37
+ pr log10(x)
38
+ print "atan(x) = "
39
+ pr atan(x)
40
+ print "atan(tan(x)) = "
41
+ pr atan(tan(x))
42
+ end
43
+
44
+ testmath NArray.sfloat(6).indgen.div!(2)
45
+ testmath NArray.float(6).indgen.div!(2)
46
+
47
+ testmath NArray.scomplex(6).indgen.div!(2)-2 - 1.im
48
+ testmath NArray.complex(6).indgen!/5-0.5# - 0.3.im
@@ -0,0 +1,46 @@
1
+ require "narray"
2
+ include NMath
3
+
4
+ def testm x,name
5
+ f = "a#{name}(#{name}(x))"
6
+ print "\n### #{f} ###\n"
7
+ y = eval(f)
8
+ p y
9
+ #d = x-y
10
+ #p d.abs
11
+ end
12
+
13
+ PI=Math::PI
14
+
15
+ i = NArray.complex(1)
16
+ i.imag=1
17
+
18
+ n=7
19
+ x = NArray.complex(n).random!(1)
20
+ x.imag= NArray.float(n).random!(1)
21
+
22
+ p x
23
+ testm x,"sin"
24
+ testm x,"cos"
25
+ testm x,"tan"
26
+ testm x,"sinh"
27
+ testm x,"cosh"
28
+ testm x,"tanh"
29
+ testm x,"sec"
30
+ testm x,"sech"
31
+
32
+ p cot(1)
33
+
34
+ exit
35
+
36
+ a= -i * log( sqrt(1-x**2)*i + x )
37
+ b= -i * log( sqrt(x**2-1) + x )
38
+ p a
39
+ p b
40
+ p a-b
41
+
42
+ a= -i * log( sqrt(1-x**2) + x*i )
43
+ b= -i * log( (-sqrt(x**2-1) + x)*i )
44
+ p a
45
+ p b
46
+ p a-b
@@ -0,0 +1,13 @@
1
+ require "narray"
2
+
3
+ m = NMatrix.float(3,3,3).indgen!
4
+
5
+ puts
6
+ puts 'm #=>'
7
+ p m #=> NMatrix.float(3,3,3):
8
+ puts
9
+ puts 'm[1,1,true] #=>'
10
+ p m[1,1,true] #=> NArray.float(3):
11
+ puts
12
+ puts 'm[0..1,2,true] #=>'
13
+ p m[0..1,2,true] #=> NMatrix.float(2,1,3):
@@ -0,0 +1,33 @@
1
+ require 'narray'
2
+ require 'irb/xmp'
3
+ # xmp :: http://www.ruby-lang.org/en/raa-list.rhtml?name=xmp
4
+
5
+ m1 = NMatrix.float(2,2).indgen!
6
+ m2 = NMatrix[[0,1.2],[1.5,0]]
7
+
8
+ v1 = NVector[0.5,1.5]
9
+ v2 = NVector.float(2,2).indgen!
10
+
11
+ a = NArray.float(2,2).indgen!
12
+
13
+ xmp 'm1'
14
+ xmp 'm1.inverse'
15
+ xmp 'm2'
16
+ xmp 'm1*m2'
17
+ xmp 'm2*m1'
18
+ xmp 'm1+m2'
19
+ xmp '3.14*m1'
20
+ xmp 'm2*1.25'
21
+ xmp 'v1'
22
+ xmp 'v2'
23
+ xmp '1.25*v1'
24
+ xmp 'NMath.sqrt(v2**2)'
25
+ xmp 'v1*v2'
26
+ xmp 'm1*v1'
27
+ xmp 'v2*m2'
28
+ xmp 'm1.diagonal([98,99])'
29
+ xmp 'NMatrix.float(4,3).unit'
30
+
31
+ puts "\n=== following will fail ...\n"
32
+ xmp 'm1+v1'
33
+ xmp 'm1+1'
@@ -0,0 +1,19 @@
1
+ require 'narray'
2
+ require 'rational'
3
+
4
+ class Rational
5
+ def inspect
6
+ @numerator.to_s+"/"+@denominator.to_s
7
+ end
8
+ end
9
+
10
+ srand(1)
11
+ n=5
12
+
13
+ m = NMatrix.object(n,n).collect{Rational(rand(10))}
14
+
15
+ puts 'm #=>'
16
+ p m
17
+
18
+ puts 'm/m #=>'
19
+ p m/m
@@ -0,0 +1,46 @@
1
+ require 'narray'
2
+
3
+ def test a
4
+ print "a = "
5
+ p a
6
+ print "a.min = "
7
+ p a.min
8
+ print "a.max = "
9
+ p a.max
10
+ print "a.sum = "
11
+ p a.sum
12
+ print "a.mean = "
13
+ p a.mean
14
+ print "a.stddev = "
15
+ p a.stddev
16
+ end
17
+
18
+ a = NArray[0,2,-2.5,3,1.4]
19
+
20
+ test a
21
+
22
+ a = NArray.float(5,1).indgen!(1)
23
+ b = NArray.float(1,3).indgen!(1)
24
+ a *= b
25
+ test a
26
+
27
+ print "a.min(0) = "
28
+ p a.min(0)
29
+ print "a.max(0) = "
30
+ p a.max(0)
31
+ print "a.min(1) = "
32
+ p a.min(1)
33
+ print "a.max(1) = "
34
+ p a.max(1)
35
+ print "a.sum(0) = "
36
+ p a.sum(0)
37
+ print "a.sum(1) = "
38
+ p a.sum(1)
39
+ print "a.mean(0) = "
40
+ p a.mean(0)
41
+ print "a.mean(1) = "
42
+ p a.mean(1)
43
+ print "a.stddev(0) = "
44
+ p a.stddev(0)
45
+ print "a.stddev(1) = "
46
+ p a.stddev(1)
@@ -0,0 +1,29 @@
1
+ require 'narray'
2
+ require 'rational'
3
+
4
+ n = 4
5
+ a = NArray.object(4,4).fill!(Rational(1))
6
+ b = NArray.object(4,4).indgen!(1).collect{|i| Rational(i)}
7
+
8
+ print 'a #=> '
9
+ p a
10
+
11
+ print 'b #=> '
12
+ p b
13
+
14
+
15
+ class Rational
16
+ def inspect
17
+ self.to_s
18
+ end
19
+ end
20
+
21
+ print 'a+b #=> '
22
+ p a+b
23
+
24
+
25
+ print 'a/b #=> '
26
+ p a/b
27
+
28
+ print 'a/b - b #=> '
29
+ p a/b - b
@@ -0,0 +1,19 @@
1
+ require "narray"
2
+
3
+ def test a
4
+ puts 'a = '
5
+ p a
6
+ puts 'a**[[-3],[0],[7]] = '
7
+ p a**[[-3],[0],[7]]
8
+ puts 'a**[[-3.0],[0],[7.0]] = '
9
+ p a**[[-3.0],[0],[7.0]]
10
+ puts 'a**(1+0.im) = '
11
+ p a**(1+0.im)
12
+ puts 'a**1.0 = '
13
+ p a**1.0
14
+ puts
15
+ end
16
+
17
+ test NArray.int(4).indgen!*2-2
18
+ test NArray.float(4).indgen!*2-2
19
+ test NArray.complex(4).indgen!*2-2
@@ -0,0 +1,23 @@
1
+ require 'narray'
2
+
3
+ def test a
4
+ print a," #=> "
5
+ p eval(a)
6
+ print "\n"
7
+ end
8
+
9
+ test "NArray.float(5).random(10)"
10
+
11
+ test "NArray.float(5).random"
12
+
13
+ test "NArray.int(5).random(10)"
14
+
15
+ test "NArray.int(1000).random(10)"
16
+
17
+ a = NArray.int(1000).random(10)
18
+
19
+ idx = (a.eq 0).where
20
+ print "a.eq 0 :: n=", idx.size, "\n"
21
+
22
+ idx = (a.eq 10).where
23
+ print "a.eq 10 :: "; p idx