Как подставить уравнение точки в линию с помощью симпы - PullRequest
1 голос
/ 10 апреля 2020
from sympy import *

Rfinal = dict()
Bfinal = dict()
x, y = symbols('x y')

Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["(1*x)+(1*y)+0","(1*x)-(1*y)+0","(1*x)+(0*y)-3","(0*x)+(1*y)-0.5"]

for exp in Lines:
    for a,b in Red:
        d = exp.subs(x,a).subs(y,b)
        if d > 0:
            Rfinal['(a,b)'] = positivepoint
        elif d == 0:
            Rfinal['(a,b)'] = pointontheline
        else:
            Rfinal['(a,b)'] = negativepoint

for exp in Lines:
    for p,q in Blue:
        d = exp.subs(x,a).subs(y,b)
        if d > 0:
            Bfinal['(p,q)'] = positivepoint
        elif d == 0:
            Bfinal['(p,q)'] = pointontheline
        else:
            Bfinal['(p,q)'] = negativepoint

ошибка: объект 'str' не имеет атрибута 'subs' *

Ответы [ 2 ]

1 голос
/ 10 апреля 2020

IIU C

from sympy import *

Rfinal = dict()
Bfinal = dict()
x, y = symbols('x y')

Red = [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue = [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines = [x+y, x-y, x+y-3, x+y-0.5]

for exp in Lines:
    Rfinal[exp]={}
    Bfinal[exp]={}

    for point in Red:
        d = exp.subs(x,point[0]).subs(y,point[1])
        if d > 0:
            Rfinal[exp][point] = 'positivepoint'
        elif d == 0:
            Rfinal[exp][point] = 'pointontheline'
        else:
            Rfinal[exp][point] = 'negativepoint'


    for point in Blue:
        d = exp.subs(x,point[0]).subs(y,point[1])
        if d > 0:
            Bfinal[exp][point] = 'positivepoint'
        elif d == 0:
            Bfinal[exp][point] = 'pointontheline'
        else:
            Bfinal[exp][point] = 'negativepoint'

Bfinal
{x + y: {
  (-2, -1): 'negativepoint',
  (-1, -2): 'negativepoint',
  (-3, -2): 'negativepoint',
  (-3, -1): 'negativepoint',
  (1, -3): 'negativepoint'},
 x - y: {
  (-2, -1): 'negativepoint',
  (-1, -2): 'positivepoint',
  (-3, -2): 'negativepoint',
  (-3, -1): 'negativepoint',
  (1, -3): 'positivepoint'},
 x + y - 3: {
  (-2, -1): 'negativepoint',
  (-1, -2): 'negativepoint',
  (-3, -2): 'negativepoint',
  (-3, -1): 'negativepoint',
  (1, -3): 'negativepoint'},
 x + y - 0.5: {
  (-2, -1): 'negativepoint',
  (-1, -2): 'negativepoint',
  (-3, -2): 'negativepoint',
  (-3, -1): 'negativepoint',
  (1, -3): 'negativepoint'}}

1 голос
/ 10 апреля 2020

Ваши строки - это строки, а не выражения SymPy. Вы можете преобразовать их в выражения типа:

>>> from sympy import Tuple
>>> Lines = Tuple(*Lines)

Но, вероятно, было бы лучше просто удалить кавычки вокруг выражений, поскольку вы уже определили x и y как символы.

(Возможно, вы захотите убрать кавычки вокруг "(a, b)" и "(p, q)" тоже.)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...