Commit 657dd7a3 authored by Tandri Gauksson's avatar Tandri Gauksson

MatrixClass synced with CodeExpert

parent a62b52c1
......@@ -19,15 +19,17 @@ The test prints `constantTriangular(3,20)`, and the correct result is:
0 0 20
```
***
> `arithmetics(int m, int n)`: This function performs some arbitrary arithmetics on a random m by n matrix (the random seed is fixed).
The test prints `arithmetics(2,5)`, and the correct result is:
```
-18.983
19.4972
```
***
> `casting()`: This function calculates the inner product of the integer vector `[2 1]` and the complex vector `[1-i, 5+i]` and returns the real part as a double.
The test prints `casting()`, and the correct result is:
```
6
```
***
> `arithmetics(int m, int n)`: This function performs some arbitrary arithmetics on complex n by n matrices.
The test prints `arithmetics(4)`, and the correct result is:
```
(-41.4706,27.1176)
(-23.7692,42.8462)
(-6.47692,48.4154)
(7.2,48.4)
```
\ No newline at end of file
// "testname", "input", "expected output", "points", "execution time limit (ms)"
"smallTriangular", "(any input)", "1 2\n0 3", "1", "5000",
"constantTriangular", "(any input)", "20 20 20\n 0 20 20\n 0 0 20", "1", "5000",
"arithmetics", "(any input)", "-18.983\n19.4972", "1", "5000",
"casting", "(any input)", "6", "1", "5000",
\ No newline at end of file
"casting", "(any input)", "6", "1", "5000",
"arithmetics", "(any input)", "(-41.4706,27.1176)\n(-23.7692,42.8462)\n(-6.47692,48.4154)\n (7.2,48.4)", "1", "5000",
......@@ -10,122 +10,153 @@
// END
Eigen::Matrix2d smallTriangular( double a, double b, double c ){
/*
* This functions returns a 2 by 2 triangular matrix of doubles a, b, c.
*/
// We know in advance that we want to create a matrix of doubles with a fixed size of 2 by 2.
// Therefore, we pass the parameters <double, 2, 2> to the template class Eigen::Matrix.
Eigen::Matrix< double, 2, 2 > A;
// We have declared the variable A of the type Eigen::Matrix<double,2,2>,
// but we have not initialized its entries.
// We can do this using comma-initialization:
A << a, b, 0, c;
// Question: Is A now an upper triangular matrix, or a lower triangular matrix?
return A;
}
/* SAM_LISTING_BEGIN_0 */
Eigen::Matrix<double, 2, 2> smallTriangular(double a, double b, double c) {
/*
* This functions returns a 2 by 2 triangular matrix of doubles a, b, c.
*/
Eigen::MatrixXd constantTriangular( int n, double val ){
/*
* This function returns an n by n upper triangular matrix with the constant value val in the upper triangular part.
*/
// Now we do not know the size of our matrix at compile time.
// Hence, we use the special value Eigen::Dynamic to set the size of A.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > A;
// Eigen::Matrix has a method Zero(n_rows,n_cols) which returns the n_rows by n_cols matrix whose entries are all equal to zero.
A = Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Zero( n, n );
// To get a triangular matrix, we must set the values above the diagonal to val.
// This we can do by a nested for-loop.
for(int row = 0; row < n; row++){
// TO DO: Write the inner for-loop.
// Hint: We can access and change the entries of A using parentheses, i.e. A(row,col) = val;
// Note that indexing starts at the value 0 (as usual), and not 1 (as in Matlab).
// START
// END
}
return A;
}
Eigen::VectorXd arithmetics(int m, int n){
/*
* This function does not do anything meaningful.
* It is only meant to show some simple Eigen arithmetics and matrix initializations.
*/
// Because the syntax Eigen::Matrix< type, n_rows, n_cols > is very cumbersome,
// Eigen provides convenience classes that work as shorthands.
// For example, Eigen::Matrix2d is shorthand for Eigen::Matrix< double, 2, 2 >.
// This declares dynamic size (signified by the letter 'X') matrices of doubles (signified by the letter 'd').
Eigen::MatrixXd A, B, C, I;
// We initialize the matrices arbitrarily.
A = constantTriangular( n, 5.0 ).transpose(); // an n by n lower triangular matrix
B = Eigen::MatrixXd::Random( m, n ); // a random m by n matrix with values between -1 and 1.
I = Eigen::MatrixXd::Identity( n, n ); // The n by n identity matrix.
// We can perform arithmetics on matrices: +, -, *
// Note that for + and -, the left hand matrix and the right hand matrix have to be the same size,
// and that matrix multiplication * is only defined if the number of columns
// in the left hand matrix is the same as the number of rows in the right hand matrix.
C = B*(A - 5.0*I);
// Eigen::VectorXd is shorthand for Eigen::Matrix< double, Eigen::Dynamic, 1 >.
// Hence, all the same arithmetics work for vectors.
Eigen::VectorXd u, v;
// TO DO: Initialize the entries of u as the integers from 1 to n, multiply u by the matrix C, and store the result in v.
// START
// END
return v;
}
// We know in advance that we want to create a matrix of doubles with a fixed
// size of 2 by 2. Therefore, we pass the parameters <double, 2, 2> to the
// template class Eigen::Matrix.
Eigen::Matrix<double, 2, 2> A;
// We have declared the variable A of the type Eigen::Matrix<double,2,2>,
// but we have not initialized its entries.
// We can do this using comma-initialization:
A << a, b, 0, c;
// Question: Is A now an upper triangular matrix, or a lower triangular
// matrix?
return A;
}
/* SAM_LISTING_END_0 */
/* SAM_LISTING_BEGIN_1 */
Eigen::MatrixXd constantTriangular(int n, double val) {
/*
* This function returns an n by n upper triangular matrix with the constant
* value val in the upper triangular part.
*/
// Now we do not know the size of our matrix at compile time.
// Hence, we use the special value Eigen::Dynamic to set the size of A.
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> A;
// Eigen::Matrix has a method Zero(n_rows,n_cols) which returns the n_rows by
// n_cols matrix whose entries are all equal to zero.
A = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(n, n);
// To get a triangular matrix, we must set the values above the diagonal to
// val. This we can do by a nested for-loop.
for (int row = 0; row < n; row++) {
// TO DO: Write the inner for-loop.
// Hint: We can access and change the entries of A using parentheses, i.e.
// A(row,col) = val; Note that indexing starts at the value 0 (as usual),
// and not 1 (as in Matlab).
double casting(){
/*
* This function does not do anything meaningful.
* It is only meant to introduce a few more typedefs and how to cast them.
*/
// RowVectorXi is a shorthand for Eigen::Matrix< int, 1, Eigen::Dynamic >
// Constant(2,1) creates a vector of size 2 and initializes the entries with the value 1.
Eigen::RowVectorXi u = Eigen::RowVectorXi::Constant(2,1);
// std::complex is a template class for complex numbers.
// Here we declare two complex numbers, with real and imaginary parts represented by doubles.
// z0 = 1 - i
// z1 = 5 + i
std::complex<double> z0(1,-1), z1(5,1);
// Declare and initialize a size 2 vector of std::complex<double>.
Eigen::Vector2cd v;
v(0) = z0;
v(1) = z1;
double x;
// TO DO: Calculate u*v and store the result in x.
// Hint: First use u.cast< NEW TYPE >() to cast the "int" vector u to a "std::complex<double>" vector.
// The result will be u*v = 1*(1-i) + 1*(5+i) = 6 + 0i, a real number. You can get the real part of a std::complex<double> by .real().
// START
// END
return x;
}
}
return A;
}
/* SAM_LISTING_END_1 */
/* SAM_LISTING_BEGIN_2 */
double casting() {
/*
* This function does not do anything meaningful.
* It is only meant to introduce vectors and how to typecast.
*/
// Because the syntax Eigen::Matrix< type, n_rows, n_cols > is very
// cumbersome, Eigen provides convenience classes that work as shorthands.
// For example, Eigen::Matrix2d is shorthand for Eigen::Matrix< double, 2, 2 >.
// Vectors are just a special type of matrices that have only one column.
// Thus, VectorXi is a shorthand for Eigen::Matrix< int, Eigen::Dynamic, 1 >.
// Constant(2,1) creates a vector of size 2 and initializes the entries with
// the value 1.
Eigen::VectorXi u = Eigen::VectorXi::Constant(2, 1);
// std::complex is a template class for complex numbers.
// Here we declare two complex numbers, with real and imaginary parts
// represented by doubles. z0 = 1 - i z1 = 5 + i
std::complex<double> z0(1, -1), z1(5, 1);
// Declare and initialize a size 2 vector of std::complex<double>.
Eigen::Vector2cd v;
v(0) = z0;
v(1) = z1;
double x;
// TO DO: Calculate the inner product of u and v, and store the result in x.
// Hint: The inner product of two vectors is given by u.dot(v), but
// first we need to cast the "int" vector u to a "std::complex<double>" vector.
// Use u.cast< NEW TYPE >() to achieve this.
// The result of the inner product will be 1*(1-i) + 1*(5+i) = 6 + 0i,
// a real number. You can get the real part of an std::complex<double>
// using the method "real()".
// START
// END
return x;
}
/* SAM_LISTING_END_2 */
/* SAM_LISTING_BEGIN_3 */
Eigen::VectorXcd arithmetics(int n) {
/*
* This function does not do anything meaningful.
* It is only meant to show some simple Eigen arithmetics and matrix
* initializations.
*/
// This declares dynamic size (signified by the letter 'X') matrices of
// complex numbers (signified by the letter c) with real and imaginary
// parts represented by doubles (signified by 'd').
Eigen::MatrixXcd A, C, I;
// We initialize the matrices arbitrarily:
// an n by n lower triangular matrix,
A = constantTriangular(n, 5.0).transpose().cast<std::complex<double>>();
// The n by n identity matrix,
I = Eigen::MatrixXcd::Identity(n, n);
// Declare the n by n matrix B.
Eigen::MatrixXcd B(n,n);
// TO DO: Fill in the matrix B such that B(k,l) = (k+l*i)/(k-l*i),
// where i is the imaginary unity, i^2 = -1.
// START
// END
// We can perform arithmetics on matrices: +, -, *
// Note that for + and -, the left hand matrix and the right hand matrix have
// to be the same size, and that matrix multiplication * is only defined if
// the number of columns in the left hand matrix is the same as the number of
// rows in the right hand matrix.
C = B * (A - 5.0 * I);
// Eigen::VectorXcd is shorthand for
// Eigen::Matrix< std::complex<double>, Eigen::Dynamic, 1>.
// Hence, all the same arithmetics work for vectors.
Eigen::VectorXcd u, v;
// TO DO: Initialize the entries of u as the integers from 1 to n,
// multiply u by the matrix C, and store the result in v.
// START
// END
return v;
}
/* SAM_LISTING_END_3 */
#endif
......@@ -19,15 +19,17 @@ The test prints `constantTriangular(3,20)`, and the correct result is:
0 0 20
```
***
> `arithmetics(int m, int n)`: This function performs some arbitrary arithmetics on a random m by n matrix (the random seed is fixed).
The test prints `arithmetics(2,5)`, and the correct result is:
```
-18.983
19.4972
```
***
> `casting()`: This function calculates the inner product of the integer vector `[2 1]` and the complex vector `[1-i, 5+i]` and returns the real part as a double.
The test prints `casting()`, and the correct result is:
```
6
```
***
> `arithmetics(int m, int n)`: This function performs some arbitrary arithmetics on complex n by n matrices.
The test prints `arithmetics(4)`, and the correct result is:
```
(-41.4706,27.1176)
(-23.7692,42.8462)
(-6.47692,48.4154)
(7.2,48.4)
```
\ No newline at end of file
......@@ -7,11 +7,10 @@ int main(){
std::cout<< smallTriangular(1,2,3) << std::endl;
std::cout<< "\nTest of constantTriangular():\n";
std::cout<< constantTriangular(3,20) << std::endl;
std::cout<< "\nTest of arithmetics():\n";
std::srand(5); // So that random behaviour is predictable.
std::cout<< arithmetics(2,5) << std::endl;
std::cout<< "\nTest of casting():\n";
std::cout<< casting() << std::endl;
std::cout<< "\nTest of arithmetics():\n";
std::cout<< arithmetics(4) << std::endl;
return 0;
}
// "testname", "input", "expected output", "points", "execution time limit (ms)"
"smallTriangular", "(any input)", "1 2\n0 3", "1", "5000",
"constantTriangular", "(any input)", "20 20 20\n 0 20 20\n 0 0 20", "1", "5000",
"arithmetics", "(any input)", "-18.983\n19.4972", "1", "5000",
"casting", "(any input)", "6", "1", "5000",
\ No newline at end of file
"casting", "(any input)", "6", "1", "5000",
"arithmetics", "(any input)", "(-41.4706,27.1176)\n(-23.7692,42.8462)\n(-6.47692,48.4154)\n (7.2,48.4)", "1", "5000",
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment