You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Using the NAG Library for Python witk Kdb+ and PyQ
1
+
# Using the NAG Library for Python with Kdb+ and PyQ
2
2
3
-
Christopher Brandt <br>
4
-
Numerical Algorithms Group (NAG), Inc. <br>
5
-
Lisle, IL, USA <br>
6
-
April 3, 2019
3
+
Christopher Brandt, Numerical Algorithms Group Inc.
7
4
8
5
## 1 Background
9
6
10
-
This paper provides detailed instructions on how to use the [NAG Library for *Python*](https://www.nag.co.uk/numeric/py/nagdoc_latest/index.html) with [kdb+](https://kx.com/) and [PyQ](https://code.kx.com/q/interfaces/pyq/). The NAG Library contains more than 1,700 mathematical and statistical routines, and is accessible by numerous programming languages (including Python, C++, Java, Fortran, etc.). PyQ is an extension to kdb+ featuring zero-copy sharing of data between Python and the q programming language. The enclosed examples will illustrate how to access routines within the NAG Library for *Python* using data stored in kdb+.
7
+
This paper provides detailed instructions on how to use the [NAG Library for *Python*](https://www.nag.com/numeric/py/nagdoc_latest/index.html) with [kdb+](https://code.kx.com/v2/) and [PyQ](https://code.kx.com/q/interfaces/pyq/). The NAG Library contains more than 1,800 mathematical and statistical routines, and is accessible by numerous programming languages (including Python, C++, Java, Fortran, etc.). PyQ is an extension to kdb+ featuring zero-copy sharing of data between Python and the q programming language. The enclosed examples will illustrate how to access routines within the NAG Library for *Python* using data stored in kdb+.
11
8
12
9
## 2 Setting Up the Workspace
13
10
@@ -16,23 +13,24 @@ Installation for both the NAG Library for *Python* and the PyQ extension to kdb+
Both the NAG Library for *Python* and kdb+ are commercial software packages that require active licenses for their respective usage. To obtain a temporary license for the NAG Library for Python, please contact NAG support at [support@nag.com](mailto:support@nag.com).
23
+
Both the NAG Library for *Python* and kdb+ are commercial software packages that require active licenses for their respective usage. To obtain a temporary license for the NAG Library for *Python*, please contact NAG support at [support@nag.com](mailto:support@nag.com).
26
24
27
25
## 3 Examples
28
26
29
-
The following three examples demonstrate how to call the NAG Library for *Python* routines using kdb+ and PyQ. These examples were carefully selected, as they cover techniques found in the majority of usage cases a customer will encounter across all 1,700+ routines within the library. If your usage case falls outside of these three examples, please contact [NAG support](mailto:support@nag.com) for assistance.
27
+
The following three examples demonstrate how to call the NAG Library for *Python* routines using kdb+ and PyQ. These examples were carefully selected, as they cover techniques found in the majority of usage cases a customer will encounter across all 1,800+ routines within the Library. If your usage case falls outside of these three examples, please contact [NAG support](mailto:support@nag.com) for assistance.
30
28
31
29
### 3.1 Example One: BLAS Routine DAXPY
32
30
33
31
Our first example demonstrates how to perform the linear algebra operation
34
32
35
-
$$ y \coloneqq \alpha x + b $$
33
+
$$ y \coloneqq \alpha x + b.$$
36
34
37
35
Below is the NAG Library for *Python* signature for this routine.
38
36
@@ -46,26 +44,26 @@ Below is the NAG Library for *Python* signature for this routine.
46
44
Returns: y: float, ndarray, shape(n)
47
45
```
48
46
49
-
Within our terminal, we begin by starting a PyQ interaction session.
47
+
Within our terminal, we begin by initiating a PyQ interactive session.
50
48
51
49
```
52
50
$ pyq
53
51
```
54
52
55
-
Next, we import PyQ and the NAG Library for *Python*.
53
+
Next, we import PyQ and the BLAS module of the NAG Library for *Python*.
56
54
57
55
```
58
56
>>> from pyq import q
59
57
>>> from naginterfaces.library import blas
60
58
```
61
59
62
-
We then enter a q environment and define our parameers as q objects.
60
+
We then enter a q environment and define our parameters as q objects.
63
61
64
62
```
65
63
>>> q()
66
64
q) alpha:0.5f
67
-
q) x:(2.0, 2.0, 2.0, 2.0f)
68
-
q) y:(4.0, 4.0, 4.0, 4.0f)
65
+
q) x:4#2 2 2 2f
66
+
q) y:4#4 4 4 4f
69
67
```
70
68
71
69
Finally, we exit the q environment and invoke the NAG routine.
@@ -87,10 +85,11 @@ where $W$ is a diagonal matrix of weights.
87
85
The NAG Library for *Python* signature for this routine is below.
With our final example, we demonstrate how to incorporate a user-defined callback function with a NAG Library for *Python* routine. This example approximates the definite integal
134
+
With our final example, we demonstrate how to incorporate a user-defined callback function with a NAG Library for *Python* routine. This example approximates the definite integral
131
135
132
136
$$ \int_{a}^{b} f(x) dx. $$
133
137
@@ -150,47 +154,53 @@ Returns: result: float
150
154
abserr: float
151
155
```
152
156
153
-
We start by entering a q environment and defining our parameters as q objects.
157
+
We start by importing the Quadrature module of the NAG Library for *Python*.
158
+
159
+
```
160
+
>>> from naginterfaces.library import quad
161
+
```
162
+
163
+
Next, we enter a q environment and define our parameters as q objects.
154
164
155
165
```
156
166
>>> q()
157
-
q) a:0.0f
158
-
q) b:2.0f
159
-
q) epsabs:0.0f
167
+
q) a:0f
168
+
q) b:2f
169
+
q) epsabs:0f
160
170
q) epsrel:0.0001f
161
171
```
162
172
163
-
Next, we exit the q environment and define an integrable Python function. To satisfy this parameter we may use either a Python function or a lambda expression.
173
+
We then exit the q environment and define an integrable Python function. To satisfy this parameter we may use either a Python function or a lambda expression.
164
174
165
175
```
166
176
q) \
167
-
>>> f(x):
168
-
... return x*x
177
+
>>> def f(x):
178
+
return x*x
169
179
...
170
180
```
171
181
172
-
With our problem now fully defined, we invoke the NAG Library routine to compute our solution.
182
+
With our problem now fully defined, we invoke the NAG routine to compute our solution.
NAG recently published the technical report [Using the NAG Library with Kdb+ in a Pure Q Environment](https://www.nag.com/doc/techrep/pdf/tr1_18.pdf) discussing how to call the NAG Library using the new Foreign Function Interface (FFI) from Kx.
184
-
185
-
Additionally, the NAG Blog titled [Calling the NAG C Library from Kdb+](http://blog.nag.com/2013/05/calling-nag-c-library-from-kdb.html) details how to incorporate the NAG Library with kdb+ within a C++ program. We speculate that among our shared clients, a mixture of these methods will be employed.
193
+
NAG recently published the technical report [Using the NAG Library with Kdb+ in a Pure Q Environment](https://www.nag.com/doc/techrep/pdf/tr1_18.pdf) discussing how to call the NAG Library using the new [Foreign Function Interface (FFI)](https://code.kx.com/q/interfaces/ffi/) from Kx. Additionally, the NAG Blog titled [Calling the NAG C Library from Kdb+](http://blog.nag.com/2013/05/calling-nag-c-library-from-kdb.html) details how to incorporate the NAG Library with kdb+ within a C++ program. We speculate that among our shared clients, a mixture of these methods will be employed.
186
194
187
-
If your desired usage case happens to fall outside of those described within our current publications, please contact NAG support at support@nag.com for asisstance with your application.
195
+
If your desired usage case happens to fall outside of those described within our current publications, please contact NAG support at support@nag.com for assistance with your application.
188
196
189
197
## 5 Links
190
198
199
+
* NAG Library for *Python* Manual [https://www.nag.com/numeric/py/nagdoc_latest/index.html](https://www.nag.com/numeric/py/nagdoc_latest/index.html)
200
+
* Get Going with Kdb+ [https://code.kx.com/v2/](https://code.kx.com/v2/)
191
201
* Using Python with kdb+ (PyQ) [https://code.kx.com/q/interfaces/pyq/](https://code.kx.com/q/interfaces/pyq/)
192
-
* Kdb+ and Python: embedPy and PyQ [https://kx.com/blog/kdb-python-embedpy-pyq/]([https://kx.com/blog/kdb-python-embedpy-pyq/)
202
+
* Kdb+ and Python: embedPy and PyQ [https://kx.com/blog/kdb-python-embedpy-pyq/](https://kx.com/blog/kdb-python-embedpy-pyq/)
193
203
* Using the NAG Library with Kdb+ in a Pure Q Environment [https://www.nag.com/doc/techrep/pdf/tr1_18.pdf](https://www.nag.com/doc/techrep/pdf/tr1_18.pdf)
204
+
* Using Foreign Functions with Kdb+ (FFI) [https://code.kx.com/q/interfaces/ffi/](https://code.kx.com/q/interfaces/ffi/)
194
205
* Calling the NAG C Library from Kdb+ [http://blog.nag.com/2013/05/calling-nag-c-library-from-kdb.html](http://blog.nag.com/2013/05/calling-nag-c-library-from-kdb.html)
195
-
* NAG Library for Python Manual [https://www.nag.com/numeric/py/nagdoc_latest/index.html](https://www.nag.com/numeric/py/nagdoc_latest/index.html)
196
206
* NAG GitHub Organisation [https://github.com/numericalalgorithmsgroup/](https://github.com/numericalalgorithmsgroup/)
0 commit comments