Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index1 #104

Merged
merged 6 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/man/a_toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
7. **[Monomial Indexing](@ref monoindex)**: Get/set individual monomial coefficients
8. **[`mono`](@ref mono)**: Creates a TPS corresponding to a specific monomial
9. **[Slicing and `par`](@ref slice)**: Indexing a specific polynomial within the TPS
10. **[TPS and Map Methods](@ref tpsmapmethods)**: Integrate, differentiate, poisson bracket, map (partial) inversion, etc.
10. **[TPS Methods](@ref tpsmethods)**: Integrate, differentiate, composition, evaluation, etc.
11. **[`@FastGTPSA`](@ref fastgtpsa)**: Speed up evaluation of expressions containing TPSs, transparent to other types
12. **[I/O](@ref io)**: Reading/writing TPSs
13. **[All Overloaded Functions](@ref all)**: List of all overloaded Base functions and more
52 changes: 43 additions & 9 deletions docs/src/man/g_monoindex.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
Individual monomial coefficients in a TPS can be get/set with two methods of indexing: **by order**, and **by sparse monomial**.

## By Order
`t[<var_1 order>, ..., <var_N order>,<param_1 order>, ..., <param_M order>]`
```
t[[<var_1 order>, ..., <var_N order>,<param_1 order>, ..., <param_M order>]]
t[(<var_1 order>, ..., <var_N order>,<param_1 order>, ..., <param_M order>)]
```

A particular monomial can be indexed by specifying the orders of each variable and parameter. For example, for a TPS `t` with variables ``x_1``, ``x_2`` and parameter ``k_1`` the ``x_1^3x_2^1k_1^2`` monomial coefficient is accessed with `t[3,1,2]`. The 0th order part (the *scalar* part) of the TPS is indexed with `t[0,0,0]` or equivalently `t[0]`, as leaving out trailing zeros for unincluded variables/parameters is allowed.
A particular monomial can be indexed by specifying the orders of each variable and parameter. For example, for a TPS `t` with variables ``x_1``, ``x_2`` and parameters ``k_1``, ``k_2``, the ``x_1^3x_2^1k_1^2`` monomial coefficient is accessed with `t[[3,1,2,0]]` or equivalently `t[[3,1,2]]`, as leaving out trailing zeros for unincluded variables/parameters is allowed. A tuple is also allowed instead of a vector for the list of orders.

### Examples
```@repl
Expand All @@ -15,16 +18,19 @@ d = Descriptor(2, 6, 3, 6); # 2 variables, 3 parameters all to 6th order
x = vars(d);
k = params(d);
f = 5 + sin(x[1])*sin(x[2])*cos(k[1])
f[3,1,2] # Leave out trailing zeros for unincluded variables/parameters
f[0] # Scalar part
f[1,1,1,1,1] = 123; # Set monomial coefficient
f[[3,1,2]] # Leave out trailing zeros for unincluded variables/parameters
f[[0]] # Scalar part
f[(1,1,1,1,1)] = 123; # Set monomial coefficient
print(f)
```

## By Sparse Monomial
`t[<ix_var> => <order>, ..., params=[<ix_param> => <order>, ...]]`
```
t[[<ix_var> => <order>, ...], params=[<ix_param> => <order>, ...]]
t[(<ix_var> => <order>, ...), params=(<ix_param> => <order>, ...)]
```

In GTPSAs with many variables and parameters, indexing-by-order is inconvenient because each order needs to be included up to the last included variable/parameter with nonzero order. In this case, a particular monomial can be indexed instead by specifying each variable/parameter number and its corresponding order in pairs. For example, for a TPS with variables ``x_1, ..., x_{15}`` and parameters ``k_1, ..., k_{10}``, the ``x_{1}^3x_{15}^1k_{10}^2`` monomial coefficient is accessed with `t[1=>3, 15=>1, params=[10=>2]]`. The scalar part of the TPS cannot be get/set with this method.
In GTPSAs with many variables and parameters, indexing-by-order is inconvenient because each order needs to be included up to the last included variable/parameter with nonzero order. In this case, a particular monomial can be indexed instead by specifying each variable/parameter number and its corresponding order in pairs. For example, for a TPS with variables ``x_1, ..., x_{15}`` and parameters ``k_1, ..., k_{10}``, the ``x_{1}^3x_{15}^1k_{10}^2`` monomial coefficient is accessed with `t[[1=>3, 15=>1], params=[10=>2]]`. The scalar part of the TPS cannot be get/set with this method. A tuple is also allowed instead of a vector for the list of pairs.

### Examples
```@repl
Expand All @@ -34,11 +40,39 @@ GTPSA.show_sparse = true; # Use sparse output
x = vars(d);
k = params(d);
f = 5 + sin(x[1])*sin(x[15])*cos(k[10])
f[1=>3, 15=>1, params=[10=>2]]
f[1=>1, 15=>2, params=[10=>3]] = 123; # Set monomial coefficient
f[[1=>3, 15=>1], params=[10=>2]]
f[(1=>1, 15=>2), params=(10=>3,)] = 123; # Set monomial coefficient
print(f)
```

## By Monomial Index
```
t[idx]
t[param=param_idx]
```

*This indexing method is not recommended/requires care when indexing monomials above first order.* Indexes the TPS with all monomials sorted by order. For example, for a TPS with one variable ``x_1`` and one parameter ``k_1`` the ``x_1`` monomial is indexed with `t[1]` and the ``x_1^2`` monomial is indexed with either `t[3]`. The ``k_1`` monomial can be indexed with either `t[2]` or equivalently using the `param` helper kwarg `t[param=1]`, which simply adds the number of variables in the GTPSA to the provided index. Note that above first-order, the `param` kwarg is basically useless. The zeroth order part, or the *scalar* part of the TPS, can be set with `t[0]`. This method requires zero allocations for indexing, unlike the other two.

### Examples
```@repl
using GTPSA; GTPSA.show_sparse = false; GTPSA.show_header=false; #hide
# Example of indexing by monomial index -----------
d = Descriptor(2, 10, 1, 10);
t = TPS(use=d); # Create zero TPS based on d

t[0] = 0;
t[1] = 1;
t[2] = 2;
t[3] = 3; # or t[param=1] = 3
t[4] = 4;
t[5] = 5;
t[6] = 6;
t[7] = 7;
t[8] = 8;
t[9] = 9;
t[10] = 10;
print(t)
```



26 changes: 13 additions & 13 deletions docs/src/man/h_mono.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@
*Creates a TPS corresponding to a specific monomial*
## Syntax
```
m = mono(var_idx [, use=(descriptor|tps|complextps)])
m = mono(param=param_idx [, use=(descriptor|tps|complextps)])

m = mono(orders [, use=(descriptor|tps|complextps)])

m = mono([vars_sparse_mono] [, params=params_sparse_mono] [, use=(descriptor|tps|complextps)])

m = mono(idx [, use=(descriptor|tps|complextps)])
m = mono(param=param_idx [, use=(descriptor|tps|complextps)])

m = complexmono(...)
```

## Description
#### Index by Variable/Parameter

`m = mono(var_idx)` creates a `TPS` equal to the variable specified by `var_idx` and the `Descriptor` in `GTPSA.desc_current`
#### Indexing by Order

`m = mono(param=param_idx)` creates a `TPS` equal to the parameter specified by `param_idx` and the `Descriptor` in `GTPSA.desc_current`
`m = mono(orders)` creates a `TPS` equal to the monomial specified by the indexing-by-order vector/tuple `orders` using the `Descriptor` in `GTPSA.desc_current`

------

#### Indexing by Order
#### Indexing by Sparse Monomial

`m = mono(orders)` creates a `TPS` equal to the monomial specified by the indexing-by-order vector `orders` and the `Descriptor` in `GTPSA.desc_current`
`m = mono(vars_sparse_mono, params=params_sparse_mono)` creates a `TPS` equal to the monomial specified by the indexing-by-sparse monomial vector/tuple `vars_sparse_mono` and `params_sparse_mono` using the `Descriptor` in `GTPSA.desc_current`

------

#### Indexing by Sparse Monomial
#### Indexing by Monomial Index

`m = mono(idx)` creates a `TPS` equal to the monomial specified by `idx` and the `Descriptor` in `GTPSA.desc_current`

`m = mono(vars_sparse_mono, params=params_sparse_mono)` creates a `TPS` equal to the monomial specified by the indexing-by-sparse monomial vector `vars_sparse_mono` and `params_sparse_mono` and the `Descriptor` in `GTPSA.desc_current`
`m = mono(param=param_idx)` creates a `TPS` equal to the monomial specified by `param_idx + nv` where `nv` is the number of variables in the GTPSA, using the `Descriptor` in `GTPSA.desc_current`

------

Expand All @@ -50,9 +50,9 @@ d1 = Descriptor(3, 15, 2, 15); # 3 vars, 2 params, all to order 15
x1 = mono(1)
k1 = mono(param=1)
m312 = mono([3,1,2])
m31221 = mono([3,1,2,2,1])
m31221 = mono((3,1,2,2,1)) # Tuples allowed for indexing
m312 = mono([1=>3, 2=>1, 3=>3])
m31221 = mono([1=>3, 2=>1, 3=>2], params=[1=>2, 2=>1])
m31221 = mono((1=>3, 2=>1, 3=>2), params=(1=>2, 2=>1))
```

## Documentation
Expand Down
36 changes: 22 additions & 14 deletions docs/src/man/j_slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ d = Descriptor(5, 10, 2, 10);
x = vars(d);
k = params(d);
f = 2*x[1]^2*x[3] + 3*x[1]^2*x[2]*x[3]*x[4]^2*x[5]*k[1] + 6*x[3] + 5
g = f[2,:,1]
h = f[2,:,1,:]
g = f[[2,:,1]]
h = f[[2,:,1,:]]
```

A TPS can also be sliced with indexing by sparse monomial. In this case, if a colon is included anywhere in the sparse monomial variable index, then all orders of all variables and parameters not explicity specified will be included (colon position does not matter in sparse monomial indexing):

```@repl slice
g = f[1=>2, :, 3=>1, 4=>0, 5=>0, params=[1=>0, 2=>0]]
h = f[1=>2, 3=>1, :] # Colon position is irrelevant in slicing with sparse monomial indexing
g = f[[1=>2, :, 3=>1, 4=>0, 5=>0], params=[1=>0, 2=>0]]
h = f[(1=>2, 3=>1, :)] # Colon position is irrelevant in slicing with sparse monomial indexing
```

When indexing by monomial index, a colon simply needs to be included after the variable index, or just a colon if a parameter is specified:

```@repl slice
fx1 = f[1,:]
fk1 = f[:,param=2]
```

## `par`
Expand All @@ -30,22 +37,15 @@ h = f[1=>2, 3=>1, :] # Colon position is irrelevant in slicing with sparse mono

### Syntax
```
f = par(tps, var_idx)
f = par(tps, param=param_idx)

f = par(tps, orders)

f = par(tps [, vars_sparse_mono] [, params=params_sparse_mono])

f = par(tps, idx)
f = par(tps, param=param_idx)
```

### Description
#### Index by Variable/Parameter
`f = par(tps, var_idx)` extracts the polynomial from the TPS with a first-order dependence on the specified variable, and removes the variable from the polynomial

`f = par(tps, param=param_idx)` extracts the polynomial from the TPS with a first-order dependence on the specified parameter, and removes the parameter from the polynomial

------

#### Indexing by Order
`f = par(tps, orders)` extracts the polynomial from the TPS with the monomial indexed-by-order in `orders`, and removes the variables/parameters included in the indexing from the polynomial

Expand All @@ -58,6 +58,14 @@ f = par(tps [, vars_sparse_mono] [, params=params_sparse_mono])

`f = par(tps, vars_sparse_mono, params=params_sparse_mono)` extracts the polynomial from the TPS with the monomial indexed-by-sparse monomial in `vars_sparse_mono` and `params_sparse_mono`, and removes the variables and/or parameters included in the indexing from the polynomial

------

#### Indexing by Monomial Index
`f = par(tps, idx)` extracts the polynomial from the TPS with a first-order dependence on the specified monomial, and removes the variable from the polynomial

`f = par(tps, param=param_idx)` extracts the polynomial from the TPS with a first-order dependence on the specified monomial with index `param_idx+nv` where `nv` is the number of variables in the GTPSA, and removes the parameter from the polynomial


### Examples

```@repl par
Expand Down
19 changes: 10 additions & 9 deletions docs/src/man/k_methods.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# [TPS and Map Methods](@id tpsmapmethods)
# [TPS Methods](@id tpsmethods)
```@docs
clear!
complex!
compose!
cutord
cutord!
deriv
deriv!
evaluate
exppb
fgrad
gethamiltonian
evaluate!
getord
getvectorfield
getord!
integ
lb
logpb
pb
ptinv
integ!
scalar
translate
translate!
```
4 changes: 2 additions & 2 deletions docs/src/man/o_all.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sin, cos, tan, csc, sec, cot, sinc, sinh, cosh, tanh, csch,
sech, coth, asin, acos, atan, acsc, asec, acot, asinh, acosh,
atanh, acsch, asech, acoth, zero, one, real, imag, conj, angle,
complex, promote_rule, getindex, setindex!, ==, <, >, <=, >=,
!=, isequal, show
!=, isequal, show, copy!
```

`GTPSA.jl` overloads (and exports) the following functions from the corresponding packages:
Expand All @@ -15,7 +15,7 @@ complex, promote_rule, getindex, setindex!, ==, <, >, <=, >=,

`GTPSA.jl` also provides the following functions NOT included in Base or any of the above packages:
```
unit, sinhc, asinc, asinhc, polar, rect
unit, sinhc, asinc, asinhc, polar, rect
```

If there is a mathematical function in Base which you'd like and is not included in the above list, feel free to submit an [issue](https://github.com/bmad-sim/GTPSA.jl/issues).
55 changes: 38 additions & 17 deletions docs/src/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,25 @@ Another global variable `GTPSA.show_eps` can be set to exclude showing monomials
!!! note
The value of a partial derivative is equal to the monomial coefficient in the Taylor series multiplied by a constant factor. E.g. for an expansion around zero ``f(x)\approx f(0) + \frac{\partial f}{\partial x}\rvert_0x + \frac{1}{2!}\frac{\partial^2 f}{\partial x^2}\rvert_0 x^2 + ...``, the 2nd order monomial coefficient is ``\frac{1}{2!}\frac{\partial^2 f}{\partial x^2}\rvert_0``.

Individual monomial coefficients in a TPS `t` can be get/set with two methods of indexing:
Individual monomial coefficients in a TPS `t` can be get/set with three methods of indexing:

1. **By Order:** `t[<x_1 order>, ..., <x_nv order>]`. For example, for a TPS with variables ``x_1``, ``x_2``, the ``x_1^3x_2^1`` monomial coefficient is accessed with `t[3,1]`. The 0th order part (the *scalar* part) of the TPS is indexed with `t[0,0]` or equivalently `t[0]`, as leaving out trailing zeros for unincluded variables is allowed.
2. **By Sparse Monomial** `t[<ix_var> => <order>, ...]`. This method of indexing is convenient when a TPS contains many variables and parameters. For example, for a TPS with variables ``x_1,x_2,...x_{100}``, the ``x_{1}^3x_{99}^1`` monomial coefficient is accessed with `t[1=>3, 99=>1]`. The scalar part of the TPS cannot be get/set with this method.
1. **By Order:** `t[[<x_1 order>, ..., <x_nv order>]]`. For example, for a TPS with three variables ``x_1``, ``x_2``, and ``x_3``, the ``x_1^3x_2^1`` monomial coefficient is accessed with `t[[3,1,0]]` or equivalently `t[[3,1]]`, as leaving out trailing zeros for unincluded variables is allowed. A tuple is also allowed instead of a vector for the list of orders.
2. **By Sparse Monomial** `t[[<ix_var> => <order>, ...]]`. This method of indexing is convenient when a TPS contains many variables and parameters. For example, for a TPS with variables ``x_1,x_2,...x_{100}``, the ``x_{1}^3x_{99}^1`` monomial coefficient is accessed with `t[[1=>3, 99=>1]]`. A tuple is also allowed instead of a vector for the list of pairs.
3. **By Monomial Index** `t[idx]`. *This method is not recommended for indexing above first order.* Indexes the TPS with all monomials sorted by order. For example, for a TPS with two variables ``x_1`` and ``x_2``, the ``x_1`` monomial is indexed with `t[1]` and the ``x_1^2`` monomial is indexed with `t[3]`. The zeroth order part, or the *scalar* part of the TPS, can be set with `t[0]`. This method requires zero allocations for indexing, unlike the other two.

These two methods of indexing are best shown with an example:
These three methods of indexing are best shown with an example:

```@example
using GTPSA; GTPSA.show_header=false; GTPSA.show_sparse=false;#hide
# Example of indexing by order -----------
d = Descriptor(3, 10);
t = TPS(use=d); # Create zero TPS based on d

t[0] = 1;
t[1] = 2;
t[0,1] = 3;
t[0,0,1] = 4;
t[2,1,3] = 5;
t[[0]] = 1;
t[[1]] = 2;
t[[0,1]] = 3;
t[(0,0,1)] = 4; # Tuples also allowed
t[(2,1,3)] = 5;

print(t)
```
Expand All @@ -80,14 +81,34 @@ using GTPSA; GTPSA.show_header=false; GTPSA.show_sparse=false; #hide
d = Descriptor(3, 10);
t = TPS(use=d); # Create zero TPS based on d

t[1=>1] = 2;
t[2=>1] = 3;
t[3=>1] = 4;
t[1=>2,2=>1,3=>3] = 5;
t[[1=>1]] = 2;
t[[2=>1]] = 3;
t[[3=>1]] = 4;
t[(1=>2,2=>1,3=>3)] = 5; # Tuples also allowed

print(t)
```

```@example
using GTPSA; GTPSA.show_sparse = false; GTPSA.show_header=false; #hide
# Example of indexing by monomial index -----------
d = Descriptor(3, 10);
t = TPS(use=d); # Create zero TPS based on d

t[0] = 0;
t[1] = 1;
t[2] = 2;
t[3] = 3;
t[4] = 4;
t[5] = 5;
t[6] = 6;
t[7] = 7;
t[8] = 8;
t[9] = 9;
t[10] = 10;
print(t)
```

### Gradients, Jacobians, Hessians
The convenience getters `gradient`, `jacobian`, and `hessian` (as well as their corresponding in-place methods `gradient!`, `jacobian!`, and `hessian!`) are also provided for extracting partial derivatives from a TPS/Vector of TPSs. Note that these functions are not actually calculating anything - at this point the TPS should already have been propagated through the system, and these functions are just extracting the corresponding partial derivatives.

Expand Down Expand Up @@ -119,8 +140,8 @@ d = Descriptor(5, 10);
x = vars(d);

f = 2*x[1]^2*x[3] + 3*x[1]^2*x[2]*x[3]*x[4]^2*x[5] + 6*x[3] + 5;
g = f[2,:,1];
h = f[2,:,1,:];
g = f[[2,:,1]];
h = f[[2,:,1,:]];

print(f)
print(g)
Expand All @@ -131,8 +152,8 @@ A TPS can also be sliced with indexing by sparse monomial. In this case, if a co

```@example slice
# Colon position does not matter in sparse-monomial indexing
g = f[1=>2, :, 3=>1, 4=>0, 5=>0];
h = f[1=>2, 3=>1, :];
g = f[[1=>2, :, 3=>1, 4=>0, 5=>0]];
h = f[[1=>2, 3=>1, :]];


print(g)
Expand Down
Loading
Loading