June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,15 +1,17 @@
|
|||
function nthroot(n::Integer, A::Real)
|
||||
A < 0 || n == 0 && throw(DomainError())
|
||||
n < 0 && return 1/nthroot(-n, A)
|
||||
A == 0 && return A
|
||||
x = A / n
|
||||
prevdx = A
|
||||
function nthroot(n::Integer, r::Real)
|
||||
r < 0 || n == 0 && throw(DomainError())
|
||||
n < 0 && return 1 / nthroot(-n, r)
|
||||
r > 0 || return 0
|
||||
x = r / n
|
||||
prevdx = r
|
||||
while true
|
||||
y = x^(n-1)
|
||||
dx = (A - y*x) / (n * y)
|
||||
abs(dx) >= abs(prevdx) && return x
|
||||
y = x ^ (n - 1)
|
||||
dx = (r - y * x) / (n * y)
|
||||
abs(dx) ≥ abs(prevdx) && return x
|
||||
x += dx
|
||||
prevdx = dx
|
||||
end
|
||||
end
|
||||
@vectorize_2arg Number nthroot
|
||||
|
||||
@show nthroot.(-5:2:5, 5.0)
|
||||
@show nthroot.(-5:2:5, 5.0) - 5.0 .^ (1 ./ (-5:2:5))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue