Commit f5afa20c authored by Tandri Gauksson's avatar Tandri Gauksson

GramSchmidt synced with CodeExpert

parent 6c8758b7
# 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
# Problem 2-2: Gram-Schmidt orthonormalization with Eigen
> For the task description of this exercise, please refer to [NCSE19_Problems.pdf](
https://www.sam.math.ethz.ch/~grsam/NCSE19/HOMEWORK/NCSE19_Problems.pdf).
> Open "gramschmidt.hpp" and fill in the missing code in between the delimiters `// START` and `// END` according to the instructions preceded by `// TO DO:`.
> To view plots, click the "Files" button on the toolbar at the bottom of the page.
\ No newline at end of file
# Tests of gramschmidt.hpp
> There are two functions in gramschmidt.hpp and main.cpp has one test for each function.
***
> `gram_schmidt(const MatrixXd & A)`: We test this function on the matrix A defined by
```
1 1 1 1
0 1 1 1
0 0 1 1
0 0 0 1
```
which the Gram-Schmidt process transforms into the identity matrix.
The test is passed if the norm( gram_schmidt(A) - Id ) < 10^{-9}.
***
> `orthogonality_test()`: This function is written by the student. It should create a random matrix A, calculate Q = gram_schmidt(A) and return err = norm(Q^TQ - Id).
The test is passed if err < 10^{-9}.
\ No newline at end of file
////
//// Copyright (C) 2016 SAM (D-MATH) @ ETH Zurich
//// Author(s): lfilippo <filippo.leonardi@sam.math.ethz.ch>
//// Contributors: tille, jgacon, dcasati
//// This file is part of the NumCSE repository.
////
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
/* \brief Performs Gram-Schidt orthonormalization
* Given a matrix $\mathbf{A}$ of linearly independent columns,
* returns the result of a Gram-Schmidt orthonormalization.
* Unstable GS algorithm: output is prone to cancellation issues.
* \param[in] $\mathbf{A}$ Matrix of linearly independent columns
* \return Matrix with ONB of $span(a_1, \cdots, a_n)$ as columns
*/
/* SAM_LISTING_BEGIN_0 */
MatrixXd gram_schmidt(const MatrixXd & A) {
// We create a matrix Q with the same size and data of A
MatrixXd Q(A);
// The first vector just gets normalized
Q.col(0).normalize();
// TO DO: (2-2.b) Implement the gram_schmidt procedure by iterating over all other columns of A.
// START
for(unsigned int j = 1; j < A.cols(); ++j) {
// See eigen documentation for usage of col and leftCols
Q.col(j) -= Q.leftCols(j) * (Q.leftCols(j).transpose() * A.col(j));
// Normalize vector, if possible
// (otherwise it means columns of $\mathbf{A}$ are
// almost linearly dependent)
double eps = std::numeric_limits<double>::denorm_min();
if( Q.col(j).norm() <= eps * A.col(j).norm() ) {
std::cerr << "Gram-Schmidt failed because "
<< "A has (almost) linearly dependent "
<< "columns." << std::endl;
break;
} else {
Q.col(j).normalize();
}
}
// END
return Q;
}
/* SAM_LISTING_END_0 */
/* SAM_LISTING_BEGIN_1 */
double orthogonality_test() {
// Orthonormality test
double err=1;
unsigned int n = 9;
MatrixXd A, Q;
A = MatrixXd::Random(n,n);
// TO DO: (2-2.c) Use gram_schmidt() to compute an orthonormalization of A,
// call it Q, and let err measure "how far Q is from being orthonormal".
// START
Q = gram_schmidt( A );
err = (Q.transpose()*Q - MatrixXd::Identity(n,n)).norm();
// END
return err;
}
/* SAM_LISTING_END_1 */
\ No newline at end of file
////
//// Copyright (C) 2016 SAM (D-MATH) @ ETH Zurich
//// Author(s): lfilippo <filippo.leonardi@sam.math.ethz.ch>
//// Contributors: tille, jgacon, dcasati
//// This file is part of the NumCSE repository.
////
#include <Eigen/Dense>
#include <iostream>
#include "gramschmidt.hpp"
using namespace Eigen;
int main(void) {
double tolerance = 1e-9;
Matrix4d A;
A << 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1;
std::cout << "Is correct for test matrix: "
<< ((gram_schmidt(A) - Matrix4d::Identity()).norm() < tolerance)
<< std::endl;
// Orthonormality test
double err;
std::srand(5);
err = orthogonality_test();
// Error has to be small, but not zero (why?)
std::cout << "Error is: "
<< err
<< std::endl;
std::cout << "Is orthogonal: " << ((err < tolerance) && (err >= 0)) << std::endl;
}
#!/bin/bash
echo "Compiling ..."
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"
echo "Running script ... "
# 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)"
"test matrix", "(any input)", "Is correct for test matrix: 1", "1", "5000",
"orthogonal", "(any input)", "Is orthogonal: 1", "1", "5000",
\ No newline at end of file
This diff is collapsed.
# 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
# Problem 2-2: Gram-Schmidt orthonormalization with Eigen
> For the task description of this exercise, please refer to [NCSE19_Problems.pdf](
https://www.sam.math.ethz.ch/~grsam/NCSE19/HOMEWORK/NCSE19_Problems.pdf).
> Open "gramschmidt.hpp" and fill in the missing code in between the delimiters `// START` and `// END` according to the instructions preceded by `// TO DO:`.
> To view plots, click the "Files" button on the toolbar at the bottom of the page.
\ No newline at end of file
# Tests of gramschmidt.hpp
> There are two functions in gramschmidt.hpp and main.cpp has one test for each function.
***
> `gram_schmidt(const MatrixXd & A)`: We test this function on the matrix A defined by
```
1 1 1 1
0 1 1 1
0 0 1 1
0 0 0 1
```
which the Gram-Schmidt process transforms into the identity matrix.
The test is passed if the norm( gram_schmidt(A) - Id ) < 10^{-9}.
***
> `orthogonality_test()`: This function is written by the student. It should create a random matrix A, calculate Q = gram_schmidt(A) and return err = norm(Q^TQ - Id).
The test is passed if err < 10^{-9}.
\ No newline at end of file
////
//// Copyright (C) 2016 SAM (D-MATH) @ ETH Zurich
//// Author(s): lfilippo <filippo.leonardi@sam.math.ethz.ch>
//// Contributors: tille, jgacon, dcasati
//// This file is part of the NumCSE repository.
////
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
/* \brief Performs Gram-Schidt orthonormalization
* Given a matrix $\mathbf{A}$ of linearly independent columns,
* returns the result of a Gram-Schmidt orthonormalization.
* Unstable GS algorithm: output is prone to cancellation issues.
* \param[in] $\mathbf{A}$ Matrix of linearly independent columns
* \return Matrix with ONB of $span(a_1, \cdots, a_n)$ as columns
*/
/* SAM_LISTING_BEGIN_0 */
MatrixXd gram_schmidt(const MatrixXd & A) {
// We create a matrix Q with the same size and data of A
MatrixXd Q(A);
// The first vector just gets normalized
Q.col(0).normalize();
// TO DO: (2-2.b) Implement the gram_schmidt procedure by iterating over all other columns of A.
// START
// END
return Q;
}
/* SAM_LISTING_END_0 */
/* SAM_LISTING_BEGIN_1 */
double orthogonality_test() {
// Orthonormality test
double err=1;
unsigned int n = 9;
MatrixXd A, Q;
A = MatrixXd::Random(n,n);
// TO DO: (2-2.c) Use gram_schmidt() to compute an orthonormalization of A,
// call it Q, and let err measure "how far Q is from being orthonormal".
// START
// END
return err;
}
/* SAM_LISTING_END_1 */
\ No newline at end of file
////
//// Copyright (C) 2016 SAM (D-MATH) @ ETH Zurich
//// Author(s): lfilippo <filippo.leonardi@sam.math.ethz.ch>
//// Contributors: tille, jgacon, dcasati
//// This file is part of the NumCSE repository.
////
#include <Eigen/Dense>
#include <iostream>
#include "gramschmidt.hpp"
using namespace Eigen;
int main(void) {
double tolerance = 1e-9;
Matrix4d A;
A << 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1;
std::cout << "Is correct for test matrix: "
<< ((gram_schmidt(A) - Matrix4d::Identity()).norm() < tolerance)
<< std::endl;
// Orthonormality test
double err;
std::srand(5);
err = orthogonality_test();
// Error has to be small, but not zero (why?)
std::cout << "Error is: "
<< err
<< std::endl;
std::cout << "Is orthogonal: " << ((err < tolerance) && (err >= 0)) << std::endl;
}
#!/bin/bash
echo "Compiling ..."
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"
echo "Running script ... "
# 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)"
"test matrix", "(any input)", "Is correct for test matrix: 1", "1", "5000",
"orthogonal", "(any input)", "Is orthogonal: 1", "1", "5000",
\ No newline at end of file
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