Skip to content

Commit

Permalink
Lim (#66)
Browse files Browse the repository at this point in the history
* different display for lim

* one more tweak

* doc string update
  • Loading branch information
jverzani committed Apr 26, 2024
1 parent 9394e08 commit 689d2c8
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "CalculusWithJulia"
uuid = "a2e0e22d-7d4c-5312-9169-8b992201a882"
version = "0.2.2"
version = "0.2.3"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
145 changes: 113 additions & 32 deletions src/limits.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
"""
lim(f, c; n=5, dir="+")
lim(f, c; n=6, m=1, dir="+-")
lim(f, c, dir; n-5)
Means to generate numeric table of values of `f` as `h` gets close to `c`.
* `n`, `m`: powers of `10` to add (subtract) to (from) `c`.
* `dir`: Either `"+-"` (show left and right), `"+"` (right limit), or `"-"` (left limit). Can also use functions `+`, `-`, `±`.
Example:
```
f(x) = sin(x) / x
lim(f, 0)
julia> f(x) = sin(x) / x
f (generic function with 1 method)
julia> lim(f, 0)
0.1 0.9983341664682815
0.01 0.9999833334166665
0.001 0.9999998333333416
0.0001 0.9999999983333334
1.0e-5 0.9999999999833332
1.0e-6 0.9999999999998334
⋮ ⋮
c L?
⋮ ⋮
-1.0e-6 0.9999999999998334
-1.0e-5 0.9999999999833332
-0.0001 0.9999999983333334
-0.001 0.9999998333333416
-0.01 0.9999833334166665
-0.1 0.9983341664682815
```
"""
function lim(f::Function, c::Real; n::Int=6, m::Int=1, dir="+")
function lim(f::Function, c::Real; n::Int=6, m::Int=1, dir="+-")
dir = string(dir)
Limit(f, c, n, m, dir)
end
lim(f::Function, c::Real, dir; n::Integer=5, m::Integer=1) = lim(f,c; n, m, dir=string(dir))
lim(f::Function, c::Real, dir; n::Int=6, m::Int=1) = lim(f,c; n, m, dir=string(dir))


struct Limit{F,R}
Expand All @@ -26,41 +47,101 @@ struct Limit{F,R}
end

# try to better align numbers
_l8(x) = length(string(x)) ÷ 8

function Base.show(io::IO, L::Limit)
(; f,c,n,m,dir) = L

ms = maximum(length string, (c-1/10^n, c+1/10^n))
sc = (sign(c-m) * sign(c+m) < 0)

h = 1/10^n
nt = 2 + maximum(_l8, (c-h, c+h)) + (n ÷ 8)

if dir == "+" || dir == "+-" || dir == "±"
show₊(io, L; sc, ms)
end
print_dots(io, "c", "L?"; sc, ms)
if dir == "-" || dir == "--" || dir == "+-" || dir == "±"
show₋(io, L; sc, ms)
end
nothing
end

function show₊(io::IO, L::Limit; sc=false, ms=0)

(; f,c,n,m,dir) = L

hs = [1/10^i for i in m:n] # close to 0
if dir == "+"
xs = c .+ hs
else
xs = c .- hs
xsᵣ = c .+ hs
ysᵣ = string.(f.(xsᵣ))

last_y = nothing

for (x,y) zip(xsᵣ, ysᵣ)
print_next(io, x, y, last_y; sc, ms)
last_y = y
println(io, "")
end
ys = string.(map(f, xs))

_l8(x) = length(string(x)) ÷ 8
nt = 2 + _l8(first(xs)) + (n ÷ 8)
nl = false
print_dots(io; sc, ms)
end

# show - case
function show₋(io::IO, L::Limit; sc=false, ms=0)
(; f,c,n,m,dir) = L

hs = [1/10^i for i in n:-1:m] # close to 0
xsₗ = c .- hs
ysₗ = string.(f.(xsₗ))

last_y = nothing
for (x,y) zip(xs, ys)
nl && println(io, "")
nl = true
print(io, x)
m = _l8(x)
print(io, "\t"^(nt-m))
if isnothing(last_y)
print(io, y)

i, l = 1, length(ysₗ)
nl = true
#dir == "-" && print_dots(io; sc, ms)
print_dots(io; sc, ms)
for (x, y) zip(xsₗ, ysₗ)
if i == l
last_y = nothing
nl = false
else
flag = true
ly = length(last_y)
for (i,yᵢ) enumerate(y)
if flag && i <= ly && yᵢ == last_y[i]
printstyled(io, yᵢ; bold=true)
else
print(io, yᵢ)
flag=false
end
last_y = ysₗ[i+1]
i += 1
end
print_next(io, x, y, last_y; sc, ms)
nl && println(io, "")
end

end

# print dots or c L
function print_dots(io::IO, l="", r=""; sc, ms)
d = ms ÷ 2
println(io, " "^d, l, " "^(4 +2d), r)
end


# print next number referring to last one for styling
function print_next(io::IO, x, y, last_y=nothing; sc=false, ms=0)
xₛ = string(x)
sc && x > 0 && (xₛ = " " * xₛ)

print(io, xₛ)
l = length(xₛ)
print(io, " "^(ms - l + 5))
if isnothing(last_y)
print(io, y)
else
flag = true
ly = length(last_y)
for (i,yᵢ) enumerate(y)
if flag && i <= ly && yᵢ == last_y[i]
printstyled(io, yᵢ; bold=true)
else
print(io, yᵢ)
flag=false
end
end
last_y = y
end
nothing
end

2 comments on commit 689d2c8

@jverzani
Copy link
Owner Author

Choose a reason for hiding this comment

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

@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/105636

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

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.2.3 -m "<description of version>" 689d2c8902f16fa460804847e9d3143a30cdc8f7
git push origin v0.2.3

Please sign in to comment.