calculus-cpp 0.1.0__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.
- calculus_cpp-0.1.0/LICENSE +21 -0
- calculus_cpp-0.1.0/PKG-INFO +9 -0
- calculus_cpp-0.1.0/README.md +481 -0
- calculus_cpp-0.1.0/calculus/__init__.py +1 -0
- calculus_cpp-0.1.0/calculus_core.cpp +117 -0
- calculus_cpp-0.1.0/calculus_cpp.egg-info/PKG-INFO +9 -0
- calculus_cpp-0.1.0/calculus_cpp.egg-info/SOURCES.txt +11 -0
- calculus_cpp-0.1.0/calculus_cpp.egg-info/dependency_links.txt +1 -0
- calculus_cpp-0.1.0/calculus_cpp.egg-info/not-zip-safe +1 -0
- calculus_cpp-0.1.0/calculus_cpp.egg-info/top_level.txt +1 -0
- calculus_cpp-0.1.0/pyproject.toml +3 -0
- calculus_cpp-0.1.0/setup.cfg +4 -0
- calculus_cpp-0.1.0/setup.py +29 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LEGEND'S DaD
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
# ๐งฎ CalCulus
|
|
2
|
+
|
|
3
|
+
*CalCulus** is a **fast, lightweight scientific computing library** written in **C++** and exposed to **Python** using **pybind11**.
|
|
4
|
+
|
|
5
|
+
[Go Back](https://github.com/LegedsDaD/CalCulus) to main page to get information about version and many more.
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
10
|
+

|
|
11
|
+

|
|
12
|
+

|
|
13
|
+

|
|
14
|
+

|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## ๐ Overview
|
|
20
|
+
|
|
21
|
+
**CalCulus** is a **fast, lightweight scientific computing library** written in **C++** and exposed to **Python** using **pybind11**.
|
|
22
|
+
|
|
23
|
+
It is designed for:
|
|
24
|
+
- ๐ Robotics projects
|
|
25
|
+
- ๐ Engineering calculations
|
|
26
|
+
- ๐ฌ Physics simulations
|
|
27
|
+
- ๐ง Educational & research use
|
|
28
|
+
|
|
29
|
+
CalCulus provides **C++-level performance** while keeping a **simple Python interface**.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## โจ Features
|
|
34
|
+
|
|
35
|
+
- โก High-performance C++ backend
|
|
36
|
+
- ๐งฎ Scalar & vector mathematics
|
|
37
|
+
- ๐ Numerical integration (Simpsonโs Rule)
|
|
38
|
+
- ๐งฑ Clean object-oriented API
|
|
39
|
+
- ๐ Python bindings via pybind11
|
|
40
|
+
- ๐งช Ideal for robotics & simulation
|
|
41
|
+
- ๐ฆ Easy `pip install .` workflow
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
## ๐ฌ NumPy vs CalCulus Comparison
|
|
45
|
+
|
|
46
|
+
| Feature | NumPy | CalCulus |
|
|
47
|
+
|------|------|---------|
|
|
48
|
+
| Core Language | C + Python | Pure C++ (pybind11) |
|
|
49
|
+
| Focus | Array-based numerical computing | Scientific math, physics & robotics |
|
|
50
|
+
| Scalar Operations | `numpy.float64` | `Scalar` C++ class |
|
|
51
|
+
| Trigonometric Functions | โ Vectorized | โ High-precision scalar |
|
|
52
|
+
| Vector Algebra | Limited (manual) | โ Built-in `Vec3` |
|
|
53
|
+
| Dot Product | โ | โ |
|
|
54
|
+
| Cross Product | โ (via `np.cross`) | โ Native |
|
|
55
|
+
| Numerical Integration | Limited | โ Simpsonโs Rule |
|
|
56
|
+
| Physical Constants | Partial | โ Built-in |
|
|
57
|
+
| Robotics Use | Medium | โ High |
|
|
58
|
+
| Physics Simulations | Medium | โ High |
|
|
59
|
+
| C++ Performance | โ | โ |
|
|
60
|
+
| SIMD / Low-level Control | โ | โ |
|
|
61
|
+
| Learning Curve | Easy | Moderate |
|
|
62
|
+
| Best Use Case | Data science, ML | Robotics, physics engines |
|
|
63
|
+
| Python Dependency | Heavy | Minimal |
|
|
64
|
+
| Custom Math Engine | โ | โ |
|
|
65
|
+
| Real-Time Applications | โ | โ |
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
### ๐ When to Use Which?
|
|
70
|
+
|
|
71
|
+
- Use **NumPy** when:
|
|
72
|
+
- Working with large datasets
|
|
73
|
+
- Doing machine learning or statistics
|
|
74
|
+
- Needing array broadcasting
|
|
75
|
+
|
|
76
|
+
- Use **CalCulus** when:
|
|
77
|
+
- Building robotics or physics engines
|
|
78
|
+
- Needing C++-level performance
|
|
79
|
+
- Working with vectors, forces, motion, integration
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
## ๐ Project Structure
|
|
83
|
+
```
|
|
84
|
+
CalCulus_CPP/
|
|
85
|
+
โ
|
|
86
|
+
โโโ calculus/
|
|
87
|
+
โ โโโ __init__.py
|
|
88
|
+
โ
|
|
89
|
+
โโโ calculus_core.cpp
|
|
90
|
+
โโโ pyproject.toml
|
|
91
|
+
โโโ setup.py
|
|
92
|
+
โโโ README.md
|
|
93
|
+
โโโ LICENSE
|
|
94
|
+
```
|
|
95
|
+
## ๐ฆ Installation
|
|
96
|
+
|
|
97
|
+
### ๐ง Requirements
|
|
98
|
+
|
|
99
|
+
- Python **3.8+**
|
|
100
|
+
- C++17 compatible compiler
|
|
101
|
+
- Visual Studio **Desktop development with C++** (Windows)
|
|
102
|
+
- MSVC v143
|
|
103
|
+
- pybind11
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
### โถ Install from Source
|
|
108
|
+
|
|
109
|
+
Run the command **from the project root directory**:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
pip install .
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## ๐งฎ API Overview
|
|
117
|
+
|
|
118
|
+
### ๐ข Scalar
|
|
119
|
+
|
|
120
|
+
The `Scalar` class represents a single numerical value with advanced scientific operations.
|
|
121
|
+
|
|
122
|
+
| Method / Operator | Description |
|
|
123
|
+
|------------------|------------|
|
|
124
|
+
| `+ - * /` | Arithmetic operations |
|
|
125
|
+
| `sin()` | Sine |
|
|
126
|
+
| `cos()` | Cosine |
|
|
127
|
+
| `tan()` | Tangent |
|
|
128
|
+
| `asin()` | Inverse sine |
|
|
129
|
+
| `acos()` | Inverse cosine |
|
|
130
|
+
| `atan()` | Inverse tangent |
|
|
131
|
+
| `sinh()` | Hyperbolic sine |
|
|
132
|
+
| `cosh()` | Hyperbolic cosine |
|
|
133
|
+
| `tanh()` | Hyperbolic tangent |
|
|
134
|
+
| `exp()` | Exponential |
|
|
135
|
+
| `log()` | Natural logarithm |
|
|
136
|
+
| `log10()` | Base-10 logarithm |
|
|
137
|
+
| `pow(n)` | Power |
|
|
138
|
+
| `sqrt()` | Square root |
|
|
139
|
+
| `cbrt()` | Cube root |
|
|
140
|
+
| `abs()` | Absolute value |
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
### ๐ Vec3 (3D Vector)
|
|
145
|
+
|
|
146
|
+
The `Vec3` class is designed for physics, robotics, and linear algebra.
|
|
147
|
+
|
|
148
|
+
| Method | Description |
|
|
149
|
+
|------|------------|
|
|
150
|
+
| `+ -` | Vector addition / subtraction |
|
|
151
|
+
| `*` | Scalar multiplication |
|
|
152
|
+
| `dot(v)` | Dot product |
|
|
153
|
+
| `cross(v)` | Cross product |
|
|
154
|
+
| `magnitude()` | Vector length |
|
|
155
|
+
| `normalize()` | Unit vector |
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
### ๐ง Solver
|
|
160
|
+
|
|
161
|
+
Numerical calculus utilities.
|
|
162
|
+
|
|
163
|
+
| Method | Description |
|
|
164
|
+
|------|------------|
|
|
165
|
+
| `integrate(func, a, b, n)` | Simpsonโs Rule integration |
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
### ๐ Constants
|
|
170
|
+
|
|
171
|
+
Physical and mathematical constants.
|
|
172
|
+
|
|
173
|
+
| Constant | Description |
|
|
174
|
+
|--------|------------|
|
|
175
|
+
| `pi` | ฯ |
|
|
176
|
+
| `e` | Eulerโs number |
|
|
177
|
+
| `c` | Speed of light |
|
|
178
|
+
| `h` | Planck constant |
|
|
179
|
+
| `k` | Boltzmann constant |
|
|
180
|
+
| `g` | Gravity |
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
## Ultimate Test Code
|
|
185
|
+
```python
|
|
186
|
+
import calculus as c
|
|
187
|
+
import math
|
|
188
|
+
|
|
189
|
+
print("\n===== CalCulus Full Constants Test =====\n")
|
|
190
|
+
|
|
191
|
+
# ---------------- Scalars ----------------
|
|
192
|
+
s = c.Scalar(2.5)
|
|
193
|
+
print("Scalar example:")
|
|
194
|
+
print("s =", s)
|
|
195
|
+
print("s * 2 =", s * c.Scalar(2))
|
|
196
|
+
print("s.sqrt() =", s.sqrt())
|
|
197
|
+
print("s.exp() =", s.exp())
|
|
198
|
+
print("s.log() =", s.log())
|
|
199
|
+
print("s.sin() =", s.sin())
|
|
200
|
+
|
|
201
|
+
# ---------------- Vec3 ----------------
|
|
202
|
+
v = c.Vec3(3, 4, 12)
|
|
203
|
+
print("\nVector example:")
|
|
204
|
+
print("v =", v)
|
|
205
|
+
print("v.magnitude() =", v.magnitude())
|
|
206
|
+
print("v.normalize() =", v.normalize())
|
|
207
|
+
|
|
208
|
+
# ---------------- Solver ----------------
|
|
209
|
+
print("\nIntegration example:")
|
|
210
|
+
area = c.Solver.integrate(lambda x: x**2, 0, 3, 1000)
|
|
211
|
+
print("โซ xยฒ dx from 0 to 3 =", area)
|
|
212
|
+
print("Expected =", (3**3)/3)
|
|
213
|
+
|
|
214
|
+
# ---------------- Constants ----------------
|
|
215
|
+
print("\nConstants:")
|
|
216
|
+
print(f"pi = {c.Constants.pi} (math.pi = {math.pi})")
|
|
217
|
+
print(f"e = {c.Constants.e} (math.e = {math.e})")
|
|
218
|
+
print(f"g = {c.Constants.g} m/sยฒ")
|
|
219
|
+
print(f"c = {c.Constants.c if hasattr(c.Constants,'c') else 'Not defined'} m/s")
|
|
220
|
+
print(f"h = {c.Constants.h if hasattr(c.Constants,'h') else 'Not defined'} Js")
|
|
221
|
+
print(f"k = {c.Constants.k if hasattr(c.Constants,'k') else 'Not defined'} J/K")
|
|
222
|
+
|
|
223
|
+
# ---------------- Check formulas with constants ----------------
|
|
224
|
+
print("\nUsing constants in formulas:")
|
|
225
|
+
# Example: E = m * g * h (potential energy formula)
|
|
226
|
+
mass = 10 # kg
|
|
227
|
+
height = 5 # meters
|
|
228
|
+
energy = mass * c.Constants.g * height
|
|
229
|
+
print(f"Potential Energy = m*g*h = {energy} J")
|
|
230
|
+
|
|
231
|
+
# Example: Circumference of a circle using pi
|
|
232
|
+
radius = 7
|
|
233
|
+
circumference = 2 * c.Constants.pi * radius
|
|
234
|
+
print(f"Circumference of circle (r=7) = {circumference}")
|
|
235
|
+
|
|
236
|
+
print("\n===== All Constants Tested Successfully =====")
|
|
237
|
+
```
|
|
238
|
+
โซ This is the Full test code. Rest are secondary and may or may not work.
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## ๐งช Examples
|
|
243
|
+
|
|
244
|
+
### Example 1 โ Basic Scalar Arithmetic
|
|
245
|
+
|
|
246
|
+
```python
|
|
247
|
+
import calculus as c
|
|
248
|
+
|
|
249
|
+
x = c.Scalar(5)
|
|
250
|
+
y = c.Scalar(3)
|
|
251
|
+
|
|
252
|
+
print(x + y)
|
|
253
|
+
print(x * y)
|
|
254
|
+
```
|
|
255
|
+
### Example 2: Distance Between Two Points (Robot Movement)
|
|
256
|
+
|
|
257
|
+
```python
|
|
258
|
+
import calculus as c
|
|
259
|
+
|
|
260
|
+
p1 = c.Vec3(1, 2, 3)
|
|
261
|
+
p2 = c.Vec3(4, 6, 3)
|
|
262
|
+
|
|
263
|
+
distance = (p2 - p1).magnitude()
|
|
264
|
+
print("Distance:", distance)
|
|
265
|
+
```
|
|
266
|
+
๐ Use case:
|
|
267
|
+
Robot path planning, obstacle distance calculation
|
|
268
|
+
|
|
269
|
+
### Example 3: Robot Arm Direction Vector (Normalization)
|
|
270
|
+
|
|
271
|
+
```python
|
|
272
|
+
import calculus as c
|
|
273
|
+
|
|
274
|
+
direction = c.Vec3(10, 5, 0)
|
|
275
|
+
unit_direction = direction.normalize()
|
|
276
|
+
|
|
277
|
+
print("Unit direction:", unit_direction)
|
|
278
|
+
```
|
|
279
|
+
๐ Use case:
|
|
280
|
+
Motor direction control, inverse kinematics
|
|
281
|
+
### Example 4: Work Done by a Force (Physics)
|
|
282
|
+
|
|
283
|
+
```python
|
|
284
|
+
import calculus as c
|
|
285
|
+
|
|
286
|
+
force = c.Vec3(10, 0, 0) # Newtons
|
|
287
|
+
displacement = c.Vec3(5, 0, 0) # meters
|
|
288
|
+
|
|
289
|
+
work = force.dot(displacement)
|
|
290
|
+
print("Work done (J):", work)
|
|
291
|
+
```
|
|
292
|
+
๐ Use case:
|
|
293
|
+
Physics engines, mechanical simulations
|
|
294
|
+
|
|
295
|
+
### Example 5: Torque Calculation (Cross Product)
|
|
296
|
+
|
|
297
|
+
```python
|
|
298
|
+
import calculus as c
|
|
299
|
+
|
|
300
|
+
r = c.Vec3(0, 2, 0) # position vector
|
|
301
|
+
F = c.Vec3(10, 0, 0) # force
|
|
302
|
+
|
|
303
|
+
torque = r.cross(F)
|
|
304
|
+
print("Torque vector:", torque)
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
๐ Use case:
|
|
308
|
+
Robot joints, rotational mechanics
|
|
309
|
+
|
|
310
|
+
### Example 6: Projectile Motion โ Area Under Velocity Curve
|
|
311
|
+
|
|
312
|
+
```python
|
|
313
|
+
import calculus as c
|
|
314
|
+
|
|
315
|
+
g = c.Constants.g
|
|
316
|
+
|
|
317
|
+
velocity = lambda t: 20 - g * t
|
|
318
|
+
|
|
319
|
+
distance = c.Solver.integrate(
|
|
320
|
+
velocity,
|
|
321
|
+
0,
|
|
322
|
+
2,
|
|
323
|
+
1000
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
print("Distance traveled:", distance)
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
๐ Use case:
|
|
330
|
+
Ballistics, drone drop calculations
|
|
331
|
+
|
|
332
|
+
### Example 7: Center of Mass (Simple 1D Integration)
|
|
333
|
+
|
|
334
|
+
```python
|
|
335
|
+
import calculus as c
|
|
336
|
+
|
|
337
|
+
mass_density = lambda x: x + 1
|
|
338
|
+
|
|
339
|
+
numerator = c.Solver.integrate(
|
|
340
|
+
lambda x: x * mass_density(x),
|
|
341
|
+
0,
|
|
342
|
+
5,
|
|
343
|
+
1000
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
denominator = c.Solver.integrate(
|
|
347
|
+
mass_density,
|
|
348
|
+
0,
|
|
349
|
+
5,
|
|
350
|
+
1000
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
center_of_mass = numerator / denominator
|
|
354
|
+
print("Center of Mass:", center_of_mass)
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
๐ Use case:
|
|
358
|
+
Mechanical design, robotics balance control
|
|
359
|
+
|
|
360
|
+
### Example 8: Angle Between Two Vectors (Robot Orientation)
|
|
361
|
+
|
|
362
|
+
```python
|
|
363
|
+
import calculus as c
|
|
364
|
+
|
|
365
|
+
v1 = c.Vec3(1, 0, 0)
|
|
366
|
+
v2 = c.Vec3(1, 1, 0)
|
|
367
|
+
|
|
368
|
+
cos_theta = v1.dot(v2) / (v1.magnitude() * v2.magnitude())
|
|
369
|
+
angle = c.acos(cos_theta)
|
|
370
|
+
|
|
371
|
+
print("Angle (rad):", angle)
|
|
372
|
+
```
|
|
373
|
+
๐ Use case:
|
|
374
|
+
Robot heading, orientation correction
|
|
375
|
+
|
|
376
|
+
### Example 9: Energy Stored in a Spring
|
|
377
|
+
|
|
378
|
+
``` python
|
|
379
|
+
import calculus as c
|
|
380
|
+
|
|
381
|
+
k = 100 # spring constant
|
|
382
|
+
|
|
383
|
+
force = lambda x: k * x
|
|
384
|
+
|
|
385
|
+
energy = c.Solver.integrate(
|
|
386
|
+
force,
|
|
387
|
+
0,
|
|
388
|
+
0.5,
|
|
389
|
+
1000
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
print("Spring Energy:", energy)
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
๐ Use case:
|
|
396
|
+
Shock absorbers, suspension modeling
|
|
397
|
+
|
|
398
|
+
### Example 10: Gravitational Force Calculation
|
|
399
|
+
|
|
400
|
+
```python
|
|
401
|
+
import calculus as c
|
|
402
|
+
|
|
403
|
+
G = 6.674e-11
|
|
404
|
+
m1 = 5.97e24
|
|
405
|
+
m2 = 1000
|
|
406
|
+
r = 6.4e6
|
|
407
|
+
|
|
408
|
+
force = G * m1 * m2 / (r * r)
|
|
409
|
+
print("Gravitational force:", force)
|
|
410
|
+
```
|
|
411
|
+
๐ Use case:
|
|
412
|
+
Astro-robotics, satellite simulation
|
|
413
|
+
|
|
414
|
+
### Example 11: Path Length Using Integration (Robot Trajectory)
|
|
415
|
+
|
|
416
|
+
```python
|
|
417
|
+
import calculus as c
|
|
418
|
+
|
|
419
|
+
speed = lambda t: (3*t*t + 2*t + 1)
|
|
420
|
+
|
|
421
|
+
path_length = c.Solver.integrate(
|
|
422
|
+
speed,
|
|
423
|
+
0,
|
|
424
|
+
4,
|
|
425
|
+
1000
|
|
426
|
+
)
|
|
427
|
+
|
|
428
|
+
print("Path length:", path_length)
|
|
429
|
+
```
|
|
430
|
+
๐ Use case:
|
|
431
|
+
Autonomous vehicle motion planning
|
|
432
|
+
|
|
433
|
+
### Example 11: Electric Field Direction (Cross Product)
|
|
434
|
+
|
|
435
|
+
```python
|
|
436
|
+
import calculus as c
|
|
437
|
+
|
|
438
|
+
velocity = c.Vec3(0, 1, 0)
|
|
439
|
+
magnetic_field = c.Vec3(0, 0, 1)
|
|
440
|
+
|
|
441
|
+
electric_field = velocity.cross(magnetic_field)
|
|
442
|
+
print("Electric field direction:", electric_field)
|
|
443
|
+
```
|
|
444
|
+
๐ Use case:
|
|
445
|
+
Physics simulation, EM field modeling
|
|
446
|
+
|
|
447
|
+
## ๐ซ Trouble shooting
|
|
448
|
+
If the steps parameter is missing, Python will raise:
|
|
449
|
+
```python
|
|
450
|
+
TypeError: integrate(): incompatible function arguments
|
|
451
|
+
```
|
|
452
|
+
### โ
Correct Usage (IMPORTANT)
|
|
453
|
+
```python
|
|
454
|
+
area = c.Solver.integrate(
|
|
455
|
+
lambda x: x * x,
|
|
456
|
+
0,
|
|
457
|
+
3,
|
|
458
|
+
1000 # <-- REQUIRED number of steps
|
|
459
|
+
)
|
|
460
|
+
```
|
|
461
|
+
### โ Incorrect Usage
|
|
462
|
+
```python
|
|
463
|
+
# This will FAIL
|
|
464
|
+
c.Solver.integrate(lambda x: x*x, 0, 3)
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
### ๐ Recommendation
|
|
468
|
+
Always use at least 1000 steps for accurate results
|
|
469
|
+
Steps must be an even number (Simpsonโs Rule requirement)
|
|
470
|
+
Higher steps = higher accuracy but slower computation
|
|
471
|
+
|
|
472
|
+
### ๐ Why This Happens
|
|
473
|
+
The integration is implemented in C++ using Simpsonโs Rule, which:
|
|
474
|
+
Requires a fixed number of subdivisions
|
|
475
|
+
Cannot infer the step count automatically
|
|
476
|
+
This design ensures high performance and numerical stability.
|
|
477
|
+
|
|
478
|
+
## โญStar
|
|
479
|
+
Star the repo if you find this helpful for performing HIGH GRADE SCIENTIFIC CALCULATIONS.
|
|
480
|
+
|
|
481
|
+
tell me which sections to this
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .calculus_core import *
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
#define PYBIND11_DETAILED_ERROR_MESSAGES
|
|
2
|
+
#include <pybind11/pybind11.h>
|
|
3
|
+
#include <pybind11/functional.h>
|
|
4
|
+
#include <cmath>
|
|
5
|
+
#include <sstream>
|
|
6
|
+
|
|
7
|
+
namespace py = pybind11;
|
|
8
|
+
|
|
9
|
+
// ================= Scalar =================
|
|
10
|
+
class Scalar {
|
|
11
|
+
double v;
|
|
12
|
+
public:
|
|
13
|
+
Scalar(double val = 0.0) : v(val) {}
|
|
14
|
+
|
|
15
|
+
Scalar operator+(const Scalar& o) const { return Scalar(v + o.v); }
|
|
16
|
+
Scalar operator-(const Scalar& o) const { return Scalar(v - o.v); }
|
|
17
|
+
Scalar operator*(const Scalar& o) const { return Scalar(v * o.v); }
|
|
18
|
+
Scalar operator/(const Scalar& o) const { return Scalar(v / o.v); }
|
|
19
|
+
|
|
20
|
+
double sin() const { return std::sin(v); }
|
|
21
|
+
double cos() const { return std::cos(v); }
|
|
22
|
+
double tan() const { return std::tan(v); }
|
|
23
|
+
double exp() const { return std::exp(v); }
|
|
24
|
+
double log() const { return std::log(v); }
|
|
25
|
+
double sqrt() const { return std::sqrt(v); }
|
|
26
|
+
|
|
27
|
+
std::string toString() const {
|
|
28
|
+
std::ostringstream ss;
|
|
29
|
+
ss << v;
|
|
30
|
+
return ss.str();
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// ================= Vec3 =================
|
|
35
|
+
class Vec3 {
|
|
36
|
+
public:
|
|
37
|
+
double x, y, z;
|
|
38
|
+
Vec3(double a, double b, double c) :x(a), y(b), z(c) {}
|
|
39
|
+
|
|
40
|
+
Vec3 operator+(const Vec3& o) const { return Vec3(x + o.x, y + o.y, z + o.z); }
|
|
41
|
+
Vec3 operator-(const Vec3& o) const { return Vec3(x - o.x, y - o.y, z - o.z); }
|
|
42
|
+
Vec3 operator*(double s) const { return Vec3(x * s, y * s, z * s); }
|
|
43
|
+
|
|
44
|
+
double dot(const Vec3& o) const { return x * o.x + y * o.y + z * o.z; }
|
|
45
|
+
|
|
46
|
+
Vec3 cross(const Vec3& o) const {
|
|
47
|
+
return Vec3(
|
|
48
|
+
y * o.z - z * o.y,
|
|
49
|
+
z * o.x - x * o.z,
|
|
50
|
+
x * o.y - y * o.x
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
double magnitude() const { return std::sqrt(x * x + y * y + z * z); }
|
|
55
|
+
|
|
56
|
+
Vec3 normalize() const {
|
|
57
|
+
double m = magnitude();
|
|
58
|
+
return Vec3(x / m, y / m, z / m);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// ================= Solver =================
|
|
63
|
+
class Solver {
|
|
64
|
+
public:
|
|
65
|
+
static double integrate(py::function f, double a, double b, int n) {
|
|
66
|
+
double h = (b - a) / n, sum = 0;
|
|
67
|
+
for (int i = 0; i < n; i++) {
|
|
68
|
+
double x = a + i * h;
|
|
69
|
+
sum += f(x).cast<double>() * h;
|
|
70
|
+
}
|
|
71
|
+
return sum;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// ================= Constants =================
|
|
76
|
+
namespace Constants {
|
|
77
|
+
const double pi = 3.141592653589793;
|
|
78
|
+
const double e = 2.718281828459045;
|
|
79
|
+
const double g = 9.80665;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ================= PYBIND =================
|
|
83
|
+
PYBIND11_MODULE(calculus_core, m) {
|
|
84
|
+
m.doc() = "CalCulus Scientific Engine";
|
|
85
|
+
|
|
86
|
+
py::class_<Scalar>(m, "Scalar")
|
|
87
|
+
.def(py::init<double>())
|
|
88
|
+
.def("__add__", &Scalar::operator+)
|
|
89
|
+
.def("__sub__", &Scalar::operator-)
|
|
90
|
+
.def("__mul__", &Scalar::operator*)
|
|
91
|
+
.def("__truediv__", &Scalar::operator/)
|
|
92
|
+
.def("__repr__", &Scalar::toString)
|
|
93
|
+
.def("sin", &Scalar::sin)
|
|
94
|
+
.def("cos", &Scalar::cos)
|
|
95
|
+
.def("tan", &Scalar::tan)
|
|
96
|
+
.def("exp", &Scalar::exp)
|
|
97
|
+
.def("log", &Scalar::log)
|
|
98
|
+
.def("sqrt", &Scalar::sqrt);
|
|
99
|
+
|
|
100
|
+
py::class_<Vec3>(m, "Vec3")
|
|
101
|
+
.def(py::init<double, double, double>())
|
|
102
|
+
.def("__add__", &Vec3::operator+)
|
|
103
|
+
.def("__sub__", &Vec3::operator-)
|
|
104
|
+
.def("__mul__", &Vec3::operator*)
|
|
105
|
+
.def("dot", &Vec3::dot)
|
|
106
|
+
.def("cross", &Vec3::cross)
|
|
107
|
+
.def("magnitude", &Vec3::magnitude)
|
|
108
|
+
.def("normalize", &Vec3::normalize);
|
|
109
|
+
|
|
110
|
+
py::class_<Solver>(m, "Solver")
|
|
111
|
+
.def_static("integrate", &Solver::integrate);
|
|
112
|
+
|
|
113
|
+
py::module_ c = m.def_submodule("Constants");
|
|
114
|
+
c.attr("pi") = Constants::pi;
|
|
115
|
+
c.attr("e") = Constants::e;
|
|
116
|
+
c.attr("g") = Constants::g;
|
|
117
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
calculus_core.cpp
|
|
4
|
+
pyproject.toml
|
|
5
|
+
setup.py
|
|
6
|
+
calculus/__init__.py
|
|
7
|
+
calculus_cpp.egg-info/PKG-INFO
|
|
8
|
+
calculus_cpp.egg-info/SOURCES.txt
|
|
9
|
+
calculus_cpp.egg-info/dependency_links.txt
|
|
10
|
+
calculus_cpp.egg-info/not-zip-safe
|
|
11
|
+
calculus_cpp.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
calculus
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from setuptools import setup, Extension
|
|
2
|
+
import sys
|
|
3
|
+
import pybind11
|
|
4
|
+
|
|
5
|
+
# Detect compiler and set correct flags
|
|
6
|
+
if sys.platform == "win32":
|
|
7
|
+
extra_compile_args = ["/O2"]
|
|
8
|
+
else:
|
|
9
|
+
extra_compile_args = ["-O3"]
|
|
10
|
+
|
|
11
|
+
ext_modules = [
|
|
12
|
+
Extension(
|
|
13
|
+
"calculus.calculus_core",
|
|
14
|
+
["calculus_core.cpp"],
|
|
15
|
+
include_dirs=[pybind11.get_include()],
|
|
16
|
+
language="c++",
|
|
17
|
+
extra_compile_args=extra_compile_args,
|
|
18
|
+
),
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
setup(
|
|
22
|
+
name="calculus-cpp",
|
|
23
|
+
version="0.1.0",
|
|
24
|
+
author="LegedsDaD",
|
|
25
|
+
description="High-performance calculus library using C++ backend",
|
|
26
|
+
packages=["calculus"],
|
|
27
|
+
ext_modules=ext_modules,
|
|
28
|
+
zip_safe=False,
|
|
29
|
+
)
|