Skip to content

Commit

Permalink
rand TPS generator implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsignorelli committed Apr 25, 2024
1 parent 6793df9 commit 5f5b93a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/src/man/o_all.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion src/GTPSA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ import Base: +,
isequal,
show,
copy!,
length
length,
lastindex,
firstindex,
rand

import LinearAlgebra: norm, mul!
import SpecialFunctions: erf, erfc
Expand Down
14 changes: 14 additions & 0 deletions src/getset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ---

Expand Down
10 changes: 10 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5f5b93a

Please sign in to comment.