Python A derivative/limit calculator using python

  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    Calculator Python
AI Thread Summary
The discussion centers on creating a derivative calculator in Python, focusing on the function `derivative_quotient(f, a, h)`. Participants clarify that the parameter `f` must be a function that takes a float and returns a float, emphasizing that Python does not enforce type checking for function arguments. There are suggestions to handle division by zero more effectively and to remove unnecessary imports and variables in the code. Additionally, the conversation touches on the possibility of symbolic differentiation using the SymPy package, although this is considered somewhat off-topic. Overall, the thread provides insights into function argument handling and best practices in Python programming.
ver_mathstats
Messages
258
Reaction score
21
Python:
import math

def derivative_quotient(f, a, h):
    return float((f(a + h) - f(a)) / h)
    if h==0:
        return "Division by zero is not allowed."

Here is my code so far, I am unsure of where to go from here or if I am even on the right track. I am a little confused what it means by "where f is function from float to float". Any help would be appreciated thank you.
 
Technology news on Phys.org
The parameter f is a function. In particular, it's a function which takes one argument (a float) and returns one result (also a float).

So if you had a function myfun(x, y) you couldn't pass that as the argument f. If you had a function myfun(x) that returned a boolean value, you shouldn't pass that as the argument f either.

I'm not sure about the syntax of passing a function as an argument in Python or whether it checks any of those things. As far as I know, Python doesn't declare or check the types of its arguments and in fact could accept many different types for the same argument.

Perhaps it's up to you to check: is the argument f a function? How many arguments does f take? What is the return type of f? Again, I'm not sure how you do those things in Python or if you can.

I'm learning Python as a matter of fact but I'm kind of a novice. I've been working with it... let's see now... (counts on fingers and toes), since last Tuesday. So 1 week today.
 
By the way, your function as written checks whether h is 0 AFTER using h. If h is 0, you're going to get an error message and never get to that check.
 
RPinPA said:
By the way, your function as written checks whether h is 0 AFTER using h. If h is 0, you're going to get an error message and never get to that check.
Yes I did end up getting the error message however I think I have programmed it because my print statements and the tests come up with the results as expected. Thank you for the help.
Python:
import math

def derivative_quotient(f, a, h):
    result = 0
    if h==0:
        return "Division by zero is not allowed."
    else:
        return (f(a + h) - f(a)) / h
This is what I came up with.
 
  • Like
Likes pbuk
RPinPA said:
The parameter f is a function. In particular, it's a function which takes one argument (a float) and returns one result (also a float).

So if you had a function myfun(x, y) you couldn't pass that as the argument f. If you had a function myfun(x) that returned a boolean value, you shouldn't pass that as the argument f either.

I'm not sure about the syntax of passing a function as an argument in Python or whether it checks any of those things. As far as I know, Python doesn't declare or check the types of its arguments and in fact could accept many different types for the same argument.

Perhaps it's up to you to check: is the argument f a function? How many arguments does f take? What is the return type of f? Again, I'm not sure how you do those things in Python or if you can.

I'm learning Python as a matter of fact but I'm kind of a novice. I've been working with it... let's see now... (counts on fingers and toes), since last Tuesday. So 1 week today.
Yes I understand. Yes I see, I have been working with Python for only about two or three weeks now so I am very new to it.
 
ver_mathstats said:
Yes I did end up getting the error message however I think I have programmed it because my print statements and the tests come up with the results as expected. Thank you for the help.
Python:
import math

def derivative_quotient(f, a, h):
    result = 0
    if h==0:
        return "Division by zero is not allowed."
    else:
        return (f(a + h) - f(a)) / h
This is what I came up with.
This won't work, since you haven't defined a function f.

For example, if the function is f(x) = x2, you could have this definition:
Python:
def f(x):
    return x*x

This should be placed above your definition for derivative_quotient.

Regarding your code, since you're not doing anything that requires the math module, there's no need to import it. Also, you have a variable named result in your code, but aren't doing anything with it, so it should be deleted.
 
  • Skeptical
  • Like
Likes pbuk and sysprog
Here's an example of passing a function as an argument.
https://www.geeksforgeeks.org/passing-function-as-an-argument-in-python/

It looks to me like Python does no type checking before trying the call. So if the function you've passed isn't structured correctly, it will give an error message. So your code should work fine, so long as the function you're passing does indeed take a float and return a float.

And apparently Python 3 added something called "annotations" which provide hints about what types the arguments are supposed to be. But again, Python itself apparently doesn't strictly enforce that. Here is a discussion of specifically how you annotate a function passed as an argument.
https://stackoverflow.com/questions...tion-that-takes-another-function-as-parameter

Looks useful to know about but probably beyond your assignment.
 
  • Like
Likes ver_mathstats
Mark44 said:
This won't work, since you haven't defined a function f.
Yes it will, f is passed as an argument to the function derivative_quotient. So after importing this code, typing print(derivative_quotient(math.sin, 0, 0.1)) in the REPL should have exactly the result asked for in the question.

Mark44 said:
This should be placed above your definition for derivative_quotient.
No.

Mark44 said:
Regarding your code, since you're not doing anything that requires the math module, there's no need to import it.
I suspect that there is a line
Code:
print(derivative_quotient(math.sin, 0, 0.1))
lower down in the OP's code that demonstrates the test case in the question which does require import math

Mark44 said:
Also, you have a variable named result in your code, but aren't doing anything with it, so it should be deleted.
Spot on!
 
Last edited:
  • Like
Likes ver_mathstats
RPinPA said:
Here's an example of passing a function as an argument.
https://www.geeksforgeeks.org/passing-function-as-an-argument-in-python/

It looks to me like Python does no type checking before trying the call. So if the function you've passed isn't structured correctly, it will give an error message. So your code should work fine, so long as the function you're passing does indeed take a float and return a float.
Python does no (implicit) type checking at all.

RPinPA said:
And apparently Python 3 added something called "annotations" which provide hints about what types the arguments are supposed to be. But again, Python itself apparently doesn't strictly enforce that. Here is a discussion of specifically how you annotate a function passed as an argument.
https://stackoverflow.com/questions...tion-that-takes-another-function-as-parameter

Looks useful to know about but probably beyond your assignment.
Annotations are exactly that, the Python interpreter doesn't use them at all. Python is not a compiled language (let's ignore JIT compilation for now) so can't work through the code checking that types are correct. That is why we almost always use an IDE with Python which DOES use those annotations, and often a "linter" to check conventions that help avoid problems.

If you are coming from a strongly typed, compiled language these things can take some time to get used to.
 
  • Like
Likes ver_mathstats
  • #10
Mark44 said:
This won't work, since you haven't defined a function f.
pbuk said:
Yes it will, f is passed as an argument to the function derivative_quotient.
In the code shown, there wasn't a call to derivative_quotient, so I didn't think that the function arg had been defined anywhere.
 
  • #11
Is it possible to do abstract differentiation and return a function instead of just a numerical value, e.g., return cosx for input sinx?
 
  • #12
WWGD said:
Is it possible to do abstract differentiation and return a function instead of just a numerical value, e.g., return cosx for input sinx?
That's somewhat off-topic, but yes you can do this with the SymPy package (this is usually called symbolic differentiation rather than abstract).
 
  • Like
Likes WWGD
  • #13
RPinPA said:
Here's an example of passing a function as an argument.
https://www.geeksforgeeks.org/passing-function-as-an-argument-in-python/

It looks to me like Python does no type checking before trying the call. So if the function you've passed isn't structured correctly, it will give an error message. So your code should work fine, so long as the function you're passing does indeed take a float and return a float.

And apparently Python 3 added something called "annotations" which provide hints about what types the arguments are supposed to be. But again, Python itself apparently doesn't strictly enforce that. Here is a discussion of specifically how you annotate a function passed as an argument.
https://stackoverflow.com/questions...tion-that-takes-another-function-as-parameter

Looks useful to know about but probably beyond your assignment.
Thank you for the comment, I will take a look at this.
 
  • #14
Mark44 said:
In the code shown, there wasn't a call to derivative_quotient, so I didn't think that the function arg had been defined anywhere.
Sorry about that, I should have put it at the bottom.
 
  • #15
EDIT: it seems I already posted almost exactly the same comment as below in #9 which in one way is comforting (in that I am not on this occasion contradicting myself), but is I'm afraid a symptom of the fact that I have been around a lot longer than Python! I will leave the duplication as a reminder to myself of this.

RPinPA said:
It looks to me like Python does no type checking before trying the call. So if the function you've passed isn't structured correctly, it will give an error message.
Python by design is not a strongly typed language so it doesn't do ANY type checking before trying ANY call* - you can try to pass a string where it is expecting a function and it won't notice until runtime. And it is not the call with the wrong type that throws the error, it is type checking code in the called function in this case, or if the called function does not do any type checking then it is the runtime that will error when it tries to execute the internal 'call' method of a string.

* The JIT compiler cannot in general know what the type of a variable is so it is left to the runtime to either implicitly coerce the type or throw an error if it is not checked in userspace.

RPinPA said:
And apparently Python 3 added something called "annotations" which provide hints about what types the arguments are supposed to be. But again, Python itself apparently doesn't strictly enforce that.
These annotations are designed to be used by an Integrated Development Environment (IDE). Programming Python (or really anything with the possible exception of assembler) without an IDE is not something I would recommend. The same goes for a linter.

Note that by IDE here I am including modern text editors with auto-indenting, brace matching, code-completion, type hints etc. such as Visual Studio Code, Sublime Text, Notepad++, Atom etc.
 

Similar threads

Replies
15
Views
2K
Replies
7
Views
5K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
4
Views
5K
Replies
10
Views
3K
Back
Top