Skip to content

Commit

Permalink
better type for derivatives
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieugomez committed Jun 19, 2024
1 parent eceeec9 commit f36bad5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "InfinitesimalGenerators"
uuid = "2fce0c6f-5f0b-5c85-85c9-2ffe1d5ee30d"
version = "2.1.0"
version = "2.2.0"

[deps]
Arpack = "7d9fca2a-8960-54d3-9f78-7d1dccf2cb97"
Expand Down
28 changes: 14 additions & 14 deletions src/derivatives.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@

struct FirstDerivative{T} <: AbstractVector{T}
x::AbstractVector{<:Real}
y::AbstractVector{T}
struct FirstDerivative{T, X <: AbstractVector{<:Real}, Y <: AbstractVector{<: Real}} <: AbstractVector{T}
x::X
y::Y
bc::NTuple{2}{T}
direction::Symbol
function FirstDerivative{T}(x, y, bc, direction) where {T}
function FirstDerivative(x, y, bc, direction)
size(x) == size(y) || throw(DimensionMismatch(
"cannot match grid of length $(length(x)) with vector of length $(length(y))"))
direction (:upward, :downward) || throw(ArgumentError("direction must be :upward or :downward"))
return new(x, y, bc, direction)
return new{float(eltype(y)), typeof(x), typeof(y)}(x, y, bc, direction)
end
end

function FirstDerivative(x::AbstractVector, y::AbstractVector; bc = (0, 0), direction = :upward)
FirstDerivative{eltype(y)}(x, y, bc, direction)
function FirstDerivative(x, y; bc = (0, 0), direction = :upward)
FirstDerivative(x, y, bc, direction)
end

Base.size(d::FirstDerivative) = (length(d.x), 1)
Expand All @@ -40,19 +40,19 @@ function Base.getindex(d::FirstDerivative{T}, i::Int) where {T}
end


struct SecondDerivative{T} <: AbstractVector{T}
x::AbstractVector{<:Real}
y::AbstractVector{T}
struct SecondDerivative{T, X <: AbstractVector{<:Real}, Y <: AbstractVector{<: Real}} <: AbstractVector{T}
x::X
y::Y
bc::NTuple{2}{T}
function SecondDerivative{T}(x, y, bc) where {T}
function SecondDerivative(x, y, bc)
length(x) == length(y) || throw(DimensionMismatch(
"cannot match grid of length $(length(x)) with vector of length $(length(y))"))
return new(x, y, bc)
return new{float(eltype(y)), typeof(x), typeof(y)}(x, y, bc)
end
end

function SecondDerivative(x::AbstractVector, y::AbstractVector; bc = (0, 0))
SecondDerivative{eltype(y)}(x, y, bc)
function SecondDerivative(x, y; bc = (0, 0))
SecondDerivative(x, y, bc)
end

Base.size(d::SecondDerivative) = (length(d.x), 1)
Expand Down
6 changes: 2 additions & 4 deletions src/jointoperator.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
function jointoperator(operators, Q::Array)
function jointoperator(operators::AbstractVector{<:Tridiagonal}, Q::Array)
N = length(operators)
wn = size(operators[1], 1)
@assert all(o isa Tridiagonal for o in operators)
# check if all os have same size
@assert all(size(o) == (wn, wn) for o in operators)
# check if the size of transition matrix is
# same as the number of operators
# check if the size of transition matrix is same as the number of operators
@assert size(Q,1) == size(Q,2) == N
J = BandedBlockBandedMatrix(Zeros(wn * N, wn * N), fill(wn, N) ,fill(wn, N), (N-1, N-1), (1, 1))
for i in 1:N
Expand Down

0 comments on commit f36bad5

Please sign in to comment.