diff --git a/docs/src/man/o_all.md b/docs/src/man/o_all.md index 26d1de13..b9b619fa 100644 --- a/docs/src/man/o_all.md +++ b/docs/src/man/o_all.md @@ -6,7 +6,8 @@ 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, zeros, one, ones, real, imag, conj, angle, complex, promote_rule, getindex, setindex!, ==, <, ->, <=, >=, !=, isequal, show, copy!, length +>, <=, >=, !=, isequal, show, copy!, length, lastindex, firstindex, +rand ``` `zeros` and `ones` are overloaded from Base so that allocated `TPS`/`ComplexTPS`s are placed in each element. If we didn't explicity overload these functions, every element would correspond to the exact same heap-allocated TPS, which is problematic when setting individual monomial coefficients of the same TPS. diff --git a/src/GTPSA.jl b/src/GTPSA.jl index c64651ba..e52eeca5 100644 --- a/src/GTPSA.jl +++ b/src/GTPSA.jl @@ -61,7 +61,10 @@ import Base: +, isequal, show, copy!, - length + length, + lastindex, + firstindex, + rand import LinearAlgebra: norm, mul! import SpecialFunctions: erf, erfc diff --git a/src/getset.jl b/src/getset.jl index 5030ad83..b3d575d7 100644 --- a/src/getset.jl +++ b/src/getset.jl @@ -120,6 +120,20 @@ end lowget(t, idx, param, params) = error("Invalid monomial index specified. Please use ONE of variable/parameter index, index by order, or index by sparse monomial.") +# --- length --- +len(t::Ptr{RTPSA}) = mad_tpsa_len(t) +len(t::Ptr{CTPSA}) = mad_ctpsa_len(t) + +""" + length(t::Union{TPS,ComplexTPS} + +Returns the maximum number of monomials (including the scalar part) +in the `TPS`/`ComplexTPS` given its `Descriptor`. +""" +length(t::Union{TPS,ComplexTPS}) = len(t.tpsa) + +firstindex(t::Union{TPS,ComplexTPS}) = 0 +lastindex(t::Union{TPS,ComplexTPS}) = length(t)-1 # --- Slicing (getter) --- diff --git a/src/operators.jl b/src/operators.jl index cd64a228..fd8c45dc 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -56,6 +56,16 @@ for (fname, felt) in ((:zeros, :zero), (:ones, :one)) end end +# --- rand --- +function rand(::Type{T}; use::Union{Descriptor,TPS,ComplexTPS}=GTPSA.desc_current) where {T<:Union{TPS,ComplexTPS}} + t = T(use=use) + len = length(t) + for i=0:len-1 + t[i] = rand(numtype(T)) + end + return t +end + # --- Unary --- # TPS: function +(t1::TPS)::TPS