CS代写 Matrix_Computations – cscodehelp代写

Matrix_Computations

Matrix Computations¶

Copyright By cscodehelp代写 加微信 cscodehelp

import numpy as np

Basic matrix arithmetics like

Matrix addition
Matrix subtraction
Matrix multiplication
Scalar product
Cross product
and other operations on matrices

The arithemtic standard Operators

are applied on the elements, this means that the arrays have to have the same size.

x = np.array([1,5,2])
y = np.array([7,4,1])

array([8, 9, 3])

array([ 7, 20, 2])

array([-6, 1, 1])

array([0.14285714, 1.25 , 2. ])

array([1, 1, 0], dtype=int32)

In mathematics, the dot product is an algebraic operation that takes two coordinate vectors of equal size and returns a single number. The result is calculated by multiplying corresponding entries and adding up those products. The name “dot product” stems from the fact that the centered dot “·” is often used to designate this operation.

x = np.array([1,2,3])
y = np.array([-7,8,9])
dot = np.dot(x,y)
np.dot(x,y)

The matrix objects are a subclass of the numpy arrays (ndarray). The matrix objects inherit all the attributes and methods of ndarray. Another difference is that numpy matrices are strictly 2-dimensional, while numpy arrays can be of any dimension, i.e. they are $n$-dimensional.

The most important advantage of matrices is that the provide convenient notations for the matrix mulitplication. If $X$ and $Y$ are two Matrices than $X * Y$ defines the matrix multiplication. While on the other hand, if $X$ and $Y$ are ndarrays, $X * Y$ define an element by element multiplication.

x = np.array( ((2,3), (3, 5)) )
y = np.array( ((1,2), (5, -1)) )

array([[ 2, 6],
[15, -5]])

x = np.matrix( ((2,3), (3, 5)) )
y = np.matrix( ((1,2), (5, -1)) )

matrix([[17, 1],
[28, 1]])

If we want to perform matrix multiplication with two numpy arrays (ndarray), we have to use the dot product:

x = np.array( ((2,3), (3, 5)) )
y = np.array( ((1,2), (5, -1)) )
np.dot(x,y)

array([[17, 1],
[28, 1]])

Alternatively, we can cast them into matrix objects and use the “*” operator:

np.mat(x) * np.mat(y)

matrix([[17, 1],
[28, 1]])

In the following practical example, we come to talk about the sweet things of life.
Let’s assume there are four people, and we call them Lucas, Mia, Leon and Hannah. Each of them has bought chocolates out of a choice of three. The brand are A, B and C, not very marketable, we have to admit. Lucas bought 100 g of brand A, 175 g of brand B and 210 of C. Mia choose 90 g of A, 160 g of B and 150 g of C. Leon bought 200 g of A, 50 of B and 100 g of C. Hannah apparently didn’t like brand B, because she hadn’t bought any of those. But she she seems to be a real fan of brand C, because she bought 310 g of them. Furthermore she bought 120 g of A.

So, what’s the price in Euro of these chocolates: A costs 2.98 per 100 g, B costs 3.90 and C only 1.99 CAD.

If we have to calculate how much each of them had to pay, we can use Python, NumPy and Matrix multiplication:

NumPersons = np.array([[100,175,210],[90,160,150],[200,50,100],[120,0,310]])
Price_per_100_g = np.array([2.98,3.90,1.99])
Price_in_Cent = np.dot(NumPersons,Price_per_100_g)
Price_in_CAD = Price_in_Cent / np.array([100,100,100,100])
Price_in_CAD

array([13.984, 11.907, 9.9 , 9.745])

This means that Lucas paid 13.98 CAD, Mia 11.91 CAD, Leon 9.90 and Hannah 9.75.

Computing determinants and eigenvalues¶

H = np.matrix( ((3,-1,0), (-1,3,0), (0,0,5)) )
eigenvalues, eigenvectors = np.linalg.eig(H)

eigenvalues

array([4., 2., 5.])

determinant = np.linalg.det(H)

determinant

40.000000000000014

程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com

Leave a Reply

Your email address will not be published. Required fields are marked *