75537


①プログラムが短くなりますか？  
②x,y,zにするためのアドバイスお願いします。

4 _x+y <= 9,x+2_y >= 4,2 _x-3_ y >= -6 ----> Ax>=b  
(参考)  
[sympyのcoeffの使い方を教えて下さい](https://ja.stackoverflow.com/questions/33677/sympy%e3%81%aecoeff%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%e3%82%92%e6%95%99%e3%81%88%e3%81%a6%e4%b8%8b%e3%81%95%e3%81%84)

[code]

    from sympy import *
    var('x y')
    def my_Str2Inequality(my_Str):
         if my_Str.find("<=") > 0:
              tmp = my_Str.split("<=")
              ans = sympify(tmp[1] + "-" + tmp[0])
         elif my_Str.find(">=") > 0:
              tmp = my_Str.split(">=")
              ans = sympify(tmp[0] + "-" + tmp[1])
         else:
              ans=""
         return ans
    def my_xykeisu(my_Pol2):
         f = symbols('f', cls=Function)
         f = sympify(my_Pol2)
         my_ans0=f.coeff(x**1)
         my_ans1=f.coeff(y**1)
         p = Poly(f, [x,y])
         my_ans2=p.coeff_monomial(x**0)
         my_ans=[my_ans0,my_ans1,my_ans2]
         return my_ans
    my_inequality=['4*x+y <= 9','x+2*y >= 4','2*x-3*y >= -6']
    my_Str=my_Str2Inequality(my_inequality[0])
    my_list0=my_xykeisu(my_Str)
    my_Str=my_Str2Inequality(my_inequality[1])
    my_list1=my_xykeisu(my_Str)
    my_Str=my_Str2Inequality(my_inequality[2])
    my_list2=my_xykeisu(my_Str)
    A=Matrix([my_list0,my_list1,my_list2])
    print("#",my_inequality)
    print("#",A)
    # ['4*x+y <= 9', 'x+2*y >= 4', '2*x-3*y >= -6']
    # Matrix([[-4, 1, 9], [1, 2, -4], [2, -3, 6]])
    
[/code]


CC BY-SA 4.0
2021-04-07T09:26:01.507
75142
2021-04-27T10:01:26.000
2021-04-07T09:41:20.603
17199
17199
question
python sympy
sympyで,連立1次不等式の係数を取り出してみました。

[code]

    from sympy import *
    var('x y')
    def my_Str2Inequality(my_Str):
         if my_Str.find("<=") > 0:
              tmp = my_Str.split("<=")
              ans = sympify(tmp[1] + "-" + tmp[0])
         elif my_Str.find(">=") > 0:
              tmp = my_Str.split(">=")
              ans = sympify(tmp[0] + "-" + tmp[1])
         else:
              ans=""
         return ans
    def my_xykeisu(my_Pol2):
         f = symbols('f', cls=Function)
         f = sympify(my_Pol2)
         my_ans0=f.coeff(x**1)
         my_ans1=f.coeff(y**1)
         p = Poly(f, [x,y])
         my_ans2=p.coeff_monomial(x**0)
         my_ans=[my_ans0,my_ans1,my_ans2]
         return my_ans
    my_inequality=['4*x+y <= 9','x+2*y >= 4','2*x-3*y >= -6']
    my_num=len(my_inequality)
    mylist=[[0]*my_num]*my_num
    myMatrix=Matrix()
    for i in range(my_num):
        mylist[i]=my_xykeisu(my_Str2Inequality(my_inequality[i]))
        myMatrix = myMatrix.row_insert(i, Matrix([mylist[i]]))
    print("#",my_inequality)
    print("#",myMatrix)
    # ['4*x+y <= 9', 'x+2*y >= 4', '2*x-3*y >= -6']
    # Matrix([[-4, 1, 9], [1, 2, -4], [2, -3, 6]])
    
[/code]


CC BY-SA 4.0
2021-04-27T10:01:26.000
75537
2021-04-27T10:01:26.000
17199
75142
answer