0

I've written program for finding critical value in Python.

My code:

from sympy import *
def find_critical_points(f, x):
    fd = diff(f)
    dRoots = solveset(fd, x)
    a = Rational(float(dRoots))
    return a

And I wrote test for that:

x = Symbol('x')
lst = find_critical_points(x**4+x**3, x)
assert lst == [-3/4,0]
lst = find_critical_points(x,x)
assert lst == []

Python return me error:

float() argument must be a string or a number, not 'FiniteSet'

Please help with this error.

jlewkovich
  • 2,725
  • 2
  • 35
  • 49
stickyjack
  • 39
  • 5
  • Can you edit your post to include `solveset`? It looks like whatever is being returned from their is causing errors in `a = Rational(float(dRoots))` – Saddy Jan 07 '20 at 21:55
  • 1
    The problem is that float takes a string or a number, and parses it or converts it to float. You need to find a different way of transferring `FiniteSet` to a `Rational` – Marko Jan 07 '20 at 21:57
  • Welcome to StackOverflow. See [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). We cannot effectively help you until you post your MRE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Others have given you good ideas, but we really want to *know* when we've solved your problem. – Prune Jan 07 '20 at 22:02
  • @Prune Hi did supply a minimal, reproducible example, including the error he was getting. – Marko Jan 07 '20 at 22:15

1 Answers1

1

The problem is that float() takes a string or a number, and parses it or converts it to float.

With a little research, we can find that FiniteSet can be transferred directly to a Python list. So your code can go like this:

from sympy import *


def find_critical_points(f, x):
    fd = diff(f)
    dRoots = solveset(fd, x)
    # a = Rational(float(dRoots))
    return list(dRoots)


x = Symbol('x')
lst = find_critical_points(x**4+x**3, x)
assert lst == [-3/4,0]
lst = find_critical_points(x,x)
assert lst == []
Marko
  • 733
  • 8
  • 21