klongpy 0.6.9__tar.gz → 0.7.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- klongpy-0.7.1/MANIFEST.in +5 -0
- klongpy-0.7.1/PKG-INFO +544 -0
- klongpy-0.7.1/README.md +487 -0
- klongpy-0.7.1/klongpy/__init__.py +18 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/adverbs.py +84 -82
- klongpy-0.7.1/klongpy/autograd.py +299 -0
- klongpy-0.7.1/klongpy/backend.py +38 -0
- klongpy-0.7.1/klongpy/backends/__init__.py +26 -0
- klongpy-0.7.1/klongpy/backends/base.py +469 -0
- klongpy-0.7.1/klongpy/backends/numpy_backend.py +123 -0
- klongpy-0.7.1/klongpy/backends/registry.py +76 -0
- klongpy-0.7.1/klongpy/backends/torch_backend.py +1047 -0
- klongpy-0.6.9/scripts/kgpy → klongpy-0.7.1/klongpy/cli.py +110 -90
- klongpy-0.7.1/klongpy/core.py +113 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/db/sys_fn_db.py +7 -6
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/db/sys_fn_kvs.py +2 -4
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/dyads.py +332 -160
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/interpreter.py +60 -15
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/monads.py +121 -75
- klongpy-0.7.1/klongpy/parser.py +328 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/repl.py +23 -5
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/sys_fn.py +170 -21
- klongpy-0.7.1/klongpy/sys_fn_autograd.py +290 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/sys_fn_ipc.py +22 -15
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/sys_fn_timer.py +13 -3
- klongpy-0.7.1/klongpy/types.py +503 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/web/sys_fn_web.py +14 -4
- klongpy-0.7.1/klongpy/writer.py +122 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/ws/sys_fn_ws.py +5 -8
- klongpy-0.7.1/klongpy.egg-info/PKG-INFO +544 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy.egg-info/SOURCES.txt +27 -19
- klongpy-0.7.1/klongpy.egg-info/entry_points.txt +2 -0
- klongpy-0.7.1/klongpy.egg-info/requires.txt +39 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy.egg-info/top_level.txt +0 -1
- klongpy-0.7.1/pyproject.toml +83 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_accel.py +14 -7
- klongpy-0.7.1/tests/test_autograd_parametrized.py +555 -0
- klongpy-0.7.1/tests/test_backend.py +405 -0
- klongpy-0.7.1/tests/test_backend_abstraction.py +31 -0
- klongpy-0.7.1/tests/test_cli_exit.py +49 -0
- klongpy-0.7.1/tests/test_core_fn_wrapper.py +48 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_eval_monad_list.py +7 -4
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_examples.py +2 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_extra_suite.py +64 -35
- klongpy-0.7.1/tests/test_helpers.py +103 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_interop.py +12 -4
- klongpy-0.7.1/tests/test_kg_asarray.py +126 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_kgtests.py +3 -2
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_known_bugs.py +7 -7
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_prog.py +3 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_reshape_strings.py +7 -6
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_suite.py +29 -3
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_suite_file.py +10 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_sys_fn.py +9 -4
- klongpy-0.7.1/tests/test_sys_fn_coverage.py +300 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_sys_fn_db.py +6 -4
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_sys_fn_ipc.py +54 -0
- klongpy-0.7.1/tests/test_sys_fn_ipc_coverage.py +151 -0
- klongpy-0.7.1/tests/test_sys_fn_kvs.py +161 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_sys_fn_timer.py +52 -1
- klongpy-0.7.1/tests/test_sys_fn_web.py +86 -0
- klongpy-0.7.1/tests/test_sys_fn_web_coverage.py +69 -0
- klongpy-0.7.1/tests/test_sys_fn_ws.py +54 -0
- klongpy-0.7.1/tests/test_sys_fn_ws_coverage.py +184 -0
- klongpy-0.7.1/tests/test_torch_backend.py +323 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_util.py +32 -14
- klongpy-0.7.1/tests/test_utils_coverage.py +82 -0
- klongpy-0.6.9/MANIFEST.in +0 -2
- klongpy-0.6.9/PKG-INFO +0 -448
- klongpy-0.6.9/README.md +0 -394
- klongpy-0.6.9/klongpy/__init__.py +0 -2
- klongpy-0.6.9/klongpy/backend.py +0 -103
- klongpy-0.6.9/klongpy/core.py +0 -974
- klongpy-0.6.9/klongpy.egg-info/PKG-INFO +0 -448
- klongpy-0.6.9/klongpy.egg-info/not-zip-safe +0 -1
- klongpy-0.6.9/klongpy.egg-info/requires.txt +0 -45
- klongpy-0.6.9/setup.py +0 -47
- klongpy-0.6.9/tests/__init__.py +0 -6
- klongpy-0.6.9/tests/gen_join_over.py +0 -119
- klongpy-0.6.9/tests/gen_py_suite.py +0 -77
- klongpy-0.6.9/tests/gen_test_fn.py +0 -259
- klongpy-0.6.9/tests/perf_async.py +0 -25
- klongpy-0.6.9/tests/perf_avg.py +0 -18
- klongpy-0.6.9/tests/perf_duckdb.py +0 -32
- klongpy-0.6.9/tests/perf_gen.py +0 -38
- klongpy-0.6.9/tests/perf_ipc_overhead.py +0 -34
- klongpy-0.6.9/tests/perf_join.py +0 -53
- klongpy-0.6.9/tests/perf_load.py +0 -17
- klongpy-0.6.9/tests/perf_prog.py +0 -18
- klongpy-0.6.9/tests/perf_serdes.py +0 -52
- klongpy-0.6.9/tests/perf_sys_fn_db.py +0 -263
- klongpy-0.6.9/tests/perf_vector.py +0 -40
- klongpy-0.6.9/tests/test_kg_asarray.py +0 -94
- klongpy-0.6.9/tests/test_sys_fn_web.py +0 -50
- klongpy-0.6.9/tests/utils.py +0 -126
- {klongpy-0.6.9 → klongpy-0.7.1}/LICENSE +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/db/__init__.py +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/db/df_cache.py +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/db/file_cache.py +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/db/helpers.py +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/csv.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/edt.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/eigenv.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/help.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/huffman.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/math.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/nstat.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/print.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/set.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/spline.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/time.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/lib/util.kg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/sys_var.py +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/utils.py +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/web/__init__.py +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy/ws/__init__.py +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/klongpy.egg-info/dependency_links.txt +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/setup.cfg +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_df_cache.py +0 -0
- {klongpy-0.6.9 → klongpy-0.7.1}/tests/test_file_cache.py +0 -0
klongpy-0.7.1/PKG-INFO
ADDED
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: klongpy
|
|
3
|
+
Version: 0.7.1
|
|
4
|
+
Summary: High-Performance Klong array language with rich Python integration.
|
|
5
|
+
Author: Brian Guarraci
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://klongpy.org
|
|
8
|
+
Project-URL: Repository, https://github.com/briangu/klongpy
|
|
9
|
+
Project-URL: Documentation, https://klongpy.org
|
|
10
|
+
Keywords: klong,array,language,interpreter,numpy,torch
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering
|
|
21
|
+
Classifier: Topic :: Software Development :: Interpreters
|
|
22
|
+
Requires-Python: <3.15,>=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: numpy>=2.0
|
|
26
|
+
Provides-Extra: torch
|
|
27
|
+
Requires-Dist: torch; extra == "torch"
|
|
28
|
+
Provides-Extra: repl
|
|
29
|
+
Requires-Dist: colorama==0.4.6; extra == "repl"
|
|
30
|
+
Provides-Extra: web
|
|
31
|
+
Requires-Dist: aiohttp==3.9.4; extra == "web"
|
|
32
|
+
Provides-Extra: db
|
|
33
|
+
Requires-Dist: pandas==3.0.0; extra == "db"
|
|
34
|
+
Requires-Dist: duckdb==1.4.4; extra == "db"
|
|
35
|
+
Provides-Extra: ws
|
|
36
|
+
Requires-Dist: websockets==12.0; extra == "ws"
|
|
37
|
+
Provides-Extra: docs
|
|
38
|
+
Requires-Dist: mkdocs>=1.6; extra == "docs"
|
|
39
|
+
Requires-Dist: mkdocs-material>=9.7; extra == "docs"
|
|
40
|
+
Provides-Extra: all
|
|
41
|
+
Requires-Dist: torch; extra == "all"
|
|
42
|
+
Requires-Dist: colorama==0.4.6; extra == "all"
|
|
43
|
+
Requires-Dist: aiohttp==3.9.4; extra == "all"
|
|
44
|
+
Requires-Dist: pandas==3.0.0; extra == "all"
|
|
45
|
+
Requires-Dist: duckdb==1.4.4; extra == "all"
|
|
46
|
+
Requires-Dist: websockets==12.0; extra == "all"
|
|
47
|
+
Requires-Dist: mkdocs>=1.6; extra == "all"
|
|
48
|
+
Requires-Dist: mkdocs-material>=9.7; extra == "all"
|
|
49
|
+
Provides-Extra: dev
|
|
50
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
51
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == "dev"
|
|
52
|
+
Requires-Dist: mkdocs>=1.6; extra == "dev"
|
|
53
|
+
Requires-Dist: mkdocs-material>=9.7; extra == "dev"
|
|
54
|
+
Requires-Dist: build>=1.2; extra == "dev"
|
|
55
|
+
Requires-Dist: twine>=5.1; extra == "dev"
|
|
56
|
+
Dynamic: license-file
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+

|
|
60
|
+
[](https://img.shields.io/github/last-commit/briangu/klongpy)
|
|
61
|
+
[](https://libraries.io/github/briangu/klongpy)
|
|
62
|
+
[](https://github.com/briangu/klongpy/issues)
|
|
63
|
+
[](https://img.shields.io/github/repo-size/briangu/klongpy)
|
|
64
|
+
[](https://star-history.com/#briangu/klongpy)
|
|
65
|
+
|
|
66
|
+
[](https://github.com/briangu/klongpy/releases)
|
|
67
|
+
[](https://pepy.tech/project/klongpy)
|
|
68
|
+
[](https://opensource.org/licenses/MIT)
|
|
69
|
+
|
|
70
|
+
# KlongPy: A High-Performance Array Language with Autograd
|
|
71
|
+
|
|
72
|
+
KlongPy is a Python adaptation of the [Klong](https://t3x.org/klong) [array language](https://en.wikipedia.org/wiki/Array_programming), offering high-performance vectorized operations. It prioritizes compatibility with Python, thus allowing seamless integration of Python's expansive ecosystem while retaining Klong's succinctness.
|
|
73
|
+
|
|
74
|
+
KlongPy backends include [NumPy](https://numpy.org/) and optional [PyTorch](https://pytorch.org/) (CPU, CUDA, and Apple MPS).
|
|
75
|
+
When PyTorch is enabled, automatic differentiation (autograd) is supported; otherwise, numeric differentiation is the default.
|
|
76
|
+
|
|
77
|
+
Full documentation: [https://klongpy.org](https://klongpy.org)
|
|
78
|
+
|
|
79
|
+
New to v0.7.0, KlongPy now brings gradient-based programming to an already-succinct array language, so you can differentiate compact array expressions directly. It's also a batteries-included system with IPC, DuckDB-backed database tooling, web/websocket support, and other integrations exposed seamlessly from the language.
|
|
80
|
+
|
|
81
|
+
Backends include NumPy and optional PyTorch (CPU, CUDA, and Apple MPS). When PyTorch is enabled, gradients use autograd; otherwise numeric differentiation is the default.
|
|
82
|
+
|
|
83
|
+
**PyTorch gradient descent (10+ lines):**
|
|
84
|
+
```python
|
|
85
|
+
import torch
|
|
86
|
+
x = torch.tensor(5.0, requires_grad=True)
|
|
87
|
+
optimizer = torch.optim.SGD([x], lr=0.1)
|
|
88
|
+
for _ in range(100):
|
|
89
|
+
loss = x ** 2
|
|
90
|
+
optimizer.zero_grad()
|
|
91
|
+
loss.backward()
|
|
92
|
+
optimizer.step()
|
|
93
|
+
print(x) # ~0
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**KlongPy gradient descent (2 lines):**
|
|
97
|
+
```klong
|
|
98
|
+
f::{x^2}; s::5.0
|
|
99
|
+
{s::s-(0.1*f:>s)}'!100 :" s -> 0"
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Array languages like APL, K, and Q revolutionized finance by treating operations as data transformations, not loops. KlongPy brings this philosophy to machine learning: gradients become expressions you compose, not boilerplate you maintain. The result is a succint mathematical-like notation that is automatically extended to machine learning.
|
|
103
|
+
|
|
104
|
+
## Quick Install
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
# REPL + NumPy backend (pick one option below)
|
|
108
|
+
pip install "klongpy[repl]"
|
|
109
|
+
kgpy
|
|
110
|
+
|
|
111
|
+
# Enable torch backend (autograd + GPU)
|
|
112
|
+
pip install "klongpy[torch]"
|
|
113
|
+
kgpy --backend torch
|
|
114
|
+
|
|
115
|
+
# Everything (web, db, websockets, torch, repl)
|
|
116
|
+
pip install "klongpy[all]"
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## REPL
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
$ kgpy
|
|
123
|
+
Welcome to KlongPy REPL v0.7.0
|
|
124
|
+
Author: Brian Guarraci
|
|
125
|
+
Web: http://klongpy.org
|
|
126
|
+
Backend: torch (mps)
|
|
127
|
+
]h for help; Ctrl-D or ]q to quit
|
|
128
|
+
|
|
129
|
+
$>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Why KlongPy?
|
|
133
|
+
|
|
134
|
+
### For Quants and Traders
|
|
135
|
+
|
|
136
|
+
Optimize portfolios with gradients in a language designed for arrays:
|
|
137
|
+
|
|
138
|
+
```klong
|
|
139
|
+
:" Portfolio optimization: gradient of Sharpe ratio"
|
|
140
|
+
returns::[0.05 0.08 0.03 0.10] :" Annual returns per asset"
|
|
141
|
+
vols::[0.15 0.20 0.10 0.25] :" Volatilities per asset"
|
|
142
|
+
w::[0.25 0.25 0.25 0.25] :" Portfolio weights"
|
|
143
|
+
|
|
144
|
+
sharpe::{(+/x*returns)%((+/((x^2)*(vols^2)))^0.5)}
|
|
145
|
+
sg::sharpe:>w :" Gradient of Sharpe ratio"
|
|
146
|
+
.d("sharpe gradient="); .p(sg)
|
|
147
|
+
sharpe gradient=[0.07257738709449768 0.032256484031677246 0.11693036556243896 -0.22176480293273926]
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### For ML Researchers
|
|
151
|
+
|
|
152
|
+
Neural networks in pure array notation:
|
|
153
|
+
|
|
154
|
+
```klong
|
|
155
|
+
:" Single-layer neural network with gradient descent"
|
|
156
|
+
.bkf(["exp"])
|
|
157
|
+
sigmoid::{1%(1+exp(0-x))}
|
|
158
|
+
forward::{sigmoid((w1*x)+b1)}
|
|
159
|
+
X::[0.5 1.0 1.5 2.0]; Y::[0.2 0.4 0.6 0.8]
|
|
160
|
+
w1::0.1; b1::0.1; lr::0.1
|
|
161
|
+
loss::{+/((forward'X)-Y)^2}
|
|
162
|
+
|
|
163
|
+
:" Train with multi-param gradients"
|
|
164
|
+
{grads::loss:>[w1 b1]; w1::w1-(lr*grads@0); b1::b1-(lr*grads@1)}'!1000
|
|
165
|
+
.d("w1="); .d(w1); .d(" b1="); .p(b1)
|
|
166
|
+
w1=1.74 b1=-2.17
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### For Scientists
|
|
170
|
+
|
|
171
|
+
Express mathematics directly:
|
|
172
|
+
|
|
173
|
+
```klong
|
|
174
|
+
:" Gradient of f(x,y,z) = x^2 + y^2 + z^2 at [1,2,3]"
|
|
175
|
+
f::{+/x^2}
|
|
176
|
+
f:>[1 2 3]
|
|
177
|
+
[2.0 4.0 6.0]
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## The Array Language Advantage
|
|
181
|
+
|
|
182
|
+
Array languages express *what* you want, not *how* to compute it. This enables automatic optimization:
|
|
183
|
+
|
|
184
|
+
| Operation | Python | KlongPy |
|
|
185
|
+
|-----------|--------|---------|
|
|
186
|
+
| Sum an array | `sum(a)` | `+/a` |
|
|
187
|
+
| Running sum | `np.cumsum(a)` | `+\a` |
|
|
188
|
+
| Dot product | `np.dot(a,b)` | `+/a*b` |
|
|
189
|
+
| Average | `sum(a)/len(a)` | `(+/a)%#a` |
|
|
190
|
+
| Gradient | *10+ lines* | `f:>x` |
|
|
191
|
+
| Multi-param grad | *20+ lines* | `loss:>[w b]` |
|
|
192
|
+
| Jacobian | *15+ lines* | `x∂f` |
|
|
193
|
+
| Optimizer | *10+ lines* | `{w::w-(lr*f:>w)}` |
|
|
194
|
+
|
|
195
|
+
KlongPy inherits from the [APL](https://en.wikipedia.org/wiki/APL_(programming_language)) family tree (APL → J → K/Q → Klong), adding Python integration and automatic differentiation.
|
|
196
|
+
|
|
197
|
+
## Performance: NumPy vs PyTorch Backend
|
|
198
|
+
|
|
199
|
+
The PyTorch backend provides significant speedups for large arrays with GPU acceleration (RTX 4090 in this case):
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
$ python3 tests/perf_vector.py
|
|
203
|
+
============================================================
|
|
204
|
+
VECTOR OPS (element-wise, memory-bound)
|
|
205
|
+
Size: 10,000,000 elements, Iterations: 100
|
|
206
|
+
============================================================
|
|
207
|
+
NumPy (baseline) 0.021854s
|
|
208
|
+
KlongPy (numpy) 0.001413s (15.46x vs NumPy)
|
|
209
|
+
KlongPy (torch, cpu) 0.000029s (761.22x vs NumPy)
|
|
210
|
+
KlongPy (torch, cuda) 0.000028s (784.04x vs NumPy)
|
|
211
|
+
|
|
212
|
+
============================================================
|
|
213
|
+
MATRIX MULTIPLY (compute-bound, GPU advantage)
|
|
214
|
+
Size: 4000x4000, Iterations: 5
|
|
215
|
+
============================================================
|
|
216
|
+
NumPy (baseline) 0.078615s
|
|
217
|
+
KlongPy (numpy) 0.075400s (1.04x vs NumPy)
|
|
218
|
+
KlongPy (torch, cpu) 0.077350s (1.02x vs NumPy)
|
|
219
|
+
KlongPy (torch, cuda) 0.002339s (33.62x vs NumPy)
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Also supporting Apple Silicon MPS (M1 Mac Studio) enables fast local work:
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
$ python tests/perf_backend.py --compare
|
|
226
|
+
Benchmark NumPy (ms) Torch (ms) Speedup
|
|
227
|
+
----------------------------------------------------------------------
|
|
228
|
+
vector_add_1M 0.327 0.065 5.02x (torch)
|
|
229
|
+
compound_expr_1M 0.633 0.070 9.00x (torch)
|
|
230
|
+
sum_1M 0.246 0.087 2.84x (torch)
|
|
231
|
+
grade_up_100K 0.588 0.199 2.96x (torch)
|
|
232
|
+
enumerate_1M 0.141 0.050 2.83x (torch)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Complete Feature Set
|
|
236
|
+
|
|
237
|
+
KlongPy is a batteries-included platform with kdb+/Q-inspired features:
|
|
238
|
+
|
|
239
|
+
### Core Language
|
|
240
|
+
- **Vectorized Operations**: NumPy/PyTorch-powered bulk array operations
|
|
241
|
+
- **Automatic Differentiation**: Native `:>` operator for exact gradients
|
|
242
|
+
- **GPU Acceleration**: CUDA and Apple MPS support via PyTorch
|
|
243
|
+
- **Python Integration**: Import any Python library with `.py()` and `.pyf()`
|
|
244
|
+
|
|
245
|
+
### Data Infrastructure (kdb+/Q-like)
|
|
246
|
+
- **[Fast Columnar Database](docs/fast_columnar_database.md)**: Zero-copy DuckDB integration for SQL on arrays
|
|
247
|
+
- **[Inter-Process Communication](docs/ipc_capabilities.md)**: Build ticker plants and distributed systems
|
|
248
|
+
- **[Table & Key-Value Store](docs/table_and_key_value_stores.md)**: Persistent storage for tables and data
|
|
249
|
+
- **[Web Server](docs/web_server.md)**: Built-in HTTP server for APIs and dashboards
|
|
250
|
+
- **[WebSockets](docs/websockets.md)**: Connect to WebSocket servers and handle messages in KlongPy
|
|
251
|
+
- **[Timers](docs/timer.md)**: Scheduled execution for periodic tasks
|
|
252
|
+
|
|
253
|
+
### Documentation
|
|
254
|
+
- **[Quick Start Guide](docs/quick-start.md)**: Get running in 5 minutes
|
|
255
|
+
- **[PyTorch Backend & Autograd](docs/torch_backend.md)**: Complete autograd reference
|
|
256
|
+
- **[Operator Reference](docs/operators.md)**: All language operators
|
|
257
|
+
- **[Performance Guide](docs/performance.md)**: Optimization tips
|
|
258
|
+
|
|
259
|
+
Full documentation: [https://briangu.github.io/klongpy](https://briangu.github.io/klongpy)
|
|
260
|
+
|
|
261
|
+
## Typing Special Characters
|
|
262
|
+
|
|
263
|
+
KlongPy uses Unicode operators for mathematical notation. Here's how to type them:
|
|
264
|
+
|
|
265
|
+
| Symbol | Name | Mac | Windows | Description |
|
|
266
|
+
|--------|------|-----|---------|-------------|
|
|
267
|
+
| `∇` | Nabla | `Option + v` then select, or Character Viewer | `Alt + 8711` (numpad) | Numeric gradient |
|
|
268
|
+
| `∂` | Partial | `Option + d` | `Alt + 8706` (numpad) | Jacobian operator |
|
|
269
|
+
|
|
270
|
+
**Mac Tips:**
|
|
271
|
+
- **Option + d** types `∂` directly
|
|
272
|
+
- For `∇`, open Character Viewer with **Ctrl + Cmd + Space**, search "nabla"
|
|
273
|
+
- Or simply copy-paste: `∇` `∂`
|
|
274
|
+
|
|
275
|
+
**Alternative:** Use the function equivalents that don't require special characters:
|
|
276
|
+
```klong
|
|
277
|
+
3∇f :" Using nabla"
|
|
278
|
+
.jacobian(f;x) :" Instead of x∂f"
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## Syntax Cheat Sheet
|
|
282
|
+
|
|
283
|
+
Functions take up to 3 parameters, always named `x`, `y`, `z`:
|
|
284
|
+
|
|
285
|
+
```klong
|
|
286
|
+
:" Operators (right to left evaluation)"
|
|
287
|
+
5+3*2 :" 11 (3*2 first, then +5)"
|
|
288
|
+
+/[1 2 3] :" 6 (sum: + over /)"
|
|
289
|
+
*/[1 2 3] :" 6 (product: * over /)"
|
|
290
|
+
#[1 2 3] :" 3 (length)"
|
|
291
|
+
3|5 :" 5 (max)"
|
|
292
|
+
3&5 :" 3 (min)"
|
|
293
|
+
|
|
294
|
+
:" Functions"
|
|
295
|
+
avg::{(+/x)%#x} :" Monad (1 arg)"
|
|
296
|
+
dot::{+/x*y} :" Dyad (2 args)"
|
|
297
|
+
clip::{(x|y)&z} :" Triad (3 args): min(max(x,y),z)"
|
|
298
|
+
|
|
299
|
+
:" Adverbs (modifiers)"
|
|
300
|
+
f::{x^2}
|
|
301
|
+
f'[1 2 3] :" Each: apply f to each -> [1 4 9]"
|
|
302
|
+
+/[1 2 3] :" Over: fold/reduce -> 6"
|
|
303
|
+
+\[1 2 3] :" Scan: running fold -> [1 3 6]"
|
|
304
|
+
|
|
305
|
+
:" Autograd"
|
|
306
|
+
f::{x^2}
|
|
307
|
+
3∇f :" Numeric gradient at x=3 -> ~6.0"
|
|
308
|
+
f:>3 :" Autograd (exact with torch) at x=3 -> 6.0"
|
|
309
|
+
f::{+/x^2} :" Redefine f as sum-of-squares"
|
|
310
|
+
f:>[1 2 3] :" Gradient -> [2 4 6]"
|
|
311
|
+
|
|
312
|
+
:" Multi-parameter gradients"
|
|
313
|
+
w::2.0; b::3.0
|
|
314
|
+
loss::{(w^2)+(b^2)}
|
|
315
|
+
loss:>[w b] :" Gradients for both -> [4.0 6.0]"
|
|
316
|
+
|
|
317
|
+
:" Jacobian (for vector functions)"
|
|
318
|
+
g::{x^2} :" Element-wise square"
|
|
319
|
+
[1 2]∂g :" Jacobian matrix -> [[2 0] [0 4]]"
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## Examples
|
|
323
|
+
|
|
324
|
+
### 1. Basic Array Operations
|
|
325
|
+
|
|
326
|
+
```klong
|
|
327
|
+
?> a::[1 2 3 4 5]
|
|
328
|
+
[1 2 3 4 5]
|
|
329
|
+
?> a*a :" Element-wise square"
|
|
330
|
+
[1 4 9 16 25]
|
|
331
|
+
?> +/a :" Sum"
|
|
332
|
+
15
|
|
333
|
+
?> (*/a) :" Product"
|
|
334
|
+
120
|
|
335
|
+
?> avg::{(+/x)%#x} :" Define average"
|
|
336
|
+
:monad
|
|
337
|
+
?> avg(a)
|
|
338
|
+
3.0
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### 2. Gradient Descent
|
|
342
|
+
|
|
343
|
+
Minimize f(x) = (x-3)^2
|
|
344
|
+
|
|
345
|
+
(with PyTorch's autograd)
|
|
346
|
+
```klong
|
|
347
|
+
$ rlwrap kgpy --backend torch
|
|
348
|
+
?> f::{(x-3)^2}
|
|
349
|
+
:monad
|
|
350
|
+
?> s::10.0; lr::0.1
|
|
351
|
+
0.1
|
|
352
|
+
?> {s::s-(lr*f:>s); s}'!10
|
|
353
|
+
[8.600000381469727 7.4800004959106445 6.584000587463379 5.8672003746032715 5.293760299682617 4.835008144378662 4.468006610870361 4.174405097961426 3.9395241737365723 3.751619338989258]
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
(Numerical differentiation)
|
|
357
|
+
```klong
|
|
358
|
+
$ rlwrap kgpy
|
|
359
|
+
?> f::{(x-3)^2}
|
|
360
|
+
:monad
|
|
361
|
+
?> s::10.0; lr::0.1
|
|
362
|
+
0.1
|
|
363
|
+
?> {s::s-(lr*f:>s); s}'!10
|
|
364
|
+
[8.60000000104776 7.480000001637279 6.584000001220716 5.867200000887465 5.2937600006031005 4.835008000393373 4.4680064002611175 4.174405120173077 3.939524096109306 3.7516192768605094]
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### 3. Linear Regression
|
|
368
|
+
|
|
369
|
+
```klong
|
|
370
|
+
:" Data: y = 2*x + 3 + noise"
|
|
371
|
+
X::[1 2 3 4 5]
|
|
372
|
+
Y::[5.1 6.9 9.2 10.8 13.1]
|
|
373
|
+
|
|
374
|
+
:" Model parameters"
|
|
375
|
+
w::0.0; b::0.0
|
|
376
|
+
|
|
377
|
+
:" Loss function"
|
|
378
|
+
mse::{(+/(((w*X)+b)-Y)^2)%#X}
|
|
379
|
+
|
|
380
|
+
:" Train with multi-parameter gradients"
|
|
381
|
+
lr::0.01
|
|
382
|
+
{grads::mse:>[w b]; w::w-(lr*grads@0); b::b-(lr*grads@1)}'!1000
|
|
383
|
+
|
|
384
|
+
.d("Learned: w="); .d(w); .d(" b="); .p(b)
|
|
385
|
+
Learned: w=2.01 b=2.97
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
### 4. Database Operations
|
|
389
|
+
|
|
390
|
+
```klong
|
|
391
|
+
?> .py("klongpy.db")
|
|
392
|
+
?> t::.table([[\"name\" [\"Alice\" \"Bob\" \"Carol\"]] [\"age\" [25 30 35]]])
|
|
393
|
+
name age
|
|
394
|
+
Alice 25
|
|
395
|
+
Bob 30
|
|
396
|
+
Carol 35
|
|
397
|
+
?> db::.db(:{},\"T\",t)
|
|
398
|
+
?> db(\"SELECT * FROM T WHERE age > 27\")
|
|
399
|
+
name age
|
|
400
|
+
Bob 30
|
|
401
|
+
Carol 35
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### 5. IPC: Distributed Computing
|
|
405
|
+
|
|
406
|
+
**Server:**
|
|
407
|
+
```klong
|
|
408
|
+
?> avg::{(+/x)%#x}
|
|
409
|
+
:monad
|
|
410
|
+
?> .srv(8888)
|
|
411
|
+
1
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
**Client:**
|
|
415
|
+
```klong
|
|
416
|
+
?> f::.cli(8888) :" Connect to server"
|
|
417
|
+
remote[localhost:8888]:fn
|
|
418
|
+
?> myavg::f(:avg) :" Get remote function reference"
|
|
419
|
+
remote[localhost:8888]:fn:avg:monad
|
|
420
|
+
?> myavg(!1000000) :" Execute on server"
|
|
421
|
+
499999.5
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
### 6. Web Server
|
|
425
|
+
|
|
426
|
+
```klong
|
|
427
|
+
.py("klongpy.web")
|
|
428
|
+
data::!10
|
|
429
|
+
index::{x; "Hello from KlongPy! Data: ",data}
|
|
430
|
+
get:::{}; get,"/",index
|
|
431
|
+
post:::{}
|
|
432
|
+
h::.web(8888;get;post)
|
|
433
|
+
.p("Server ready at http://localhost:8888")
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
```bash
|
|
437
|
+
$ curl http://localhost:8888
|
|
438
|
+
['Hello from KlongPy! Data: ' 0 1 2 3 4 5 6 7 8 9]
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
## Installation Options
|
|
442
|
+
|
|
443
|
+
### Basic Runtime (NumPy only)
|
|
444
|
+
```bash
|
|
445
|
+
pip install klongpy
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
### REPL Support
|
|
449
|
+
```bash
|
|
450
|
+
pip install "klongpy[repl]"
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
### With PyTorch Autograd (Recommended)
|
|
454
|
+
```bash
|
|
455
|
+
pip install "klongpy[torch]"
|
|
456
|
+
kgpy --backend torch # Enable torch backend
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
### Web / DB / WebSockets Extras
|
|
460
|
+
```bash
|
|
461
|
+
pip install "klongpy[web]"
|
|
462
|
+
pip install "klongpy[db]"
|
|
463
|
+
pip install "klongpy[ws]"
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
### Full Installation (REPL, DB, Web, WebSockets, Torch)
|
|
467
|
+
```bash
|
|
468
|
+
pip install "klongpy[all]"
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
## Lineage and Inspiration
|
|
472
|
+
|
|
473
|
+
KlongPy stands on the shoulders of giants:
|
|
474
|
+
|
|
475
|
+
- **[APL](https://en.wikipedia.org/wiki/APL_(programming_language))** (1966): Ken Iverson's revolutionary notation
|
|
476
|
+
- **[J](https://www.jsoftware.com/)**: ASCII-friendly APL successor
|
|
477
|
+
- **[K/Q/kdb+](https://code.kx.com/)**: High-performance time series and trading systems
|
|
478
|
+
- **[Klong](https://t3x.org/klong)**: Nils M Holm's elegant, accessible array language
|
|
479
|
+
- **[NumPy](https://numpy.org/)**: The "Iverson Ghost" in Python's scientific stack
|
|
480
|
+
- **[PyTorch](https://pytorch.org/)**: Automatic differentiation and GPU acceleration
|
|
481
|
+
|
|
482
|
+
KlongPy combines Klong's simplicity with Python's ecosystem and PyTorch's autograd creating something new: an array language where gradients are first-class citizens.
|
|
483
|
+
|
|
484
|
+
## Use Cases
|
|
485
|
+
|
|
486
|
+
- **Quantitative Finance**: Self-optimizing trading strategies, risk models, portfolio optimization
|
|
487
|
+
- **Machine Learning**: Neural networks, gradient descent, optimization in minimal code
|
|
488
|
+
- **Scientific Computing**: Physics simulations, numerical methods, data analysis
|
|
489
|
+
- **Time Series Analysis**: Signal processing, feature engineering, streaming data
|
|
490
|
+
- **Rapid Prototyping**: Express complex algorithms in few lines, then optimize
|
|
491
|
+
|
|
492
|
+
## Status
|
|
493
|
+
|
|
494
|
+
KlongPy is a superset of the Klong array language, passing all Klong integration tests plus additional test suites. The PyTorch backend provides GPU acceleration (CUDA, MPS) and automatic differentiation.
|
|
495
|
+
|
|
496
|
+
Ongoing development:
|
|
497
|
+
- Expanded torch backend coverage
|
|
498
|
+
- Additional built-in tools and integrations
|
|
499
|
+
- Improved error messages and debugging
|
|
500
|
+
|
|
501
|
+
## Related Projects
|
|
502
|
+
|
|
503
|
+
- [Klupyter](https://github.com/briangu/klupyter) - KlongPy in Jupyter Notebooks
|
|
504
|
+
- [VS Code Syntax Highlighting](https://github.com/briangu/klongpy-vscode)
|
|
505
|
+
- [Advent of Code Solutions](https://github.com/briangu/aoc)
|
|
506
|
+
|
|
507
|
+
## Development
|
|
508
|
+
|
|
509
|
+
```bash
|
|
510
|
+
git clone https://github.com/briangu/klongpy.git
|
|
511
|
+
cd klongpy
|
|
512
|
+
pip install -e ".[dev]" # Install in editable mode with dev dependencies
|
|
513
|
+
python3 -m pytest tests/ # Run tests
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
## Issues
|
|
517
|
+
|
|
518
|
+
This project does not accept direct issue submissions.
|
|
519
|
+
|
|
520
|
+
Please start with a GitHub Discussion.
|
|
521
|
+
Maintainers will promote validated discussions to Issues.
|
|
522
|
+
|
|
523
|
+
Active contributors may be invited to open issues directly.
|
|
524
|
+
|
|
525
|
+
## Contributors
|
|
526
|
+
|
|
527
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for contribution workflow, discussion-first policy, and code standards.
|
|
528
|
+
|
|
529
|
+
## Documentation
|
|
530
|
+
|
|
531
|
+
```bash
|
|
532
|
+
# Install docs tooling
|
|
533
|
+
pip install -e ".[docs]"
|
|
534
|
+
|
|
535
|
+
# Build the site into ./site
|
|
536
|
+
mkdocs build
|
|
537
|
+
|
|
538
|
+
# Serve locally with live reload
|
|
539
|
+
mkdocs serve
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
## Acknowledgements
|
|
543
|
+
|
|
544
|
+
Huge thanks to [Nils M Holm](https://t3x.org) for creating Klong and writing the [Klong Book](https://t3x.org/klong/book.html), which made this project possible.
|