%% Use Python Numeric Variables in MATLAB
% This example shows how to use Python® numeric types in MATLAB®.
%% Use Python Numeric Types in MATLAB
% When calling a Python function that takes a numeric input argument, MATLAB converts double values into types that best represent the data to the Python language. For example, to call trigonometry functions in the Python math module, pass a MATLAB double value.

pynum = py.math.radians(90)

% For functions that return Python float types, MATLAB automatically converts this type to double.

class(pynum)

% For Python functions returning integer types, MATLAB automatically converts this type to int64. For example, the bit_length function returns the number of bits necessary to represent an integer in binary as an int value.

py.int(intmax).bit_length

%
%% Call Python Methods with Numeric iterable Arguments
% The Python math.fsum function sums floating-point values in an iterable input argument. You can pass a MATLAB vector to this function. For example, open the MATLAB patients.mat data file and read the numeric array Height.

load patients.mat
class(Height)
size(Height)

% When you pass this argument to Python, MATLAB automatically converts the numeric values to Python numeric values and Python iterates over the vector values.

py.math.fsum(Height)

%
%% Use Python array Types in MATLAB
% Suppose that you have a Python function that returns the following Python array.array of type double.

P = py.array.array('d', 1:5)

% To pass P to the MATLAB function sum, convert P to a MATLAB array of type double.

sum(double(P))

%
%% Use Python Integer array Types in MATLAB
% Suppose that you have a Python array of 8-byte signed integers. Call the Python reverse function on the array, then convert the result to a MATLAB array.

arr = py.array.array('q',[int64(5),int64(1),int64(-5)])
arr.reverse
A = int64(arr)

%
%% Default Numeric Types
% By default, a number in MATLAB is a double type. By default, a number (without a fractional part) in Python is an integer type. This difference can cause confusion when passing numbers to Python functions.
% For example, when you pass these MATLAB numbers to the Python datetime function, Python reads them as float types and displays an error:
% d = py.datetime.date(2014,12,31)
% Python Error: TypeError: integer argument expected, got float
% To correct the error, explicitly convert each number to an integer type:

d = py.datetime.date(int32(2014),int32(12),int32(31))

%
%% Why Do I See Properties When I Display a Number?
% MATLAB displays all Python types as objects, which includes a list of object properties. For numeric types, MATLAB displays the expected output value on the last line.

py.int(5)

% Copyright 2020 The MathWorks, Inc.