CARVIEW |
Select Language
HTTP/2 200
date: Wed, 23 Jul 2025 23:30:42 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/"05b938d5cddbf7b5fb665cd12f387b92"
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=NSvu8hhqpJbwKWwgD1WQHeeQv4YQT6yGCKp477XSDLawzcRjfOWdDzN6sno%2BF2buGLIXrooHlUdLLhfai008V1jHtaJpRsuxUYOVa%2B2V4Kxn0tNzLBbnvfuKuW%2B04ifPyxCqUyDRHF5CY5fqmI3x%2FLSapxoj49g2b6ljMBM33ewqpMy0HkcEECR3txyzRRsDV4yD474hJDyfe8X6ctf9CjQ2eY%2BNVjnLhLocPtP%2BLjGOzXciKnnvw3he0izTIWaKGp1sNDMkaUb9MWHiWlOwXw%3D%3D--3udh%2BLzN3e3%2FaEep--E%2FBD%2Bmk4ONnSBbk1icya7w%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.2142940178.1753313441; Path=/; Domain=github.com; Expires=Thu, 23 Jul 2026 23:30:41 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Thu, 23 Jul 2026 23:30:41 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: 9328:B5FA6:114F2E6:14CE209:688170A1
Casting · rbotafogo/mdarray Wiki · GitHub
Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 5
Casting
rbotafogo edited this page Apr 11, 2013
·
1 revision
class MDArrayTest < Test::Unit::TestCase
context "Arithmetic Tests" do
setup do
# create a = [20 30 40 50]
@a = MDArray.arange(20, 60, 10)
# create b = [0 1 2 3]
@b = MDArray.arange(4)
# create c = [1.87 5.34 7.18 8.84]
@c = MDArray.double([4], [1.87, 5.34, 7.18, 8.84])
# create d = [[1 2] [3 4]]
@d = MDArray.int([2, 2], [1, 2, 3, 4])
# creates an array from a function (actually a block). The name fromfunction
# is preserved to maintain API compatibility with NumPy (is it necessary?)
@e = MDArray.fromfunction("double", [4, 5, 6]) do |x, y, z|
3.21 * (x + y + z)
end
@f = MDArray.fromfunction("double", [4, 5, 6]) do |x, y, z|
9.57 * x + y + z
end
@bool = MDArray.boolean([4], [true, true, false, false])
end # setup
#-------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------
should "do down-casting when in-place operation" do
# in place operation between an "int" array and a "double" array will keep the
# type of a to "int", no up-casting. Values will be truncated
@a.add! @c
assert_equal(21, @a[0])
assert_equal(@a.type, "int")
end
#-------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------
should "do up-casting" do
# when doing operations between two arrays, the resulting array will have the
# highest casting value. result will be of type double
result = @a + @c
assert_equal(21.87, result[0])
assert_equal("double", result.dtype)
end
#-------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------
should "do up-casting with basic types" do
result = 1.57 * @a
# since @a is an int, this should be converted to double, as there is no "float"
# type in ruby
assert_equal("double", result.dtype)
assert_equal(result.to_string, "31.400000000000002 47.1 62.800000000000004 78.5 ")
result = @c * 1.57
assert_equal("double", result.dtype)
# array @a will be cast to "double"
result = @a ** 2.5
assert_equal("double", result.dtype)
# array @c will keep its double type
result = @c ** 2.5
assert_equal("double", result.dtype)
# force result to be of given type. Cannot use binary operators, need to use the
# corresponding function. Upcast int @a to float
result = @a.mul(1.57, "float")
assert_equal("float", result.dtype)
result = @a.div(2, "short")
assert_equal("short", result.dtype)
# force result to be of given type. Cannot use binary operators, need to use the
# corresponding function.
result = @e.mul(1.57, "float")
assert_equal("float", result.dtype)
end
#-------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------
should "allow forced casting" do
# force result to be of type float, by normal rules it would be of type double
result = @a.mul(1.57, "float")
assert_equal("float", result.dtype)
# final result depends on when casting occurs. It is possible that the operation
# is done in double and cast when storing on the float array, or the operation
# can be done in float already. Ruby operations perform the former, i.e.,
# double operation and cast to float.
if (MDArray.binary_operator == RubyBinaryOperator)
assert_equal("31.4 47.1 62.8 78.5 ", result.to_string)
else
assert_equal("31.400002 47.100002 62.800003 78.5 ", result.to_string)
end
# force result to be ot type "int"
# @a.binary_operator = BinaryOperator
result = @a.mul(3.43, "int")
assert_equal(result.type, "int")
if (MDArray.binary_operator == RubyBinaryOperator)
# When BinaryOperator with force casting, values are operated first and then
# casted. So, in the example above we have 20 * 3.43 = 68,6 and then the value
# is casted to int for a result of 68
assert_equal(68, result[0])
else
# When we cast earlier values are first cast to the the selected type
# and then operated upon. So, in the example we have 20 * 3.43, which becomes
# 20 * 3 = 60
assert_equal(60, result[0])
end
end
#-------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------
should "allow mixing types" do
# adding double to number
res = @c + 10.5
assert_equal(res.to_string, "12.370000000000001 15.84 17.68 19.34 ")
# adding two double arrays
res = @e + @f
# adding a double to an int, should cast to double
res = @c + @a
assert_equal("21.87 35.34 47.18 58.84 ", res.to_string)
# adding an int to a double, should cast to double
res = @a + @c
assert_equal("21.87 35.34 47.18 58.84 ", res.to_string)
# adding an int array to a (ruby) float/double number, should cast to double
res = @a + 10.55
assert_equal("30.55 40.55 50.55 60.55 ", res.to_string)
# adding two ints
res = @a + @b
assert_equal(res.to_string, "20 31 42 53 ")
# unary operation on arrays
res = @c.floor
assert_equal(res.to_string, "1.0 5.0 7.0 8.0 ")
# Can also operate with the number first. Still doing elementwise operation
c = 10 / @c
assert_equal("5.347593582887701 1.8726591760299627 1.392757660167131 1.1312217194570136 ",
c.to_string)
end
end
end
You can’t perform that action at this time.