Commit b1565919 authored by ralfh's avatar ralfh

Added new demo codes

parent dc24e3fb
......@@ -10,3 +10,7 @@ endif()
set(SRCS cpptutorial.cpp)
add_executable(tutorial ${SRCS})
add_executable(myvector myvector.cpp)
add_executable(lambdatransform lambdatransform.cpp)
add_executable(fnvec fnvec.cpp)
add_executable(tietuple tietuple.cpp)
/***********************************************************************
* *
* Demo code *
* (Prof. Dr. R. Hiptmair) *
* Author: R.H. *
* Date: May 2018 *
* (C) Seminar for Applied Mathematics, ETH Zurich *
* This code can be freely used for non-commercial purposes as long *
* as this header is left intact. *
***********************************************************************/
// Header for basic IO
#include <iostream>
// Provides random acccess container class
#include <vector>
// Provides function types
#include <functional>
// Definition of functions
double binop(double arg1,double arg2) { return (arg1/arg2); }
void stdfunctiontest(void) {
// Vector of objects of a particular signature
std::vector<std::function<double(double,double)>> fnvec;
// Store reference to a regular function
fnvec.push_back(binop);
// Store are lambda function
fnvec.push_back([] (double x,double y) -> double { return y/x; });
for (auto fn : fnvec) { std::cout << fn(3,2) << std::endl; }
}
int main() {
// Call testing function;
stdfunctiontest();
return (0);
}
/***********************************************************************
* *
* Demo code *
* (Prof. Dr. R. Hiptmair) *
* Author: R.H. *
* Date: May 2018 *
* (C) Seminar for Applied Mathematics, ETH Zurich *
* This code can be freely used for non-commercial purposes as long *
* as this header is left intact. *
***********************************************************************/
// Header for basic IO
#include <iostream>
// Provides random acccess container class
#include <vector>
// Provides algorithms operating on generic containers
#include <algorithm>
using namespace std;
int main() {
// initialize a vector from an initializer list
std::vector<double> v({1.2,2.3,3.4,4.5,5.6,6.7,7.8});
// A vector of the same length
std::vector<double> w(v.size());
// Do cumulative summation of v and store result in w
double sum = 0;
std::transform(v.begin(),v.end(),w.begin(),
[&sum] (double x) { sum += x; return sum;});
cout << "sum = " << sum << ", w = [ ";
for(auto x: w) cout << x << ' '; cout << ']' << endl;
return(0);
}
/***********************************************************************
* *
* Demo code *
* (Prof. Dr. R. Hiptmair) *
* Author: R.H. *
* Date: May 2018 *
* (C) Seminar for Applied Mathematics, ETH Zurich *
* This code can be freely used for non-commercial purposes as long *
* as this header is left intact. *
***********************************************************************/
// Header for basic IO
#include <iostream>
// Provides random acccess container class
#include <vector>
// Provides algorithms operating on generic containers
#include <algorithm>
// Miscellaneous utilities
#include <tuple>
using namespace std;
template<typename T>
std::tuple<T,T,std::vector<T>> extcumsum(const std::vector<T> &v) {
// Local summation variable captured by reference by lambda function
T sum{};
// temporary vector for returning cumulative sum
std::vector<T> w{};
// cumulative summation
std::transform(v.cbegin(),v.cend(),back_inserter(w),
[&sum] (T x) { sum += x; return(sum); });
return(std::tuple<T,T,std::vector<T>>
(*std::min_element(v.cbegin(),v.cend()),
*std::max_element(v.cbegin(),v.cend()),w));
}
int main () {
// initialize a vector from an initializer list
std::vector<double> v({1.2,2.3,3.4,4.5,5.6,6.7,7.8});
// Variables for return values
double minv,maxv; // Extremal elements
std::vector<double> cs; // Cumulative sums
std::tie(minv,maxv,cs) = extcumsum(v);
cout << "min = " << minv << ", max = " << maxv << endl;
cout << "cs = [ "; for(double x: cs) cout << x << ' '; cout << "]" << endl;
return(0);
}
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