CARVIEW |
Select Language
HTTP/2 200
date: Wed, 23 Jul 2025 17:50:37 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
x-robots-tag: none
etag: W/"bef3d165e5126f01398a03020008081e"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=HfQqUOeVubXg%2BCcYS7AUkJPJ00XL3XyVpxY8eUGO2EjSi1bd3HHEhm2AT8mUomfIGLnMqavMZoGHh1yaH%2BvKucmBdssn4hSbCvl2rhHdmdlO7fH%2FElMNwEvZEwQGUqibQLN%2FcA40Nbpdr%2BSKUj%2Fay65jFE%2BQBLG4wxWz12vG4lsnxfaKOmVcwP%2FNpdwXMWg%2BYn8LtSy1aThNUYk1wPzTPSxhRwhw6MMjeQXXVjTV%2FNt7We324WYuGS0XmrfHnTRCzFuWY%2Fl9YcAbhAEH%2F0TkNw%3D%3D--l4RLpDXsp9J3eUNc--TyMzphStF1Sxl2Hf8bcHLg%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.268245387.1753293037; Path=/; Domain=github.com; Expires=Thu, 23 Jul 2026 17:50:37 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Thu, 23 Jul 2026 17:50:37 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: A9D2:2FAFD7:F6FC10:124173E:688120ED
MDArray Access · rbotafogo/mdarray Wiki · GitHub
Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 5
MDArray Access
rbotafogo edited this page Apr 3, 2013
·
3 revisions
#-------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------
should "allow data assignment and retrieval" do
assert_equal(0, @a[1, 2, 3])
# assign values to a using [] operator
@a[0, 0, 0] = 10
@a[0, 1, 2] = 10
@a[0, 1, 3] = 10
assert_equal(10, @a[0, 0, 0])
assert_equal(10, @a[0, 1, 2])
assert_equal(10, @a[0, 1, 3])
assert_equal(0, @a[0, 0, 1])
# assigning a float value to a byte array type will truncate the data
@a[0, 0, 0] = 1.5
assert_equal(1, @a[0, 0, 0])
assert_equal(0.0, @b[1, 1, 1])
# assign values to b using set method
@b.set([1, 1, 1], 5.25)
assert_equal(5.25, @b.get([1, 1, 1]))
end
#-------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------
should "generate RuntimeError and RangeError when counter is wrong" do
# When counter error in [], we get a RuntimeError.
# RuntimeError... dimension error on b
assert_raise ( RuntimeError ) { @b[0, 0, 0, 0] = 11.34 }
# RuntimeError... shape of first dimension too big
assert_raise ( RuntimeError ) { @b[5, 1, 1] = 7.32 }
# RangeError... shape of second dimension too big
assert_raise ( RuntimeError ) { @b[1, 3, 1] = 7.32 }
# When counter error is in get, we get a RangeError. Method get is faster than
# method [] as get does not do any counter validation nor convertion. [] does
# counter validation and convertion allowing for the use of negative counter
# and range.
assert_raise ( RangeError ) { @b.set([0, 0, 0, 0], 11.34) }
assert_raise ( RangeError ) { @b.set([5, 1, 1], 7.32) }
assert_raise ( RangeError ) { @b.set([1, 3, 1], 7.32) }
end
#-------------------------------------------------------------------------------------
# It is possible to use negative indexing with [], but not with methods get and set.
#-------------------------------------------------------------------------------------
should "allow working with negative indices" do
@b[1, -1, -1] = 4.20
assert_equal(4.20, @b[1, 2, 3])
@b[-1, -1, -1] = 10.35
# using negative indices with set raises an exeption...
assert_raise ( RangeError ) { @b.set([-1, -1, -1], 11.40) }
# but works with []
assert_equal(10.35, @b[-1, -1, -1])
assert_equal(@b[-1, -1, -1], 10.35)
# using negative indices with get raises an exeption
assert_raise ( RangeError ) { @b.get([-1, -1, -1]) }
assert_equal(10.35, @b[1, 2, 3])
# RuntimeError... shape of first dimension too big (on the negative side)
assert_raise ( RangeError ) { @b.set([-5, 1, 1], 7.32) }
# RuntimeError... shape of second dimension too big (on the negative side)
assert_raise ( RangeError ) { @b.set([1, -4, 1], 7.32) }
end
#-------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------
should "output data in string format" do
a = MDArray.arange(10)
assert_equal("0 1 2 3 4 5 6 7 8 9 ", a.to_string)
end
#-------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------
should "allow array traversal in cannonical order" do
a = MDArray.arange(10)
# set the value at the current index position. Raise an exception as there
# is no currently defined position. Note that a[] refers to the current counter
# position
assert_raise ( RuntimeError ) { a[] = 18.25 }
# gets the value at the current index position. Raises an exception as
# current position not defined
assert_raise ( RuntimeError) { a[] }
# reset_traversal initializes the traversal, but leaves the counter in an invalid
# position
a.reset_traversal
# move to the first position
a.next
assert_equal(0, a[])
# sets the value at the current position. No need to give the counter.
a[] = 10
assert_equal(10, a[])
# move to the next position
a.next
assert_equal(1, a[])
# get_next returns the next position and moves the counter. Method get_next and
# set_next are "fast" methods in the sense that they do not require any counter
# correction.
assert_equal(2, a.get_next)
assert_equal(2, a[])
assert_equal(3, a.get_next)
assert_equal(4, a.get_next)
end
#-------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------
should "allow array traversal using each like methods" do
a = MDArray.arange(10)
counter = 0
a.each do |elmt|
assert_equal(counter, elmt)
counter += 1
end
a.reset_traversal
a.next
assert_equal(0, a[])
# continues the each from where the counter stoped previously
counter = 1
a.each_cont do |elmt|
assert_equal(counter, elmt)
counter += 1
end
# each_with_counter returns the elmt and it's index
a.reshape!([5, 2])
counter = 0
a.each_with_index do |elmt, index|
assert_equal(counter, elmt)
counter += 1
end
# Collect returns a one dimensional ruby array
b = a.collect { |val| val * 2}
end
#-------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------
should "have the right precision for the type" do
b = MDArray.float([2, 3], [0, 1, 2, 3, 4, 5])
assert_equal(1.0, b[0, 1])
# float should work with 1 decimal precision
b[0, 1] = 1.5
assert_equal(1.5, b[0, 1])
c = MDArray.build("double", [2, 3], [0, 1, 2, 3, 4, 5])
c[0, 0] = 1.51
assert_equal(1.51, c[0, 0])
end
You can’t perform that action at this time.