If you need to perform complex transformations or mathematical calculations with matrices or arrays, then Python Numpy library is exactly what you need. You can easy run this library with Python4Delphi (P4D). Python4Delphi is a free tool with which you can work with Python scripts and objects in the Windows GUI. In this post, we will look at how to run Numpy library with P4D.
Delphi itself offers a number of matrix multiplication features in TMatrix. If you have an existing Python application and you need matrix multiplication you could use the Numpy library or you could send the data to Delphi and do the multiplication there. Alternately, you could do all of your calculations in Numpy and then display it in the Delphi Windows GUI app. You can use Python4Delphi a number of different ways such as:

Numpy library allows you to create multidimensional arrays and matrices and work with their properties. It also contains various functions for processing arrays and matrices. Let’s look at some simple examples of working with Numpy. If you have
trouble compiling the Python you may need to have Numpy 1.19.3 installed.

And this is only a small part of what Numpy library allows you to do. Download
Numpy and check how many possibilities it opens for your applications. Install Python4Delphi for building Python GUIs for Windows using Delphi easily.

- Create a Windows GUI around you existing Python app.
- Add Python scripting to your Delphi Windows apps.
- Add parallel processing to your Python apps through Delphi threads.
- Enhance your speed sensitive Python apps with functions from Delphi for more speed.

**Demo1**project. Then paste the Python script into the text field, press**Execute**button and get the result. Go to GitHub to download**Demo1**.
1 2 3 4 |
procedure TForm1.Button1Click(Sender: TObject); begin PythonEngine1.ExecStrings( Memo1.Lines ); end; |

### 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 |
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) |

### Basic operations with arrays

Let’s take a look at the simplest conversions you can perform on arrays. Using*function, you can combine the values of two arrays into one. With function***concatenate****()***you can sort ascending the values in an array. Function***sort()***allows you to change the dimension of the array.***reshape()**
1 2 3 4 5 6 7 8 |
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) |

### Mathematical operations with matrix

Function*allows you to fill a matrix with random values. You can use integer or real numbers. In this example, we fill the matrix with integer values. Then we find the maximum and minimum element, the sum of all the elements in the matrix. It is also shown how you can multiply a matrix by a number and sum two matrices with the same dimension.***default_rng()**
1 2 3 4 5 6 7 8 9 10 11 12 |
import numpy as np from numpy.random import default_rng rng = default_rng() arr=rng.integers(20, size=(2, 4)) print(arr) print(arr.max()) print(arr.min()) print(arr.sum()) print(arr*2) arr2=rng.integers(5, size=(2, 4)) print(arr2) print(arr+arr2) |

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.

Design. Code. Compile. Deploy.

Start Free Trial Upgrade Today

Free Delphi Community Edition Free C++Builder Community Edition

Design. Code. Compile. Deploy.

Start Free Trial Upgrade Today

Free Delphi Community Edition Free C++Builder Community Edition

Can you bring values computed by numpy back to Delphi? And in reverse can you send values from Delphi to numpy?