Solutions

This commit is contained in:
Markus Schmidt 2025-10-21 19:36:38 +02:00
commit d3aa42a3e0
64 changed files with 2726 additions and 0 deletions

6
sheet1/G/.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,6 @@
{
"files.associations": {
"vector": "cpp",
"ostream": "cpp"
}
}

30
sheet1/G/Makefile Normal file
View file

@ -0,0 +1,30 @@
#
# use GNU-Compiler tools
COMPILER=GCC_
# alternatively from the shell
# export COMPILER=GCC_
# or, alternatively from the shell
# make COMPILER=GCC_
# use Intel compilers
#COMPILER=ICC_
# use PGI compilers
# COMPILER=PGI_
SOURCES = main.cpp mylib.cpp
OBJECTS = $(SOURCES:.cpp=.o)
PROGRAM = main.${COMPILER}
# uncomment the next to lines for debugging and detailed performance analysis
CXXFLAGS += -g
LINKFLAGS += -g
# do not use -pg with PGI compilers
ifndef COMPILER
COMPILER=GCC_
endif
include ../${COMPILER}default.mk

View file

BIN
sheet1/G/main.GCC_ Executable file

Binary file not shown.

109
sheet1/G/main.cpp Normal file
View file

@ -0,0 +1,109 @@
#include "mylib.h"
#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
int main()
{
//b)
DenseMatrix const M(5,3); // Dense matrix, also initialized
vector<double> const u{{1,2,3}};
vector<double> f1 = M.Mult(u);
vector<double> const v{{-1,2,-3,4,-5}};
vector<double> f2 = M.MultT(v);
for(unsigned int i = 0; i < f1.size(); i++)
{
cout << f1.at(i) << " ";
}
cout << endl;
for(unsigned int i = 0; i < f2.size(); i++)
{
cout << f2.at(i) << " ";
}
cout << endl;
//c)
int n=5000;
DenseMatrix const A(n,n);
vector<double> w = {};
for(int i = -n/2; i < n/2; i++)
{
w.push_back(i);
}
int const NLOOPS=100;
double t1 = clock(); // start timer
vector<double> f3 = A.Mult(w);
for (int k=1; k<NLOOPS; ++k)
{
f3 = A.Mult(w);
}
t1 = (clock()-t1)/CLOCKS_PER_SEC/NLOOPS;
cout << "Time for Mult per it: " << t1 << endl;
double t2 = clock(); // start timer
vector<double> f4 = A.MultT(w);
for (int k=1; k<NLOOPS; ++k)
{
f4 = A.MultT(w);
}
t2 = (clock()-t2)/CLOCKS_PER_SEC/NLOOPS;
cout << "Time for MultT per it: " << t2 << endl;
//slower because consecutive calls to data vector of Matrix are more apart (+m) due to column wise access (non contiguous memory)
double maxDiff = 0.0;
for (size_t i = 0; i < f3.size(); ++i) {
double diff = abs(f3[i] - f4[i]);
if (diff > maxDiff) {
maxDiff = diff;
}
}
cout << "difference f3 and f4: " << maxDiff << endl;
//d)
cout << "-----------------DYADIC------------------" <<endl;
vector<double> x = {};
for(int k=0; k < n; k++)
{
x.push_back(f(k, n));
}
Dyadic const D(x,x);
double t3 = clock(); // start timer
vector<double> f5 = D.Mult(w);
for (int k=1; k<NLOOPS; ++k)
{
f5 = D.Mult(w);
}
t3 = (clock()-t3)/CLOCKS_PER_SEC/NLOOPS;
cout << "Time for Mult per it: " << t3 << endl;
double t4 = clock(); // start timer
vector<double> f6 = D.MultT(w);
for (int k=1; k<NLOOPS; ++k)
{
f6 = D.MultT(w);
}
t4 = (clock()-t4)/CLOCKS_PER_SEC/NLOOPS;
cout << "Time for MultT per it: " << t4 << endl;
//slower because consecutive calls to data vector of Matrix are more apart (+m) due to column wise access (non contiguous memory)
double maxDiff2 = 0.0;
for (size_t i = 0; i < f5.size(); ++i) {
double diff = abs(f5[i] - f6[i]);
if (diff > maxDiff2) {
maxDiff2 = diff;
}
}
cout << "difference f5 and f6: " << maxDiff2 << endl;
return 0;
}

BIN
sheet1/G/main.o Normal file

Binary file not shown.

109
sheet1/G/mylib.cpp Normal file
View file

@ -0,0 +1,109 @@
#include "mylib.h"
#include <cmath>
#include <iostream>
using namespace std;
double f(unsigned int k, unsigned int nm) {
return 1.0 / (1.0 + exp(-(10.0*k/(nm-1)-5)));
}
DenseMatrix::DenseMatrix(unsigned int n, unsigned int m): n_(n), m_(m), data_(n*m)
{
unsigned int nm = max(n,m);
for(unsigned int rowIt = 0; rowIt < n_; rowIt++)
{
for(unsigned int colIt=0; colIt <m_; colIt++)
{
data_.at(rowIt*m+colIt) = (f(rowIt,nm)*f(colIt,nm));
}
}
}
vector<double> DenseMatrix::Mult(const vector<double> &u) const{
if(u.size() != m_)
{
cout << "Dimension mismatch: expected " << m_ << " but got " << u.size() << "!" << endl;
return {};
}
vector<double> f1 = {};
double sum;
for(unsigned int rowIt = 0; rowIt < n_; rowIt++)
{
sum = 0;
for(unsigned int colIt=0; colIt <m_; colIt++)
{
sum += data_.at(rowIt*m_+colIt) * u.at(colIt);
}
f1.push_back(sum);
}
return f1;
}
vector<double> DenseMatrix::MultT(const vector<double> &v) const{
if(v.size() != n_)
{
cout << "Dimension mismatch: expected " << n_ << " but got " << v.size() << "!" << endl;
return {};
}
vector<double> f2 = {};
double sum;
for(unsigned int colIt = 0; colIt < m_; colIt++)
{
sum = 0;
for(unsigned int rowIt=0; rowIt <n_; rowIt++)
{
sum += data_.at(rowIt*m_+colIt) * v.at(rowIt);
}
f2.push_back(sum);
}
return f2;
}
//-----------------------------------------------------------------------------------------------
Dyadic::Dyadic(vector<double>& u, vector<double>& v):u_(u), v_(v){
}
vector<double> Dyadic::Mult(const vector<double> &w) const{
unsigned int m = v_.size();
if(w.size() != m)
{
cout << "Dimension mismatch!" << endl;
return {};
}
vector<double> f1 = {};
double skalar = 0;
for(unsigned int vIt = 0; vIt < m; vIt++)
{
skalar += v_.at(vIt)*w.at(vIt);
}
for(unsigned int uIt=0; uIt < u_.size(); uIt++)
{
f1.push_back(u_.at(uIt)*skalar);
}
return f1;
}
vector<double> Dyadic::MultT(const vector<double> &w) const{
unsigned int n = u_.size();
if(w.size() != n)
{
cout << "Dimension mismatch!" << endl;
return {};
}
vector<double> f1 = {};
double skalar = 0;
for(unsigned int uIt = 0; uIt < n; uIt++)
{
skalar += u_.at(uIt)*w.at(uIt);
}
for(unsigned int vIt=0; vIt < v_.size(); vIt++)
{
f1.push_back(v_.at(vIt)*skalar);
}
return f1;
}

36
sheet1/G/mylib.h Normal file
View file

@ -0,0 +1,36 @@
#ifndef MYLIB_H_INCLUDED
#define MYLIB_H_INCLUDED
#include <vector>
using namespace std;
double f(unsigned int k, unsigned int nm);
class DenseMatrix{
public:
DenseMatrix(unsigned int n, unsigned int m);
vector<double> Mult(const vector<double> &u) const;
vector<double> MultT(const vector<double> &v) const;
private:
unsigned int n_,m_;
vector<double> data_;
};
class Dyadic{
public:
Dyadic(vector<double>& u, vector<double>& v);
vector<double> Mult(const vector<double> &u) const;
vector<double> MultT(const vector<double> &v) const;
private:
vector<double> u_;
vector<double> v_;
};
#endif // MYLIB_H_INCLUDED

BIN
sheet1/G/mylib.o Normal file

Binary file not shown.