Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: two
Path: KP2.ipynb
Views: 146
Kernel: Python 3 (old Anaconda 3)
#num1
import numpy as np
X = np.linspace(0,20,6);print(X) Y = np.copy(X);print(Y,'\n') print(np.round(np.sin(Y**2),3),'\n') X = X.reshape(2,3) Y = Y.reshape(3,2) print(X,'\n',Y,'\n') print(X.dot(Y),'\n') Y = Y.reshape(2,3) print(np.hstack((X,Y)),'\n',np.vstack((X,Y)))
[ 0. 4. 8. 12. 16. 20.] [ 0. 4. 8. 12. 16. 20.] [ 0. -0.288 0.92 -0.491 -0.999 -0.851] [[ 0. 4. 8.] [ 12. 16. 20.]] [[ 0. 4.] [ 8. 12.] [ 16. 20.]] [[ 160. 208.] [ 448. 640.]] [[ 0. 4. 8. 0. 4. 8.] [ 12. 16. 20. 12. 16. 20.]] [[ 0. 4. 8.] [ 12. 16. 20.] [ 0. 4. 8.] [ 12. 16. 20.]]
#num2
import numpy.linalg as li
A = np.array([np.random.randint(-5,5) for x in np.arange(9)]) A = A.reshape(3,3) B = np.array([np.random.randint(-5,5) for x in np.arange(3)]) print(A,'\n',B,'\n') print(' det = ', li.det(A),'\n','rank = ', np.rank(A),'\n','spur = ', np.trace(A),'\n','norm = ', li.norm(B),'\n') print(li.solve(A,B)) a = li.inv(A) print(a.dot(B),'\n') x = a.dot(B) print(x.dot(B),'\n', np.cross(x,B),'\n', np.outer(x,B),'\n', '\n', li.eig(A))
[[ 4 -5 3] [-5 2 3] [-5 1 -3]] [ 3 -4 -1] det = 129.0 rank = 2 spur = 3 norm = 5.09901951359 [ 0.3255814 -0.58139535 -0.40310078] [ 0.3255814 -0.58139535 -0.40310078] 3.70542635659 [-1.03100775 -0.88372093 0.44186047] [[ 0.97674419 -1.30232558 -0.3255814 ] [-1.74418605 2.3255814 0.58139535] [-1.20930233 1.6124031 0.40310078]] (array([ 7.94155764+0.j , -2.47077882+3.18416658j, -2.47077882-3.18416658j]), array([[-0.62293876+0.j , 0.51135609-0.02741701j, 0.51135609+0.02741701j], [ 0.70027022+0.j , 0.61012430+0.j , 0.61012430-0.j ], [ 0.34866736+0.j , -0.05698345+0.60188412j, -0.05698345-0.60188412j]]))
/ext/anaconda3/lib/python3.5/site-packages/ipykernel/__main__.py:5: VisibleDeprecationWarning: `rank` is deprecated; use the `ndim` attribute or function instead. To find the rank of a matrix see `numpy.linalg.matrix_rank`.
#num3
con = lambda i, j: 1/np.sqrt((i + 1)**2 + (j + 1)**2)
import time t1 = time.clock() A = np.zeros((1000,1000)) for i in np.arange(1000): for j in np.arange(1000): A[i,j] = con(i,j) t2 = time.clock() print(t2-t1)
2.3240479999999977
t1 = time.clock() A = np.reshape([con(i,j) for i in np.arange(1000) for j in np.arange(1000)],(1000,1000)) t2 = time.clock() print(t2-t1)
2.0921179999999993
t1 = time.clock() A = np.fromfunction(con,(1000,1000)) t2 = time.clock() print(t2-t1)
0.029004000000000474
########################
import numpy as np import numpy.linalg as li
N = [np.random.randint(-10,10) for i in range(30)];print(N)
[-10, -5, 5, -9, -10, 0, 4, 2, 5, 5, 5, 6, -2, -4, 7, 6, 9, -7, 6, 2, -8, -8, 2, 9, 9, -2, 6, 8, -7, -6]
n = 0 M = [] for i in range(len(N)): if N[i] > 0: M += [N[i]] n += 1
print(M,'\n',n,'\n', sum(M)/n)
[5, 4, 2, 5, 5, 5, 6, 7, 6, 9, 6, 2, 2, 9, 9, 6, 8] 17 5.647058823529412
Np = [x for x in N if x > 0] print(sum(Np)/len(Np))
5.647058823529412