From db2309142981324c5ff8917e9f0cc95b5529dfe5 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Sun, 13 Sep 2015 18:14:38 +0200 Subject: Assignment 1, 1.1 & 1.2 start --- Assignment 1/ex11.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Assignment 1/ex11.py (limited to 'Assignment 1/ex11.py') diff --git a/Assignment 1/ex11.py b/Assignment 1/ex11.py new file mode 100644 index 0000000..b3c3683 --- /dev/null +++ b/Assignment 1/ex11.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Sep 9 16:03:05 2015 + +@author: camilstaps +""" + +from __future__ import print_function +import numpy as np + +# 1.1.1 + +vector_x = np.array([6,7,8,9,10,11,12]) +vector_y = np.array([3,7,11,15,19,23,27]) +vector_w = np.array([1,1,0,0.5,1,1.5,2,0,0]) +vector_s = np.array([100,98.8,97.6,96.4,95.2]) +vector_z = np.array([0.7 + 0.3 * i for i in range(0,7)]) + +# 1.1.1 a +vector_v = np.multiply(3, vector_x) + vector_y +print("1.1.1 a:", vector_v) + +# 1.1.1 b +dot_product = np.dot(vector_x, vector_y) +print("1.1.1 b:", dot_product) + +# 1.1.1 c +vector_t = np.multiply(np.pi, vector_s + 4) +print("1.1.1 c:", vector_t) + +# 1.1.1 d +vector_z = vector_z - 1 +print("1.1.1 d:", vector_z) + +# 1.1.1 e +vector_x = np.concatenate((vector_x[:4], [4,4,4])) +print("1.1.1 e:", vector_x) + +# 1.1.1 f +vector_r = np.multiply(2, vector_w) - 5 +print("1.1.1 f:", vector_r) + +# 1.1.2 + +matrix_m = np.asmatrix([[1,2,3],[6,8,4],[6,7,5]]) +matrix_n = np.asmatrix([[4,6],[7,2],[5,1]]) +matrix_p = np.asmatrix([[2,5],[5,5]]) + +# 1.1.2 a +matrix_a = matrix_m * matrix_n + matrix_n +print("1.1.2 a:", matrix_a) + +# 1.1.2 b +matrix_b = np.dot(np.transpose(matrix_n), matrix_m) +print("1.1.2 b:", matrix_b) + +# 1.1.2 c +matrix_c = np.invert(matrix_p) + matrix_p +print("1.1.2 c:", matrix_c) + +# 1.1.2 d +#matrix_d = matrix_a * matrix_c * (matrix_c + matrix_b) +print("1.1.2 d: C and B cannot be added") + +# 1.1.2 e +# Only square matrices have eigenvalues (see for example +# http://math.stackexchange.com/q/583938/63495), so N is omitted +val_m, vec_m = np.linalg.eig(matrix_m) +val_p, vec_p = np.linalg.eig(matrix_p) +print("1.1.2 e (M):", val_m, vec_m) +print("1.1.2 e (N): non-square matrices don't have eigenv*s") +print("1.1.2 e (P):", val_p, vec_p) -- cgit v1.2.3