*نکته: برای اجرای دستورات زیر کتابخانه numpy حتما باید در سیستم شما نصب باشد.
کد برنامه:
.
clc;clear;
py.importlib.import_module('numpy');
% Mathematical Operations
sqrt2_math = py.math.sqrt(2) % Calculating the square root of 2 using Python's built-in math module
sqrt2_numpy = py.numpy.sqrt(2) % Calculating the square root of 2 using NumPy
x_rad=90; x=((x_rad*pi)/180); % Convert the angle from radians to degrees
cos=py.numpy.cos(x) % Calculate the cosine of the angle
sin=py.numpy.sin(x) % Calculate the sine of the angle
tan=py.numpy.tan(x) % Calculate the tangent of the angle
% Array Operations
sum_array = py.numpy.sum(py.list({1,2,3,4,5})) % Calculating the sum of an array
poly_roots = py.numpy.roots(py.list({1,2,3,4,5})) % Calculating the roots of a polynomial
numpy_array = py.numpy.array(py.list({1,2,3,4,5})) % Creating a NumPy array
mean_array = py.numpy.mean(py.list({1,2,3,4,5})) % Calculating the mean of an array
std_array = py.numpy.std(py.list({1,2,3,4,5})) % Calculating the standard deviation of an array
% Vector Operations
dot_product = py.numpy.dot(py.list({1,2,3}), py.list({4,5,6})) % Calculating the dot product of two arrays
cross_product = py.numpy.cross(py.list({1,2,3}), py.list({4,5,6})) % Calculating the cross product of two arrays
% Array Creation
zeroArray = py.numpy.zeros(int32(3)) % Creating a 1D array of length 3 with all values 0
onesArray = py.numpy.ones(py.tuple({int32(3), int32(4)})) % Creating a 3 x 4 array with all values 1
identityMatrix = py.numpy.eye(int32(5)) % Creating a 5 x 5 identity matrix
linspaceArray = py.numpy.linspace(int32(0), int32(100), int32(6)) % Creating an array of 6 evenly divided values from 0 to 100
arangeArray = py.numpy.arange(int32(0), int32(10), int32(3)) % Creating an array of values from 0 to less than 10 with step 3 (eg [0,3,6,9])
fullArray = py.numpy.full(py.tuple({int32(3), int32(4)}), int32(8)) % Creating a 3 x 4 array with all values 8
% Random Array Creation
randArray1 = py.numpy.random.rand(int32(4),int32(5)) % Creating a 4 x 5 array of random floats between 0 - 1
randArray2 = py.numpy.random.rand(int32(2), int32(2))*100 % Creating a 2 x 2 array of random floats between 0 - 100
randIntMatrix = py.numpy.random.randint(5, pyargs('size', py.tuple({int32(2), int32(3)}))) % Creating a 2 x 3 array with random ints between 0 - 4
% Inspecting Properties
arr = py.numpy.array(py.list({1,2,3,4,5})); % Define arr
num_elements = numel(arr); % Get the number of elements in arr
arr_size = arr.size % Returns number of elements in arr
arr_shape = arr.shape % Returns dimensions of arr (rows,columns)
dtype ='int32';
arr_dtype = arr.dtype % Returns type of elements in arr
arr_converted = arr.astype(dtype) % Convert arr elements to type dtype
arr_list = arr.tolist() % Convert arr to a Python list
N = int32(5); % Define N
identity_matrix = py.numpy.eye(N) % Create a 5 x 5 identity matrix
% Copying/sorting/reshaping
arr_copy = py.numpy.copy(arr) % Copies arr to new memory
arr_view = arr.view(dtype) % Creates view of arr elements with type dtype
arr_sorted = arr.sort() % Sorts arr
arr_axis_sorted = arr.sort(pyargs('axis',int32(0))) % Sorts specific axis of arr
two_d_arr = py.numpy.array(py.list({py.list({1,2,3}), py.list({4,5,6})})) % Define two_d_arr
flattened_arr = two_d_arr.flatten() % Flatten two_d_arr to 1D
arr_transposed = arr.T % Transposes arr (rows become columns and vice versa)
arr_resized = arr.resize(int32(5), int32(1)) % Changes arr shape to 5 x 1 and fills new values with 0
arr_reshaped = arr.reshape(int32(5), int32(1)) % Reshapes arr to 5 rows, 1 columns without changing data
% Adding/removing Elements
values = py.numpy.array(py.list({6, 7, 8}));% Define the values you want to append or insert
arr = py.numpy.append(arr, values)% Append values to the end of arr
arr = py.numpy.insert(arr, int32(2), values)% Insert values into arr before index 2
arr = py.numpy.delete(arr, int32(3))% Delete the element at index 3 of arr
arr = py.numpy.array(py.list({py.list({1, 2, 3}), py.list({4, 5, 6}), py.list({7, 8, 9})}))% Define your array as a 2D array
arr = py.numpy.delete(arr, int32(1), pyargs('axis', int32(1)))% Delete column at index 1 of arr
% Adding/Subtracting/Multiplying/Dividing
arr1 = py.numpy.array(py.list({1,2,3,4,5})) % Define arr1
arr2 = py.numpy.array(py.list({6,7,8,9,10})) % Define arr2
sum_arr = py.numpy.add(arr1, arr2) % Add arr1 and arr2
diff_arr = py.numpy.subtract(arr1, arr2) % Subtract arr2 from arr1
prod_arr = py.numpy.multiply(arr1, arr2) % Multiply arr1 and arr2
quot_arr = py.numpy.divide(arr1, arr2) % Divide arr1 by arr2
% Array Math
sqrt_arr = py.numpy.sqrt(arr1) % Square root of each element in arr1
log_arr = py.numpy.log(arr1) % Natural log of each element in arr1
abs_arr = py.numpy.abs(arr1) % Absolute value of each element in arr1
ceil_arr = py.numpy.ceil(arr1) % Rounds up to the nearest int
floor_arr = py.numpy.floor(arr1) % Rounds down to the nearest int
round_arr = py.numpy.round(arr1) % Rounds to the nearest int
دیدگاهها
هیچ نظری هنوز ثبت نشده است.