jet_traits_test.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: sergiu.deitsch@gmail.com (Sergiu Deitsch)
  30. #include "ceres/internal/jet_traits.h"
  31. #include <Eigen/Core>
  32. #include <type_traits>
  33. #include <utility>
  34. namespace ceres::internal {
  35. using J = Jet<double, 2>;
  36. // Don't care about the dual part for scalar part categorization and comparison
  37. // tests
  38. template <typename T>
  39. using J0 = Jet<T, 0>;
  40. using J0d = J0<double>;
  41. // Extract the ranks of given types
  42. using Ranks001 = Ranks_t<Jet<double, 0>, double, Jet<double, 1>>;
  43. using Ranks1 = Ranks_t<Jet<double, 1>>;
  44. using Ranks110 = Ranks_t<Jet<double, 1>, Jet<double, 1>, double>;
  45. using Ranks023 = Ranks_t<double, Jet<double, 2>, Jet<double, 3>>;
  46. using EmptyRanks = Ranks_t<>;
  47. // Ensure extracted ranks match the expected integer sequence
  48. static_assert(
  49. std::is_same<Ranks001, std::integer_sequence<int, 0, 0, 1>>::value,
  50. "ranks do not match");
  51. static_assert(std::is_same<Ranks1, std::integer_sequence<int, 1>>::value,
  52. "ranks do not match");
  53. static_assert(
  54. std::is_same<Ranks110, std::integer_sequence<int, 1, 1, 0>>::value,
  55. "ranks do not match");
  56. static_assert(
  57. std::is_same<Ranks023, std::integer_sequence<int, 0, 2, 3>>::value,
  58. "ranks do not match");
  59. static_assert(std::is_same<EmptyRanks, std::integer_sequence<int>>::value,
  60. "ranks sequence is not empty");
  61. // Extract the underlying floating-point type
  62. static_assert(std::is_same<UnderlyingScalar_t<double>, double>::value,
  63. "underlying type is not a double");
  64. static_assert(std::is_same<UnderlyingScalar_t<J0d>, double>::value,
  65. "underlying type is not a double");
  66. static_assert(std::is_same<UnderlyingScalar_t<J0<J0d>>, double>::value,
  67. "underlying type is not a double");
  68. static_assert(std::is_same<UnderlyingScalar_t<J0<J0<J0d>>>, double>::value,
  69. "underlying type is not a double");
  70. static_assert(CompatibleJetOperands_v<Jet<double, 1>, Jet<double, 1>>,
  71. "Jets must be compatible");
  72. static_assert(CompatibleJetOperands_v<Jet<double, 1>, double>,
  73. "Jet and scalar must be compatible");
  74. static_assert(CompatibleJetOperands_v<Jet<double, 2>>,
  75. "single Jet must be compatible");
  76. static_assert(!CompatibleJetOperands_v<Jet<double, 1>, double, Jet<double, 2>>,
  77. "Jets and scalar must not be compatible");
  78. static_assert(!CompatibleJetOperands_v<double, double>,
  79. "scalars must not be compatible");
  80. static_assert(!CompatibleJetOperands_v<double>,
  81. "single scalar must not be compatible");
  82. static_assert(!CompatibleJetOperands_v<>,
  83. "empty arguments must not be compatible");
  84. static_assert(!PromotableJetOperands_v<double>,
  85. "single scalar must not be Jet promotable");
  86. static_assert(!PromotableJetOperands_v<double, float, int>,
  87. "multiple scalars must not be Jet promotable");
  88. static_assert(PromotableJetOperands_v<J0d, float, int>,
  89. "Jet and several scalars must be promotable");
  90. static_assert(PromotableJetOperands_v<J0<J0d>, float, int>,
  91. "nested Jet and several scalars must be promotable");
  92. static_assert(!PromotableJetOperands_v<Eigen::Array<double, 2, 3>, float, int>,
  93. "Eigen::Array must not be Jet promotable");
  94. static_assert(!PromotableJetOperands_v<Eigen::Matrix<double, 3, 2>, float, int>,
  95. "Eigen::Matrix must not be Jet promotable");
  96. } // namespace ceres::internal