Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pugs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
code
pugs
Commits
44bc6f27
Commit
44bc6f27
authored
4 years ago
by
Emmanuel Labourasse
Browse files
Options
Downloads
Patches
Plain Diff
Implement a template loop for compose
parent
7ffaf169
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/analysis/Polynomial.hpp
+46
-2
46 additions, 2 deletions
src/analysis/Polynomial.hpp
tests/test_Polynomial.cpp
+7
-7
7 additions, 7 deletions
tests/test_Polynomial.cpp
with
53 additions
and
9 deletions
src/analysis/Polynomial.hpp
+
46
−
2
View file @
44bc6f27
...
@@ -199,6 +199,36 @@ class Polynomial
...
@@ -199,6 +199,36 @@ class Polynomial
}
}
return
P
;
return
P
;
}
}
template
<
size_t
M
,
size_t
I
>
PUGS_INLINE
constexpr
Polynomial
<
M
*
N
>
power
(
const
Polynomial
<
M
>&
Q
)
const
{
return
coefficients
()[
I
]
*
Q
.
template
pow
<
N
>(
I
);
}
template
<
size_t
M
,
size_t
...
I
>
PUGS_INLINE
constexpr
Polynomial
<
M
*
N
>
compose2
(
const
Polynomial
<
M
>&
Q
,
std
::
index_sequence
<
I
...
>
)
const
{
Polynomial
<
M
*
N
>
P
;
P
.
coefficients
()
=
zero
;
P
=
(
power
<
M
,
I
>
(
Q
)
+
...);
return
P
;
}
template
<
size_t
M
>
PUGS_INLINE
constexpr
Polynomial
<
M
*
N
>
compose2
(
const
Polynomial
<
M
>&
Q
)
const
{
Polynomial
<
M
*
N
>
P
;
P
.
coefficients
()
=
zero
;
using
IndexSequence
=
std
::
make_index_sequence
<
N
+
1
>
;
return
compose2
<
M
>
(
Q
,
IndexSequence
{});
// for (size_t i = 0; i <= N; ++i) {
// P += Q.template pow<N>(i) * coefficients()[i];
// }
return
P
;
}
template
<
size_t
M
>
template
<
size_t
M
>
PUGS_INLINE
constexpr
Polynomial
<
M
*
N
>
PUGS_INLINE
constexpr
Polynomial
<
M
*
N
>
...
@@ -408,8 +438,8 @@ class Polynomial
...
@@ -408,8 +438,8 @@ class Polynomial
}
}
PUGS_INLINE
PUGS_INLINE
constexpr
Polynomial
<
N
>
constexpr
friend
Polynomial
<
N
>
lagrangeToCanonical
(
const
TinyVector
<
N
+
1
>
lagrange_coefs
,
TinyVector
<
N
+
1
,
Polynomial
<
N
>>&
basis
)
lagrangeToCanonical
(
const
TinyVector
<
N
+
1
>
lagrange_coefs
,
const
TinyVector
<
N
+
1
,
Polynomial
<
N
>>&
basis
)
{
{
Polynomial
<
N
>
P
(
zero
);
Polynomial
<
N
>
P
(
zero
);
// lagrangeBasis({0, 0, 0}, basis);
// lagrangeBasis({0, 0, 0}, basis);
...
@@ -428,4 +458,18 @@ class Polynomial
...
@@ -428,4 +458,18 @@ class Polynomial
constexpr
Polynomial
()
noexcept
=
default
;
constexpr
Polynomial
()
noexcept
=
default
;
~
Polynomial
()
=
default
;
~
Polynomial
()
=
default
;
};
};
template
<
size_t
N
>
PUGS_INLINE
constexpr
TinyVector
<
N
,
Polynomial
<
N
-
1
>>
lagrangeBasis
(
const
TinyVector
<
N
>&
zeros
)
{
static_assert
(
N
>=
1
,
"invalid degree"
);
TinyVector
<
N
,
Polynomial
<
N
-
1
>>
basis
;
Polynomial
<
N
-
1
>
lj
;
for
(
size_t
j
=
0
;
j
<
N
;
++
j
)
{
lagrangePolynomial
(
zeros
,
j
,
basis
[
j
]);
}
return
basis
;
}
#endif // POLYNOMIAL_HPP
#endif // POLYNOMIAL_HPP
This diff is collapsed.
Click to expand it.
tests/test_Polynomial.cpp
+
7
−
7
View file @
44bc6f27
...
@@ -174,6 +174,7 @@ TEST_CASE("Polynomial", "[analysis]")
...
@@ -174,6 +174,7 @@ TEST_CASE("Polynomial", "[analysis]")
Polynomial
<
2
>
R
({
2
,
-
1
,
3
});
Polynomial
<
2
>
R
({
2
,
-
1
,
3
});
Polynomial
<
2
>
S
({
1
,
2
,
2
});
Polynomial
<
2
>
S
({
1
,
2
,
2
});
REQUIRE
(
P
.
compose
(
Q
)
==
Polynomial
<
2
>
({
2
,
-
6
,
12
}));
REQUIRE
(
P
.
compose
(
Q
)
==
Polynomial
<
2
>
({
2
,
-
6
,
12
}));
REQUIRE
(
P
.
compose2
(
Q
)
==
Polynomial
<
2
>
({
2
,
-
6
,
12
}));
REQUIRE
(
R
(
S
)
==
Polynomial
<
4
>
({
4
,
10
,
22
,
24
,
12
}));
REQUIRE
(
R
(
S
)
==
Polynomial
<
4
>
({
4
,
10
,
22
,
24
,
12
}));
}
}
...
@@ -187,12 +188,11 @@ TEST_CASE("Polynomial", "[analysis]")
...
@@ -187,12 +188,11 @@ TEST_CASE("Polynomial", "[analysis]")
Polynomial
<
2
>
R
;
Polynomial
<
2
>
R
;
lagrangePolynomial
({
-
1
,
0
,
1
},
0
,
R
);
lagrangePolynomial
({
-
1
,
0
,
1
},
0
,
R
);
REQUIRE
(
R
==
P
);
REQUIRE
(
R
==
P
);
TinyVector
<
3
,
Polynomial
<
2
>>
basis
;
const
TinyVector
<
3
,
Polynomial
<
2
>>
basis
=
lagrangeBasis
(
TinyVector
<
3
>
{
-
1
,
0
,
1
});
// lagrangeBasis<2>({-1,0,1},basis);
// lagrangeBasis({-1, 0, 1}, basis);
basis
[
0
]
=
Polynomial
<
2
>
({
0
,
-
0.5
,
0.5
});
// basis[0] = Polynomial<2>({0, -0.5, 0.5});
basis
[
1
]
=
Polynomial
<
2
>
({
1
,
0
,
-
1
});
// basis[1] = Polynomial<2>({1, 0, -1});
basis
[
2
]
=
Polynomial
<
2
>
({
0
,
0.5
,
0.5
});
// basis[2] = Polynomial<2>({0, 0.5, 0.5});
// Q = lagrangeToCanonical({1,0,1},basis);
REQUIRE
(
lagrangeToCanonical
({
1
,
0
,
1
},
basis
)
==
Polynomial
<
2
>
({
0
,
0
,
1
}));
// REQUIRE(lagrangeToCanonical<2>({1, 0, 1}, basis) == Polynomial<2>({0, 0, 1}));
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment