Skip to content

Commit

Permalink
find zeros and values, middle_exp, release v0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
islent committed Oct 22, 2024
1 parent 2438857 commit 772ee24
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
version:
- '1.9.4'
- '1.11.1'
os:
- ubuntu-latest
- macOS-latest
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AstroSimBase"
uuid = "c6a34d98-f626-4d8d-b450-a3f124eaada6"
authors = ["islent <[email protected]>"]
version = "0.1.2"
version = "0.1.3"

[deps]
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Expand Down
55 changes: 55 additions & 0 deletions src/AstroSimBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export randin
export mkpathIfNotExist
export need_to_interrupt, interrupt

export findfirstzero, findzero, findfirstvalue, findvalue
export middle_exp

# Traits
abstract type LoggingMode end
struct NormalMode <: LoggingMode end
Expand Down Expand Up @@ -85,6 +88,11 @@ function emptyfunction(args...) end
"""
function emptyfunction(args...) end

"""
function mkpathIfNotExist(dir)
If the `dir` does not exist, create the path.
"""
function mkpathIfNotExist(dir)
if !isdir(dir)
mkpath(dir)
Expand Down Expand Up @@ -128,6 +136,53 @@ Generate uniform random number in `[a,b]`. It avoids error from `rand(a:b)` wher
randin(T, a, b) = T(rand(T) * (b-a) + a)
randin(a, b) = rand() * (b-a) + a


"""
function findfirstzero(a::AbstractVector)
find first zero point of discrete value array
"""
function findfirstzero(a::AbstractVector)
findfirst(!iszero, diff(sign.(a)))
end

"""
function findzero(a::AbstractVector)
find all zero points (indices of left-hand-side and right-hand-side boundaries) of discrete value array
"""
function findzero(a::AbstractVector)
findall(!iszero, diff(sign.(a)))
end

"""
function findfirstvalue(a::AbstractVector, v)
find zero point of discrete value array
"""
function findfirstvalue(a::AbstractVector, v)
findfirstzero(a .- v)
end

"""
function findvalue(a::AbstractVector, v)
find all value points (indices of left-hand-side and right-hand-side boundaries) of discrete value array
"""
function findvalue(a::AbstractVector, v)
findzero(a .- v)
end


"""
function middle_exp(a::AbstractVector)
Find 1/e value ralative to maximum and minimum
"""
function middle_exp(a::AbstractVector)
(maximum(a) - minimum(a))/exp(1) - minimum(a)
end

include("precompile.jl")

end # module AstroSimBase
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,25 @@ end

r = randin(2,3)
@test 2<=r<=3

@test middle_exp([0,1,exp(1),1,0]) == 1

## find functions
@test findfirstzero([-1, 0, 1]) == 1

@test all(
findzero([-1, 0, 1, 0, -1]) .== [1, 2, 3, 4]
)
@test all(
findzero([-2, -1, 0, 1, 2]) .== [2, 3]
)

@test findfirstvalue([1,2,3], 2) == 1

@test all(
findvalue([1,2,3,2,1], 2) .== [1, 2, 3, 4]
)
@test all(
findvalue([1,2,3,4,5], 3) .== [2, 3]
)
end

2 comments on commit 772ee24

@islent
Copy link
Member Author

@islent islent commented on 772ee24 Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

  • find zeros and values, middle_exp

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/117799

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.3 -m "<description of version>" 772ee2491209e5ea7e24ef9ee58a93f2e43b3b69
git push origin v0.1.3

Please sign in to comment.