20191203-python解多元一次方程

https://blog.csdn.net/qq_24395387/article/details/103370434
python 解 多元一次方程

间歇性学习狂躁症 2019-12-03 16:43:20  3806  收藏 7
分类专栏: python
版权

python
专栏收录该内容
5 篇文章0 订阅
订阅专栏
1. 待解方程式
已知ai,bi,ci,求解x,y,z。

a1*x + b1*y+c1*z = 1
a2*x + b2*y+c2*z = 2
a3*x + b3*y+c3*z = 3
1
2
3
2. 需要的模块与方法

numpy
np.linalg.solve(m, n)
3. 输入格式
其中 np.linalg.solve(m, n)的m,n输入格式为:

 m = np.array([[a1, b1, c1], [a2, b2. c2], [a3, b3, c3]])
 n = np.array([1, 2, 3]) #可替换为式子右边的常数
1
2
4. 结果

solution = np.linalg.solve(m, n) #solution format: np.array([x, y, z])
1
完整伪代码如下:

import numpy as np
"""
a1*x + b1*y+c1*z = 1
a2*x + b2*y+c2*z = 2
a3*x + b3*y+c3*z = 3
"""
 m = np.array([[a1, b1, c1], [a2, b2. c2], [a3, b3, c3]])
 n = np.array([0, 0, 0]) #可替换为式子右边的常数
 solution = np.linalg.solve(m, n) #solution format: np.array([x, y, z])
1
2
3
4
5
6
7
8
9
注意: np.linalg.solve前提是m矩阵可逆。
————————————————
版权声明:本文为CSDN博主「间歇性学习狂躁症」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_24395387/article/details/103370434

Xshell 7 (Build 0065)
Copyright (c) 2020 NetSarang Computer, Inc. All rights reserved.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

Connecting to 192.168.31.246:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.11.0-41-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

84 updates can be applied immediately.
To see these additional updates run: apt list --upgradable

Your Hardware Enablement Stack (HWE) is supported until April 2025.
Last login: Tue Dec 14 16:33:43 2021 from 192.168.31.232
rootroot@rootroot-System-Product-Name:~$ ifconfig
docker0: flags=4099  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:cc:dc:65:5f  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp4s0: flags=4099  mtu 1500
        ether 88:d7:f6:7c:3e:a5  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp6s1: flags=4099  mtu 1500
        ether e0:91:f5:21:e0:20  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enxf8e43b617e5c: flags=4163  mtu 1500
        inet 192.168.31.246  netmask 255.255.255.0  broadcast 192.168.31.255
        inet6 fe80::95d7:3334:fe65:6c62  prefixlen 64  scopeid 0x20
        ether f8:e4:3b:61:7e:5c  txqueuelen 1000  (Ethernet)
        RX packets 54706041  bytes 71637222327 (71.6 GB)
        RX errors 43689  dropped 0  overruns 0  frame 43689
        TX packets 13281305  bytes 1071363266 (1.0 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 984  bytes 76996 (76.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 984  bytes 76996 (76.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

rootroot@rootroot-System-Product-Name:~$ python
Python 2.7.18 (default, Mar  8 2021, 13:02:45) 
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ python3
Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit9)
  File "", line 1
    exit9)
         ^
SyntaxError: unmatched ')'
>>> 
>>> 
>>> exit()
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ python3
Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
Traceback (most recent call last):
  File "", line 1, in
ModuleNotFoundError: No module named 'numpy'
>>> exit()
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ python3 -m pip install numpy
/usr/bin/python3: No module named pip
rootroot@rootroot-System-Product-Name:~$ sudo apt-get install pip
[sudo] password for rootroot: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'python3-pip' instead of 'pip'
The following additional packages will be installed:
  libexpat1-dev libpython3-dev libpython3.8-dev python-pip-whl python3-dev python3-wheel python3.8-dev zlib1g-dev
The following NEW packages will be installed:
  libexpat1-dev libpython3-dev libpython3.8-dev python-pip-whl python3-dev python3-pip python3-wheel python3.8-dev zlib1g-dev
0 upgraded, 9 newly installed, 0 to remove and 88 not upgraded.
Need to get 6,797 kB of archives.
After this operation, 25.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://cn.archive.ubuntu.com/ubuntu focal/main amd64 libexpat1-dev amd64 2.2.9-1build1 [116 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu focal-updates/main amd64 libpython3.8-dev amd64 3.8.10-0ubuntu1~20.04.1 [3,948 kB]
Get:3 http://cn.archive.ubuntu.com/ubuntu focal/main amd64 libpython3-dev amd64 3.8.2-0ubuntu2 [7,236 B]
Get:4 http://cn.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python-pip-whl all 20.0.2-5ubuntu1.6 [1,805 kB]
Get:5 http://cn.archive.ubuntu.com/ubuntu focal-updates/main amd64 zlib1g-dev amd64 1:1.2.11.dfsg-2ubuntu1.2 [155 kB]
Get:6 http://cn.archive.ubuntu.com/ubuntu focal-updates/main amd64 python3.8-dev amd64 3.8.10-0ubuntu1~20.04.1 [510 kB]
Get:7 http://cn.archive.ubuntu.com/ubuntu focal/main amd64 python3-dev amd64 3.8.2-0ubuntu2 [1,212 B]
Get:8 http://cn.archive.ubuntu.com/ubuntu focal/universe amd64 python3-wheel all 0.34.2-1 [23.8 kB]
Get:9 http://cn.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB]
Fetched 6,797 kB in 3s (2,340 kB/s)  
Selecting previously unselected package libexpat1-dev:amd64.
(Reading database ... 202310 files and directories currently installed.)
Preparing to unpack .../0-libexpat1-dev_2.2.9-1build1_amd64.deb ...
Unpacking libexpat1-dev:amd64 (2.2.9-1build1) ...
Selecting previously unselected package libpython3.8-dev:amd64.
Preparing to unpack .../1-libpython3.8-dev_3.8.10-0ubuntu1~20.04.1_amd64.deb ...
Unpacking libpython3.8-dev:amd64 (3.8.10-0ubuntu1~20.04.1) ...
Selecting previously unselected package libpython3-dev:amd64.
Preparing to unpack .../2-libpython3-dev_3.8.2-0ubuntu2_amd64.deb ...
Unpacking libpython3-dev:amd64 (3.8.2-0ubuntu2) ...
Selecting previously unselected package python-pip-whl.
Preparing to unpack .../3-python-pip-whl_20.0.2-5ubuntu1.6_all.deb ...
Unpacking python-pip-whl (20.0.2-5ubuntu1.6) ...
Selecting previously unselected package zlib1g-dev:amd64.
Preparing to unpack .../4-zlib1g-dev_1%3a1.2.11.dfsg-2ubuntu1.2_amd64.deb ...
Unpacking zlib1g-dev:amd64 (1:1.2.11.dfsg-2ubuntu1.2) ...
Selecting previously unselected package python3.8-dev.
Preparing to unpack .../5-python3.8-dev_3.8.10-0ubuntu1~20.04.1_amd64.deb ...
Unpacking python3.8-dev (3.8.10-0ubuntu1~20.04.1) ...
Selecting previously unselected package python3-dev.
Preparing to unpack .../6-python3-dev_3.8.2-0ubuntu2_amd64.deb ...
Unpacking python3-dev (3.8.2-0ubuntu2) ...
Selecting previously unselected package python3-wheel.
Preparing to unpack .../7-python3-wheel_0.34.2-1_all.deb ...
Unpacking python3-wheel (0.34.2-1) ...
Selecting previously unselected package python3-pip.
Preparing to unpack .../8-python3-pip_20.0.2-5ubuntu1.6_all.deb ...
Unpacking python3-pip (20.0.2-5ubuntu1.6) ...
Setting up python3-wheel (0.34.2-1) ...
Setting up libexpat1-dev:amd64 (2.2.9-1build1) ...
Setting up libpython3.8-dev:amd64 (3.8.10-0ubuntu1~20.04.1) ...
Setting up zlib1g-dev:amd64 (1:1.2.11.dfsg-2ubuntu1.2) ...
Setting up python-pip-whl (20.0.2-5ubuntu1.6) ...
Setting up libpython3-dev:amd64 (3.8.2-0ubuntu2) ...
Setting up python3-pip (20.0.2-5ubuntu1.6) ...
Setting up python3.8-dev (3.8.10-0ubuntu1~20.04.1) ...
Setting up python3-dev (3.8.2-0ubuntu2) ...
Processing triggers for man-db (2.9.1-1) ...
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ python3 -m pip install numpy
Collecting numpy
  Downloading numpy-1.21.4-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.7 MB)
     |████████████████████████████████| 15.7 MB 124 kB/s 
Installing collected packages: numpy
  WARNING: The scripts f2py, f2py3 and f2py3.8 are installed in '/home/rootroot/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed numpy-1.21.4
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ 
rootroot@rootroot-System-Product-Name:~$ python3
Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> m = np.array([[a1, b1, c1], [a2, b2. c2], [a3, b3, c3]])
Traceback (most recent call last):
  File "", line 1, in
NameError: name 'a1' is not defined
>>> m = np.array([[0.8, 0.1, 0.2], [0.15, 0.9. 0.15], [0.2, 0.1, 0.95]])
  File "", line 1
    m = np.array([[0.8, 0.1, 0.2], [0.15, 0.9. 0.15], [0.2, 0.1, 0.95]])
                                               ^
SyntaxError: invalid syntax
>>> m = np.array([[0.8, 0.1, 0.2], [0.15, 0.9, 0.15], [0.2, 0.1, 0.95]])
>>> n = np.array([0, 0, 0])
>>> n
array([0, 0, 0])
>>> n = np.array([1, 2, 3])
>>> solution = np.linalg.solve(m, n)
>>> solution
array([0.31063321, 1.68458781, 2.91517324])
>>> 
 

你可能感兴趣的:(ubuntu,python,算法,开发语言)