Exercise 1 (Strings, Arrays, Classes, Methods)
Completion requirements
Assumptions
Opened: Wednesday, 11 March 2015, 12:00 AM
Due: Wednesday, 18 March 2015, 11:55 PM
- Let \( f\colon \mathbb{R}^n\rightarrow \mathbb{R}^m \) be a linear map.
- Compute the transformation matrix \( M \) for arbitrary \( f \) such that \( f(\vec{x})=M\vec{x} \) for all \( \vec{x}\in\mathbb{R}^n \).
Assumptions
- We approximate \( x\in \mathbb{R} \) by float values and vectors \( \vec{x} \) by float arrays.
- A linear function \( f \) is represented by a class named LinearFnc.
- Assume that LinearFnc contains a method applyTo which maps a float array of length \( n \) to a float array of length \( m \).
public class LinearFnc {
...
public float[] applyTo(float[] domVec) {
float[] ranVec = new float[m];
...
return ranVec;
}
}
Implementation
- Write an arbitrary test class which contains the entry point public static void main(...) {...}. This class is for testing purpose only and must not be submitted.
- Write a class Matrix which represents \( M \) in a separate file Matrix.java. This class should not contain an entry point public static void main(...) {...}.
- The constructor of Matrix fixes the dimension \( m\times n \).
- The class Matrix should contain three methods:
public void initMatrix(LinearFnc lfc) {...
public float[] applyTo(float[] domVec) {...
public String toString() {...
- initMatrix computes the internal representation of \( M \), such that \( M\vec{x} \) can be computed without \( f \), for arbitrary \( \vec{x}\in\mathbb{R}^n \). The internal representation should be private.
- applyTo computes \( M\vec{x} \), such that \( f(\vec{x})=M\vec{x} \) for all \( \vec{x}\in\mathbb{R}^n \).
- toString returns a String representation of the transformation matrix \( M \).
- Submit only the file Matrix.java.
Hint
Implement a class LinearFnc which represents some fixed \( f \) and compare the result of Matrix.applyTo with the result of LinearFnc.applyTo for some test cases.