Advertisement
wowonline

Untitled

Apr 9th, 2022
1,173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <array>
  2. #include <complex>
  3. #include <utility>
  4. #include <vector>
  5. #include <math.h>
  6. #include <limits>
  7. #include <iostream>
  8.  
  9.  
  10. namespace Equations {
  11.  
  12. template<typename t>
  13. std::pair<bool, std::vector<std::complex<t> > > quadratic(std::array< std::complex<t>, 3> arr)
  14. {  
  15.  
  16.     std::pair<bool, std::vector<std::complex<t> > > p;
  17.     if (std::norm(arr[2]) < std::numeric_limits<t>::epsilon() &&
  18.         std::norm(arr[1]) < std::numeric_limits<t>::epsilon() &&
  19.         std::norm(arr[0]) < std::numeric_limits<t>::epsilon()) {
  20.         std::vector<std::complex<t> > v;
  21.         p.second = v;
  22.         p.first = false;
  23.     } else if (std::norm(arr[2]) < std::numeric_limits<t>::epsilon() &&
  24.                 std::norm(arr[1]) < std::numeric_limits<t>::epsilon() &&
  25.                 std::norm(arr[0]) >= std::numeric_limits<t>::epsilon()) {
  26.         std::vector<std::complex<t> > v;
  27.         p.second = v;
  28.         p.first = true;
  29.     } else if (std::norm(arr[2]) < std::numeric_limits<t>::epsilon() &&
  30.                 std::norm(arr[1]) > std::numeric_limits<t>::epsilon()) {
  31.         std::vector<std::complex<t> > v = {-arr[0]/arr[1]};
  32.         p.first = true;
  33.         p.second = v;
  34.     } else {
  35.         std::vector<std::complex<t> > v;
  36.         std::complex<t> d = arr[1] * arr[1] - std::complex<t>(4) * arr[2] * arr[0];
  37.         v.push_back((-arr[1] - std::sqrt(d))/(std::complex<t>(2) * arr[2]));
  38.         v.push_back((-arr[1] + std::sqrt(d))/(std::complex<t>(2) * arr[2]));
  39.  
  40.         p.first = true;
  41.         p.second = v;
  42.     }
  43.  
  44.     return p;
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement