CSC344 – Assignment 2


Category: You will Instantly receive a download link for .zip solution file upon Payment


5/5 - (3 votes)

Write a Clojure function which takes as input an unevaluated list of names of functions which operate on pairs of numbers and two numbers. Apply each function to the two numbers and return the list (or vector) of results.

Example usage:

You will want to familiarize yourself with list operations such as first, rest, conj, concat, etc. You may use eval in the microproject.

Main Project

Write a set of Clojure functions that perform symbolic simplification and evaluation of an expression computing a series of affine transforms of a point.

An affine transform is a transformation of points such that ratios of distances between points is preserved along with collinearity (points on a line before the transformation are still on a line after). Such a transformation in two dimensional space can be represented as a 2×2 matrix. The transformation of a point is calculated by multiplying this matrix with a 2×1 matrix containing the original x and y coordinates of the point, as follows:

\begin{bmatrix}a & b\\c & d\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}=\begin{bmatrix}ax+by\\cx+dy\end{bmatrix}See the first 15 slides of this lecture from Texas A&M for further background and pictorial examples.

In Clojure a transform matrix will be represented as a vector of arity two, both of whose elements are arity 2 vectors.

Expressions representing application of one or more of these transformations are created as unevaluated lists. For example:

Any non-numeric value appearing in the transform matrix or the point should be considered a variable.

Your goal is to maximally simplify and evaluate these expressions. In some cases this will result in a concrete numeric answer. In other cases, as in p3 above, your final result may not be fully evaluable and will contain variables. The p3 example is simplified as follows:

In the process of writing your program you will need to simplify a limited set of mathematical expressions (those of arity 2 with * and +, and arity 1 with -). You will implement at least the following simplification rules for these expressions:

You will also implement the evaluation of the affine transforms, using the above simplifications. Some examples are shown below.

Your program will also allow binding of some or all of the variables in the expression with constants (numbers) before simplification and (partial) evaluation. To do so, you should make use of the substitution functions discussed in class.

The main entry point of your program, evalexp, will call functions that simplify, bind, and evaluate the input expression.

The evalexp function should take a symbolic expression and a binding map and return the simplest form. One way to define this is


binds a and y (but not x) in p1, leading to (transform [[5 3] [0 0]] [x 2])) and then further simplifies to [(+ (* 5 x) 6) 0].

Note: You may not use eval except in cases of simple math (e.g., (+ 3 4))

Suggested Development Environment

Eclipse + Counterclockwise