Tutorial 3 -  Linear Algebra 1

generic ECE - U. Houston

                                                                                      Han Q. Le (c) copyrighted

page 1              page 2               page 3

1. Vector dot (inner) product

1.1 Example 1

Consider this data:

Out[10]=

Trip category A B C D E F G
# of trips 5 2 6 3 3 1 4
mileage/trip 1052 740 576 1380 824 10850 438

Exercise 1.1

Define variable ntrip that contains data of the first row, and mileage that contains the data in the second row. Then find the total frequent-flyer mileage.

Answer

Tutorial_3_answer_1.gif

Out[13]=

Tutorial_3_answer_2.png

END exercise

In the above, we see that any one-D array can be considered as a vector in space (linear algebra concept). The length of the array is the dimension of the space.

What we learn above with vector ntrip and mileage is their “dot product”, one of the MOST frequent and useful math operations of array data.

Consider this example: one has a number of shares of various stock shareX at priceX, what is the value of each stock and what is the net value?

In[27]:=

Tutorial_3_answer_3.gif

Out[28]=

Tutorial_3_answer_4.png

Out[29]=

Tutorial_3_answer_5.png

In[30]:=

Tutorial_3_answer_6.png

Out[30]=

Tutorial_3_answer_7.png

The dimension of the space of priceX is 4.

1.2 Dot product

The operation between 2 arrays:
       Tutorial_3_answer_8.png and Tutorial_3_answer_9.png
        Tutorial_3_answer_10.png
as mentioned, a most common operation in computer calculation of arrays, is mathematically formally defined as the dot product:
        Tutorial_3_answer_11.png
where x array and y array are considered as vectors.

In all higher-level language, using function dot means we can skip step in more basic language, such as using do loop:

In[1]:=

Tutorial_3_answer_12.gif

Tutorial_3_answer_13.gif

Tutorial_3_answer_14.gif

Out[4]=

Tutorial_3_answer_15.png

The trend of computing is to create more and more built-in high-level functions such that the user can obtain results with less and less programming effort. Mathematica, Matlab, high-level library and special packages (R, Spyder) in Python, etc. are designed toward this goal.

Examples of dot product are abound and common in spreadsheet calculations. In fact, even the sum (total) of an array is considered as the dot product of the array and vector {1,1,1,...}.

Here is a common application:

In[34]:=

Tutorial_3_answer_16.gif

Out[37]=

Tutorial_3_answer_17.png

Tutorial_3_answer_18.png

Out[42]=

John Jane Bob George Sue
35 40 45 20 38
15 30 27 38 22
525 1200 1215 760 836
total =   4536

Below is what we learn about dot product between physical vector in 2 D space:

In[8]:=

Tutorial_3_answer_19.png

Out[8]=

Exercise 1.2

Consider a circuit with n devices, make your choice of n between 5 and 8. Generate arrays of the device voltages and currents (your choice of values). Find the net power consumption of all devices.

Answer

In[109]:=

Tutorial_3_answer_21.gif

Out[109]=

Tutorial_3_answer_22.png

Out[110]=

Tutorial_3_answer_23.png

Out[111]=

Tutorial_3_answer_24.png

Out[112]=

Tutorial_3_answer_25.png

Out[113]=

Tutorial_3_answer_26.gif

Exercise 1.3

Consider the score of a student as a two-D vector: {homework, classwork}. One way to combine this into a final score is to assign a weight to each score, for example:
              Tutorial_3_answer_27.png
Generate a multinormal random distribution to simulate a class (e. g. 100), and obtain a histogram of the combined score as a function of weight

Answer (given)

In[141]:=

Tutorial_3_answer_28.png

2. Matrix and vector product

2.1 Table of higher dimension: List of List

Consider this:

Out[8]=



price($)/unit



# units store A store B store C store D
juice 3 1.55 1.45 1.65 1.4
eggs 4 1.95 2.4 2 2.2
fruits 12 0.85 0.8 0.7 0.8
vegetables 8 1.35 1 1 1.1
milk 2 2.55 2.25 2.55 3.1
cereals 6 2.7 3.35 3.05 3.45
coffee 1 10.85 7.5 8.45 8.5
tea 2 4.2 4.15 3.75 3.95
ice cream 3 7.35 6.75 6.75 4.95
napkins 5 1.1 1.2 1.2 1.25
foils 2 3.5 3.75 3.6 3.75
storage bags 10 0.8 0.7 0.85 0.7
toothpaste 4 1.6 1.6 1.55 1.6
shampoo 3 3.55 2.6 2.9 2.7
detergent 2 9.55 9.05 8.65 7.1

How do we represent such data? It is a table. The first row is just

Tutorial_3_answer_29.png

Tutorial_3_answer_30.gif

In[68]:=

Tutorial_3_answer_31.png

In[69]:=

Tutorial_3_answer_32.png

Out[69]=



price($)/unit



# units store A store B store C store D
juice 3 1.55 1.45 1.65 1.4
eggs 4 1.95 2.4 2 2.2
fruits 12 0.85 0.8 0.7 0.8
vegetables 8 1.35 1 1 1.1
milk 2 2.55 2.25 2.55 3.1
cereals 6 2.7 3.35 3.05 3.45
coffee 1 10.85 7.5 8.45 8.5
tea 2 4.2 4.15 3.75 3.95
ice cream 3 7.35 6.75 6.75 4.95
napkins 5 1.1 1.2 1.2 1.25
foils 2 3.5 3.75 3.6 3.75
storage bags 10 0.8 0.7 0.85 0.7
toothpaste 4 1.6 1.6 1.55 1.6
shampoo 3 3.55 2.6 2.9 2.7
detergent 2 9.55 9.05 8.65 7.1

Exercise 2.1

Import the data from .csv file given, obtain the price table and name it something.

Answer

In[4]:=

Tutorial_3_answer_33.png

Out[4]=

Tutorial_3_answer_34.png

In[5]:=

Tutorial_3_answer_35.png

Out[5]=

Tutorial_3_answer_36.png

In[13]:=

Tutorial_3_answer_37.png

Out[13]=



price($)/unit



# units store A store B store C store D
juice 3 1.55 1.45 1.65 1.4
eggs 4 1.95 2.4 2 2.2
fruits 12 0.85 0.8 0.7 0.8
vegetables 8 1.35 1 1 1.1
milk 2 2.55 2.25 2.55 3.1
cereals 6 2.7 3.35 3.05 3.45
coffee 1 10.85 7.5 8.45 8.5
tea 2 4.2 4.15 3.75 3.95
ice cream 3 7.35 6.75 6.75 4.95
napkins 5 1.1 1.2 1.2 1.25
foils 2 3.5 3.75 3.6 3.75
storage bags 10 0.8 0.7 0.85 0.7
toothpaste 4 1.6 1.6 1.55 1.6
shampoo 3 3.55 2.6 2.9 2.7
detergent 2 9.55 9.05 8.65 7.1

Tutorial_3_answer_38.gif

In typical Jupyter notebook, this is what it looks like
Tutorial_3_answer_39.gif

Tutorial_3_answer_40.gif

2.2 Referencing elements from a multi-D array

2.2.1 Review: from Tutorial 2

Consider this two dimensional array

In[29]:=

Tutorial_3_answer_41.gif

Out[30]=

0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
32 33 34 35 36 37 38 39

In[31]:=

Tutorial_3_answer_42.png

Out[31]=

Tutorial_3_answer_43.png

In[67]:=

Tutorial_3_answer_44.png

Out[67]=

2.2.2 Sub-array and slice

Consider this:

Out[13]=



price($)/unit



# units store A store B store C store D
juice 3 1.55 1.45 1.65 1.4
eggs 4 1.95 2.4 2 2.2
fruits 12 0.85 0.8 0.7 0.8
vegetables 8 1.35 1 1 1.1
milk 2 2.55 2.25 2.55 3.1
cereals 6 2.7 3.35 3.05 3.45
coffee 1 10.85 7.5 8.45 8.5
tea 2 4.2 4.15 3.75 3.95
ice cream 3 7.35 6.75 6.75 4.95
napkins 5 1.1 1.2 1.2 1.25
foils 2 3.5 3.75 3.6 3.75
storage bags 10 0.8 0.7 0.85 0.7
toothpaste 4 1.6 1.6 1.55 1.6
shampoo 3 3.55 2.6 2.9 2.7
detergent 2 9.55 9.05 8.65 7.1

What if we want a row or a column of the above? Obviously, we can use Table:

In[64]:=

Tutorial_3_answer_46.png

Out[64]=

Tutorial_3_answer_47.png

But there is a way to do that with less typing

Tutorial_3_answer_48.png

Out[65]=

Tutorial_3_answer_49.png

Exercise 2.2

Define variable itemname and obtain from the table.

Answer

In[68]:=

Tutorial_3_answer_50.png

Out[68]=

Tutorial_3_answer_51.png

Tutorial_3_answer_52.gif

Tutorial_3_answer_53.gif

Don’t forget to do conversion from string to float.

Tutorial_3_answer_54.gif

For an experienced numpy user:

Tutorial_3_answer_55.gif

Exercise 2.3

Define variable storename and obtain from the table.

Answer

In[70]:=

Tutorial_3_answer_56.png

Out[70]=

Tutorial_3_answer_57.png

2.3 Matrix and vector dot product

Now, should we obtain the price list of each store? It’s OK to do for 4 stores, but what if you are doing market research for 10,000’s of store in the US? We don’t do price list for each store, we do for all stores.

Out[13]=



price($)/unit



# units store A store B store C store D
juice 3 1.55 1.45 1.65 1.4
eggs 4 1.95 2.4 2 2.2
fruits 12 0.85 0.8 0.7 0.8
vegetables 8 1.35 1 1 1.1
milk 2 2.55 2.25 2.55 3.1
cereals 6 2.7 3.35 3.05 3.45
coffee 1 10.85 7.5 8.45 8.5
tea 2 4.2 4.15 3.75 3.95
ice cream 3 7.35 6.75 6.75 4.95
napkins 5 1.1 1.2 1.2 1.25
foils 2 3.5 3.75 3.6 3.75
storage bags 10 0.8 0.7 0.85 0.7
toothpaste 4 1.6 1.6 1.55 1.6
shampoo 3 3.55 2.6 2.9 2.7
detergent 2 9.55 9.05 8.65 7.1

In[14]:=

Tutorial_3_answer_58.gif

Out[15]=

1.55 1.45 1.65 1.4
1.95 2.4 2 2.2
0.85 0.8 0.7 0.8
1.35 1 1 1.1
2.55 2.25 2.55 3.1
2.7 3.35 3.05 3.45
10.85 7.5 8.45 8.5
4.2 4.15 3.75 3.95
7.35 6.75 6.75 4.95
1.1 1.2 1.2 1.25
3.5 3.75 3.6 3.75
0.8 0.7 0.85 0.7
1.6 1.6 1.55 1.6
3.55 2.6 2.9 2.7
9.55 9.05 8.65 7.1

storeprice is now a 2D-array, which can also be called matrix.
Now, it is too tall, for convenience, we will flip it to the side, this is called Transpose

In[10]:=

Tutorial_3_answer_59.gif

Out[11]=

1.55 1.95 0.85 1.35 2.55 2.7 10.85 4.2 7.35 1.1 3.5 0.8 1.6 3.55 9.55
1.45 2.4 0.8 1 2.25 3.35 7.5 4.15 6.75 1.2 3.75 0.7 1.6 2.6 9.05
1.65 2 0.7 1 2.55 3.05 8.45 3.75 6.75 1.2 3.6 0.85 1.55 2.9 8.65
1.4 2.2 0.8 1.1 3.1 3.45 8.5 3.95 4.95 1.25 3.75 0.7 1.6 2.7 7.1

what is the first row of storePrT?

In[75]:=

Tutorial_3_answer_60.png

Out[75]=

Tutorial_3_answer_61.png

this is the price of store A. Thus:

In[22]:=

Tutorial_3_answer_62.png

Out[22]=

So, it is easy to calculate the cost for each store:

In[84]:=

Tutorial_3_answer_64.png

Out[84]=

Tutorial_3_answer_65.png

Is it the same as matrix vector product? The answer is yes, and we can simply the code by do this:

In[85]:=

Tutorial_3_answer_66.png

Out[85]=

Tutorial_3_answer_67.png

For writing, this is what it usually looks like:

In[86]:=

Tutorial_3_answer_68.png

Out[86]=

Tutorial_3_answer_69.png

But as we learn in linear algebra, we can also do this:

In[87]:=

Tutorial_3_answer_70.png

Out[87]=

Tutorial_3_answer_71.png

In[88]:=

Tutorial_3_answer_72.png

Out[88]=

Tutorial_3_answer_73.png

So, the HW 1B problem is quite simple with a few lines:

In[144]:=

Tutorial_3_answer_74.gif

Out[145]=

Tutorial_3_answer_75.png 

Out[146]=

Tutorial_3_answer_76.png

Out[147]=

Tutorial_3_answer_77.png

Out[148]=

Tutorial_3_answer_78.png

Out[149]=

Tutorial_3_answer_79.png

Out[150]=

the lowest cost is {139.,store D}

Out[151]=

Tutorial_3_answer_80.gif

Out[152]=

Tutorial_3_answer_81.gif

Continue to:  page 2       page 3

Created with the Wolfram Language