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:
- Python has built-in support for Scientific Computing.
- Python is a highly extensible language.
- Python has very good input/output (I/O) options.
- Python has strong support for task automation.
- Python has bridges to MATLAB or Octave.
- 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.
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
C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38/Lib/site-packages C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38/Scripts C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38 |
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):
arr = np.array([7, 10, 3, 11, 29, 15, 18])
# Sort ascending the values in an array
print(np.sort(arr))
a = np.array([1, 2, 3, 4, 5, 6])
b = np.array([7, 8, 9, 10, 11, 12])
# Combine the values of two arrays into one
print(np.concatenate((a, b)))
# Change the dimension of the array
c = a.reshape(3, 2)
print(c)
import numpy as np
arr = np.array([7, 10, 3, 11, 29, 15, 18]) # Sort ascending the values in an array print(np.sort(arr))
a = np.array([1, 2, 3, 4, 5, 6]) b = np.array([7, 8, 9, 10, 11, 12]) # Combine the values of two arrays into one print(np.concatenate((a, b)))
# Change the dimension of the array c = a.reshape(3, 2) print(c) |
Here is the result in Python GUI
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):
np.random.seed(0)
# Our test function
def f(t, omega, phi):
return np.cos(omega * t + phi)
# Our x and y data
x = np.linspace(0, 3, 50)
y = f(x, 1.5, 1) + .1*np.random.normal(size=50)
# Fit the model: the parameters omega and phi can be found in the
# ‘params’ vector
params, params_cov = optimize.curve_fit(f, x, y)
# plot the data and the fitted curve
t = np.linspace(0, 3, 1000)
plt.figure(1)
plt.clf()
plt.plot(x, y, ‘bx’)
plt.plot(t, f(t, *params), ‘r-‘)
plt.show()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import numpy as np from scipy import optimize import matplotlib.pyplot as plt
np.random.seed(0)
# Our test function def f(t, omega, phi): return np.cos(omega * t + phi)
# Our x and y data x = np.linspace(0, 3, 50) y = f(x, 1.5, 1) + .1*np.random.normal(size=50)
# Fit the model: the parameters omega and phi can be found in the # ‘params’ vector params, params_cov = optimize.curve_fit(f, x, y)
# plot the data and the fitted curve t = np.linspace(0, 3, 1000)
plt.figure(1) plt.clf() plt.plot(x, y, ‘bx’) plt.plot(t, f(t, *params), ‘r-‘) plt.show() |
Here is the SciPy result in the Python GUI:
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):
x, t, z, nu = symbols(‘x t z nu’)
init_printing(use_unicode=True)
# Solve differential
print(diff(sin(x)*exp(x), x))
# Solve indefinite integral
print(integrate(exp(x)*sin(x) + exp(x)*cos(x), x))
# Solve definite integral
print(integrate(sin(x**2), (x, -oo, oo)))
# Solve limit
print(limit(sin(x)/x, x, 0))
# Solve quadratic equation
print(solve(x**2 – 2, x))
# Solve differential equation
y = Function(‘y’)
print(dsolve(Eq(y(t).diff(t, t) – y(t), exp(t)), y(t)))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from sympy import *
x, t, z, nu = symbols(‘x t z nu’) init_printing(use_unicode=True)
# Solve differential print(diff(sin(x)*exp(x), x))
# Solve indefinite integral print(integrate(exp(x)*sin(x) + exp(x)*cos(x), x))
# Solve definite integral print(integrate(sin(x**2), (x, –oo, oo)))
# Solve limit print(limit(sin(x)/x, x, 0))
# Solve quadratic equation print(solve(x**2 – 2, x))
# Solve differential equation y = Function(‘y’) print(dsolve(Eq(y(t).diff(t, t) – y(t), exp(t)), y(t))) |
SymPy Python4Delphi results
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:
pip install -U 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):
h = .02 # step size in the mesh
names = [“Nearest Neighbors”, “Linear SVM”, “RBF SVM”, “Gaussian Process”,
“Decision Tree”, “Random Forest”, “Neural Net”, “AdaBoost”,
“Naive Bayes”, “QDA”]
classifiers = [
KNeighborsClassifier(3),
SVC(kernel=”linear”, C=0.025),
SVC(gamma=2, C=1),
GaussianProcessClassifier(1.0 * RBF(1.0)),
DecisionTreeClassifier(max_depth=5),
RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
MLPClassifier(alpha=1, max_iter=1000),
AdaBoostClassifier(),
GaussianNB(),
QuadraticDiscriminantAnalysis()]
X, y = make_classification(n_features=2, n_redundant=0, n_informative=2,
random_state=1, n_clusters_per_class=1)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(size=X.shape)
linearly_separable = (X, y)
datasets = [make_moons(noise=0.3, random_state=0),
make_circles(noise=0.2, factor=0.5, random_state=1),
linearly_separable
]
figure = plt.figure(figsize=(27, 9))
i = 1
# iterate over datasets
for ds_cnt, ds in enumerate(datasets):
# preprocess dataset, split into training and test part
X, y = ds
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test =
train_test_split(X, y, test_size=.4, random_state=42)
x_min, x_max = X[:, 0].min() – .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() – .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
# just plot the dataset first
cm = plt.cm.RdBu
cm_bright = ListedColormap([‘#FF0000’, ‘#0000FF’])
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
if ds_cnt == 0:
ax.set_title(“Input data”)
# Plot the training points
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright,
edgecolors=’k’)
# Plot the testing points
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6,
edgecolors=’k’)
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xticks(())
ax.set_yticks(())
i += 1
# iterate over classifiers
for name, clf in zip(names, classifiers):
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
if hasattr(clf, “decision_function”):
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
else:
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
# Put the result into a color plot
Z = Z.reshape(xx.shape)
ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)
# Plot the training points
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright,
edgecolors=’k’)
# Plot the testing points
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright,
edgecolors=’k’, alpha=0.6)
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xticks(())
ax.set_yticks(())
if ds_cnt == 0:
ax.set_title(name)
ax.text(xx.max() – .3, yy.min() + .3, (‘%.2f’ % score).lstrip(‘0′),
size=15, horizontalalignment=’right’)
i += 1
plt.tight_layout()
plt.show()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.datasets import make_moons, make_circles, make_classification from sklearn.neural_network import MLPClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from sklearn.gaussian_process import GaussianProcessClassifier from sklearn.gaussian_process.kernels import RBF from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier from sklearn.naive_bayes import GaussianNB from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
h = .02 # step size in the mesh
names = [“Nearest Neighbors”, “Linear SVM”, “RBF SVM”, “Gaussian Process”, “Decision Tree”, “Random Forest”, “Neural Net”, “AdaBoost”, “Naive Bayes”, “QDA”]
classifiers = [ KNeighborsClassifier(3), SVC(kernel=“linear”, C=0.025), SVC(gamma=2, C=1), GaussianProcessClassifier(1.0 * RBF(1.0)), DecisionTreeClassifier(max_depth=5), RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1), MLPClassifier(alpha=1, max_iter=1000), AdaBoostClassifier(), GaussianNB(), QuadraticDiscriminantAnalysis()]
X, y = make_classification(n_features=2, n_redundant=0, n_informative=2, random_state=1, n_clusters_per_class=1) rng = np.random.RandomState(2) X += 2 * rng.uniform(size=X.shape) linearly_separable = (X, y)
datasets = [make_moons(noise=0.3, random_state=0), make_circles(noise=0.2, factor=0.5, random_state=1), linearly_separable ]
figure = plt.figure(figsize=(27, 9)) i = 1 # iterate over datasets for ds_cnt, ds in enumerate(datasets): # preprocess dataset, split into training and test part X, y = ds X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.4, random_state=42)
x_min, x_max = X[:, 0].min() – .5, X[:, 0].max() + .5 y_min, y_max = X[:, 1].min() – .5, X[:, 1].max() + .5 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# just plot the dataset first cm = plt.cm.RdBu cm_bright = ListedColormap([‘#FF0000’, ‘#0000FF’]) ax = plt.subplot(len(datasets), len(classifiers) + 1, i) if ds_cnt == 0: ax.set_title(“Input data”) # Plot the training points ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright, edgecolors=‘k’) # Plot the testing points ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6, edgecolors=‘k’) ax.set_xlim(xx.min(), xx.max()) ax.set_ylim(yy.min(), yy.max()) ax.set_xticks(()) ax.set_yticks(()) i += 1
# iterate over classifiers for name, clf in zip(names, classifiers): ax = plt.subplot(len(datasets), len(classifiers) + 1, i) clf.fit(X_train, y_train) score = clf.score(X_test, y_test)
# Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, x_max]x[y_min, y_max]. if hasattr(clf, “decision_function”): Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) else: Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
# Put the result into a color plot Z = Z.reshape(xx.shape) ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)
# Plot the training points ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright, edgecolors=‘k’) # Plot the testing points ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, edgecolors=‘k’, alpha=0.6)
ax.set_xlim(xx.min(), xx.max()) ax.set_ylim(yy.min(), yy.max()) ax.set_xticks(()) ax.set_yticks(()) if ds_cnt == 0: ax.set_title(name) ax.text(xx.max() – .3, yy.min() + .3, (‘%.2f’ % score).lstrip(‘0’), size=15, horizontalalignment=‘right’) i += 1
plt.tight_layout() plt.show() |
scikit-learn Classifier Comparisons Result
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):
# declare variables
x = theano.tensor.fvector(‘x’)
target = theano.tensor.fscalar(‘target’)
W = theano.shared(numpy.asarray([0.2, 0.7]), ‘W’)
# create expressions
y = (x * W).sum()
cost = theano.tensor.sqr(target – y)
gradients = theano.tensor.grad(cost, [W])
W_updated = W – (0.1 * gradients[0])
updates = [(W, W_updated)]
# create a callable object from expression
f = theano.function([x, target], y, updates=updates)
# call the function and print results
for i in range(10):
result = f([1.0, 1.0], 20.0)
print(result)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# Minimal Training Theano Example import theano import numpy
# declare variables x = theano.tensor.fvector(‘x’) target = theano.tensor.fscalar(‘target’) W = theano.shared(numpy.asarray([0.2, 0.7]), ‘W’)
# create expressions y = (x * W).sum() cost = theano.tensor.sqr(target – y) gradients = theano.tensor.grad(cost, [W])
W_updated = W – (0.1 * gradients[0]) updates = [(W, W_updated)]
# create a callable object from expression f = theano.function([x, target], y, updates=updates)
# call the function and print results for i in range(10): result = f([1.0, 1.0], 20.0) print(result) |
Theano results in the Python4Delphi GUI
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):
# Pick a qubit.
qubit = cirq.GridQubit(0, 0)
# Create a circuit
circuit = cirq.Circuit(
cirq.X(qubit)**0.5, # Square root of NOT.
cirq.measure(qubit, key=’m’) # Measurement.
)
print(“Circuit:”)
print(circuit)
# Simulate the circuit several times.
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=20)
print(“Results:”)
print(result)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import cirq
# Pick a qubit. qubit = cirq.GridQubit(0, 0)
# Create a circuit circuit = cirq.Circuit( cirq.X(qubit)**0.5, # Square root of NOT. cirq.measure(qubit, key=‘m’) # Measurement. ) print(“Circuit:”) print(circuit)
# Simulate the circuit several times. simulator = cirq.Simulator() result = simulator.run(circuit, repetitions=20) print(“Results:”) print(result) |
Cirq results in the Python4Delphi GUI
Want to know some more? Then check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.