Powerful Advanced Scientific Computing – it’s easy!

What is Scientific Computing?

According to Golub and Ortega in 1992, Scientific Computing is the collection of tools, techniques, and theories required to solve mathematical models of problems in Science and Engineering.

A majority of these tools, techniques, and theories are originally developed in Mathematics, which are many of them having their genesis long before the advent of electronic computers.

Computation becomes crucially important in situations such as:

  • The problem at hand cannot be solved by traditional experimental or theoretical means, such as attempting to predict climate change
  • Experimentation may be dangerous, e.g., characterization of toxic materials
  • The problem would be too expensive or time-consuming to try to solve by other avenues, e.g. determination of the structure of proteins

Nowadays, Scientific Computing is acknowledged as the “third pillar of science”, standing right next to theoretical analysis and experiments for scientific discovery.

Why use Python for Scientific Computing?

Here are the 6 reasons to use Python for Scientific Computing:

  1. Python has built-in support for Scientific Computing.
  2. Python is a highly extensible language.
  3. Python has very good input/output (I/O) options.
  4. Python has strong support for task automation.
  5. Python has bridges to MATLAB or Octave.
  6. Python can use a web front end.

Delphi adds Powerful GUI Features and Functionalities to Python

In this tutorial, we’ll build Windows Apps with extensive Scientific Computing capabilities by integrating Python’s Scientific Computing libraries with Embarcadero’s Delphi, using Python4Delphi (P4D).

P4D empowers Python users with Delphi’s award-winning VCL functionalities for Windows which enables us to build native Windows apps 5x faster. This integration enables us to create a modern GUI with Windows 10 looks and responsive controls for our Python Web Scraping applications. Python4Delphi also comes with an extensive range of demos, use cases, and tutorials.

We’re going to cover the following…

How to use NumPy, SciPy, SymPy, scikit-learn, Theano, and Cirq Python libraries to perform Scientific Computing tasks

All of them would be integrated with Python4Delphi to create Windows Apps with Scientific Computing capabilities.

Prerequisites

Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in the Getting Started With Python4Delphi video by Jim McKeeth.

Time to get Started!

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 button, and get the result in the upper Memo. You can find the Demo1 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.

python4delphi run demo01
Open Demo01.dproj.

1. How do I enable NumPy inside Python4Delphi in Windows?

NumPy is a popular Python library used for scientific computing applications. NumPy 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.

After installing Python4Delphi properly, you can get NumPy using pip or easy install to your command prompt:

and don’t forget to put the path where your NumPy installed, to the System Environment Variables:

System Environment Variable Examples

The following is a code example of NumPy to perform Basic Array Operations. We will perform sorting, concatenate, and reshaping the dimension of the array (run this inside the lower Memo of Python4Delphi Demo01 GUI):

Here is the result in Python GUI

NumPy Demo with Python4Delphi in Windows.
NumPy Demo with Python4Delphi in Windows.

Read more: https://pythongui.org/learn-to-build-a-python-gui-for-scientific-computing-with-the-numpy-library-in-a-delphi-windows-app/

2. How do I enable SciPy for Scientific Computing inside Python4Delphi in Windows?

If you need a Python-based ecosystem of open-source software for mathematics, science, and engineering, then Python’s SciPy (pronounced “Sigh Pie”) library is exactly what you need.

SciPy contains modules for optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers, and other tasks common in science and engineering.

SciPy is a collection of mathematical algorithms and convenience functions built on the NumPy extension of Python. It adds significant power to the interactive Python session by providing the user with high-level commands and classes for manipulating and visualizing data.

Are you looking for a Python-based ecosystem of open-source software for mathematics, science, and engineering, and build a nice Windows GUI for them? This section will show you how to get started!

First, here is how you can get SciPy:

The following  is a code example of SciPy for curve fitting (run this inside the lower Memo of Python4Delphi Demo01 GUI):

Here is the SciPy result in the Python GUI:

SciPy Demo with Python4Delphi in Windows.
SciPy Demo with Python4Delphi in Windows.

Read more: https://pythongui.org/learn-to-build-a-python-gui-for-scientific-computing-with-the-scipy-library-in-a-delphi-windows-app/

3. How do I Solve Mathematical Equations with SymPy in Windows?

SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python.

Symbolic computation deals with the computation of mathematical objects symbolically. This means that the mathematical objects are represented exactly, not approximately, and mathematical expressions with unevaluated variables are left in symbolic form.

SymPy can simplify expressions, compute derivatives, integrals, and limits, solve equations, work with matrices, and much, much more, and do it all symbolically. SymPy includes modules for plotting, printing (like 2D pretty printed output of math formulas, or LATEX), code generation, physics, statistics, combinatorics, number theory, geometry, logic, and more.

This section will guide you to combine Python4Delphi with the SymPy library, inside Delphi and C++Builder, from installing SymPy with pip to solve several introductory calculus!

First, here is how you can get SymPy:

The following  is a code example of SymPy to solve calculus symbolically: From differential, indefinite & definite integral, limit, quadratic equation, and solve differential equation (run this inside the lower Memo of Python4Delphi Demo01 GUI):

SymPy Python4Delphi results

SymPy Demo with Python4Delphi in Windows.
SymPy Demo with Python4Delphi in Windows.

4. How do I Perform Predictive Data Analysis using Python scikit-learn?

scikit-learn is an open-source Python library for Machine Learning. Scikit-Learn has simple and efficient tools for predictive data analysis that are built on top of SciPy, NumPy, and Matplotlib.

Scikit-Learn features various classification, regression, and clustering algorithms including support vector machines, random forests, gradient boosting, k-means, and DBSCAN.

First, here is how you can get scikit-learn:

The following is a code example of scikit-learn to  compare several classifiers in scikit-learn on synthetic datasets (run this inside the lower Memo of Python4Delphi Demo01 GUI):

scikit-learn Classifier Comparisons Result

scikit-learn Demo with Python4Delphi in Windows.
scikit-learn Demo with Python4Delphi in Windows.

The above code for comparing classifiers is credited to Gaël Varoquaux, Andreas Müller, and Jaques Grobler (visit the source here).

Notes about the result:

The intuition conveyed by these examples does not necessarily carry over to real datasets. Particularly in high-dimensional spaces, data can more easily be separated linearly and the simplicity of classifiers such as naive Bayes and linear SVMs might lead to better generalization than is achieved by other classifiers.

5. How do I Perform Multidimensional Array Computation with Theano in Windows?

Theano is a Python library that allows you to define, optimize, and efficiently evaluate mathematical expressions involving multi-dimensional arrays. It is built on top of NumPy.

The following are Theano features:

  • Tight integration with NumPy: A similar interface to NumPy’s. numpy.ndarrays are also used internally in Theano-compiled functions
  • Transparent use of a GPU: Perform data-intensive computations up to 140x faster than on a CPU (support for float32 only)
  • Efficient symbolic differentiation: Theano can compute derivatives for functions of one or many inputs
  • Speed and stability optimizations: Avoid nasty bugs when computing expressions such as log(1 + exp(x)) for large values of x
  • Dynamic C code generation: Evaluate expressions faster
  • Extensive unit-testing and self-verification: Includes tools for detecting and diagnosing bugs and/or potential problems

Run this pip command to install Theano:

The following is a Theano code example to use gradient descent to train weights in W so that we get better results from the model than existing (run this inside the lower Memo of Python4Delphi Demo01 GUI):

Theano results in the Python4Delphi GUI

Theano Demo with Python4Delphi in Windows.
Theano Demo with Python4Delphi in Windows.

6. How do I enable Cirq for Programming Quantum Computers inside Python4Delphi in Windows?

The quantum computing research field has been booming in the past years with exponential growth in research papers, capital investment, and a growing number of startups. You can start programming quantum computers using Cirq, which is developed by Google.

Cirq is a Python library for writing, manipulating, and optimizing quantum circuits and running them on quantum computers and simulators.

Cirq provides useful abstractions for dealing with today’s noisy intermediate-scale quantum computers, where details of the hardware are vital to achieving state-of-the-art results.

Run this pip command to install Cirq:

The following is a Hello World with Cirq code example (run this inside the lower Memo of Python4Delphi Demo01 GUI):

Cirq results in the Python4Delphi GUI

Cirq Demo with Python4Delphi in Windows.
Cirq Demo with Python4Delphi in Windows.

Want to know some more? Then check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.