1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonのsympyで連立一次方程式を解いてみよう!

Posted at

Sympyとは

SymPyは、Pythonで記号計算を行うためのライブラリ(無料!)
Mathematicaみたいなやつ!

公式ページ

こちら

やってみよう

環境

$ python3 -V
Python 3.7.3

仮想環境の構築

$ mkdir algebra-homework
$ cd algebra-homework
$ python3 -m venv venv
$ source ./venv/bin/activate

sympyインストール


$ pip install sympy
Collecting sympy
  Downloading https://files.pythonhosted.org/packages/ce/5b/acc12e3c0d0be685601fc2b2d20ed18dc0bf461380e763afc9d0a548deb0/sympy-1.5.1-py2.py3-none-any.whl (5.6MB)
    100% |████████████████████████████████| 5.6MB 1.2MB/s 
Collecting mpmath>=0.19 (from sympy)
  Downloading https://files.pythonhosted.org/packages/ca/63/3384ebb3b51af9610086b23ea976e6d27d6d97bf140a76a365bd77a3eb32/mpmath-1.1.0.tar.gz (512kB)
    100% |████████████████████████████████| 522kB 1.6MB/s 
Installing collected packages: mpmath, sympy
  Running setup.py install for mpmath ... done
Successfully installed mpmath-1.1.0 sympy-1.5.1
You are using pip version 19.0.3, however version 20.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

連立方程式を解いてみる

-7x +2y +10 = 0
3x +5y +9 = 0

$ python
Python 3.7.3 (default, Mar  6 2020, 22:34:30) 
[Clang 11.0.3 (clang-1103.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import sympy
>>> x=sympy.Symbol('x')
>>> y=sympy.Symbol('y')
>>> sympy.solve([-7*x+2*y+10,3*x+5*y+9])
{x: 32/41, y: -93/41}

手でxについて解いてみる

-7x +2y +10 = 0
3x +5y +9 = 0

-35x+10y+50=0
6x+10y+18=0

-41x+0y+32=0

x=32/41


なんかあってそうね!よかったよかった

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?