Dateien nach „BSP_1_G“ hochladen

This commit is contained in:
Georg Thomas Mandl 2025-10-23 00:49:21 +02:00
commit 174e3841cd
4 changed files with 242 additions and 0 deletions

48
BSP_1_G/bsp_1_g.cbp Normal file
View file

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="bsp_1_g" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/bsp_1_g" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
<Add directory="./" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/bsp_1_g" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
<Add directory="./" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="main.cpp" />
<Unit filename="matrixdense.cpp" />
<Unit filename="matrixdense.h" />
<Unit filename="matrixprod.cpp" />
<Unit filename="matrixprod.h" />
<Unit filename="sigmoid.cpp" />
<Unit filename="sigmoid.h" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

109
BSP_1_G/main.cpp Normal file
View file

@ -0,0 +1,109 @@
#include "matrixdense.h"
#include "matrixprod.h"
#include "sigmoid.h"
#include <iostream>
#include <chrono>
#include <algorithm>
// BSP 1_G
using namespace std;
int main()
{
// ---- b ----
MatrixDense const M(5,3);
// Dense matrix, also initialized
vector<double> const u{1,2,3};
vector<double> f1 = M.Mult(u);
cout << "M*u = ";
for(size_t k=0; k<f1.size(); ++k)
{
cout << f1[k] << " ";
}
cout << endl;
vector<double> const v{-1,2,-3,4,-5};
vector<double> f2 = M.MultT(v);
cout << "M^t*v = ";
for(size_t k=0; k<f2.size(); ++k)
{
cout << f2[k] << " ";
}
cout << endl;
// ---- c -----
cout << "C" << endl;
int nloops = 100;
int n = 1000;
MatrixDense const Mc(n,n);
vector<double> x(n,2.0);
vector<double> fa, fb;
auto time1start = std::chrono::system_clock::now();
for(int k=0; k<nloops; ++k)
{
fa = Mc.Mult(x);
}
auto time1end = std::chrono::system_clock::now();
auto time1 = std::chrono::duration_cast<std::chrono::milliseconds>(time1end - time1start);
auto time2start = std::chrono::system_clock::now();
for(int k=0; k<nloops; ++k)
{
fb = Mc.MultT(x);
}
auto time2end = std::chrono::system_clock::now();
auto time2 = std::chrono::duration_cast<std::chrono::milliseconds>(time2end - time2start);
cout << "Zeit fuer Mult pro loop " << time1.count() << " ms" << endl;
cout << "Zeit fuer MultT pro loop " << time2.count() << " ms" << endl;
// Check ob beide Ergebnisse gleich sind
vector<double> err(fa.size());
for(size_t k = 0; k<fa.size(); ++k)
{
err[k] = abs(fa[k] - fb[k]);
}
double maxerr = *max_element(err.begin(), err.end());
cout << "maximaler Fehler in Komponente " << maxerr << endl;
// ----- d -----
cout << "D" << endl;
vector<double> u1(n,0);
for(size_t k=0; k<u1.size(); ++k)
{
u1[k] = sigmoid(xk(k,u1.size()));
}
MatrixProd const Mb(u1,u1);
vector<double> y(n,2.0);
vector<double> fc, fd;
auto time3start = std::chrono::system_clock::now();
for(int k=0; k<nloops; ++k)
{
fc = Mb.Mult(y);
}
auto time3end = std::chrono::system_clock::now();
auto time3 = std::chrono::duration_cast<std::chrono::milliseconds>(time3end - time3start);
auto time4start = std::chrono::system_clock::now();
for(int k=0; k<nloops; ++k)
{
fd = Mb.MultT(y);
}
auto time4end = std::chrono::system_clock::now();
auto time4 = std::chrono::duration_cast<std::chrono::milliseconds>(time4end - time4start);
cout << "Zeit fuer Mult pro loop " << time3.count() << " ms" << endl;
cout << "Zeit fuer MultT pro loop " << time4.count() << " ms" << endl;
// Check ob beide Ergebnisse gleich sind
vector<double> err1(fc.size());
for(size_t k = 0; k<fc.size(); ++k)
{
err1[k] = abs(fc[k] - fd[k]);
}
double maxerr1 = *max_element(err1.begin(), err1.end());
cout << "maximaler Fehler in Komponente " << maxerr1 << endl;
return 0;
}

53
BSP_1_G/matrixdense.cpp Normal file
View file

@ -0,0 +1,53 @@
#include "matrixdense.h"
#include "sigmoid.h"
MatrixDense::MatrixDense()
{
//ctor
}
MatrixDense::MatrixDense(int n, int m)
{
nrow = n;
mcol = m;
mat = vector<double>(n*m);
int nm = max(n,m);
for(int i = 0; i<n; ++i)
{
for(int j = 0; j<m; ++j)
{
mat[i*m+j] = sigmoid(xk(i,nm))*sigmoid(xk(j,nm));
}
}
}
vector<double> MatrixDense::Mult(const vector<double> &x) const
{
vector<double> y(nrow,0);
for(int n=0; n<nrow; ++n)
{
for(int m=0; m<mcol; ++m)
{
y[n] += mat[n*mcol+m]*x[m];
}
}
return y;
}
vector<double> MatrixDense::MultT(const vector<double> &x) const
{
vector<double> y(mcol,0);
for(int m=0; m<mcol; ++m) // over columns
{
for(int n=0; n<nrow; ++n) // over rows
{
y[m] += mat[n*mcol+m]*x[n];
}
}
return y;
}
MatrixDense::~MatrixDense()
{
//dtor
}

32
BSP_1_G/matrixdense.h Normal file
View file

@ -0,0 +1,32 @@
#ifndef MATRIXDENSE_H
#define MATRIXDENSE_H
#include <vector>
using namespace std;
class MatrixDense
{
public:
/** Default constructor */
MatrixDense();
/** Constructor */
MatrixDense(int n, int m);
vector<double> Mult(const vector<double> &x) const;
vector<double> MultT(const vector<double> &x) const;
/** Default destructor */
virtual ~MatrixDense();
protected:
private:
vector<double> mat; //!< Member variable "m"
int nrow; //!< Member variable "nrow"
int mcol; //!< Member variable "mcol"
};
#endif // MATRIXDENSE_H