Skip to content

Commit

Permalink
Improved show, removed 'use' from vars again
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsignorelli committed Feb 23, 2024
1 parent e856645 commit af4d385
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 45 deletions.
2 changes: 1 addition & 1 deletion docs/src/man/fastgtpsa.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using GTPSA, BenchmarkTools
GTPSA.show_sparse = false; GTPSA.show_header=false; # hide
d = Descriptor(3, 5);
x = vars(use=d);
x = vars(d);
@btime $x[1]^3*sin($x[2])/log(2+$x[3])-exp($x[1]*$x[2])*im;
Expand Down
2 changes: 1 addition & 1 deletion docs/src/man/gjh.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ hessian!(H, f [, include_params=bool])
```@repl
using GTPSA; #hide
d = Descriptor(2,10);
x = vars(use=d);
x = vars(d);
f = x[1] + 2*x[2] + 3*x[1]^2 + 4*x[1]*x[2] + 5*x[2]^2;
g = 5*x[1] + 4*x[2] + 3*x[1]^2 + 2*x[1]*x[2] + x[2]^2;
grad = gradient(f)
Expand Down
8 changes: 4 additions & 4 deletions docs/src/man/monoindex.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ A particular monomial can be indexed by specifying the orders of each variable a
```@repl
using GTPSA; GTPSA.show_sparse = false; GTPSA.show_header=false;#hide
d = Descriptor(2, 6, 3, 6); # 2 variables, 3 parameters all to 6th order
x = vars(use=d);
k = params(use=d);
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
Expand All @@ -31,8 +31,8 @@ In GTPSAs with many variables and parameters, indexing-by-order is inconvenient
using GTPSA; GTPSA.show_sparse = false; GTPSA.show_header=false; #hide
d = Descriptor(15, 6, 10, 6); # 15 variables, 10 parameters all to 6th order
GTPSA.show_sparse = true; # Use sparse output
x = vars(use=d);
k = params(use=d);
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
Expand Down
8 changes: 4 additions & 4 deletions docs/src/man/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ A polynomial within the TPS with certain variable orders can be extracted by sli
```@repl slice
using GTPSA; GTPSA.show_sparse = false; GTPSA.show_header = false; #hide
d = Descriptor(5, 10, 2, 10);
x = vars(use=d);
k = params(use=d);
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,:]
Expand Down Expand Up @@ -63,8 +63,8 @@ f = par(tps [, vars_sparse_mono] [, params=params_sparse_mono])
```@repl par
using GTPSA; GTPSA.show_sparse = false; GTPSA.show_header=false; #hide
d = Descriptor(5, 10, 2, 10);
x = vars(use=d);
k = params(use=d);
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
par(f, 3)
par(f, param=1)
Expand Down
16 changes: 8 additions & 8 deletions docs/src/man/varsparams.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
*Creates a vector of TPSs corresponding to each variable/parameter in the GTPSA*
## Syntax
```
x = vars([use=(descriptor|tps|complextps)])
xc = complexvars([use=(descriptor|tps|complextps)])
x = vars([(descriptor|tps|complextps)])
xc = complexvars([(descriptor|tps|complextps)])
k = params([use=(descriptor|tps|complextps)])
kc = complexparams([use=(descriptor|tps|complextps)])
k = params([(descriptor|tps|complextps)])
kc = complexparams([(descriptor|tps|complextps)])
```

## Description
Expand All @@ -20,11 +20,11 @@ kc = complexparams([use=(descriptor|tps|complextps)])

`k = complexparams()` creates a vector of `ComplexTPS`s corresponding to each of the parameters in the GTPSA defined by the `Descriptor` in `GTPSA.desc_current`

### Optional Keyword Argument
### Optional Argument

`use=descriptor` creates a vector of `TPS`/`ComplexTPS`s corresponding to each of the variables/parameters in the GTPSA defined by the passed `Descriptor`
`descriptor` creates a vector of `TPS`/`ComplexTPS`s corresponding to each of the variables/parameters in the GTPSA defined by the passed `Descriptor`

`use=(tps|complextps)` creates a vector of `TPS`/`ComplexTPS`s corresponding to each of the variables/parameters in the GTPSA defined by the `Descriptor` of the passed `TPS` or `ComplexTPS`
`(tps|complextps)` creates a vector of `TPS`/`ComplexTPS`s corresponding to each of the variables/parameters in the GTPSA defined by the `Descriptor` of the passed `TPS` or `ComplexTPS`


## Examples
Expand All @@ -37,7 +37,7 @@ k1 = params()
d2 = Descriptor(2, 5, 1, 5); # 2 vars, 1 param, all to order 5
x2 = vars()
k2 = params()
k1 = params(use=d1)
k1 = params(d1)
```

## Documentation
Expand Down
10 changes: 5 additions & 5 deletions docs/src/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using GTPSA #hide
d = Descriptor([4, 1]);
# Returns a Vector of each variable as a TPS
x = vars(use=d)
x = vars(d)
```

These `TPS`s can then be manipulated just like any other mathematical quantity in Julia:
Expand All @@ -37,7 +37,7 @@ When a TPS contains a lot of variables, the default output showing each variable
```@example
using GTPSA #hide
d = Descriptor(10, 10);
x = vars(use=d);
x = vars(d);
GTPSA.show_sparse = true;
g = sin(x[1]*x[3]^2) + cos(x[2]*x[7]);
Expand Down Expand Up @@ -95,7 +95,7 @@ The convenience getters `gradient`, `jacobian`, and `hessian` (as well as their
using GTPSA #hide
# 2nd Order TPSA with 100 variables
d = Descriptor(100, 2);
x = vars(use=d);
x = vars(d);
out = cumsum(x);
Expand All @@ -116,7 +116,7 @@ Parts of a TPS with certain variable orders can be extracted by slicing the TPS.
```@example slice
using GTPSA; #hide
d = Descriptor(5, 10);
x = vars(use=d);
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];
Expand Down Expand Up @@ -147,7 +147,7 @@ The macro `@FastGTPSA` can be used to speed up evaluation of expressions that co
using GTPSA, BenchmarkTools
d = Descriptor(3, 5);
x = vars(use=d);
x = vars(d);
@btime $x[1]^3*sin($x[2])/log(2+$x[3])-exp($x[1]*$x[2])*im;
Expand Down
16 changes: 8 additions & 8 deletions src/GTPSA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ end
# --- Variable/parameter generators ---

"""
vars(;use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{TPS}
vars(use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{TPS}
Returns `TPS`s corresponding to the variables for the `Descriptor` specified by `use`.
Default value is `GTPSA.desc_current`.
Expand All @@ -508,7 +508,7 @@ Default value is `GTPSA.desc_current`.
### Output
- `x` -- `Vector` containing unit `TPS`s corresponding to each variable
"""
function vars(;use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{TPS}
function vars(use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{TPS}
t1 = TPS(use=use)
desc = unsafe_load(mad_tpsa_desc(t1.tpsa))
nv = desc.nv
Expand All @@ -527,7 +527,7 @@ function vars(;use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector
end

"""
params(;use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{TPS}
params(use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{TPS}
Returns `TPS`s corresponding to the parameters for the `Descriptor` specified by `use`.
Default value is `GTPSA.desc_current`.
Expand All @@ -538,7 +538,7 @@ Default value is `GTPSA.desc_current`.
### Output
- `k` -- `Vector` containing unit `TPS`s corresponding to each parameter
"""
function params(;use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{TPS}
function params(use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{TPS}
t1 = TPS(use=use)
desc = unsafe_load(mad_tpsa_desc(t1.tpsa))
nv = desc.nv
Expand All @@ -559,7 +559,7 @@ end


"""
complexvars(;use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{ComplexTPS}
complexvars(use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{ComplexTPS}
Returns `ComplexTPS`s corresponding to the variables for the `Descriptor` specified by `use`.
Default value is `GTPSA.desc_current`.
Expand All @@ -570,7 +570,7 @@ Default value is `GTPSA.desc_current`.
### Output
- `x` -- `Vector` containing unit `ComplexTPS`s corresponding to each variable
"""
function complexvars(;use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{ComplexTPS}
function complexvars(use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{ComplexTPS}
ct1 = ComplexTPS(use=use)
desc = unsafe_load(mad_ctpsa_desc(ct1.tpsa))
nv = desc.nv
Expand All @@ -589,7 +589,7 @@ function complexvars(;use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current):
end

"""
complexparams(;d::Descriptor=GTPSA.desc_current)::Vector{ComplexTPS}
complexparams(d::Descriptor=GTPSA.desc_current)::Vector{ComplexTPS}
Returns `ComplexTPS`s corresponding to the parameters for the `Descriptor` specified by `use`.
Default value is `GTPSA.desc_current`.
Expand All @@ -600,7 +600,7 @@ Default value is `GTPSA.desc_current`.
### Output
- `k` -- `Vector` containing unit `ComplexTPS`s corresponding to each parameter
"""
function complexparams(;use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{ComplexTPS}
function complexparams(use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current)::Vector{ComplexTPS}
ct1 = ComplexTPS(use=use)
desc = unsafe_load(mad_ctpsa_desc(ct1.tpsa))
nv = desc.nv
Expand Down
2 changes: 1 addition & 1 deletion src/fast_gtpsa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ to expressions while still maintaining type-generic code.
```julia-repl
julia> using GTPSA, BenchmarkTools
julia> d = Descriptor(3,5); x = vars(use=d);
julia> d = Descriptor(3,5); x = vars(d);
julia> @btime \$x[1]^3*sin(\$x[2])/log(2+\$x[3])-exp(\$x[1]*\$x[2])*im;
2.114 μs (10 allocations: 160 bytes)
Expand Down
2 changes: 1 addition & 1 deletion src/getset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Extracts a polynomial from the TPS containing the specified monomial, and remove
# Examples: Variable/Parameter Index:
```julia-repl
julia> d = Descriptor(5, 10, 2, 10); x = vars(use=d); k = params(use=d);
julia> d = Descriptor(5, 10, 2, 10); x = vars(d); k = params(d);
julia> 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
TPS:
Expand Down
17 changes: 9 additions & 8 deletions src/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ alternatively any monomial specified by indexing-by-order OR indexing-by-sparse
```julia-repl
julia> d = Descriptor(1,5,1,5);
julia> x1 = vars(use=d)[1]; k1 = params(use=d)[1];
julia> x1 = vars(d)[1]; k1 = params(d)[1];
julia> deriv(x1*k1, 1)
TPS:
Expand Down Expand Up @@ -239,7 +239,7 @@ is negative, will cut monomials with orders at and below `abs(order)`.
```julia-repl
julia> d = Descriptor(1,10);
julia> x = vars(use=d);
julia> x = vars(d);
julia> cutord(sin(x[1]), 5)
TPS:
Expand Down Expand Up @@ -293,7 +293,7 @@ of the scalar functions `f` and `g`. The Poisson bracket of two functions `{f, g
```julia-repl
julia> d = Descriptor(4,10);
julia> x = vars(use=d);
julia> x = vars(d);
julia> f = (x[1]^2 + x[2]^2)/2 + (x[3]^2 + x[4]^2)/2;
Expand Down Expand Up @@ -483,7 +483,7 @@ exppb!(na::Cint, ma::Vector{Ptr{RTPSA}}, mb::Vector{Ptr{RTPSA}}, mc::Vector{Ptr{
exppb!(na::Cint, ma::Vector{Ptr{CTPSA}}, mb::Vector{Ptr{CTPSA}}, mc::Vector{Ptr{CTPSA}}) = (@inline; mad_ctpsa_exppb!(na, ma, mb, mc))

"""
exppb(F::Vector{<:Union{TPS,ComplexTPS}}, m::Vector{<:Union{TPS,ComplexTPS}}=vars(use=first(F)))
exppb(F::Vector{<:Union{TPS,ComplexTPS}}, m::Vector{<:Union{TPS,ComplexTPS}}=vars(first(F)))
Calculates `exp(F⋅∇)m = m + F⋅∇m + (F⋅∇)²m/2! + ...`. If `m` is not provided, it is assumed
to be the identity.
Expand All @@ -510,7 +510,7 @@ julia> map = exppb(-time*hf, [x, p])
2: 9.9001665555952378e-01 1 0 1
```
"""
function exppb(F::Vector{<:Union{TPS,ComplexTPS}}, m::Vector{<:Union{TPS,ComplexTPS}}=vars(use=first(F)))
function exppb(F::Vector{<:Union{TPS,ComplexTPS}}, m::Vector{<:Union{TPS,ComplexTPS}}=vars(first(F)))
descF = unsafe_load(Base.unsafe_convert(Ptr{Desc}, unsafe_load(F[1].tpsa).d))
if length(F) != descF.nv
error("Vector length != number of variables in the GTPSA")
Expand All @@ -529,7 +529,7 @@ logpb!(na::Cint, ma::Vector{Ptr{RTPSA}}, mb::Vector{Ptr{RTPSA}}, mc::Vector{Ptr{
logpb!(na::Cint, ma::Vector{Ptr{CTPSA}}, mb::Vector{Ptr{CTPSA}}, mc::Vector{Ptr{CTPSA}}) = (@inline; mad_ctpsa_logpb!(na, ma, mb, mc))

"""
logpb(mf::Vector{<:Union{TPS,ComplexTPS}}, mi::Vector{<:Union{TPS,ComplexTPS}}=vars(use=first(F)))
logpb(mf::Vector{<:Union{TPS,ComplexTPS}}, mi::Vector{<:Union{TPS,ComplexTPS}}=vars(first(F)))
Given a final map `mf` and initial map `mi`, this function calculates the vector field `F`
such that `mf=exp(F⋅∇)mi`. If `mi` is not provided, it is assumed to be the identity.
Expand All @@ -549,7 +549,7 @@ julia> logpb(map) == -time*hf
true
```
"""
function logpb(mf::Vector{<:Union{TPS,ComplexTPS}}, mi::Vector{<:Union{TPS,ComplexTPS}}=vars(use=first(mf)))
function logpb(mf::Vector{<:Union{TPS,ComplexTPS}}, mi::Vector{<:Union{TPS,ComplexTPS}}=vars(first(mf)))
desc = unsafe_load(Base.unsafe_convert(Ptr{Desc}, unsafe_load(mf[1].tpsa).d))
if length(mf) != desc.nv || length(mi) != desc.nv
error("Vector length != number of variables in the GTPSA")
Expand Down Expand Up @@ -590,6 +590,7 @@ end
mnrm(na::Cint, ma::Vector{Ptr{RTPSA}})::Float64 = mad_tpsa_mnrm(na, ma)
mnrm(na::Cint, ma::Vector{Ptr{CTPSA}})::ComplexF64 = mad_ctpsa_mnrm(na, ma)

#=
"""
norm(ma::Vector{<:Union{TPS,ComplexTPS}})
Expand All @@ -599,7 +600,7 @@ sum of the absolute value of all coefficients in each TPS.
function norm(ma::Vector{<:Union{TPS,ComplexTPS}})
return mnrm(Cint(length(ma)), map(x->x.tpsa, ma))
end

=#
# --- map inversion ---
minv!(na::Cint, ma::Vector{Ptr{RTPSA}}, mc::Vector{Ptr{RTPSA}}) = (@inline; mad_tpsa_minv!(na, ma, mc))
minv!(na::Cint, ma::Vector{Ptr{CTPSA}}, mc::Vector{Ptr{CTPSA}}) = (@inline; mad_ctpsa_minv!(na, ma, mc))
Expand Down
6 changes: 6 additions & 0 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ function show_map!(io::IO, m::Vector{<:Union{TPS,ComplexTPS}}, lines_used::Ref=R
end
# Check if sparse monomial or exponent:
!get(io, :limit, false) || lines_used[] < displaysize(io)[1]-5 || (println(io, "\t"); return)
cols = length(out[1,:])
if GTPSA.show_sparse
if eltype(m) == TPS
println(io, " Out Coefficient Order Monomial")
Expand All @@ -351,12 +352,17 @@ function show_map!(io::IO, m::Vector{<:Union{TPS,ComplexTPS}}, lines_used::Ref=R
else
if eltype(m) == TPS
println(io, " Out Coefficient Order Exponent")
cols += 105
else
println(io, " Out Real Imag Order Exponent")
cols += 108
end
end
lines_used[] += 1
!get(io, :limit, false) || lines_used[] < displaysize(io)[1]-5 || (println(io, "\t"); return)
pretty_table(io, out,tf=tf_GTPSA,formatters=(ft_printf("%3i:",1), formatters...),show_header=false, alignment=:l, hlines=hlines, body_hlines_format=('-','-','-','-'),display_size=(displaysize(io)[1]-2-lines_used[],displaysize(io)[2]),vlines=[])
lines_used[] += length(out[:,1])+N # each border is a line
if cols >= displaysize(io)[2] #&& !(lines_used[]+1 < displaysize(io)[1]-5)
lines_used[] += 1
end
end
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ end

@testset "Indexing" begin
d = Descriptor(3,10,2,10)
v = vars(use=d)
p = params(use=d)
v = vars(d)
p = params(d)
tol = 1e-18

f = sin(v[1])
Expand Down
4 changes: 2 additions & 2 deletions test/type_stable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ function type_stable_test()
norm(complex(t) - t)

d = Descriptor(3,10,2,10)
v = vars(use=d)
p = params(use=d)
v = vars(d)
p = params(d)
tol = 1e-18

f = sin(v[1])
Expand Down

0 comments on commit af4d385

Please sign in to comment.