1 #ifndef STAN_MATH_PRIM_FUN_INV_SQUARE_HPP
2 #define STAN_MATH_PRIM_FUN_INV_SQUARE_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/fun/inv.hpp>
6 #include <stan/math/prim/fun/square.hpp>
7 #include <stan/math/prim/functor/apply_vector_unary.hpp>
8 
9 namespace stan {
10 namespace math {
11 
12 /**
13  * Returns `1 / square(x)`.
14  *
15  * @tparam Container type of container
16  * @param x container
17  * @return `1 / square(x)` of each value in x.
18  */
19 template <typename Container,
20           require_not_container_st<std::is_arithmetic, Container>* = nullptr,
21           require_all_not_nonscalar_prim_or_rev_kernel_expression_t<
22               Container>* = nullptr>
inv_square(const Container & x)23 inline auto inv_square(const Container& x) {
24   return inv(square(x));
25 }
26 
27 /**
28  * Version of inv_square() that accepts Eigen Matrix/Array objects or
29  * expressions.
30  *
31  * @tparam T Type of x
32  * @param x Eigen Matrix/Array or expression
33  * @return 1 / the square of each value in x.
34  */
35 template <typename Container,
36           require_container_st<std::is_arithmetic, Container>* = nullptr>
inv_square(const Container & x)37 inline auto inv_square(const Container& x) {
38   return apply_vector_unary<Container>::apply(
39       x, [](const auto& v) { return v.array().square().inverse(); });
40 }
41 
42 }  // namespace math
43 }  // namespace stan
44 
45 #endif
46