Problem Set 1

Due Thursday Jan. 30, 12:30 pm

Comments

  • I haven’t fully worked these problems myself, so if you run into any strange issues, please post on Ed as there could be mistakes/oversights on my part.
  • Please submit as a PDF to Gradescope.
  • Please generate the PDF using Quarto. Feel free to work in a Jupyter notebook and then convert to Quarto before rendering to PDF. Other formats that look professional and are designed for working with code and math notation may also be fine - just check with me via a public post on Ed first.
  • Remember to note at the start of your document the names of any other students that you worked with on the problem set (or indicating you didn’t work with anyone if that was the case) and then indicate in the text or in code comments any specific ideas or code you borrowed from another student or any online reference (including ChatGPT or the like).
  • For problems 1 and 5, your solution should not just be code - you should have text describing how you approached the problem and what decisions/conclusions you made, though for simple problems, this can be quite short. Your code should have comments indicating what each function or block of code does, and for any lines of code or code constructs that may be hard to understand, a comment indicating what that code does.
  • You do not need to (and should not) show exhaustive output, but in general you should show short examples of what your code does to demonstrate its functionality. The output should be produced as a result of the code chunks being run during the rendering process, not by copy-pasting of output from running the code separately (and definitely not as screenshots).
  • I do not recommend writing initial answers using a ChatBot, as I think you are likely to fool yourself in terms of how much you are learning about Julia and programming concepts/skills more generally. But it’s up to you to decide how heavily to rely on a ChatBot. And refining your initial answers using a ChatBot seems like a good strategy. Using your own knowledge and information online to check the results of a ChatBot and using a ChatBot to check your own coding can both be important/useful.

Problems

  1. Write a function that implements a basic version of Newton’s method for minimizing a function of one variable.

    1. Start with a simple implementation. Use an existing Julia package to implement the finite difference estimates for the gradient and Hessian. Think about the arguments and any defaults, as well as the type of the output. For the moment don’t set the types of the input arguments. You can start by assuming the function takes only one argument and simply returns the value at which the function is minimized.

    2. Now consider returning richer output. Consider using a named tuple, a dictionary, or a struct. What seem like the advantages/disadvantages? Choose one and implement it.

    3. Set up an array and save the progression of values along the optimization path.

    4. (We won’t cover this in class until Tuesday Jan. 28.) Add argument typing to your Newton function. Allow the initial value to be of any numeric type. See what the the type of the return value is for various numeric input types.

    5. Use a ChatBot (lmarena.ai provides free access if you don’t want to sign up for the free tier of one of the commercial services) to write the code. Compare it to your code and indicate strengths/weaknesses.

  2. Define the matrix A = [1:4 5:8 ones(Int64,4)]. Predict the result of performing the following operations in Julia (before checking your answers by running them). Note that the lines are meant to be run one-by-one in the same workspace, so when you change an array, this will affect the subsequent statements.

    1. x = A[3,:]
    2. B = A[2:2,1:3]
    3. A[1,1] = 9 + A[2,3]
    4. A[1:3,2:3] = [0 0; 0 0; 0 0]
    5. A[1:2,2:3] = [1 3; 1 3]
    6. y = A[1:4, 3]
    7. A = [A [2 1 7; 7 4 5; ones(Int64,2,3)]]
    8. C = A[[1,3],2]
    9. D = A[[1,2,4],[1,3,4]]
  3. Experiment with some ways to extract the elements of a vector that correspond to the even indices, i.e., x[2], x[4],…

  4. Use array functions and vectorization to solve the problems below using only a single line of code for each problem, where A = reshape((-22:22) .% 11, 9, 5).

    1. Count the number of elements for which \(A_{i,j}^2<10\).
    2. Create a matrix containing only the columns of A where the first element \(A_{1,j} >= 0\).
    3. Modify A such that all elements that are even are multiplied by 3.
  5. Consider dictionaries, named tuples, and structs. Experiment with sizeof and pointer_from_objref to try to understand memory use (including any pointers) of these data structures. Next consider arrays that have homogeneous types and those with heterogeneous types.