uwhpsc-2016 / homework2

Homework #2
0 stars 3 forks source link

mat_add parameters? #14

Open nishalad95 opened 8 years ago

nishalad95 commented 8 years ago

I'm having some trouble with the mat_add function we need to write. I'm not quite sure how C knows what parameters you need to pass into the function or not, because taking a look at the vec_add function we do not need to pass in every single parameter. Can anyone help me understand why this is? So that I can apply this to the mat_add function. Also - do we pass a 1d array into the mat_add function? or a 2d matrix? As when I pass in a 2d matrix, It fails the test. I'm slightly confused on the pointers parameters in these functions.

cswiercz commented 8 years ago

By "pass in 2d matrix" do you mean at the C level or Python level?

Within C, the "matrices" A and B are represented by arrays of length MxN of type double, just like in Lecture 08. You need to write a function that takes in two such arrays representing matrices and form an array out of length MxN that represents a matrix which is a sum of the two input matrices.

The confusion might come from the way you create a matrix using Numpy. Take a look at the Python code below:

>>> import numpy
>>> A = numpy.array([[1,2,3], [4,5,6]], dtype=numpy.double)

A represents a matrix and when we print A in Python it shows the elements in a 2d fashion. However, underlying this array is an actual C array but this C array is a 1d array of length 6. The wrappers I have written in homework2/wrappers.py send a pointer to this underlying C array to the function mat_add.

Does this make more sense, now?

nishalad95 commented 8 years ago

I mean pass in at the Python level in our test_homework2.py.

I think that makes a lot more sense now, taking a look a the wrappers.py also. One other question - does this also mean than when comparing the added matrices in our tests in the test_homework2.py to the actual answer, should we use .all() to compare the values in the matrix produced by the mat_add function and the true answer?

cswiercz commented 8 years ago

Another good question. Remember that every floating point operation comes with it a tiny amount of error. Performing the following in Python:

C = mat_add(A, B)
self.assertEqual(C, A+B)  # evaluates: C == (A + B)

may return false because of this error!

So what are some ways to check if two matrices / arrays are close in value? There are many decent ways and I'll let you think about it.

nishalad95 commented 8 years ago

well there is the way that you can check every single element using the .all() but I feel as though this is very costly with time. I think I will have to compute the norm of the vector/matrix or the norm of the difference between my vector/matrix and the true answer, compare this with zero to see if they are very similar.

I also had one last question. It doesn't say anywhere within this homework that we should use parallelism within our code, but now the deadline has been extended to Friday, so are you expecting that we should implement it in, or can this be an option?

cswiercz commented 8 years ago

well there is the way that you can check every single element using the .all() but I feel as though this is very costly with time. I think I will have to compute the norm of the vector/matrix or the norm of the difference between my vector/matrix and the true answer, compare this with zero to see if they are very similar.

I think using the matrix norm is a good method.

When it comes to "costly", the infinity norm would be the cheapest: compute the absolute value of every element and return the largest one. However, there are some scientific applications where the infinity norm may be insufficient or inappropriate.

I also had one last question. It doesn't say anywhere within this homework that we should use parallelism within our code, but now the deadline has been extended to Friday, so are you expecting that we should implement it in, or can this be an option?

This will not be an option because we won't use the -fopenmp flag during compilation. In particular, we'll use the name exact Makefile as provided with the homework.

nishalad95 commented 8 years ago

Thank you!