uwhpsc-2016 / homework1

Homework #1
1 stars 1 forks source link

Exercise 3 Linear Algebra Nature #28

Open slai7880 opened 8 years ago

slai7880 commented 8 years ago

This is part of the code I copied from the provided test suite:

def test_jacobi_step(self):
    # the test written below only tests if jacobi step works in the case
    # when A is the identity matrix. In this case, jacobi_step() should
    # converge immediately the the answer. (Can you see why based on the
    # definition of Jacobi iteration?) This is not sufficient for testing
    # if jacobi_step() works properly but is a good start.
    D = eye(3)
    L = zeros((3,3))
    U = zeros((3,3))
    b = array([1,2,3])
    x0 = ones(3)
    x1 = jacobi_step(D, L, U, b, x0)

It looks like it's trying to define an equation Ax = b, where A is 3 by 3, x(variable x0) is 1 by 3 and b is also 1 by 3. But this violates the rule of matrix operation doesn't it? Should we manually solve it in our implementation in exercise 3? Or should we just keep it this way and pretend x to be 3 by 1 and b 3 by 1?

alyfarahat commented 8 years ago

We do not need to worry about b and x being 1x3 or 3x1. Numpy functions will take care of that for you. You can first try them on Jupyter notebook to gain some confidence.

slai7880 commented 8 years ago

The problem is we do. In the header of exercise3.py some functions are imported and we are only allowed to use those functions however clearly we need to perform matrix addition and subtraction when computing things like (L + U), while add and subtract is not included in the header so I suppose we need to implement them. In this case we do need to worry about the dimension, unless I miss some tricks.

cswiercz commented 8 years ago

We do not need to worry about b and x being 1x3 or 3x1. Numpy functions will take care of that for you. You can first try them on Jupyter notebook to gain some confidence.

To clarify, as mentioned in class when you perform

M = numpy.array( ... )  # m x n array
v = numpy.array( ... )  # array of n elements
numpy.dot(M, v)

the array v is treated as if it were a column vector.

This design decision will make more sense, interestingly enough, when we start talking about C this week. The question being, what is an efficient way to store matrices and vectors in C?