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
Add support for plain functions in @lie macro for Lie brackets
- Update documentation warning to reflect new CTFlows capability
- Add comprehensive examples with plain functions (autonomous, non-autonomous, variable)
- Add nested brackets and mixed VectorField/function examples
- Add tip comparing plain functions vs explicit VectorField creation
- Add extensive test suite for @lie macro with plain functions
- All tests pass (89/89) ✅
Copy file name to clipboardExpand all lines: docs/src/manual-differential-geometry.md
+95-12Lines changed: 95 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -173,7 +173,7 @@ Xf([1, 2], 1)
173
173
174
174
You can also create the VectorField explicitly with the keywords, then use it without keywords in the Lie function:
175
175
176
-
```@example main-8
176
+
```@example main-7a
177
177
using OptimalControl # hide
178
178
# Non-autonomous VectorField created with keywords
179
179
X = OptimalControl.VectorField((t, x) -> [t + x[2], -x[1]]; autonomous=false)
@@ -184,7 +184,7 @@ Xf = Lie(X, f)
184
184
Xf(1, [1, 2])
185
185
```
186
186
187
-
```@example main-9
187
+
```@example main-7b
188
188
using OptimalControl # hide
189
189
# Variable VectorField created with keywords
190
190
X = OptimalControl.VectorField((x, v) -> [x[2] + v, -x[1]]; variable=true)
@@ -267,28 +267,28 @@ Hfg(1, [1, 2], [3, 4])
267
267
268
268
You can also create Hamiltonian objects explicitly with keywords, then use them without keywords in the Poisson function. **Important**: both Hamiltonians must have the same time and variable dependencies:
269
269
270
-
```@example main-10
270
+
```@example main-9a
271
271
using OptimalControl # hide
272
272
# Non-autonomous Hamiltonians created with keywords
273
-
f_na(t, x, p) = t + p[1] * x[2] + p[2] * x[1]
274
-
g_na(t, x, p) = t^2 + x[1]^2 + p[2]^2
273
+
f(t, x, p) = t + p[1] * x[2] + p[2] * x[1]
274
+
g(t, x, p) = t^2 + x[1]^2 + p[2]^2
275
275
276
-
F = OptimalControl.Hamiltonian(f_na; autonomous=false)
277
-
G = OptimalControl.Hamiltonian(g_na; autonomous=false)
276
+
F = OptimalControl.Hamiltonian(f; autonomous=false)
277
+
G = OptimalControl.Hamiltonian(g; autonomous=false)
278
278
279
279
# No keywords needed here - both Hamiltonians are already non-autonomous
280
280
Hfg = Poisson(F, G)
281
281
Hfg(1, [1, 2], [3, 4])
282
282
```
283
283
284
-
```@example main-11
284
+
```@example main-9b
285
285
using OptimalControl # hide
286
286
# Variable Hamiltonians created with keywords
287
-
f_var(x, p, v) = x[1]^2 + p[2]^2 + v
288
-
g_var(x, p, v) = x[2]^2 + p[1]^2 + 2*v
287
+
f(x, p, v) = x[1]^2 + p[2]^2 + v
288
+
g(x, p, v) = x[2]^2 + p[1]^2 + 2*v
289
289
290
-
F = OptimalControl.Hamiltonian(f_var; variable=true)
291
-
G = OptimalControl.Hamiltonian(g_var; variable=true)
290
+
F = OptimalControl.Hamiltonian(f; variable=true)
291
+
G = OptimalControl.Hamiltonian(g; variable=true)
292
292
293
293
# Both are variable, so the Poisson bracket is also variable
The `@Lie` macro provides a convenient syntax for computing Lie brackets (for vector fields) and Poisson brackets (for Hamiltonians).
386
386
387
+
!!! warning "Important distinction"
388
+
389
+
-**Square brackets `[...]`** denote **Lie brackets** and work with:
390
+
-`VectorField` objects
391
+
- Plain Julia functions (automatically wrapped as `VectorField`)
392
+
-**Curly braces `{...}`** denote **Poisson brackets** and work with:
393
+
- Plain Julia functions (automatically wrapped as `Hamiltonian`)
394
+
-`Hamiltonian` objects
395
+
396
+
When using plain functions, specify `autonomous` and `variable` keywords as needed to match your function signature.
397
+
387
398
### Lie brackets with VectorField
388
399
389
400
```@example main-12
@@ -407,6 +418,78 @@ F123 = @Lie [[F1, F2], F3]
407
418
F123([1, 2, 3])
408
419
```
409
420
421
+
### Lie brackets with plain Julia functions
422
+
423
+
You can also use plain Julia functions directly with the `@Lie` macro. The functions will be automatically wrapped in `VectorField` objects:
424
+
425
+
```@example main-12a
426
+
using OptimalControl # hide
427
+
# Define plain Julia functions
428
+
X(x) = [x[2], -x[1]]
429
+
Y(x) = [x[1], x[2]]
430
+
431
+
# Compute Lie bracket using macro with plain functions
432
+
Z = @Lie [X, Y]
433
+
434
+
# Evaluate
435
+
Z([1, 2])
436
+
```
437
+
438
+
### With keyword arguments for plain functions
439
+
440
+
For non-autonomous or variable cases, specify the keywords:
441
+
442
+
```@example main-12b
443
+
using OptimalControl # hide
444
+
# Non-autonomous plain functions
445
+
X(t, x) = [t + x[2], -x[1]]
446
+
Y(t, x) = [x[1], t*x[2]]
447
+
448
+
# Use autonomous=false keyword
449
+
Z = @Lie [X, Y] autonomous=false
450
+
Z(1, [1, 2])
451
+
```
452
+
453
+
```@example main-12c
454
+
using OptimalControl # hide
455
+
# Variable plain functions
456
+
X(x, v) = [x[2] + v, -x[1]]
457
+
Y(x, v) = [x[1], x[2] + v]
458
+
459
+
# Use variable=true keyword
460
+
Z = @Lie [X, Y] variable=true
461
+
Z([1, 2], 1)
462
+
```
463
+
464
+
### Nested brackets with plain functions
465
+
466
+
```@example main-12d
467
+
using OptimalControl # hide
468
+
X(x) = [0, -x[3], x[2]]
469
+
Y(x) = [x[3], 0, -x[1]]
470
+
Z_func(x) = [x[1], x[2], x[3]]
471
+
472
+
# Nested Lie brackets
473
+
nested = @Lie [[X, Y], Z_func]
474
+
nested([1, 2, 3])
475
+
```
476
+
477
+
!!! tip "Plain functions vs VectorField"
478
+
479
+
Using plain functions with `@Lie [X, Y]` is convenient for quick computations. However, if you need to reuse the same vector field multiple times or want explicit control over the autonomy/variability, consider creating `VectorField` objects explicitly:
480
+
481
+
```julia
482
+
# Explicit VectorField (keywords at creation)
483
+
X = OptimalControl.VectorField((t, x) -> [t + x[2], -x[1]]; autonomous=false)
484
+
Y = OptimalControl.VectorField((t, x) -> [x[1], t*x[2]]; autonomous=false)
485
+
Z =@Lie [X, Y] # No keywords needed
486
+
487
+
# Plain functions (keywords at macro call)
488
+
X_func(t, x) = [t + x[2], -x[1]]
489
+
Y_func(t, x) = [x[1], t*x[2]]
490
+
Z =@Lie [X_func, Y_func] autonomous=false
491
+
```
492
+
410
493
### Poisson brackets from plain functions
411
494
412
495
For Hamiltonian functions (plain Julia functions), use curly braces `{_, _}`:
0 commit comments