Dateien nach „BSP_1_D“ hochladen
This commit is contained in:
parent
4c4507811c
commit
dde42b7628
4 changed files with 127 additions and 0 deletions
40
BSP_1_D/bsp_1_d.cbp
Normal file
40
BSP_1_D/bsp_1_d.cbp
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<CodeBlocks_project_file>
|
||||||
|
<FileVersion major="1" minor="6" />
|
||||||
|
<Project>
|
||||||
|
<Option title="bsp_1_d" />
|
||||||
|
<Option pch_mode="2" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Build>
|
||||||
|
<Target title="Debug">
|
||||||
|
<Option output="bin/Debug/bsp_1_d" prefix_auto="1" extension_auto="1" />
|
||||||
|
<Option object_output="obj/Debug/" />
|
||||||
|
<Option type="1" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-g" />
|
||||||
|
</Compiler>
|
||||||
|
</Target>
|
||||||
|
<Target title="Release">
|
||||||
|
<Option output="bin/Release/bsp_1_d" prefix_auto="1" extension_auto="1" />
|
||||||
|
<Option object_output="obj/Release/" />
|
||||||
|
<Option type="1" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-O2" />
|
||||||
|
</Compiler>
|
||||||
|
<Linker>
|
||||||
|
<Add option="-s" />
|
||||||
|
</Linker>
|
||||||
|
</Target>
|
||||||
|
</Build>
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-Wall" />
|
||||||
|
<Add option="-fexceptions" />
|
||||||
|
</Compiler>
|
||||||
|
<Unit filename="bsp_1_d.cpp" />
|
||||||
|
<Unit filename="bsp_1_d.h" />
|
||||||
|
<Unit filename="main.cpp" />
|
||||||
|
<Extensions />
|
||||||
|
</Project>
|
||||||
|
</CodeBlocks_project_file>
|
||||||
34
BSP_1_D/bsp_1_d.cpp
Normal file
34
BSP_1_D/bsp_1_d.cpp
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
#include "bsp_1_d.h"
|
||||||
|
|
||||||
|
#include <cassert> // assert()
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
double scalar(vector<double> const &x, vector<double> const &y)
|
||||||
|
{
|
||||||
|
assert(x.size() == y.size()); // switch off via compile flag: -DNDEBUG
|
||||||
|
size_t const N = x.size();
|
||||||
|
double sum = 0.0;
|
||||||
|
for (size_t i = 0; i < N; ++i)
|
||||||
|
{
|
||||||
|
sum += x[i] * y[i];
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Kahan_skalar(vector<double> const &x, vector<double> const &y)
|
||||||
|
{
|
||||||
|
assert(x.size() == y.size()); // switch off via compile flag: -DNDEBUG
|
||||||
|
size_t const N = x.size();
|
||||||
|
double sum = 0.0;
|
||||||
|
double c = 0.0;
|
||||||
|
for (size_t i = 0; i < N; ++i)
|
||||||
|
{
|
||||||
|
double z = x[i]*y[i] - c;
|
||||||
|
double t = sum + z;
|
||||||
|
c = (t - sum) - z;
|
||||||
|
sum = t;
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
25
BSP_1_D/bsp_1_d.h
Normal file
25
BSP_1_D/bsp_1_d.h
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef BSP_1_D_H_INCLUDED
|
||||||
|
#define BSP_1_D_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
/** \brief Berechnet das Skalarprodukt zweier Vektoren
|
||||||
|
*
|
||||||
|
* \param[in] x erster Vektor
|
||||||
|
* \param[in] y zweiter Vektor
|
||||||
|
* \return Skalarprodukt von @x und @y
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
double scalar(std::vector<double> const &x, std::vector<double> const &y);
|
||||||
|
|
||||||
|
|
||||||
|
/** \brief Berechnet das Skalarprodukt zweier Vektoren unter Anwendung der Kahan-Summation
|
||||||
|
*
|
||||||
|
* \param[in] x erster Vektor
|
||||||
|
* \param[in] y zweiter Vektor
|
||||||
|
* \return Skalarprodukt von @x und @y
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
double Kahan_skalar(std::vector<double> const &x, std::vector<double> const &y);
|
||||||
|
|
||||||
|
#endif // BSP_1_D_H_INCLUDED
|
||||||
28
BSP_1_D/main.cpp
Normal file
28
BSP_1_D/main.cpp
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
#include "bsp_1_d.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <math.h>
|
||||||
|
// BSP 1_D
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
vector<int> nvec{100,500,1000,10000,100000,1000000,10000000,100000000}; // Vektor mit verschiedene n zum Testen
|
||||||
|
double exact = M_PI*M_PI/6;
|
||||||
|
for(size_t k = 0; k<nvec.size(); ++k)
|
||||||
|
{
|
||||||
|
vector<double> x;
|
||||||
|
for(int i = 0; i<nvec[k]; ++i)
|
||||||
|
{
|
||||||
|
x.push_back(1.0/(i+1));
|
||||||
|
}
|
||||||
|
double sum = scalar(x,x);
|
||||||
|
double sumkahan = Kahan_skalar(x,x);
|
||||||
|
|
||||||
|
cout << "n = " << nvec[k] << endl;
|
||||||
|
cout << "sum = " << sum << endl;
|
||||||
|
cout << "sumkahan = " << sumkahan << endl;
|
||||||
|
cout << "Differenz mit/ohne Kahan: " << abs(sum - sumkahan) << endl;
|
||||||
|
cout << "Differenz zu exakt fuer n to inf (mit/ohne Kahan): " << abs(exact - sum) << " / " << abs(exact - sumkahan) << endl << endl;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue