Skip to content

Commit 4142aa4

Browse files
committed
sphere area
1 parent 0460b35 commit 4142aa4

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

src/geometry/shapes/sphere_area.h

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,71 +70,51 @@ template <unsigned N, typename T>
7070
T sphere_relative_area(const std::type_identity_t<T> a, const std::type_identity_t<T> b)
7171
{
7272
static_assert(std::is_floating_point_v<T>);
73+
static_assert(N >= 2);
7374

7475
// Assuming[Element[n,Integers]&&n>=0,Integrate[Sin[x]^n,x]]
7576
// -Cos[x] Hypergeometric2F1[1/2,(1-n)/2,3/2,Cos[x]^2] Sin[x]^(1+n) (Sin[x]^2)^(1/2 (-1-n))
7677
// For[i=2,i<=10,++i,f=Integrate[Sin[x]^(i-2),{x, a, b}];Print[i];Print[f]]
7778
// For[i=2,i<=10,++i,f=Simplify[Integrate[Sin[x]^(i-2),x]];Print[i];Print[f]]
7879

79-
if constexpr (N == 2)
80+
switch (N)
8081
{
82+
case 2:
8183
return b - a;
82-
}
83-
else if constexpr (N == 3)
84-
{
84+
case 3:
8585
return std::cos(a) - std::cos(b);
86-
}
87-
else if constexpr (N == 4)
88-
{
86+
case 4:
8987
return (2 * b - 2 * a - std::sin(2 * b) + std::sin(2 * a)) / 4;
90-
}
91-
else if constexpr (N == 5)
92-
{
88+
case 5:
9389
return (9 * std::cos(a) - std::cos(3 * a) - 9 * std::cos(b) + std::cos(3 * b)) / 12;
94-
}
95-
else if constexpr (N == 6)
96-
{
90+
case 6:
9791
return (-12 * a + 12 * b + 8 * std::sin(2 * a) - std::sin(4 * a) - 8 * std::sin(2 * b)
9892
+ std::sin(4 * b))
9993
/ 32;
100-
}
101-
else if constexpr (N == 7)
102-
{
94+
case 7:
10395
return (150 * std::cos(a) - 25 * std::cos(3 * a) + 3 * std::cos(5 * a) - 150 * std::cos(b)
10496
+ 25 * std::cos(3 * b) - 3 * std::cos(5 * b))
10597
/ 240;
106-
}
107-
else if constexpr (N == 8)
108-
{
98+
case 8:
10999
return (-60 * a + 60 * b + 45 * std::sin(2 * a) - 9 * std::sin(4 * a) + std::sin(6 * a)
110100
- 45 * std::sin(2 * b) + 9 * std::sin(4 * b) - std::sin(6 * b))
111101
/ 192;
112-
}
113-
else if constexpr (N == 9)
114-
{
102+
case 9:
115103
return (1225 * std::cos(a) - 245 * std::cos(3 * a) + 49 * std::cos(5 * a) - 5 * std::cos(7 * a)
116104
- 1225 * std::cos(b) + 245 * std::cos(3 * b) - 49 * std::cos(5 * b) + 5 * std::cos(7 * b))
117105
/ 2240;
118-
}
119-
else if constexpr (N == 10)
120-
{
106+
case 10:
121107
return (-840 * a + 840 * b + 672 * std::sin(2 * a) - 168 * std::sin(4 * a) + 32 * std::sin(6 * a)
122108
- 3 * std::sin(8 * a) - 672 * std::sin(2 * b) + 168 * std::sin(4 * b) - 32 * std::sin(6 * b)
123109
+ 3 * std::sin(8 * b))
124110
/ 3072;
125-
}
126-
else if constexpr (N >= 11)
127-
{
111+
default:
128112
return ns::numerical::integrate<T>(
129113
[](const T x)
130114
{
131115
return std::pow(std::sin(x), static_cast<T>(N - 2));
132116
},
133117
a, b, /*count=*/100);
134118
}
135-
else
136-
{
137-
static_assert(false);
138-
}
139119
}
140120
}

0 commit comments

Comments
 (0)