
My solution to the ultimate shower-related relationship problem:That sounds like a variation of a bad pickup line. “We’ll accelerate our relationship exponentially if we take a shower together!”
Alan is also working on this. To recall, it has all boiled down toIt is a tea-table talk. You're in the best position to find such alternative solutions. I don't even understand what you're doing.
That's what they all say. I suppose I'll just have to take your word for it.If you post a nice cat photo I'll tell you how to do this faster and better.
// TestComplexStep.cpp
//
// Complex-step method to compute approximate derivatives.
// Example is scalar-valued function of a scalar argument.
//
// https://pdfs.semanticscholar.org/3de7/e8ae217a4214507b9abdac66503f057aaae9.pdf
//
// http://mdolab.engin.umich.edu/sites/default/files/Martins2003CSD.pdf
//
// (C) Datasim Education BV 2019-2020
// dduffy@datasim.nl
//
#include <functional>
#include <complex>
#include <iostream>
#include <iomanip>
#include <cmath>
// Notation and function spaces
using value_type = double;
using cvalue_type = std::complex< value_type>;
template <typename T>
using FunctionType = std::function < T(const T& c)>;
using CFunctionType = FunctionType<std::complex<value_type>>;
// Test case from Squire&Trapp 1998
template <typename T> T SquireTrapp(const T& t)
{
T n1 = std::exp(t);
T d1 = std::sin(t);
T d2 = std::cos(t);
return n1 / (d1*d1*d1 + d2*d2*d2);
}
template <typename T> T func2(const T& t)
{ // Derivative of e^t, sanity check
return std::exp(std::pow(t,1));
// return std::exp(std::pow(t, 5));
}
value_type Derivative(const CFunctionType& f, value_type x, value_type h)
{ // df/dx at x using tbe Complex step method
// std::complex<value_type> z(x, h); // x + ih, i = sqrt(-1)
return std::imag(f(cvalue_type(x,h))) / h;
}
int main()
{
// Squire Trapp
double x = 1.5; double h = 0.1;
do
{
std::cout << std::setprecision(12) << Derivative(SquireTrapp<cvalue_type>, x, h) << '\n';
h *= 0.1;
} while (h > 1.0e-300);
// Exponential function (101 sanity check)
x = 5.0;
h = 1.0e-10;
std::cout << "Exponential 1: " << std::setprecision(12) << Derivative(func2<cvalue_type>, x, h) << '\n';
return 0;
}
Dude, do we need to start a flame over the complex step method for the THIRD time?CSM is a nice method indeed. I'm wondering about the computational efficiency of complex arithmetic operations, though.
Nice? It's (very) robust and accurate.
Efficiency is a non-issue; take an example to compare the number of function calls and operations
(f(x+h) - f(x-h))/2h
versus
Imaginary part of f(x + ih)/h
But all the functions will be complexified; can be easy (polynomials) or not. It becomes very easy to be drawn into functions of (several) complex variables.
// CSM is a well-kept secret.
Obviously, this is not true, and surprising to come in the discussion on differentiating holomorphic functions.But all the functions will be complexified; can be easy (polynomials) or not. It becomes very easy to be drawn into functions of (several) complex variables.