Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise NotImplementedError("Equation not in exact domain. Try converting to rational") Error #21905

Open
Leovmoreno opened this issue Aug 20, 2021 · 1 comment

Comments

@Leovmoreno
Copy link

Leovmoreno commented Aug 20, 2021

I'm trying to run the following code in the email and it works as I expect for FUCNTION = 0.06xy + 9/y + 22.5/x
The x and y value for critical points are FiniteSet((9.78716910292216, 3.91486764116886), (-4.89358455146108 - 8.47593707426474I, -1.95743382058443 - 3.3903748297059I), (-4.89358455146108 + 8.47593707426474I, -1.95743382058443 + 3.3903748297059I))

Analyzing critical point (9.78716910292216, 3.91486764116886)

The discriminant of our critical value is 0.0108000000000000
The fxx of our critical value is 0.0480000000000000
This is a relative minima
My Value f(a,b) = 6.89678489154198

it produces the values as expected below,
My Value Heigth = 3.91
My Value Front Width = 3.91
My Value Depth = 9.79

Process finished with exit code 0

Howerve, I need some help because when I run this function 0.07xy + 10.0/y + 30.0/x. it errors with this error message below,
Traceback (most recent call last):
File "/Users/admin/PycharmProjects/codeinplace/northwestern_university/400/Week 9/PS9_Q13.py", line 72, in
results = nonlinsolve([partial_x, partial_y], [x, y])
File "/Users/admin/PycharmProjects/codeinplace/venv/lib/python3.7/site-packages/sympy/solvers/solveset.py", line 3675, in nonlinsolve
raise NotImplementedError("Equation not in exact domain. Try converting to rational")
NotImplementedError: Equation not in exact domain. Try converting to rational

Process finished with exit code 1

I appreciate any help

Leo

from sympy import symbols, S, nonlinsolve, lambdify, Abs


# Q13
from sympy.solvers.solveset import _solve_as_rational


def D(func, x_sym, y_sym, x_crit, y_crit):
    # Calculate the discriminant for a given function
    f_x_x = func.diff(x_sym, x_sym)
    f_y_y = func.diff(y_sym, y_sym)
    f_x_y = func.diff(x_sym, y_sym)

    # Create callable functions for each of the derivitives we created
    lambd_x_x = lambdify([x_sym, y_sym], f_x_x)
    lambd_y_y = lambdify([x_sym, y_sym], f_y_y)
    lambd_x_y = lambdify([x_sym, y_sym], f_x_y)

    fxx_ab = lambd_x_x(x_crit, y_crit)
    fyy_ab = lambd_y_y(x_crit, y_crit)
    fxy_ab = lambd_x_y(x_crit, y_crit)

    d = fxx_ab * fyy_ab - Abs(fxy_ab) ** 2

    print(f"The discriminant of our critical value is {d}")
    print(f"The fxx of our critical value is {fxx_ab}")
    if d < 0:
        print("This is a saddle point")

    elif d > 0:
        if fxx_ab < 0:
            print("This is relative maxima")
        else:
            print("This is a relative minima")


# Call our new function and pass in the values


x, y = symbols('x, y', real=True)

# variables sets
cf = 0.10  # cost front
cb = 0.07  # cost base
ct = 0  # cost top (it is 0 because open top)
cs1 = 0.02  # cost side1
cs2 = 0.02  # cost side2
cbk = 0.02  # cost back
vol = 250  # volumen

# bock areas Remember Z = Heigh, Y = Front Width, X = Depth

z = (vol / (x * y))
Bfront = z * y
Bbase = x * y
BTop = x * y
BSide1 = x * z
BSide2 = x * z
BBack = z * y

# print("My Value FUCNTION =",z )
f_x_y = cf * Bfront + cb * Bbase + ct * BTop + cs1 * BSide1 + cs2 * BSide2 + cbk * BBack

f_x_y = 22.5*x**(-1)+9*y**(-1)+.06*x*y

partial_x = f_x_y.diff(x)
partial_y = f_x_y.diff(y)

print("My Value FUCNTION1 =", f_x_y)

# Get nonlinear solution and remove imaginary numbers
results = nonlinsolve([partial_x, partial_y], [x, y])

print(f"The x and y value for critical points are {results}")
print(" ")
for result in list(results):
    if result[0].is_real and result[1].is_real:  # Ignore any solutions that are not real numbers
        print(f"Analyzing critical point {result}")
        print(" ")
        #  f_x_x = partial_x.diff(x)
        #  print("My Value) f_x_x(a,b) =",f_x_x.subs({x:result[0], y:result[1]}))
        D(f_x_y, x, y, result[0], result[1])
        # declare the symbols
        print("My Value f(a,b) =", f_x_y.subs({x: result[0], y: result[1]}))
        print(" ")

        print("My Value Heigth =", round(150 / (result[0] * result[1]), 2))
        print("My Value Front Width =", round(result[1], 2))
        print("My Value Depth =", round(result[0], 2))

/Users/admin/PycharmProjects/codeinplace/venv/bin/python "/Users/admin/PycharmProjects/codeinplace/northwestern_university/400/Week 9/PS9_Q13.py"
My Value FUCNTION1 = 0.06xy + 9/y + 22.5/x
The x and y value for critical points are FiniteSet((9.78716910292216, 3.91486764116886), (-4.89358455146108 - 8.47593707426474I, -1.95743382058443 - 3.3903748297059I), (-4.89358455146108 + 8.47593707426474I, -1.95743382058443 + 3.3903748297059I))

Analyzing critical point (9.78716910292216, 3.91486764116886)

The discriminant of our critical value is 0.0108000000000000
The fxx of our critical value is 0.0480000000000000
This is a relative minima
My Value f(a,b) = 6.89678489154198

My Value Heigth = 3.91
My Value Front Width = 3.91
My Value Depth = 9.79

Process finished with exit code 0

@oscarbenjamin
Copy link
Contributor

The error is this:

In [19]: f = 0.07*x*y + 10.0/y + 30.0/x

In [20]: nonlinsolve([f.diff(x), f.diff(y)], [x, y])
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-20-d95965d816fe> in <module>
----> 1 nonlinsolve([f.diff(x), f.diff(y)], [x, y])

~/current/sympy/sympy/sympy/solvers/solveset.py in nonlinsolve(system, *symbols)
   3668         res = _handle_positive_dimensional(polys, symbols, denominators)
   3669         if res is EmptySet and any(not p.domain.is_Exact for p in polys):
-> 3670             raise NotImplementedError("Equation not in exact domain. Try converting to rational")
   3671         else:
   3672             return res

NotImplementedError: Equation not in exact domain. Try converting to rational
> /Users/enojb/current/sympy/sympy/sympy/solvers/solveset.py(3670)nonlinsolve()
   3668         res = _handle_positive_dimensional(polys, symbols, denominators)
   3669         if res is EmptySet and any(not p.domain.is_Exact for p in polys):
-> 3670             raise NotImplementedError("Equation not in exact domain. Try converting to rational")
   3671         else:
   3672             return res

If you do as the error message says and convert to rational then it works fine:

In [25]: f = 0.07*x*y + 10.0/y + 30.0/x

In [26]: f
Out[26]: 
           10.0   30.0
0.07xy + ──── + ────
            y      x  

In [27]: f = nsimplify(f)

In [28]: f
Out[28]: 
7xy   10   30
───── + ── + ──
 100    y    x 

In [29]: nonlinsolve([f.diff(x), f.diff(y)], [x, y])
Out[29]: 
⎧⎛     2/3       2/3⎞  ⎛      2/3      6 ___  2/3          2/3     6 ___  2/3  ⎞  ⎛      2/3      6 ___  2/3          2/3     6 ___  2/3  ⎞⎫
⎪⎜1021     1021   ⎟  ⎜  521      15⋅╲╱ 37    521      5⋅╲╱ 37⎟  ⎜  521      15⋅╲╱ 37    521      5⋅╲╱ 37⎟⎪
⎨⎜────────, ────────⎟, ⎜- ─────── - ───────────────, - ─────── - ──────────────⎟, ⎜- ─────── + ───────────────, - ─────── + ──────────────⎟⎬
⎪⎝   7         21   ⎠  ⎝     7             7              21           7       ⎠  ⎝     7             7              21           7       ⎠⎪
⎩                                                                                                                                          ⎭

Maybe nonlinsolve should be able to handle this with floats though.

skirpichev added a commit to skirpichev/diofant that referenced this issue Aug 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants