Upgrade to Pro — share decks privately, control downloads, hide ads and more …

introduction to modern numerical analysis

N@N
December 19, 2015

introduction to modern numerical analysis

数物セミナー冬の大談話会2015 in 大阪での発表資料

N@N

December 19, 2015
Tweet

More Decks by N@N

Other Decks in Technology

Transcript

  1. ! #include <iostream> int fact(int n) { if(n == 0)

    { return 1; } return n*fact(n-1); } int main() { std::cout<<fact(10); } fact(10) -> 10 * fact(9) -> 10 * 9 * fact(8) -> 10 * ... * 1 * 1 -> 10! = 3628800
  2. ! #include <iostream> int fact(int n) { if(n == 0)

    { return 1; } return n*fact(n-1); } int main() { std::cout<<fact(10); } fact 0 = 1 fact n = n*fact(n-1) main = do print $ fact 10
  3. ! int fact(int n) { if(n == 0) { return

    1; } else if(n < 0) { return -1; } return n*fact(n-1); } fact 0 = 1 fact n | n > 0 = n*fact(n-1)
  4. ( ∈ ℤ) ∈ ℤ≥0 ; 1 ≤ ≤ =

    + +1 + ⋯ + +−1 1 ≤ ≤ − + 1
  5. 0 = 0 + 1 + ⋯ + −1 1

    = 1 + 2 + ⋯ + ⋮ = + + ⋯ + + ⋮ −+1 = −+1 + −+2 + ⋯ + • × − + 1
  6. 0 = 0 + 1 + ⋯ + −1 1

    = 1 + 2 + ⋯ + = 0 − 0 + ⋮ = −1 − −1 + ⋮ = −1 − −1 + • 2 − •
  7. op binary decimal 1 0000 0001 1 1 << 6

    0100 0000 64 78 0100 1110 78 78 << 5 1100 0000 192
  8. #include <iostream> #define N 1 << 8 // 1<<8 is

    256. int main() { std::cout << N * 2; } 1. 512 (256 * 2) 2. 65536 (1<<16) 3.
  9. #include <iostream> #define N 1 << 8 // 1<<8 is

    256. int main() { std::cout << N * 2; } // -> 116
  10. #include <iostream> #define N 1 << 8 int main() {

    std::cout << N*2; } // -> 116 #include <iostream> int main() { std::cout<<1<<8*2; } // -> 116
  11. #include <iostream> #define N 1 << 8 int main() {

    std::cout << N*2; } // -> 116 #include <iostream> int main() { std::cout<<1<<8*2; } // -> 116 #define
  12. #include <iostream> int main() { constexpr int N = 1

    << 8; std::cout << N * 2; } // -> 512
  13. auto x = 1; // x : int auto x

    = 1.3; // x : double auto x; // error!
  14. #include <iostream> #include <vector> int main() { std::vector<int> vec =

    {0, 1, 2}; for(auto const& v : vec) { std::cout << v << " "; } } // 0 1 2
  15. #include <iostream> int main() { auto add = [](int x,

    double y) {return x + y;}; std::cout << add(3, 2.2); }
  16. #include <iostream> int main() { auto add = [](auto x,

    auto y) { return x + y; }; std::cout << add(3, 2.2); }
  17. #include <iostream> void func(int x) { x++; } int main()

    { int a = 42; func(a); std::cout << a << std::endl; // =>42 }
  18. #include <iostream> void func(int &x) { x++; } int main()

    { int a = 42; func(a); std::cout << a << std::endl; // =>43 }
  19. #include <iostream> void func(const int &x) { x++; // error!

    read-only! } int main() { int a = 42; func(a); std::cout << a << std::endl; }
  20. • + −1 −1 −1 + ⋯ + 0 =

    • + −1 −1 −1 + ⋯ + 0 = 0 ,
  21. ≔ lim Δ→0 + Δ − Δ Δ , ≈

    + Δ − Δ + Δ = + , Δ Δ = 0 + 0 , Δ = 0 2Δ = Δ + Δ , Δ Δ, ⋯
  22. +1 = + Δ 6 ,1 + 2,2 + 2,3

    + ,4 , ∈ ℕ 0 = 0 ,1 = , ,2 = + 1 2 Δ, + 1 2 Δ,1 ,3 = + 1 2 Δ, + 1 2 Δ,2 ,4 = + Δ, + Δ,3
  23. void ode(const T &x, T &dxdt, const double t) {

    dxdt = ~~~~; } void output(const T &x, const double t) { std::cout << ~~~~; } int main() { T x; boost::numeric::odeint::integrate( ode, x, 0.0, 10.0, 0.1, output); } ∶ 0 → 10, Δ = 0.1
  24. = − = − − = − dxdt[0] = sigma

    * ( x[1] - x[0] ); dxdt[1] = x[0] * (rho - x[2]) - x[1]; dxdt[2] = x[0] * x[1] - beta * x[2];
  25. typedef array<double, 3> T; constexpr double sigma = 10.0; constexpr

    double rho = 28.0; constexpr double beta = 8.0 / 3.0;
  26. void ode(const T &x, T &dxdt, const double t) {

    dxdt[0] = sigma * (x[1] - x[0]); dxdt[1] = x[0] * (rho - x[2]) - x[1]; dxdt[2] = x[0] * x[1] - beta * x[2]; } void output(const T &x, const double t) { cout << t << '¥t' << x[0] << '¥t' << x[1] << '¥t' << x[2] << endl; } int main() { T x0 = {10.0, 1.0, 1.0}; // initial condition integrate(ode, x0, 0.0, 25.0, 0.1, output); }
  27. typedef complex<double> T; constexpr complex<double> i(0.0, 1.0); void ode(const T

    &x, T &dxdt, const double t) { dxdt=(1.0+eta*i)*x+(1.0+alpha*i)*norm(x)*x; } int main() { T x(5.3, -2.0); }
  28. + −1 −1 −1 + ⋯ + 0 = 0

    • 1 1 = , 2 = , ⋯ , = −1 −1 1 = 2 , 2 = 3 , ⋯ , −1 = = − −1 + −2 −1 + ⋯ + 0 1
  29. 2 2 + + = N ⋅ s2 m ,

    N ⋅ s m , N m
  30. • m + m = + m • m ×

    m = × m2 • m + kg → • Ω = Ω ⋅ m × m m2 • •
  31. quantity<si::length, int> len; len = 1 * si::meters; // default

    : double quantity<si::length> len2(5.0*si::meter); quantity<si::mass> mass; mass = 5.0 * si::kilogram;
  32. quantity<si::length> len(10*si::meter); cout << len; // 10 m s^-1 cout

    << len.value(); // 10 cout << len; // 10 m s^-1