From 13a95dbd4762e178ef503509a32263383ed6e1d1 Mon Sep 17 00:00:00 2001 From: Sarah Gilmore Date: Mon, 8 May 2023 16:09:26 -0400 Subject: [PATCH 1/4] Add arrow.array.Float32Array MATLAB class Co-authored-by: Kevin Gurney --- matlab/src/cpp/arrow/matlab/proxy/factory.cc | 2 +- .../src/matlab/+arrow/+array/Float32Array.m | 40 ++++++++ matlab/test/arrow/array/tFloat32Array.m | 99 +++++++++++++++++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 matlab/src/matlab/+arrow/+array/Float32Array.m create mode 100644 matlab/test/arrow/array/tFloat32Array.m diff --git a/matlab/src/cpp/arrow/matlab/proxy/factory.cc b/matlab/src/cpp/arrow/matlab/proxy/factory.cc index c767d7dc3750..fb00aaafd208 100644 --- a/matlab/src/cpp/arrow/matlab/proxy/factory.cc +++ b/matlab/src/cpp/arrow/matlab/proxy/factory.cc @@ -26,8 +26,8 @@ namespace arrow::matlab::proxy { std::shared_ptr Factory::make_proxy(const ClassName& class_name, const FunctionArguments& constructor_arguments) { // Register MATLAB Proxy classes with corresponding C++ Proxy classes. + REGISTER_PROXY(arrow.array.proxy.Float32Array, arrow::matlab::array::proxy::NumericArray); REGISTER_PROXY(arrow.array.proxy.Float64Array, arrow::matlab::array::proxy::NumericArray); - // TODO: Decide what to do in the case that there isn't a Proxy match. std::cout << "Did not find a matching C++ proxy for: " + class_name << std::endl; return nullptr; diff --git a/matlab/src/matlab/+arrow/+array/Float32Array.m b/matlab/src/matlab/+arrow/+array/Float32Array.m new file mode 100644 index 000000000000..5f5af94a705d --- /dev/null +++ b/matlab/src/matlab/+arrow/+array/Float32Array.m @@ -0,0 +1,40 @@ +classdef Float32Array < arrow.array.Array + % arrow.array.Float32Array + + % Licensed to the Apache Software Foundation (ASF) under one or more + % contributor license agreements. See the NOTICE file distributed with + % this work for additional information regarding copyright ownership. + % The ASF licenses this file to you under the Apache License, Version + % 2.0 (the "License"); you may not use this file except in compliance + % with the License. You may obtain a copy of the License at + % + % http://www.apache.org/licenses/LICENSE-2.0 + % + % Unless required by applicable law or agreed to in writing, software + % distributed under the License is distributed on an "AS IS" BASIS, + % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + % implied. See the License for the specific language governing + % permissions and limitations under the License. + + properties (Hidden, SetAccess=private) + MatlabArray = single([]) + end + + methods + function obj = Float32Array(data, opts) + arguments + data + opts.DeepCopy = false + end + validateattributes(data, "single", ["2d", "nonsparse", "real"]); + if ~isempty(data), validateattributes(data, "single", "vector"); end + obj@arrow.array.Array("Name", "arrow.array.proxy.Float32Array", "ConstructorArguments", {data, opts.DeepCopy}); + % Store a reference to the array if not doing a deep copy + if (~opts.DeepCopy), obj.MatlabArray = data; end + end + + function data = single(obj) + data = obj.Proxy.ToMatlab(); + end + end +end diff --git a/matlab/test/arrow/array/tFloat32Array.m b/matlab/test/arrow/array/tFloat32Array.m new file mode 100644 index 000000000000..45966d554efd --- /dev/null +++ b/matlab/test/arrow/array/tFloat32Array.m @@ -0,0 +1,99 @@ +classdef tFloat32Array < matlab.unittest.TestCase + % Tests for arrow.array.Float32rray + + % Licensed to the Apache Software Foundation (ASF) under one or more + % contributor license agreements. See the NOTICE file distributed with + % this work for additional information regarding copyright ownership. + % The ASF licenses this file to you under the Apache License, Version + % 2.0 (the "License"); you may not use this file except in compliance + % with the License. You may obtain a copy of the License at + % + % http://www.apache.org/licenses/LICENSE-2.0 + % + % Unless required by applicable law or agreed to in writing, software + % distributed under the License is distributed on an "AS IS" BASIS, + % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + % implied. See the License for the specific language governing + % permissions and limitations under the License. + + properties (TestParameter) + MakeDeepCopy = {true false} + end + + methods(TestClassSetup) + function verifyOnMatlabPath(testCase) + % arrow.array.Float32Array must be on the MATLAB path. + testCase.assertTrue(~isempty(which('arrow.array.Float32Array')), ... + '''arrow.array.Float32Array'' must be on the MATLAB path. Use ''addpath'' to add folders to the MATLAB path.'); + end + end + + methods(Test) + function Basic(testCase, MakeDeepCopy) + A = arrow.array.Float32Array(single([1, 2, 3]), DeepCopy=MakeDeepCopy); + className = string(class(A)); + testCase.verifyEqual(className, "arrow.array.Float32Array"); + end + + function ShallowCopy(testCase) + % By default, Float32Array does not create a deep copy on + % construction when constructed from a MATLAB array. Instead, + % it stores a shallow copy of the array keep the memory alive. + A = arrow.array.Float32Array(single([1, 2, 3])); + testCase.verifyEqual(A.MatlabArray, single([1 2 3])); + testCase.verifyEqual(single(A), single([1 2 3]')); + + A = arrow.array.Float32Array(single([1, 2, 3]), DeepCopy=false); + testCase.verifyEqual(A.MatlabArray, single([1 2 3])); + testCase.verifyEqual(single(A), single([1 2 3]')); + end + + function DeepCopy(testCase) + % Verify Float32Array does not store shallow copy of the MATLAB + % array if DeepCopy=true was supplied. + A = arrow.array.Float32Array(single([1, 2, 3]), DeepCopy=true); + testCase.verifyEqual(A.MatlabArray, single([])); + testCase.verifyEqual(single(A), single([1 2 3]')); + end + + function Double(testCase, MakeDeepCopy) + % Create a Float32Array from a scalar double + A1 = arrow.array.Float32Array(single(100), DeepCopy=MakeDeepCopy); + data = single(A1); + testCase.verifyEqual(data, single(100)); + + % Create a Float32Array from a double vector + A2 = arrow.array.Float32Array(single([1 2 3]), DeepCopy=MakeDeepCopy); + data = single(A2); + testCase.verifyEqual(data, single([1 2 3]')); + + % Create a Float32Array from an empty double vector + A3 = arrow.array.Float32Array(single([]), DeepCopy=MakeDeepCopy); + data = single(A3); + testCase.verifyEqual(data, single.empty(0, 1)); + end + + function MinValue(testCase, MakeDeepCopy) + A1 = arrow.array.Float32Array(realmin("single"), DeepCopy=MakeDeepCopy); + data = single(A1); + testCase.verifyEqual(data, realmin("single")); + end + + function MaxValue(testCase, MakeDeepCopy) + A1 = arrow.array.Float32Array(realmax('single'), DeepCopy=MakeDeepCopy); + data = single(A1); + testCase.verifyEqual(data, realmax('single')); + end + + function InfValues(testCase, MakeDeepCopy) + A1 = arrow.array.Float32Array(single([Inf -Inf]), DeepCopy=MakeDeepCopy); + data = single(A1); + testCase.verifyEqual(data, single([Inf -Inf]')); + end + + function ErrorIfComplex(testCase, MakeDeepCopy) + fcn = @() arrow.array.Float32Array(single([10 + 1i, 4]), DeepCopy=MakeDeepCopy); + testCase.verifyError(fcn, "MATLAB:expectedReal"); + end + end +end \ No newline at end of file From 1b153ba2ec18c7d684a5ed3d5761a6a256e3e351 Mon Sep 17 00:00:00 2001 From: Sarah Gilmore Date: Fri, 12 May 2023 10:11:34 -0400 Subject: [PATCH 2/4] 1. Fix typo in tFloat32Array.m --- matlab/test/arrow/array/tFloat32Array.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matlab/test/arrow/array/tFloat32Array.m b/matlab/test/arrow/array/tFloat32Array.m index 45966d554efd..375481a5def2 100644 --- a/matlab/test/arrow/array/tFloat32Array.m +++ b/matlab/test/arrow/array/tFloat32Array.m @@ -56,7 +56,7 @@ function DeepCopy(testCase) testCase.verifyEqual(single(A), single([1 2 3]')); end - function Double(testCase, MakeDeepCopy) + function Single(testCase, MakeDeepCopy) % Create a Float32Array from a scalar double A1 = arrow.array.Float32Array(single(100), DeepCopy=MakeDeepCopy); data = single(A1); @@ -96,4 +96,4 @@ function ErrorIfComplex(testCase, MakeDeepCopy) testCase.verifyError(fcn, "MATLAB:expectedReal"); end end -end \ No newline at end of file +end From c563c32592776a3b9c70fff0d338a1a0ac445cc2 Mon Sep 17 00:00:00 2001 From: sgilmore10 <74676073+sgilmore10@users.noreply.github.com> Date: Mon, 15 May 2023 09:01:16 -0400 Subject: [PATCH 3/4] Indent comments Co-authored-by: Sutou Kouhei --- matlab/test/arrow/array/tFloat32Array.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matlab/test/arrow/array/tFloat32Array.m b/matlab/test/arrow/array/tFloat32Array.m index 375481a5def2..72cf1fd9533c 100644 --- a/matlab/test/arrow/array/tFloat32Array.m +++ b/matlab/test/arrow/array/tFloat32Array.m @@ -36,9 +36,9 @@ function Basic(testCase, MakeDeepCopy) end function ShallowCopy(testCase) - % By default, Float32Array does not create a deep copy on - % construction when constructed from a MATLAB array. Instead, - % it stores a shallow copy of the array keep the memory alive. + % By default, Float32Array does not create a deep copy on + % construction when constructed from a MATLAB array. Instead, + % it stores a shallow copy of the array keep the memory alive. A = arrow.array.Float32Array(single([1, 2, 3])); testCase.verifyEqual(A.MatlabArray, single([1 2 3])); testCase.verifyEqual(single(A), single([1 2 3]')); From 89ca1895e7618195037e547776d667f1a524c99e Mon Sep 17 00:00:00 2001 From: sgilmore10 <74676073+sgilmore10@users.noreply.github.com> Date: Mon, 15 May 2023 09:01:28 -0400 Subject: [PATCH 4/4] Indent comments Co-authored-by: Sutou Kouhei --- matlab/test/arrow/array/tFloat32Array.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matlab/test/arrow/array/tFloat32Array.m b/matlab/test/arrow/array/tFloat32Array.m index 72cf1fd9533c..a512dec87d19 100644 --- a/matlab/test/arrow/array/tFloat32Array.m +++ b/matlab/test/arrow/array/tFloat32Array.m @@ -49,8 +49,8 @@ function ShallowCopy(testCase) end function DeepCopy(testCase) - % Verify Float32Array does not store shallow copy of the MATLAB - % array if DeepCopy=true was supplied. + % Verify Float32Array does not store shallow copy of the MATLAB + % array if DeepCopy=true was supplied. A = arrow.array.Float32Array(single([1, 2, 3]), DeepCopy=true); testCase.verifyEqual(A.MatlabArray, single([])); testCase.verifyEqual(single(A), single([1 2 3]'));