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
be1a785f
Commit
be1a785f
authored
4 years ago
by
Emmanuel Labourasse
Browse files
Options
Downloads
Patches
Plain Diff
Some more feature for polynomials (power,compostition)
parent
47ecb6ff
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/analysis/Polynomial.hpp
+76
-0
76 additions, 0 deletions
src/analysis/Polynomial.hpp
tests/CMakeLists.txt
+1
-0
1 addition, 0 deletions
tests/CMakeLists.txt
tests/test_Polynomial.cpp
+38
-0
38 additions, 0 deletions
tests/test_Polynomial.cpp
with
115 additions
and
0 deletions
src/analysis/Polynomial.hpp
+
76
−
0
View file @
be1a785f
...
@@ -83,6 +83,32 @@ class Polynomial
...
@@ -83,6 +83,32 @@ class Polynomial
return
P
;
return
P
;
}
}
template
<
size_t
M
>
PUGS_INLINE
constexpr
Polynomial
<
N
>&
operator
=
(
const
Polynomial
<
M
>&
Q
)
{
coefficients
()
=
zero
;
for
(
size_t
i
=
N
+
1
;
i
<=
M
;
++
i
)
{
Assert
(
Q
.
coefficients
()[
i
]
==
0
,
"degree of polynomial to small in assignation"
);
}
// static_assert(N >= M, "degree of polynomial to small in assignation");
for
(
size_t
i
=
0
;
i
<=
std
::
min
(
M
,
N
);
++
i
)
{
coefficients
()[
i
]
=
Q
.
coefficients
()[
i
];
}
return
*
this
;
}
template
<
size_t
M
>
PUGS_INLINE
constexpr
Polynomial
<
N
>&
operator
+=
(
const
Polynomial
<
M
>&
Q
)
{
static_assert
(
N
>=
M
,
"Polynomial degree to small in affectation addition"
);
for
(
size_t
i
=
0
;
i
<=
M
;
++
i
)
{
coefficients
()[
i
]
+=
Q
.
coefficients
()[
i
];
}
return
*
this
;
}
template
<
size_t
M
>
template
<
size_t
M
>
PUGS_INLINE
constexpr
Polynomial
<
M
+
N
>
operator
*
(
const
Polynomial
<
M
>&
Q
)
const
PUGS_INLINE
constexpr
Polynomial
<
M
+
N
>
operator
*
(
const
Polynomial
<
M
>&
Q
)
const
{
{
...
@@ -104,6 +130,56 @@ class Polynomial
...
@@ -104,6 +130,56 @@ class Polynomial
return
M
;
return
M
;
}
}
template
<
size_t
M
>
PUGS_INLINE
constexpr
Polynomial
<
M
*
N
>
compose
(
const
Polynomial
<
M
>&
Q
)
const
{
Polynomial
<
M
*
N
>
P
;
P
.
coefficients
()
=
zero
;
Polynomial
<
M
*
N
>
R
;
R
.
coefficients
()
=
zero
;
for
(
size_t
i
=
0
;
i
<=
N
;
++
i
)
{
R
=
Q
.
template
pow
<
N
>(
i
)
*
coefficients
()[
i
];
P
+=
R
;
// R;
}
return
P
;
}
template
<
size_t
M
>
PUGS_INLINE
constexpr
Polynomial
<
M
*
N
>
operator
()(
const
Polynomial
<
M
>&
Q
)
const
{
Polynomial
<
M
*
N
>
P
;
P
.
coefficients
()
=
zero
;
Polynomial
<
M
*
N
>
R
;
R
.
coefficients
()
=
zero
;
for
(
size_t
i
=
0
;
i
<=
N
;
++
i
)
{
R
=
Q
.
template
pow
<
N
>(
i
)
*
coefficients
()[
i
];
P
+=
R
;
// R;
}
return
P
;
}
template
<
size_t
M
>
PUGS_INLINE
constexpr
Polynomial
<
M
*
N
>
pow
(
size_t
power
)
const
{
Assert
(
power
<=
M
,
"You declared a polynomial of degree too small for return of the pow function"
);
Polynomial
<
M
*
N
>
R
;
R
.
coefficients
()
=
zero
;
if
(
power
==
0
)
{
R
.
coefficients
()[
0
]
=
1
;
}
else
{
R
=
*
this
;
for
(
size_t
i
=
1
;
i
<
power
;
++
i
)
{
R
=
R
*
*
this
;
}
}
return
R
;
}
PUGS_INLINE
PUGS_INLINE
constexpr
friend
Polynomial
<
N
>
operator
*
(
const
double
&
lambda
,
const
Polynomial
<
N
>
P
)
constexpr
friend
Polynomial
<
N
>
operator
*
(
const
double
&
lambda
,
const
Polynomial
<
N
>
P
)
{
{
...
...
This diff is collapsed.
Click to expand it.
tests/CMakeLists.txt
+
1
−
0
View file @
be1a785f
...
@@ -68,6 +68,7 @@ add_executable (unit_tests
...
@@ -68,6 +68,7 @@ add_executable (unit_tests
test_NameProcessor.cpp
test_NameProcessor.cpp
test_OStreamProcessor.cpp
test_OStreamProcessor.cpp
test_PCG.cpp
test_PCG.cpp
test_Polynomial.cpp
test_PugsFunctionAdapter.cpp
test_PugsFunctionAdapter.cpp
test_PugsAssert.cpp
test_PugsAssert.cpp
test_RevisionInfo.cpp
test_RevisionInfo.cpp
...
...
This diff is collapsed.
Click to expand it.
tests/test_Polynomial.cpp
+
38
−
0
View file @
be1a785f
...
@@ -102,4 +102,42 @@ TEST_CASE("Polynomial", "[analysis]")
...
@@ -102,4 +102,42 @@ TEST_CASE("Polynomial", "[analysis]")
Polynomial
<
0
>
R
(
0
);
Polynomial
<
0
>
R
(
0
);
REQUIRE
(
derivative
(
P2
)
==
R
);
REQUIRE
(
derivative
(
P2
)
==
R
);
}
}
SECTION
(
"affectation"
)
{
Polynomial
<
2
>
Q
({
2
,
-
3
,
3
});
Polynomial
<
4
>
R
({
2
,
-
3
,
3
,
0
,
0
});
Polynomial
<
4
>
P
({
0
,
1
,
2
,
3
,
3
});
P
=
Q
;
REQUIRE
(
P
==
R
);
}
SECTION
(
"affectation addition"
)
{
Polynomial
<
2
>
Q
({
2
,
-
3
,
3
});
Polynomial
<
4
>
R
({
2
,
-
2
,
5
,
3
,
3
});
Polynomial
<
4
>
P
({
0
,
1
,
2
,
3
,
3
});
P
+=
Q
;
REQUIRE
(
P
==
R
);
}
SECTION
(
"power"
)
{
Polynomial
<
2
>
P
({
2
,
-
3
,
3
});
Polynomial
<
4
>
R
({
4
,
-
12
,
21
,
-
18
,
9
});
Polynomial
<
1
>
Q
({
0
,
2
});
Polynomial
<
2
>
S
=
Q
.
pow
<
2
>
(
2
);
REQUIRE
(
P
.
pow
<
2
>
(
2
)
==
R
);
REQUIRE
(
S
==
Polynomial
<
2
>
({
0
,
0
,
4
}));
}
SECTION
(
"composition"
)
{
Polynomial
<
2
>
P
({
2
,
-
3
,
3
});
Polynomial
<
1
>
Q
({
0
,
2
});
Polynomial
<
2
>
R
({
2
,
-
1
,
3
});
Polynomial
<
2
>
S
({
1
,
2
,
2
});
REQUIRE
(
P
.
compose
(
Q
)
==
Polynomial
<
2
>
({
2
,
-
6
,
12
}));
REQUIRE
(
R
(
S
)
==
Polynomial
<
4
>
({
4
,
10
,
22
,
24
,
12
}));
}
}
}
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