Skip to content

Commit cb42f36

Browse files
Merge pull request #80 from johnmyleswhite/ml/gradient
deprecate extension of Base.gradient
2 parents a28ed36 + e17b523 commit cb42f36

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Most users will want to work with a limited set of basic functions:
1818

1919
* `derivative()`: Use this for functions from R to R
2020
* `second_derivative()`: Use this for functions from R to R
21-
* `gradient()`: Use this for functions from R^n to R
21+
* `Calculus.gradient()`: Use this for functions from R^n to R
2222
* `hessian()`: Use this for functions from R^n to R
2323
* `differentiate()`: Use this to perform symbolic differentiation
2424
* `simplify()`: Use this to perform symbolic simplification
@@ -42,14 +42,14 @@ There are a few basic approaches to using the Calculus package:
4242
# Compare with cos(1.0)
4343
derivative(sin, 1.0)
4444
# Compare with cos(pi)
45-
derivative(sin, float64(pi))
45+
derivative(sin, float(pi))
4646

4747
# Compare with [cos(0.0), -sin(0.0)]
48-
gradient(x -> sin(x[1]) + cos(x[2]), [0.0, 0.0])
48+
Calculus.gradient(x -> sin(x[1]) + cos(x[2]), [0.0, 0.0])
4949
# Compare with [cos(1.0), -sin(1.0)]
50-
gradient(x -> sin(x[1]) + cos(x[2]), [1.0, 1.0])
50+
Calculus.gradient(x -> sin(x[1]) + cos(x[2]), [1.0, 1.0])
5151
# Compare with [cos(pi), -sin(pi)]
52-
gradient(x -> sin(x[1]) + cos(x[2]), [float64(pi), float64(pi)])
52+
Calculus.gradient(x -> sin(x[1]) + cos(x[2]), [float64(pi), float64(pi)])
5353

5454
# Compare with -sin(0.0)
5555
second_derivative(sin, 0.0)
@@ -74,7 +74,7 @@ There are a few basic approaches to using the Calculus package:
7474
g1(1.0)
7575
g1(pi)
7676

77-
g2 = gradient(x -> sin(x[1]) + cos(x[2]))
77+
g2 = Calculus.gradient(x -> sin(x[1]) + cos(x[2]))
7878
g2([0.0, 0.0])
7979
g2([1.0, 1.0])
8080
g2([pi, pi])

src/Calculus.jl

-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ module Calculus
1010
deparse,
1111
derivative,
1212
differentiate,
13-
gradient,
1413
hessian,
1514
jacobian,
1615
second_derivative
1716

18-
import Base: gradient
19-
2017
# TODO: Debate type system more carefully
2118
# abstract BundledFunction
2219
# abstract ScalarFunction

src/derivative.jl

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ derivative(f::Function, dtype::Symbol = :central) = derivative(f, :scalar, dtype
1414
Compat.@compat gradient{T <: Number}(f::Function, x::Union{T, Vector{T}}, dtype::Symbol = :central) = finite_difference(f, float(x), dtype)
1515
gradient(f::Function, dtype::Symbol = :central) = derivative(f, :vector, dtype)
1616

17+
Compat.@compat function Base.gradient{T <: Number}(f::Function, x::Union{T, Vector{T}}, dtype::Symbol = :central)
18+
Base.warn_once("The finite difference methods from Calculus.jl no longer extend Base.gradient and should be called as Calculus.gradient instead. This usage is deprecated.")
19+
Calculus.gradient(f,x,dtype)
20+
end
21+
22+
function Base.gradient(f::Function, dtype::Symbol = :central)
23+
Base.warn_once("The finite difference methods from Calculus.jl no longer extend Base.gradient and should be called as Calculus.gradient instead. This usage is deprecated.")
24+
Calculus.gradient(f,dtype)
25+
end
26+
1727
ctranspose(f::Function) = derivative(f)
1828

1929
function jacobian{T <: Number}(f::Function, x::Vector{T}, dtype::Symbol)

test/derivative.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ end
2727
#
2828

2929
f4(x::Vector) = (100.0 - x[1])^2 + (50.0 - x[2])^2
30-
@test norm(gradient(f4, :forward)([100.0, 50.0]) - [0.0, 0.0]) < 10e-4
31-
@test norm(gradient(f4, :central)([100.0, 50.0]) - [0.0, 0.0]) < 10e-4
32-
@test norm(gradient(f4)([100.0, 50.0]) - [0.0, 0.0]) < 10e-4
30+
@test norm(Calculus.gradient(f4, :forward)([100.0, 50.0]) - [0.0, 0.0]) < 10e-4
31+
@test norm(Calculus.gradient(f4, :central)([100.0, 50.0]) - [0.0, 0.0]) < 10e-4
32+
@test norm(Calculus.gradient(f4)([100.0, 50.0]) - [0.0, 0.0]) < 10e-4
3333

3434
#
3535
# second_derivative()
@@ -45,5 +45,5 @@ f4(x::Vector) = (100.0 - x[1])^2 + (50.0 - x[2])^2
4545
#
4646

4747
f5(x) = sin(x[1]) + cos(x[2])
48-
@test norm(gradient(f5)([0.0, 0.0]) - [cos(0.0), -sin(0.0)]) < 10e-4
48+
@test norm(Calculus.gradient(f5)([0.0, 0.0]) - [cos(0.0), -sin(0.0)]) < 10e-4
4949
@test norm(hessian(f5)([0.0, 0.0]) - [-sin(0.0) 0.0; 0.0 -cos(0.0)]) < 10e-4

test/finite_difference.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
#
4848

4949
fx(x) = sin(x[1]) + cos(x[2])
50-
gx = gradient(fx)
50+
gx = Calculus.gradient(fx)
5151
@test norm(gx([0.0, 0.0]) - [cos(0.0), -sin(0.0)]) < 10e-4
5252
@test norm(Calculus.finite_difference_hessian(fx, gx, [0.0, 0.0], :central) - [-sin(0.0) 0.0; 0.0 -cos(0.0)]) < 10e-4
5353
@test norm(Calculus.finite_difference_hessian(fx, [0.0, 0.0]) - [-sin(0.0) 0.0; 0.0 -cos(0.0)]) < 10e-4

0 commit comments

Comments
 (0)