Commit 9dc0c178 authored by Tandri Gauksson's avatar Tandri Gauksson

MatrixBlocks synced with CodeExpert

parent c397ec0a
#ifndef MATRIXBLOCK_HPP
#define MATRIXBLOCK_HPP
// The purpose of this exercise is introduce block operations on Eigen matrices.
// TO DO: Include the appropriate header files.
// START
// END
// We can use the Eigen namespace to improve the readability of our code.
// This allows us to skip the "Eigen::" in "Eigen::MatrixXd" for example.
// TO DO: Add the following line below: using namespace Eigen;
// START
// END
/* SAM_LISTING_BEGIN_0 */
MatrixXd zero_row_col(const MatrixXd &A, int p, int q) {
/*
* This function takes a matrix A, and returns a matrix that is exactly the
* same, except that row p and column q are set to zero.
*/
// Make a copy of A.
MatrixXd Anew(A);
// TO DO: Set the entries of row number p and column number q to zero.
// Hint: We can access rows and columns of A by A.row() and A.col().
// The setZero() is useful here. START
// END
return Anew;
}
/* SAM_LISTING_END_0 */
/* SAM_LISTING_BEGIN_1 */
MatrixXd swap_left_right_blocks(const MatrixXd &A, int p) {
/*
* Writing as a block matrix A = [B C], where B denotes the first p columns of
* A, and C denotes the q=(A.cols() - p) last columns, this functions returns
* D = [C B].
*/
MatrixXd B, C;
// We can use .rows() and .cols() to get the number of rows and columns in A.
int q = A.cols() - p;
// A.block( i, j, m, n ) returns the m by n block that has its top-left corner
// at the index (i,j) in A. Hence, the first p columns of A can be accessed in
// the following way:
B = A.block(0, 0, A.rows(), p);
// TO DO: Use A.block() to define C as the matrix containing the last q
// columns of A. START
// END
// Make a copy of A.
MatrixXd Anew(A);
// The block() method can access arbitrary blocks within a matrix.
// For our purposes, it is actually simpler to use leftCols() and rightCols().
Anew.leftCols(q) = C;
// TO DO: Use A.rightCols() to fill in the remaining columns of the new matrix
// A. START
// END
// Tip: Many more methods exist that are special cases of block(),
// e.g. topRows(), bottomRows(), topLeftCorner(), bottomLeftCorner(), ...
// For vectors we have head(), tail(), and segment().
return Anew;
}
/* SAM_LISTING_END_1 */
/* SAM_LISTING_BEGIN_2 */
MatrixXd tridiagonal(int n, double a, double b, double c) {
/*
* This function creates an n by n tridiagonal matrix with the values
* a on the first subdiagonal,
* b on the diagonal, and
* c on the first superdiagonal.
* Example for n=5:
* [ b c 0 0 0 ]
* [ a b c 0 0 ]
* A = [ 0 a b c 0 ]
* [ 0 0 a b c ]
* [ 0 0 0 a b ]
*/
MatrixXd A;
// TO DO: Fill the matrix A with zeros. Then fill the subdiagonal with the
// value a, the diagonal with b and the superdiagonal with c. Hint: You can
// get the diagonal of A by A.diagonal(). Moreover, you can get the super- and
// subdiagonals by passing +1 or -1 as arguments to A.diagonal(). START
// END
return A;
}
/* SAM_LISTING_END_2 */
#endif
# Code Expert Generic Project Configuration.
#
# In case this file is omitted, the execution is done with the default values.
# You can either define a cmd or a script file to be executed.
# Default to script file setting a cmd overrides it.
cmd:
# compile: echo "COMPILE"
# run: echo "RUN"
# test: echo "TEST"
# submit: echo "SUBMIT"
# All scripts are set by default to "/scripts/${ACTION}.sh"
script:
compile: /scripts/compile.sh
run: /scripts/run.sh
test: /scripts/test.sh
submit: /scripts/submit.sh
\ No newline at end of file
descriptions:
default:
name: Default
default: true
# Optionally add mulitple versions here.
# Use the file name without the .md extension as key and add a `name:` to it.
\ No newline at end of file
# Tutorial: part 2/3
## Matrix blocks
> Eigen matrices have a range of methods to access their submatrices (blocks), instead of individual entries at a time.Suppose A is an Eigen::MatrixXd object, then a few useful methods are:
* `A.row(i)`: the i-th row of A,
* `A.col(j)`: the j-th column of A,
* `A.topRows(p)`: the first p rows of A,
* `A.leftCols(q)`: the first q columns of A,
* `A.bottomRightCorner(p,q)`: the p by q matrix formed by the intersection of the last p rows and last q columns of A, and more generally
* `A.block(i,j,p,q)`: the p by q submatrix of A, whose top-left corner is at the index (i,j).
> The purpose of this exercise is to learn to declare and initialize Eigen matrices, and to get familiar with some handy typedefs and methods.
> Open "MatrixBlocks.hpp" and fill in the missing code in between the delimiters `// START` and `// END` according to the instructions preceded by `// TO DO:`.
> Read each function of the program carefully. The code will not compile until after the first two tasks are finished, which is to include headers and set the namespace.
\ No newline at end of file
# Tests of MatrixBlocks.hpp
> There are three functions in MatrixBlocks.hpp and main.cpp has one test for each function.
***
> `zero_row_col( MatrixXd A, int p, int q )`: This function returns a copy of A with row p and column q set to zero.
The test prints `zero_row_col(Matrix3d::Constant(-1),0,1)`, and the correct result is:
```
0 0 0
-1 0 -1
-1 0 -1
```
***
> `swap_left_right_blocks( MatrixXd A, int p )`: This function swaps splits the matrix A into two blocks, the first p columns and the rest, and returns a matrix in which these blocks have been swapped.
The test prints `swap_left_right_blocks(MatrixXd::Identity(4,3),2)`, and the correct result is:
```
0 1 0
0 0 1
1 0 0
0 0 0
```
***
> `tridiagonal( int n, double a, double b, double c)`: This function creates an n by n tridiagonal matrix with constant diagonals with the values a, b, and c.
The test prints `tridiagonal(4,-1,2,-1)`, and the correct result is:
```
2 -1 0 0
-1 2 -1 0
0 -1 2 -1
0 0 -1 2
```
#include "MatrixBlocks.hpp"
#include <iostream>
int main(){
std::cout<< "\nTest of zero_row_col():\n";
Matrix3d A = Matrix3d::Constant(-1);
std::cout<< zero_row_col( A, 0, 1 ) << "\n\n";
std::cout<< "Test of swap_left_right_blocks():\n";
MatrixXd B = MatrixXd::Identity( 4, 3 );
std::cout<< swap_left_right_blocks( B, 2 ) << "\n\n";
std::cout<< "Test of tridiagonal():\n";
std::cout<<tridiagonal(4,-1,2,-1)<< "\n\n";
return 0;
}
#!/bin/bash
echo "Compiling ..."
# compile c code
# find . -iname '*.cpp' | sort | xargs g++ -fdiagnostics-color=always --pedantic -Wextra -Wall -std=c++14 -I/usr/include/python3.7m -llibpython -o bin/a.out || exit 1
# Compiling for 'matplotlibcpp.h'
mkdir -p bin
find . -iname '*.cpp' | sort | xargs \
g++ -fdiagnostics-color=always -std=c++11 \
-I/usr/local/include/python3.7m \
-I/usr/local/lib/python3.7/site-packages/numpy/core/include \
-I/usr/include/eigen3/ \
-lpython3.7m \
-lpthread -lutil -ldl \
-Xlinker -export-dynamic \
-o bin/a.out
echo "Compilation successful"
\ No newline at end of file
#!/bin/bash
export PYTHONIOENCODING="UTF-8"
# compile (call compile script)
bash "${WORKDIR}/scripts/compile.sh"
# run
bin/a.out
\ No newline at end of file
#!/bin/bash
echo "Not implemented"
\ No newline at end of file
#!/bin/bash
# run (call run script)
bash "${WORKDIR}/scripts/run.sh"
\ No newline at end of file
// "testname", "input", "expected output", "points", "execution time limit (ms)"
"zero_row_col", "(any input)", " 0 0 0\n-1 0 -1\n-1 0 -1", "1", "5000",
"swap_left_right_blocks", "(any input)", "0 1 0\n0 0 1\n1 0 0\n0 0 0", "1", "5000",
"tridiagonal", "(any input)", " 2 -1 0 0\n-1 2 -1 0\n 0 -1 2 -1\n 0 0 -1 2", "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