A derivative/limit calculator using python

  • Context: Python 
  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    Calculator Python
Click For Summary

Discussion Overview

The discussion revolves around creating a derivative and limit calculator using Python. Participants explore the implementation of a function that computes the derivative using the quotient definition, address issues related to function parameters, and consider the possibility of symbolic differentiation.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • Some participants clarify that the parameter f should be a function that takes a float and returns a float, noting that functions with different signatures should not be passed.
  • Concerns are raised about the order of operations in the provided code, specifically that the check for division by zero occurs after the division is attempted.
  • One participant suggests that the math module is unnecessary for the current implementation, while others argue that it may be required for certain test cases.
  • Some participants discuss the lack of type checking in Python, indicating that errors will occur if the function passed does not conform to expected input and output types.
  • There is a suggestion to use function annotations in Python 3, although it is noted that these annotations are not enforced by the interpreter.
  • A question is posed about the possibility of performing abstract differentiation to return a function instead of a numerical value, with a later reply indicating that this can be done using the SymPy package.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of the math module and the handling of function parameters. There is no consensus on the best approach to implement the derivative function, and the discussion remains unresolved regarding the implementation details and the broader topic of symbolic differentiation.

Contextual Notes

Some participants mention the need for a defined function f to test the derivative function, and there are unresolved questions about how to check the properties of the function passed as an argument.

Who May Find This Useful

Individuals interested in Python programming, particularly those learning about function arguments, derivatives, and symbolic computation may find this discussion beneficial.

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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 7 ·
Replies
7
Views
6K