Math 236  •  Spring 2001

About function proj

Version 1.0:  5 April 2001

⚠  Caution!

If you need AppendRows or other function from the package LinearAlgebra`MatrixManipulation` be sure to load this package before you evaluate the cell where your proj is defined!  Otherwise, you will get "shadowing" warning messages and your proj will not be able to access the functions from that package

If you disregard this caution and get the shadowing message, do not despair.  Simply type and evaluate the expression
	Remove[AppendRows]  (* or other referenced object from the package *)
and then load the package and again evaluate the cell where your proj is defined.

Useful auxiliary function someNonzeroQ

Feel free to use as is, or to modify, the function someNonzeroQ defined below.

someNonzeroQ::usage  =  "someNonzeroQ[vec] returns True or False \
according to whether the list vec has some nonzero entry or not.";

someNonzeroQ[v_List] := Apply[Or, Map[N[#] != 0.&, v]]

The shape of vectors in arguments to proj

For purposes of the function proj, vectors are going to be represented as ordinary lists rather than as 1-column matrices.  Thus your function proj should accept as its second argument—the vector that is to be projected—an ordinary list representing the vector, e.g.
	{1,0,4,2}
Your function proj should accept as its first argument—the vectors onto whose span the projection is to be done—a list of ordinary lists representing those vectors, e.g.:
	{{1,2,3,5},{4,5,6,2},{5,7,9,7}}

If you wish, for extra credit you may design your function proj so that it accepts 1-column matrices in its input to represent vectors, too.  Thus your function could accept as its second argument
	{ {1}, {0}, {4}, {2} }
and as its second argument the list
	{ { {1},{2},{3},{5} }, { {4},{5},{6},{2} }, { {5},{7},{9},{7} } }
of 1-column matrices.  In such a case, the result should again be a 1-column matrix.  Whatever the "shape" of the vector being projected, that should be the shape of the resulting projected vector.

For such extra credit, you must not use branching (by an If, Which, or Switch expression) to separate program sections that handle the various shape inputs.

The validator is designed to test for the situation of vectors as 1-column matrices.  Unless you intend to do the extra credit task of making your proj work in this case, too, you may ignore any error message from such a test.

Define your function proj

In a separate notebook—not this one—define your function proj and any other objects of your own that you might happen to need for it.

Examples

Try out your own proj on the following examples—and compare the results with those shown below—before you attempt to run the validator!

vecs = {{1, 2, 3, 5}, {4, 5, 6, 2}, {5, 7, 9, 7}};
x = {1, 0, 4, 2};

The result of 
	proj[vecs, x]
should be:
	{(183/155), (272/155), (361/155), (351/155)}

y = {1., 0, 4., 2.};

The result of 
	proj[vecs, y]
should be:
	{1.1806451612903228`, 1.7548387096774196`, 
          2.329032258064516`, 2.264516129032258`}

Thus if any of the entries in the vectors involved are floating-point numbers, then so should be the entries in the resulting projected vector.  Otherwise, if the entries in all the vectors are exact numbers, then so should be the entries in the resulting projected vector.

Get the validator package vdproj.m

At any time before you are ready to validate your function proj, download my encoded package vdproj.m in any one of the following three ways:

•  At UMASTR lab only: from the Math_236 directory on drive M: (labelled Math-Stat Files).  An icon for this drive is on the desktop of UMASTR PCs.

• Obtain the file vdproj.zip from the Files page of the course web site http://www.math.umass.edu/Courses/Math_236.  Then extract the single file vdproj.m from vdproj.zip by using WinZip or another "unzip" program.  Be sure that the unzip program is configured so that it does not change end-of-line characters (unless you are going to run the validator on a Mac or on a Linux PC)!

•  If you have an ftp client that does not change end-of-line characters (so not Netscape or Internet Explorer!), get the file vdproj.m via anonymous ftp from:
	umastr.math.umass.edu
Set the initial remote directory to
 	/pub/lab/Math_236
and use binary mode (not text mode) to download the file vdproj.m.  For network security reasons, such anonymous ftp access is limited to client computers that are in the umass.edu domain.   This means that the ftp access must be made directly through a campus network-connected computer or via dial-up connection to OIT PPP ("UMAccess").

If you are using Mathematica on a Mac or under Linux

You need to change the end-of-line characters from the DOS convention of CR LF to the convention on your platform (just CR for Mac; just LF for Linux).  For example, under Linux, if you have downloaded vdproj.zip as a binary file, give the command:
	unzip -a vdproj.zip
This will extract vdproj.m and, as it does so, convert the end-of-lines to the *nix LF.

Validating your function proj

A program of mine is going to generate a set of test data pairs, each pair consisting of a list of vectors onto whose span projection is to be done along with a vector to be projected; it will evaluate your function project in turn at each of these pairs.  The test data it uses for you are not necessarily the same as the test data it uses for others.  In fact, some of these data involve random numbers, and these random numbers depend in part on the digits in your student ID number.

Do the following four steps:

1.  Be sure that at this Mathematica session you have defined your proj and all the other objects it references directly or indirectly.  In particular, be sure you have already defined any needed functions from past homework and have loaded the package LinearAlgebra`MatrixManipulation` if you need access, for example, to the function AppendRows from that package.

2.  Inside the double quotes in the next cell, type your  student ID number and then evaluate the cell.

myID = "";           
(* type your student ID number inside the quotes *)

Check that you correctly defined myID—evaluate the following cell now.  You should see your ID number as result.

myID

3.  Tell Mathematica where to look for my validator package vdproj.m—edit the following cell so that its first line shows the correct path to the directory to which you downloaded or extracted vdproj.m:

vdprojpath  =  "c:/data";              
AppendTo[$Path,  vdprojpath];

4.  Evaluate the preceding cell.  Check that the preceding cell was indeed evaluated—evaluate the following cell.  The result should include at its end the path to the directory holding the file vdproj.m.

$Path

5.  Run the validator  that tests your function—evaluate the following cell now:

Needs["vdproj`"]            (* run the validator *)

If you have forgotten to define myID correctly, then you can simply edit myID to make it correct—be sure your ID number is enclosed in double quotes—and then run the preceding cell with Needs["vdproj`"] again.

If, however, at the current Mathematica session you forgot to define proj or some auxiliary function needed by it—in particular, if you forgot to load a requisite package such as LinearAlgebra`MatrixManipulation`—or if you need to fix up your proj to get it to work properly, then you should:
•  save your notebook containing your function definitions;
•  save this notebook Aboutproj.nb with your myID and vdprojpath defined;
•  quit Mathematica and start it again (or, if you know how, leave Mathematica open but quit the kernel and restart the kernel);
•  open your notebook with your function definitions and evaluate them all;
•  open this notebook Aboutproj.nb, evaluate its cells defining my auxiliary functions and your myID and vdprojpath; and
•  run the validator again by evaluating the cell Needs["vdproj`"] .

What to turn in

Do not turn in a hardcopy of this entire notebook!  Rather, turn in only:

•  A listing of your proj and any other objects you defined for it.

•  The validation output that results from evaluating the cell Needs["vdproj`"].