mdarray 0.4.0-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.txt +54 -0
- data/LICENSE.txt~ +32 -0
- data/README.md +21 -0
- data/Rakefile +40 -0
- data/lib/env.rb +11 -0
- data/lib/mdarray.rb +414 -0
- data/lib/mdarray/access.rb +237 -0
- data/lib/mdarray/counter.rb +779 -0
- data/lib/mdarray/creation.rb +413 -0
- data/lib/mdarray/fast_non_numerical.rb +102 -0
- data/lib/mdarray/function_creation.rb +100 -0
- data/lib/mdarray/function_map.rb +56 -0
- data/lib/mdarray/hierarchy.rb +177 -0
- data/lib/mdarray/operators.rb +220 -0
- data/lib/mdarray/printing.rb +275 -0
- data/lib/mdarray/proc_util.rb +159 -0
- data/lib/mdarray/ruby_functions.rb +78 -0
- data/lib/mdarray/ruby_generic_functions.rb +37 -0
- data/lib/mdarray/ruby_math.rb +57 -0
- data/lib/mdarray/ruby_numeric_functions.rb +187 -0
- data/lib/mdarray/ruby_operators.rb +201 -0
- data/lib/mdarray/ruby_stats.rb +149 -0
- data/lib/mdarray/slices.rb +185 -0
- data/lib/mdarray/statistics.rb +86 -0
- data/test/arithmetic_casting.rb +195 -0
- data/test/env.rb +50 -0
- data/test/test_access.rb +247 -0
- data/test/test_boolean.rb +67 -0
- data/test/test_comparison.rb +126 -0
- data/test/test_complete.rb +69 -0
- data/test/test_counter.rb +184 -0
- data/test/test_creation.rb +364 -0
- data/test/test_error.rb +53 -0
- data/test/test_lazy.rb +52 -0
- data/test/test_operator.rb +337 -0
- data/test/test_printing.rb +66 -0
- data/test/test_shape.rb +96 -0
- data/test/test_slices.rb +146 -0
- data/test/test_speed.rb +311 -0
- data/test/test_statistics.rb +45 -0
- data/test/test_trigonometry.rb +60 -0
- data/vendor/netcdfAll-4.3.16.jar +0 -0
- data/version.rb +2 -0
- metadata +197 -0
@@ -0,0 +1,69 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
5
|
+
# and distribute this software and its documentation, without fee and without a signed
|
6
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
7
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
8
|
+
# distributions.
|
9
|
+
#
|
10
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
11
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
12
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
13
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
14
|
+
#
|
15
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
16
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
17
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
18
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
19
|
+
# OR MODIFICATIONS.
|
20
|
+
##########################################################################################
|
21
|
+
|
22
|
+
require 'simplecov'
|
23
|
+
|
24
|
+
#=begin
|
25
|
+
SimpleCov.start do
|
26
|
+
@filters = []
|
27
|
+
add_group "MDArray", "F:/rbotafogo/cygwin/home/zxb3/Desenv/MDArray/src/lib"
|
28
|
+
end
|
29
|
+
#=end
|
30
|
+
|
31
|
+
# MDArray main object is the homogeneous multidimensional array. It is a table
|
32
|
+
# of elements (usually numbers), all of the same type, indexed by a tuple of
|
33
|
+
# positive integers. In MDArray dimensions are called <axes>. The number of
|
34
|
+
# <axes> is rank.
|
35
|
+
#
|
36
|
+
# For example, the coordinates of a point in 3D space [1, 2, 1] is an array of
|
37
|
+
# rank 1, because it has one <axis>. That <axis> has a length of 3. In
|
38
|
+
# the example pictured below, the array has rank 2 (it is 2-dimensional). The
|
39
|
+
# first dimension (axis) has length of 2, the second dimension has length of 3
|
40
|
+
# [[1, 0, 0],
|
41
|
+
# [0, 1, 2]]
|
42
|
+
# The more important attributes of a MDArray object are:
|
43
|
+
# * ndim: the number of axes (dimensions) of the array. The number of dimensions
|
44
|
+
# is referred to as rank
|
45
|
+
# * shape: the dimensions of the array. This is a tuple of integers indicating
|
46
|
+
# the size of the array in each dimension. For a matrix with n rows and m
|
47
|
+
# columns, shape will be (n, m). The length of the shape tuple is therefore the
|
48
|
+
# rank, or number of dimensions, ndim
|
49
|
+
# * size: the total number of elements of the array. This is equal to the product
|
50
|
+
# of the elements of shape
|
51
|
+
# * dtype: an object describing the type of the elements in the array. One can
|
52
|
+
# create or specify dtype's using standard Ruby types. Additionally MDArray
|
53
|
+
# provides types of its own. (examples?)
|
54
|
+
# * itemsize: ??
|
55
|
+
# Differently from NumPy, it is not possible to get the internal buffer
|
56
|
+
|
57
|
+
|
58
|
+
require 'test_creation'
|
59
|
+
require 'test_access'
|
60
|
+
require 'test_operator' # Fix user's operators contruction
|
61
|
+
require 'arithmetic_casting'
|
62
|
+
require 'test_comparison'
|
63
|
+
# require 'test_boolean'
|
64
|
+
require 'test_shape'
|
65
|
+
require 'test_counter'
|
66
|
+
require 'test_trigonometry'
|
67
|
+
# require 'test_statistics'
|
68
|
+
# require 'test_slices'
|
69
|
+
# require 'test_speed'
|
@@ -0,0 +1,184 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
5
|
+
# and distribute this software and its documentation, without fee and without a signed
|
6
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
7
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
8
|
+
# distributions.
|
9
|
+
#
|
10
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
11
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
12
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
13
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
14
|
+
#
|
15
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
16
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
17
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
18
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
19
|
+
# OR MODIFICATIONS.
|
20
|
+
##########################################################################################
|
21
|
+
|
22
|
+
require 'rubygems'
|
23
|
+
require "test/unit"
|
24
|
+
require 'shoulda'
|
25
|
+
|
26
|
+
require 'mdarray'
|
27
|
+
|
28
|
+
class MDArrayTest < Test::Unit::TestCase
|
29
|
+
|
30
|
+
context "Building blocks" do
|
31
|
+
|
32
|
+
setup do
|
33
|
+
|
34
|
+
# creates an array from a function (actually a block). The name fromfunction
|
35
|
+
# is preserved to maintain API compatibility with NumPy (is it necessary?)
|
36
|
+
|
37
|
+
# Testing for the financial market. We have 5 values (open, high, low, close, volume)
|
38
|
+
# 3 securities, each with 60 working days, for 2 years. For those
|
39
|
+
# parameters we need to change the Java heap size.
|
40
|
+
@a = MDArray.fromfunction("double", [2, 60, 3, 5]) do |x, y, z, k|
|
41
|
+
x + y + z + k
|
42
|
+
end
|
43
|
+
|
44
|
+
@b = MDArray.int([2, 2])
|
45
|
+
|
46
|
+
# can get counter by either calling get_counter on the array, or Counter.new passing
|
47
|
+
# the array
|
48
|
+
|
49
|
+
end # setup
|
50
|
+
|
51
|
+
#-------------------------------------------------------------------------------------
|
52
|
+
#
|
53
|
+
#-------------------------------------------------------------------------------------
|
54
|
+
|
55
|
+
should "allow random access to elements through counters" do
|
56
|
+
|
57
|
+
counter = MDArray::Counter.new(@a)
|
58
|
+
# access the array element by indexing the array with the index
|
59
|
+
assert_equal(1 + 1 + 2 + 2, counter.get([1, 1, 2, 2]))
|
60
|
+
# access the array element by indexing the array with the index
|
61
|
+
assert_equal(1 + 1 + 2 + 2, counter[1, 1, 2, 2])
|
62
|
+
# can use negative values for counters
|
63
|
+
assert_equal(1 + 1 + 2 + 2, counter[-1, 1, -1, 2])
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
#-------------------------------------------------------------------------------------
|
68
|
+
#
|
69
|
+
#-------------------------------------------------------------------------------------
|
70
|
+
|
71
|
+
should "do proper counter checking" do
|
72
|
+
|
73
|
+
counter = MDArray::Counter.new(@a)
|
74
|
+
|
75
|
+
# When using get, counter is check against shape. If not compatible, raises a
|
76
|
+
# runtime error
|
77
|
+
assert_raise ( RangeError ) { counter.get([1, 2, 5, 2]) }
|
78
|
+
# When usign [], counter is not check against shape. This is faster access.
|
79
|
+
# When not compatible, will get a RangeError.
|
80
|
+
assert_raise ( RuntimeError ) { counter[1, 2, 5, 2] }
|
81
|
+
assert_raise ( RuntimeError ) { counter[1, 2] }
|
82
|
+
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
#-------------------------------------------------------------------------------------
|
87
|
+
#
|
88
|
+
#-------------------------------------------------------------------------------------
|
89
|
+
|
90
|
+
should "walk the index in cannonical order" do
|
91
|
+
|
92
|
+
# canonical access using index. The use of index for cannonical order walking
|
93
|
+
# is probably slower than using access directly through the array, as the
|
94
|
+
# example bellow
|
95
|
+
counter = MDArray::Counter.new(@a)
|
96
|
+
|
97
|
+
counter.each do |elmt|
|
98
|
+
# access the array element by indexing the array with get method
|
99
|
+
assert_equal(elmt[0] + elmt[1] + elmt[2] + elmt[3], @a.get(elmt))
|
100
|
+
# access the array element by indexing the array with []. Need to use
|
101
|
+
# *elmt as elmt is an array
|
102
|
+
assert_equal(elmt[0] + elmt[1] + elmt[2] + elmt[3], @a[*elmt])
|
103
|
+
# access the array element directly through the index
|
104
|
+
assert_equal(elmt[0] + elmt[1] + elmt[2] + elmt[3], counter.get_current)
|
105
|
+
end
|
106
|
+
|
107
|
+
# cannonical access using direct access through the array
|
108
|
+
@a.each_with_counter do |elmt, index|
|
109
|
+
assert_equal(index[0] + index[1] + index[2] + index[3], elmt)
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
#-------------------------------------------------------------------------------------
|
115
|
+
#
|
116
|
+
#-------------------------------------------------------------------------------------
|
117
|
+
|
118
|
+
should "allow access through axes" do
|
119
|
+
|
120
|
+
counter = MDArray::Counter.new(@a)
|
121
|
+
counter.each_along_axes([0, 2]) do |counter|
|
122
|
+
p counter
|
123
|
+
end
|
124
|
+
|
125
|
+
printf "\n"
|
126
|
+
|
127
|
+
counter.each_along_axes([0, 1, 2]) do |counter|
|
128
|
+
p counter
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
#-------------------------------------------------------------------------------------
|
134
|
+
#
|
135
|
+
#-------------------------------------------------------------------------------------
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
=begin
|
142
|
+
#-------------------------------------------------------------------------------------
|
143
|
+
#
|
144
|
+
#-------------------------------------------------------------------------------------
|
145
|
+
|
146
|
+
should "issue errors if range is used as index" do
|
147
|
+
|
148
|
+
index = MDArray::Index.new(@a)
|
149
|
+
index.set_start([/1:1/, /0:2/, 1, 1])
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
=end
|
154
|
+
|
155
|
+
|
156
|
+
=begin
|
157
|
+
should "allow partial access to the array" do
|
158
|
+
|
159
|
+
index = MDArray::Index.new(@a)
|
160
|
+
|
161
|
+
index.set_start([1, 1, 1, 1])
|
162
|
+
assert_raise ( RuntimeError ) { index.set_finish([0, 1, 0, 0]) }
|
163
|
+
assert_raise ( RuntimeError ) { index.set_finish([1, 1, 0, 0]) }
|
164
|
+
|
165
|
+
index.set_finish([1, 1, 1, 1])
|
166
|
+
index.each do |elmt|
|
167
|
+
assert_equal([1, 1, 1, 1], elmt)
|
168
|
+
end
|
169
|
+
|
170
|
+
index.set_start([0, 1, 2, 2])
|
171
|
+
index.set_finish([1, 1, 1, 2])
|
172
|
+
|
173
|
+
index.each do |elmt|
|
174
|
+
# access the array element directly through the index
|
175
|
+
# p index.counter
|
176
|
+
assert_equal(elmt[0] + elmt[1] + elmt[2] + elmt[3], index.get_current)
|
177
|
+
end
|
178
|
+
|
179
|
+
index.each_along_axes([0, 2, 3]) do |counter|
|
180
|
+
p counter
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
=end
|
@@ -0,0 +1,364 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
5
|
+
# and distribute this software and its documentation, without fee and without a signed
|
6
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
7
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
8
|
+
# distributions.
|
9
|
+
#
|
10
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
11
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
12
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
13
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
14
|
+
#
|
15
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
16
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
17
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
18
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
19
|
+
# OR MODIFICATIONS.
|
20
|
+
##########################################################################################
|
21
|
+
|
22
|
+
require 'rubygems'
|
23
|
+
require "test/unit"
|
24
|
+
require 'shoulda'
|
25
|
+
|
26
|
+
require 'mdarray'
|
27
|
+
|
28
|
+
|
29
|
+
class MDArrayTest < Test::Unit::TestCase
|
30
|
+
|
31
|
+
context "Array Creation" do
|
32
|
+
|
33
|
+
#-------------------------------------------------------------------------------------
|
34
|
+
# array creation always requires the type and shape, differently from NumPy
|
35
|
+
# narray in which the type and shape can be inferred from the initialization
|
36
|
+
# data. MDArray can be numerical and non-numerical. Numerical MDArrays can be of
|
37
|
+
# type: byte, short, int, long, double. Non-numerical MDArrays can be of type
|
38
|
+
# boolean, char, string.
|
39
|
+
#-------------------------------------------------------------------------------------
|
40
|
+
|
41
|
+
should "create byte array" do
|
42
|
+
|
43
|
+
# build int array with given shape and all values 0
|
44
|
+
a = MDArray.byte([2, 2, 3])
|
45
|
+
assert_equal([2, 2, 3], a.shape)
|
46
|
+
assert_equal(3, a.ndim)
|
47
|
+
assert_equal(12, a.size)
|
48
|
+
assert_equal("byte", a.dtype)
|
49
|
+
assert_equal(0, a[0, 0, 0])
|
50
|
+
assert_equal(0, a[1, 1, 2])
|
51
|
+
|
52
|
+
# Cannot write a boolean value on a byte array
|
53
|
+
assert_raise ( RuntimeError ) { a[0, 0, 0] = true }
|
54
|
+
# writing a double value on byte array will cast double to byte
|
55
|
+
a[0, 0, 0] = 10.25
|
56
|
+
assert_equal(a[0, 0, 0], 10)
|
57
|
+
a[0, 0, 0] = 200
|
58
|
+
assert_equal(a[0, 0, 0], -56)
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
#-------------------------------------------------------------------------------------
|
63
|
+
#
|
64
|
+
#-------------------------------------------------------------------------------------
|
65
|
+
|
66
|
+
should "create short array" do
|
67
|
+
|
68
|
+
# build int array with given shape and all values 0
|
69
|
+
a = MDArray.build("short", [2, 2, 3])
|
70
|
+
assert_equal([2, 2, 3], a.shape)
|
71
|
+
assert_equal(3, a.ndim)
|
72
|
+
assert_equal(12, a.size)
|
73
|
+
assert_equal("short", a.dtype)
|
74
|
+
assert_equal(0, a[0, 0, 0])
|
75
|
+
assert_equal(0, a[1, 1, 2])
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
#-------------------------------------------------------------------------------------
|
80
|
+
#
|
81
|
+
#-------------------------------------------------------------------------------------
|
82
|
+
|
83
|
+
should "create int array" do
|
84
|
+
|
85
|
+
# build int array with given shape and all values 0
|
86
|
+
a = MDArray.build("int", [2, 2, 3])
|
87
|
+
assert_equal([2, 2, 3], a.shape)
|
88
|
+
assert_equal(3, a.ndim)
|
89
|
+
assert_equal(12, a.size)
|
90
|
+
assert_equal("int", a.dtype)
|
91
|
+
assert_equal(0, a[0, 0, 0])
|
92
|
+
assert_equal(0, a[1, 1, 2])
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
#-------------------------------------------------------------------------------------
|
97
|
+
#
|
98
|
+
#-------------------------------------------------------------------------------------
|
99
|
+
|
100
|
+
should "create float array" do
|
101
|
+
|
102
|
+
# build a float array with shape [2, 3] and the given data.
|
103
|
+
# Note that the data is shaped according to the given shape, so, in this case
|
104
|
+
# the array is:
|
105
|
+
# [[0.0 1.0 2.0]
|
106
|
+
# [3.0 4.0 5.0]]
|
107
|
+
# Note also that although the data is in "int" format the resulting array
|
108
|
+
# is of type float
|
109
|
+
b = MDArray.float([2, 3], [0, 1, 2, 3, 4, 5, 6])
|
110
|
+
assert_equal("float", b.dtype)
|
111
|
+
assert_equal(1.0, b[0, 1])
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
#-------------------------------------------------------------------------------------
|
116
|
+
#
|
117
|
+
#-------------------------------------------------------------------------------------
|
118
|
+
|
119
|
+
should "create arrays from arange functions" do
|
120
|
+
|
121
|
+
# use method arange to build an int array with a sequence of numbers
|
122
|
+
# d = [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
|
123
|
+
d = MDArray.arange(15)
|
124
|
+
counter = 0
|
125
|
+
d.each do |elmt|
|
126
|
+
assert_equal(counter, elmt)
|
127
|
+
counter += 1
|
128
|
+
end
|
129
|
+
|
130
|
+
# with 2 arguments we have the begining and ending values for arange
|
131
|
+
# e = [5 6 7 8 9 10 11 12 13 14]
|
132
|
+
e = MDArray.arange(5, 15)
|
133
|
+
counter = 5
|
134
|
+
e.each do |elmt|
|
135
|
+
assert_equal(counter, elmt)
|
136
|
+
counter += 1
|
137
|
+
end
|
138
|
+
|
139
|
+
# with 3 arguments we have the begining, ending and step arguments
|
140
|
+
# f = [10 15 20 25]
|
141
|
+
f = MDArray.arange(10, 30, 5)
|
142
|
+
counter = 10
|
143
|
+
f.each do |elmt|
|
144
|
+
assert_equal(counter, elmt)
|
145
|
+
counter += 5
|
146
|
+
end
|
147
|
+
|
148
|
+
# typed_arange does the same as arange but for arrays of other type
|
149
|
+
g = MDArray.typed_arange("double", 10, 30)
|
150
|
+
|
151
|
+
# h = [10.0 12.5 15.0 17.5 20.0 22.5 25.0 27.5]
|
152
|
+
h = MDArray.typed_arange("double", 10, 30, 2.5)
|
153
|
+
counter = 10
|
154
|
+
h.each do |elmt|
|
155
|
+
assert_equal(counter, elmt)
|
156
|
+
counter += 2.5
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
#-------------------------------------------------------------------------------------
|
162
|
+
#
|
163
|
+
#-------------------------------------------------------------------------------------
|
164
|
+
|
165
|
+
should "create array from fromfunction" do
|
166
|
+
|
167
|
+
# creates an array from a function (actually a block). The name fromfunction
|
168
|
+
# is preserved to maintain API compatibility with NumPy (is it necessary?)
|
169
|
+
arr = MDArray.fromfunction("double", [5]) do |x|
|
170
|
+
x
|
171
|
+
end
|
172
|
+
|
173
|
+
# creates an array from a function (actually a block). The name fromfunction
|
174
|
+
# is preserved to maintain API compatibility with NumPy (is it necessary?)
|
175
|
+
arr = MDArray.fromfunction("double", [5, 5]) do |x, y|
|
176
|
+
x + y
|
177
|
+
end
|
178
|
+
|
179
|
+
# creates an array from a function (actually a block). The name fromfunction
|
180
|
+
# is preserved to maintain API compatibility with NumPy (is it necessary?)
|
181
|
+
arr = MDArray.fromfunction("double", [2, 3, 4]) do |x, y, z|
|
182
|
+
3.21 * x + y + z
|
183
|
+
end
|
184
|
+
assert_equal("double", arr.type)
|
185
|
+
assert_equal(0, arr[0, 0, 0])
|
186
|
+
assert_equal(4.21, arr[1, 1, 0])
|
187
|
+
|
188
|
+
# creates an array from a function (actually a block). The name fromfunction
|
189
|
+
# is preserved to maintain API compatibility with NumPy (is it necessary?)
|
190
|
+
arr = MDArray.fromfunction("int", [2, 3, 4]) do |x, y, z|
|
191
|
+
x + y + z
|
192
|
+
end
|
193
|
+
assert_equal("int", arr.type)
|
194
|
+
|
195
|
+
# creates an array from a function (actually a block). The name fromfunction
|
196
|
+
# is preserved to maintain API compatibility with NumPy (is it necessary?)
|
197
|
+
arr = MDArray.fromfunction("double", [5, 5, 5, 5]) do |x, y, z, k|
|
198
|
+
x + y + z + k
|
199
|
+
end
|
200
|
+
|
201
|
+
# creates an array from a function (actually a block). The name fromfunction
|
202
|
+
# is preserved to maintain API compatibility with NumPy (is it necessary?)
|
203
|
+
arr = MDArray.fromfunction("double", [5, 5, 5, 5, 5]) do |x, y, z, k, w|
|
204
|
+
x + y + z + k + w
|
205
|
+
end
|
206
|
+
|
207
|
+
# creates an array from a function (actually a block). The name fromfunction
|
208
|
+
# is preserved to maintain API compatibility with NumPy (is it necessary?)
|
209
|
+
arr = MDArray.fromfunction("double", [5, 5, 5, 5, 5, 5]) do |x, y, z, k, w, i|
|
210
|
+
x + y + z + k + w + i
|
211
|
+
end
|
212
|
+
|
213
|
+
# creates an array from a function (actually a block). The name fromfunction
|
214
|
+
# is preserved to maintain API compatibility with NumPy (is it necessary?)
|
215
|
+
arr = MDArray.fromfunction("double", [5, 5, 5, 5, 5, 5, 5]) do |x, y, z, k, w, i, l|
|
216
|
+
x + y + z + k + w + i + l
|
217
|
+
end
|
218
|
+
|
219
|
+
# creates an array from a function (actually a block). The name fromfunction
|
220
|
+
# is preserved to maintain API compatibility with NumPy (is it necessary?)
|
221
|
+
# Arrays with dimension larger than 7, the data is treated as an array, and we
|
222
|
+
# cannot use the same notation as before.
|
223
|
+
arr = MDArray.fromfunction("double", [5, 5, 5, 5, 5, 5, 5, 5]) do |x|
|
224
|
+
x.inject(:+)
|
225
|
+
end
|
226
|
+
|
227
|
+
# creates an array from a function (actually a block). The name fromfunction
|
228
|
+
# is preserved to maintain API compatibility with NumPy (is it necessary?)
|
229
|
+
# A similar notation as the array can be used for lower dimensions using ruby *
|
230
|
+
# operator. This is a little less efficient though.
|
231
|
+
arr = MDArray.fromfunction("double", [5, 5, 5, 5, 5]) do |*x|
|
232
|
+
x.inject(:+)
|
233
|
+
end
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
#-------------------------------------------------------------------------------------
|
238
|
+
#
|
239
|
+
#-------------------------------------------------------------------------------------
|
240
|
+
|
241
|
+
should "create array from arange" do
|
242
|
+
|
243
|
+
arr = MDArray.arange(10)
|
244
|
+
assert_equal("0 1 2 3 4 5 6 7 8 9 ", arr.to_string)
|
245
|
+
arr = MDArray.arange(2, 30)
|
246
|
+
assert_equal("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 ",
|
247
|
+
arr.to_string)
|
248
|
+
arr = MDArray.arange(2, 30, 3)
|
249
|
+
assert_equal("2 5 8 11 14 17 20 23 26 29 ", arr.to_string)
|
250
|
+
# inconsistent result, better to use linspace
|
251
|
+
arr = MDArray.arange(2, 30, 2.5)
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
|
256
|
+
#-------------------------------------------------------------------------------------
|
257
|
+
#
|
258
|
+
#-------------------------------------------------------------------------------------
|
259
|
+
|
260
|
+
should "create array from linspace" do
|
261
|
+
|
262
|
+
arr = MDArray.linspace("double", 0, 2, 9)
|
263
|
+
assert_equal(0.0, arr[0])
|
264
|
+
assert_equal(0.5, arr[2])
|
265
|
+
assert_equal(1.0, arr[4])
|
266
|
+
assert_equal(1.5, arr[6])
|
267
|
+
assert_equal(2.0, arr[8])
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
#-------------------------------------------------------------------------------------
|
272
|
+
#
|
273
|
+
#-------------------------------------------------------------------------------------
|
274
|
+
|
275
|
+
should "create array with ones" do
|
276
|
+
|
277
|
+
# creates an array with all 1's
|
278
|
+
ones = MDArray.ones("byte", [2, 2, 2, 2])
|
279
|
+
assert_equal(1, ones[1, 1, 1, 1])
|
280
|
+
assert_equal(1, ones[1, 0, 1, 0])
|
281
|
+
|
282
|
+
end
|
283
|
+
|
284
|
+
#-------------------------------------------------------------------------------------
|
285
|
+
#
|
286
|
+
#-------------------------------------------------------------------------------------
|
287
|
+
|
288
|
+
should "create array with given value" do
|
289
|
+
|
290
|
+
# creates an array with a given value and type
|
291
|
+
fives = MDArray.init_with("double", [3, 3], 5.34)
|
292
|
+
assert_equal(5.34, fives[2, 1])
|
293
|
+
assert_equal(5.34, fives[0, 2])
|
294
|
+
|
295
|
+
end
|
296
|
+
|
297
|
+
#-------------------------------------------------------------------------------------
|
298
|
+
#
|
299
|
+
#-------------------------------------------------------------------------------------
|
300
|
+
|
301
|
+
should "allow filling the array with data" do
|
302
|
+
|
303
|
+
# fill b with a given value
|
304
|
+
b = MDArray.double([2, 3], [0, 1, 2, 3, 4, 5])
|
305
|
+
b.fill(5.47)
|
306
|
+
b.each do |elmt|
|
307
|
+
assert_equal(5.47, elmt)
|
308
|
+
end
|
309
|
+
|
310
|
+
# typed_arange does the same as arange but for arrays of other type
|
311
|
+
g = MDArray.typed_arange("double", 6)
|
312
|
+
g.reshape!([2, 3])
|
313
|
+
b.fill(g)
|
314
|
+
assert_equal(b.to_string, g.to_string)
|
315
|
+
|
316
|
+
end
|
317
|
+
|
318
|
+
#-------------------------------------------------------------------------------------
|
319
|
+
#
|
320
|
+
#-------------------------------------------------------------------------------------
|
321
|
+
|
322
|
+
should "create boolean arrays" do
|
323
|
+
|
324
|
+
bool = MDArray.boolean([2, 2])
|
325
|
+
bool[0, 0] = true
|
326
|
+
assert_raise ( RuntimeError ) { bool[0, 1] = 10.0 }
|
327
|
+
assert_equal(bool[0, 0], true)
|
328
|
+
bool[0, 1] = false
|
329
|
+
assert_equal(bool[0, 1], false)
|
330
|
+
|
331
|
+
end
|
332
|
+
|
333
|
+
#-------------------------------------------------------------------------------------
|
334
|
+
#
|
335
|
+
#-------------------------------------------------------------------------------------
|
336
|
+
|
337
|
+
should "create string arrays" do
|
338
|
+
|
339
|
+
sarray = MDArray.string([2, 3], ["hello", "this", "is", "a", "string", "array"])
|
340
|
+
assert_equal(6, sarray.size)
|
341
|
+
assert_equal("hello", sarray.get([0, 0]))
|
342
|
+
assert_equal("hello this is a string array ", sarray.to_string)
|
343
|
+
|
344
|
+
end
|
345
|
+
|
346
|
+
#-------------------------------------------------------------------------------------
|
347
|
+
# A struct array is an array of pointers to structures
|
348
|
+
#-------------------------------------------------------------------------------------
|
349
|
+
|
350
|
+
should "create struct arrays" do
|
351
|
+
|
352
|
+
m = Hash.new
|
353
|
+
m[:hello] = "world"
|
354
|
+
m[:test] = 1.23
|
355
|
+
|
356
|
+
struct = MDArray.structure([10])
|
357
|
+
struct[0] = m
|
358
|
+
struct.print
|
359
|
+
|
360
|
+
end
|
361
|
+
|
362
|
+
end
|
363
|
+
|
364
|
+
end
|