Unable to numerically differentiate in Calculus.jl Integers not Reals

using Calculs
f(x) = sin(x)
f'(1.0) - cos(1.0)

When I run this, I get an error saying it doesn’t accept 64bit integers. How do I change the type of a function to a real number?

you can use:

f(x) = sin(float(x)) 
1 Like

I tried that and I got this error (both real and float)

the function b(x)=sin(real(x)) works though

for what i see, it seems that x' is not a valid syntax for the derivative of a function, can you try using
derivative(b, 1.0) ?

Yes that works, but the documentation says that b’(1) should work too. The difference though, is that b’ is numeric, while differentiate() is symbolic, and I don’t know how to put symbolic outputs into functions. I can use ForwardDiff.jl to get a numeric result in a formula, but I thought this would look better.

if you want something pretty, you can use unicode:

∂(f,x) = ForwardDiff.derivative(f,x)
∂(b,1.0)
2 Likes

I do quite like that. Is there a way to save differentiate, so that I can plug it into a function, either as a function of x, or a function of an integer?

sorry, i didnt understand, can you give an example of what you want?

1 Like

no idea what this will do, and I don’t expect it to work.

δ(x)=differentiate(x^2)
f(x)=δ+5x
f(1)
7

a working version

δ(x)=differentiate(x->x^2)
f(x)=δ(x)+5x
f(1)
7

EDIT; wait, it is symbolic?

Ok, that was surprisingly easy

yes. that’s why I didn’t think I could, lol.

ahh, i see, differenciate has as inputs an String an a series of Symbols, and returns an expression. i dont think that you can add two expressions, the system in that way its not composable

I didn’t think so, or that it might be way to complicated, and not at all worth my time for minor aesthetics, when I have a working numeric option. Do you get why f’(x) is described in the source code though?

i dont know. i checked the source code and i couldnt find any way where ' are defined. maybe submit an issue on the repo?. asking to update the readme?

sure I can do that. like I said, it would be a nice feature, but by no means essential.

It looks like the prime notation was removed last year because it overwrote the adjoint operator: https://github.com/JuliaMath/Calculus.jl/issues/143#issue-536837155

But weirdly this still worked for me just now…

julia> using Calculus

julia> f(x) = sin(x)
f (generic function with 1 method)

julia> f'(1) - cos(1)
0.0
2 Likes

That makes a lot of sense actually. having δ(function) as an opperator would be cool, and having δ(number) as a numeric opperator would be really handy and make neat code. I might suggest that on github. I think that can be done with dictonaries. It would also be cool if δ(x) was a generic function, but I don’t know if that’s practitcal.

something like

δ(x^3)
3x^2

δ(1)
3

δ(3)
27

δ(x)=δ(x^3)
generic function with 1 method

δ(3)
27

d(x)=(δ(x)/3)
generic function with 1 method

d(3)
3

Where did the b come from?

the b is a mistake, but otherwise this works good for finding the derivative at a certain value of x. can this be used with Roots.jl to find x at a certain derivative?