![developer9 | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/developer9.jpg?resize=1140%2C694&ssl=1)
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.
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 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 |
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 default_rng() 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.
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) |
![numpy3 | Embarcadero RAD Studio Delphi C++Builder Blogs numpy3](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/11/numpy3-1192755.png?resize=750%2C381&ssl=1)
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?
I have a similar question to that of mr. Sauro. I use SciKit Learn to perform KMeans clustering. In the Python environment everything works as intended. The resulting classification is returned by Sklearn as ‘kmeans.labels_’ (numpy data type ‘ndarray’). I now want to import this numpy array back into Delphi. What would be the best way to do this?