AttributeError: 'LessThan' object has no attribute 'as_poly'

106 views
Skip to first unread message

mpierro3

unread,
Feb 11, 2021, 4:42:35 PM2/11/21
to sympy
New to Sympy. I am trying to solve an equation for a variable with given boundaries and I am reviving the following error:

M_3a = symbols('M_3a', positive=true, nonzero=true)
eqn = nonlinsolve([Eq((M_e / M_3a) * Pow((2 + (gamma_4 - 1) * Pow(M_3a, 2)) / \
(2 + (gamma_4 - 1) * Pow(M_e, 2)), a_4 / 2), A_4_A_1), M_3a <= 1], M_3a)
print(eqn)

AttributeError: 'LessThan' object has no attribute 'as_poly'


I was trying to create a multi-equation/multi-variable solver with given boundaries through Sympy but could not get that to work. So I tried simplifying it down to one equation with one unknown with one boundary and I am getting the same exact error. My questions are:
1) Is Sympy even capable of doing such solver?
2) How can I get past this error?

Oscar Benjamin

unread,
Feb 11, 2021, 4:44:43 PM2/11/21
to sympy
On Thu, 11 Feb 2021 at 21:42, mpierro3 <michael...@outlook.com> wrote:
>
> New to Sympy. I am trying to solve an equation for a variable with given boundaries and I am reviving the following error:
>
> M_3a = symbols('M_3a', positive=true, nonzero=true)
> eqn = nonlinsolve([Eq((M_e / M_3a) * Pow((2 + (gamma_4 - 1) * Pow(M_3a, 2)) / \
> (2 + (gamma_4 - 1) * Pow(M_e, 2)), a_4 / 2), A_4_A_1), M_3a <= 1], M_3a)
> print(eqn)
>
> AttributeError: 'LessThan' object has no attribute 'as_poly'

This could be a bug in sympy but the code as shown is missing most of
the variable definitions so it's hard for me to judge. Can you post a
complete minimum working example?


Oscar

mpierro3

unread,
Feb 11, 2021, 5:54:02 PM2/11/21
to sympy
Yes, thank you. The rest is as follows:

from sympy import *
import numpy as np

M_e = 1
gamma_1 = 1.667
gamma_4 = 1.667
MW_1 = 39.948
MW_4 = 4.0026
D_4 = 5 / 39.37
D_1 = 3 / 39.37
A_4 = (np.pi / 4) * Pow(D_4, 2)
A_1 = (np.pi / 4) * Pow(D_1, 2)
a_4 = (gamma_4 + 1) / (gamma_4 - 1)
a_1 = (gamma_1 + 1) / (gamma_1 - 1)
A_4_A_1 = A_4 / A_1


M_3a = symbols('M_3a', positive=true, nonzero=true)
eqn = nonlinsolve([Eq((M_e / M_3a) * Pow((2 + (gamma_4 - 1) * Pow(M_3a, 2)) / \
(2 + (gamma_4 - 1) * Pow(M_e, 2)), a_4 / 2), A_4_A_1), M_3a <= 1], M_3a)
print(eqn)

Oscar Benjamin

unread,
Feb 11, 2021, 6:14:34 PM2/11/21
to sympy
On Thu, 11 Feb 2021 at 22:54, mpierro3 <michael...@outlook.com> wrote:
>
> Yes, thank you. The rest is as follows:
>
> from sympy import *
> import numpy as np
>
> M_e = 1
> gamma_1 = 1.667
> gamma_4 = 1.667
> MW_1 = 39.948
> MW_4 = 4.0026
> D_4 = 5 / 39.37
> D_1 = 3 / 39.37
> A_4 = (np.pi / 4) * Pow(D_4, 2)
> A_1 = (np.pi / 4) * Pow(D_1, 2)
> a_4 = (gamma_4 + 1) / (gamma_4 - 1)
> a_1 = (gamma_1 + 1) / (gamma_1 - 1)
> A_4_A_1 = A_4 / A_1
>
> M_3a = symbols('M_3a', positive=true, nonzero=true)
> eqn = nonlinsolve([Eq((M_e / M_3a) * Pow((2 + (gamma_4 - 1) * Pow(M_3a, 2)) / \
> (2 + (gamma_4 - 1) * Pow(M_e, 2)), a_4 / 2), A_4_A_1), M_3a <= 1], M_3a)
> print(eqn)

Your equation to be solved is (truncating the numbers for clarity):

Eq(0.56*(0.33*M_3a**2 + 1)**1.99/M_3a, 2.77)

I think it is unlikely that you will get analytic expressions for the
solutions to an equation like this.

Sympy can solve it numerically though:

In [31]: nsolve(eq[0], M_3a, 0.2)
Out[31]: 0.208399106769846


--
Oscar

mpierro3

unread,
Feb 11, 2021, 11:26:15 PM2/11/21
to sympy
Okay yeah, that was my main question as to whether Sympy had the capability of doing so. Thanks! 

mpierro3

unread,
Feb 12, 2021, 9:33:19 AM2/12/21
to sympy
I tried using nsolve like you mentioned and got it to work. I then tried again, except adding a bound, and it produced the following error:

from sympy import *

M_3a = symbols('M_3a')
eqn = nsolve([Eq(0.56*(0.33*M_3a**2 + 1)**1.99/M_3a, 2.77), M_3a <= 1], [M_3a], 0.2)
print(eqn)

AttributeError: 'LessThan' object has no attribute 'diff'

Is nsolve able to compute with bounds?


Oscar Benjamin

unread,
Feb 12, 2021, 4:18:03 PM2/12/21
to sympy
On Fri, 12 Feb 2021 at 14:33, mpierro3 <michael...@outlook.com> wrote:
>
> I tried using nsolve like you mentioned and got it to work. I then tried again, except adding a bound, and it produced the following error:
>
> from sympy import *
>
> M_3a = symbols('M_3a')
> eqn = nsolve([Eq(0.56*(0.33*M_3a**2 + 1)**1.99/M_3a, 2.77), M_3a <= 1], [M_3a], 0.2)
> print(eqn)
>
> AttributeError: 'LessThan' object has no attribute 'diff'
>
> Is nsolve able to compute with bounds?

You can use the bisect solver with both upper and lower bounds:

In [17]: nsolve(sin(x), x, [3, 4], solver='bisect')
Out[17]: 3.14159265358979

https://docs.sympy.org/latest/modules/solvers/solvers.html#sympy.solvers.solvers.nsolve

Oscar
Reply all
Reply to author
Forward
0 new messages