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
ac5c82fd
Commit
ac5c82fd
authored
4 years ago
by
Emmanuel Labourasse
Browse files
Options
Downloads
Patches
Plain Diff
construction of Lagrange polynomial
parent
be1a785f
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
+36
-1
36 additions, 1 deletion
src/analysis/Polynomial.hpp
tests/test_Polynomial.cpp
+19
-0
19 additions, 0 deletions
tests/test_Polynomial.cpp
with
55 additions
and
1 deletion
src/analysis/Polynomial.hpp
+
36
−
1
View file @
ac5c82fd
...
@@ -122,6 +122,24 @@ class Polynomial
...
@@ -122,6 +122,24 @@ class Polynomial
return
P
;
return
P
;
}
}
template
<
size_t
M
>
PUGS_INLINE
constexpr
Polynomial
<
N
>&
operator
*=
(
const
Polynomial
<
M
>&
Q
)
{
static_assert
(
N
>=
M
,
"Degree to small in affectation product"
);
for
(
size_t
i
=
N
-
M
+
1
;
i
<=
N
;
++
i
)
{
Assert
(
coefficients
()[
i
]
==
0
,
"Degree of affectation product greater than the degree of input polynomial"
);
}
Polynomial
<
N
>
P
(
zero
);
for
(
size_t
i
=
0
;
i
<=
N
-
M
;
++
i
)
{
for
(
size_t
j
=
0
;
j
<=
M
;
++
j
)
{
P
.
coefficients
()[
i
+
j
]
+=
coefficients
()[
i
]
*
Q
.
coefficients
()[
j
];
}
}
coefficients
()
=
P
.
coefficients
();
return
*
this
;
}
PUGS_INLINE
PUGS_INLINE
constexpr
Polynomial
<
N
>
operator
*
(
const
double
&
lambda
)
const
constexpr
Polynomial
<
N
>
operator
*
(
const
double
&
lambda
)
const
{
{
...
@@ -306,6 +324,24 @@ class Polynomial
...
@@ -306,6 +324,24 @@ class Polynomial
return
os
;
return
os
;
}
}
PUGS_INLINE
constexpr
friend
void
lagrangePolynomial
(
const
TinyVector
<
N
+
1
>
zeros
,
const
size_t
k
,
Polynomial
<
N
>&
lk
)
{
for
(
size_t
i
=
0
;
i
<
N
;
++
i
)
{
Assert
(
zeros
[
i
]
<
zeros
[
i
+
1
],
"Interpolation values must be strictly increasing in Lagrange polynomials"
);
}
lk
.
coefficients
()
=
zero
;
lk
.
coefficients
()[
0
]
=
1
;
for
(
size_t
i
=
0
;
i
<
N
+
1
;
++
i
)
{
if
(
k
==
i
)
continue
;
double
factor
=
1.
/
(
zeros
[
k
]
-
zeros
[
i
]);
Polynomial
<
1
>
P
({
-
zeros
[
i
]
*
factor
,
factor
});
lk
*=
P
;
}
}
PUGS_INLINE
constexpr
Polynomial
(
const
TinyVector
<
N
+
1
>&
coefficients
)
noexcept
:
m_coefficients
{
coefficients
}
{}
PUGS_INLINE
constexpr
Polynomial
(
const
TinyVector
<
N
+
1
>&
coefficients
)
noexcept
:
m_coefficients
{
coefficients
}
{}
PUGS_INLINE
PUGS_INLINE
...
@@ -315,5 +351,4 @@ class Polynomial
...
@@ -315,5 +351,4 @@ class Polynomial
constexpr
Polynomial
()
noexcept
=
default
;
constexpr
Polynomial
()
noexcept
=
default
;
~
Polynomial
()
=
default
;
~
Polynomial
()
=
default
;
};
};
#endif // POLYNOMIAL_HPP
#endif // POLYNOMIAL_HPP
This diff is collapsed.
Click to expand it.
tests/test_Polynomial.cpp
+
19
−
0
View file @
ac5c82fd
...
@@ -64,7 +64,14 @@ TEST_CASE("Polynomial", "[analysis]")
...
@@ -64,7 +64,14 @@ TEST_CASE("Polynomial", "[analysis]")
{
{
Polynomial
<
2
>
P
({
2
,
3
,
4
});
Polynomial
<
2
>
P
({
2
,
3
,
4
});
Polynomial
<
3
>
Q
({
1
,
2
,
-
1
,
1
});
Polynomial
<
3
>
Q
({
1
,
2
,
-
1
,
1
});
Polynomial
<
4
>
R
;
Polynomial
<
5
>
S
;
R
=
P
;
S
=
P
;
S
*=
Q
;
REQUIRE
(
Polynomial
<
5
>
({
2
,
7
,
8
,
7
,
-
1
,
4
})
==
(
P
*
Q
));
REQUIRE
(
Polynomial
<
5
>
({
2
,
7
,
8
,
7
,
-
1
,
4
})
==
(
P
*
Q
));
REQUIRE
(
Polynomial
<
5
>
({
2
,
7
,
8
,
7
,
-
1
,
4
})
==
S
);
// REQUIRE_THROWS_AS(R *= Q, AssertError);
}
}
SECTION
(
"evaluation"
)
SECTION
(
"evaluation"
)
{
{
...
@@ -140,4 +147,16 @@ TEST_CASE("Polynomial", "[analysis]")
...
@@ -140,4 +147,16 @@ TEST_CASE("Polynomial", "[analysis]")
REQUIRE
(
P
.
compose
(
Q
)
==
Polynomial
<
2
>
({
2
,
-
6
,
12
}));
REQUIRE
(
P
.
compose
(
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
}));
}
}
SECTION
(
"Lagrange polynomial"
)
{
Polynomial
<
1
>
S
({
0.5
,
-
0.5
});
Polynomial
<
1
>
Q
;
lagrangePolynomial
({
-
1
,
1
},
0
,
Q
);
REQUIRE
(
S
==
Q
);
Polynomial
<
2
>
P
({
0
,
-
0.5
,
0.5
});
Polynomial
<
2
>
R
;
lagrangePolynomial
({
-
1
,
0
,
1
},
0
,
R
);
REQUIRE
(
R
==
P
);
}
}
}
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