环境:Win10操作系统;Python3.7;Pycharm
编程实例1:实现两个向量的加法和减法
1 a = [0,1,2,3,4] 2 b = [2,3,4,5,7] 3 r = [] 4 re = [] 5 length = len(a) 6 i = 0 7 while(i < length): 8 add = a[i] + b[i] 9 sub = a[i] - b[i] 10 r.append(add) 11 re.append(sub) 12 i = i + 1 13 print('a+b =',r) 14 print('a-b =',re)
计算结果:
编程实例2:实现一个矩阵的加法、减法和乘法
1 import numpy as np 2 3 a = np.array([[2,3],[4,5]]) 4 b = np.array([[6,7],[8,9]]) 5 6 c = np.mat(a) 7 d = np.mat(b) 8 9 print('a+b =',a+b) 10 print('a-b =',a-b) 11 print('a*b =',a*b)
计算结果:
遇到的问题:最开始引用numpy函数的时候,pycharm一直报错,显示numpy ModuleNotFoundError: No module named 'numpy'
解决方法:在pycharm中 选择 Files->Setting->Project workspace->Project Interpreter,直接搜索numpy,选择安装,具体按钮如下