If you need to perform complex transformations or mathematical calculations with matrices or arrays, then Python’s Numpy library is exactly what you need. You can easily run this library and give it a nice GUI using Python4Delphi (P4D). Python4Delphi is a free tool that allows you to work with Python scripts and objects, even create new Python modules and types in the Windows GUI.

The **NumPy** library is a popular Python library used for scientific computing applications, and is an acronym for “Numerical Python”. NumPy is the fundamental package for array computing with Python.

NumPy’s operations are divided into three main categories: Fourier Transform and Shape Manipulation, Mathematical and Logical Operations, and Linear Algebra and Random Number Generation. To make it as fast as possible, NumPy is written in C and Python.

Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.

This post will guide you on **how to run the NumPy library using Python for Delphi to display it in the Delphi Windows GUI app**.

First, open and run our Python GUI using project ** Demo1 **from Python4Delphi with RAD Studio. Then insert the script into the lower

`Memo`

, click the `Execute script`

button, and get the result in the upper Memo. You can find the **source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.**

`Demo1`

Table of Contents

## 1. Create a Matrix and Get Some Properties

This example shows how to create a 3-dimensional array and fill it with numbers from `0`

to `29`

. Then, using the properties of this array, we can find out its shape, dimension, data type, number of elements:

1 2 3 4 5 6 7 8 9 10 |
import numpy as np a = np.arange(30).reshape(2, 3, 5) print(a) print(a.shape) print(a.ndim) print(a.dtype.name) print(a.itemsize) print(a.size) |

Let’s see the output in our P4D VCL:

## 2. Basic Operations with Arrays

Let’s take a look at the simplest conversions you can perform on arrays. Using `concatenate()`

function, you can combine the values of two arrays into one. With function `sort()`

you can sort ascending the values in an array. Function `reshape()`

allows you to change the dimension of the array:

1 2 3 4 5 6 7 8 9 10 11 |
import numpy as np arr = np.array([7, 10, 3, 11, 29, 15, 18]) print(np.sort(arr)) a = np.array([1, 2, 3, 4, 5, 6]) b = np.array([7, 8, 9, 10, 11, 12]) print(np.concatenate((a, b))) c = a.reshape(3, 2) print(c) |

The result:

## 3. Create an Array of All Zeros

We can use the `zeros`

method to create an array of all zeros as shown below:

1 2 3 4 5 6 7 8 9 |
import numpy as np # 1-Dim array of 5 zeros zeros1 = np.zeros(5) print(zeros1) # 2-Dim array of 7 rows and 7 columns: zeros2 = np.zeros((7, 7)) print(zeros2) |

The result in Python GUI:

Congratulations, now you have learned **how to run the NumPy library using Python for Delphi to display it in the Delphi Windows GUI app**! These are only some basic examples of what Numpy library allows us to do.

Check out the ** numpy** for numerical computing library for Python and use it in your projects: https://pypi.org/project/numpy/ and

Check out ** Python4Delphi** which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi

**References & further readings**

[1] Malik, U. (2018). *NumPy Tutorial: A Simple Example-Based Guide.* StackAbuse. stackabuse.com/numpy-tutorial-a-simple-example-based-guide

[2] NumPy (2023). *NumPy: The fundamental package for scientific computing with Python. *NumPy Website. numpy.org

[3] PyPI (Python Package Index). (2023). *numpy 1.24.2: Fundamental package for array computing in Python.* Python Software Foundation. pypi.org/project/numpy