Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
392d8d7
Remove an unused member func.
sukeya May 16, 2026
88ef36d
Add clang-format to pre-commit setting.
sukeya May 16, 2026
d963694
Fix format.
sukeya May 16, 2026
edb48c7
Simplyfy Integrate.
sukeya May 16, 2026
cd53f86
Simplify the impl of operator().
sukeya May 16, 2026
7a7daf0
Remove deleting funcs.
sukeya May 16, 2026
22175af
Remove unused alias type.
sukeya May 16, 2026
c5c801b
Simplify .clang-format.
sukeya May 16, 2026
9f04675
Format.
sukeya May 16, 2026
99f6815
Fix operator*=(scalar).
sukeya May 16, 2026
eef1856
Fix operator-=(scalar).
sukeya May 16, 2026
c50667e
Fix test cmake setting.
sukeya May 16, 2026
d7de6db
Fix README.md.
sukeya May 16, 2026
682a37a
Add using std header.
sukeya May 16, 2026
fba80e9
Fix.
sukeya May 16, 2026
daf4863
Take measure to be collapsed format.
sukeya May 16, 2026
833b7c1
Remove empty mem func becuase mvPoly must not be empty.
sukeya May 16, 2026
6882a39
Remove const_cast.
sukeya May 16, 2026
87adb53
Add abs and relative tolerance.
sukeya May 17, 2026
75c7f42
Check index at operator[] and fix format.
sukeya May 17, 2026
3de42f1
Add tests and fix how to delete zero coeff.
sukeya May 17, 2026
be04a96
Optimize operator()(coord_type).
sukeya May 17, 2026
1167380
Fix a bug.
sukeya May 17, 2026
27b2070
Optimize operator()(MVPolynomial).
sukeya May 17, 2026
423e9ec
Fix to pass allocator.
sukeya May 17, 2026
c7da6de
Fix bugs destructing normalization of MVPoly.
sukeya May 17, 2026
f65c4ac
Add tests.
sukeya May 17, 2026
06d6f4e
Fix bugs breaking 0 coeff normalization.
sukeya May 17, 2026
72e976d
Unify index access to index2value_.
sukeya May 17, 2026
c845508
Unify allocator type.
sukeya May 17, 2026
2d5cf1d
Update README.
sukeya May 17, 2026
5249a1f
Fix bugs and add random tests for operator().
sukeya May 17, 2026
8db2044
Unify responsibility for normalization.
sukeya May 17, 2026
ba7b5d9
Add a func normalizing single term.
sukeya May 17, 2026
e6d0f1a
Fix try_get and optimize.
sukeya May 17, 2026
386aa03
Remove copy.
sukeya May 17, 2026
6cb9b66
Update README.
sukeya May 17, 2026
7aac879
Add binary op with scalar.
sukeya May 17, 2026
5aa910b
Update README.
sukeya May 17, 2026
70cb874
Fix.
sukeya May 17, 2026
43c1ba2
Set ASAN_OPTIONS=alloc_dealloc_mismatch=0.
sukeya May 17, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 7 additions & 212 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,225 +1,20 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
Language: Cpp
BasedOnStyle: Google
AlignAfterOpenBracket: BlockIndent
AlignArrayOfStructures: Right
AlignConsecutiveMacros: Consecutive
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveBitFields: Consecutive
AlignConsecutiveDeclarations: Consecutive
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
ColumnLimit: 120
IndentRequires: false
#PackArguments.BinPackArgumentsStyle: OnePerLine
PackConstructorInitializers: NextLine
BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*\.h>'
Priority: 1
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 3
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '([-_](test|unittest))?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: true
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: false
IndentWidth: 2
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Never
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PenaltyIndentedWhitespace: 0
#PackParameters.BinPackParametersStyle: OnePerLine
PointerAlignment: Left
PPIndentWidth: -1
RawStringFormats:
- Language: Cpp
Delimiters:
- cc
- CC
- cpp
- Cpp
- CPP
- 'c++'
- 'C++'
CanonicalDelimiter: ''
BasedOnStyle: google
- Language: TextProto
Delimiters:
- pb
- PB
- proto
- PROTO
EnclosingFunctions:
- EqualsProto
- EquivToProto
- PARSE_PARTIAL_TEXT_PROTO
- PARSE_TEST_PROTO
- PARSE_TEXT_PROTO
- ParseTextOrDie
- ParseTextProtoOrDie
- ParseTestProto
- ParsePartialTestProto
CanonicalDelimiter: pb
BasedOnStyle: google
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: Never
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Auto
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
SortIncludes: Never
...
2 changes: 1 addition & 1 deletion .github/workflows/pre_commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ jobs:
- name: Set up pre-commit
run: |
python -m pip install --upgrade pip
pip install pre-commit
pip install pre-commit clang-format
- name: Run pre-commit for all files
run: pre-commit run --all-files
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ jobs:
cd ./build/debug/clang && ninja
- name: Run tests
run: |
./build/debug/clang/test/mvPolynomial_test
# libc++ exception internals can trip ASan's alloc/dealloc mismatch check
# on Ubuntu runners even when the library code itself is fine.
ASAN_OPTIONS=alloc_dealloc_mismatch=0 ./build/debug/clang/test/mvPolynomial_test

clang_memory:
# libstdc++ and libc++ have uninitialized variables,
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repos:
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v21.1.0
rev: v22.1.5
hooks:
- id: clang-format
types_or: [c++]
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ set(gcc "$<COMPILE_LANG_AND_ID:CXX,GNU>")
set(clang "$<COMPILE_LANG_AND_ID:CXX,Clang>")
set(msvc "$<COMPILE_LANG_AND_ID:CXX,MSVC>")

set(gcc_like $<OR:${gcc},${clang})
set(gcc_like $<OR:${gcc},${clang}>)

if (MVPOLYNOMIAL_BUILD_TESTS)
enable_testing()
Expand Down
86 changes: 75 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,81 @@ The examples exist in "test" directory.

## Multi-variable polynomial
A class `MVPolynomial` implements multi-variable polynomials.
Its interface is the same as Boost's flat_map except that it lacks `merge` member function.
So, please see the document of Boost's flat_map.

A class `ExactOf` calculates f of x as less multiplication as possible, but uses more memories.
```cpp
#include "mvPolynomial/mvPolynomial.hpp"

## Polynomial product
A class `PolynomialProduct` implements a product of polynomials which have different variable each other (example: (1 + x + x^2) * (y + 6 * y^3 + y^10) * (1 + z)).
Its interface is the same as std::array.
So, please see the document of std::array.
using MP2 = mvPolynomial::MVPolynomial<int, double, 2>;
```

You can construct a polynomial from an initializer list of `(index, coefficient)` pairs.

```cpp
auto p = MP2({
{{0, 0}, 1.0},
{{1, 0}, 2.0},
{{0, 1}, 3.0},
});
```

The constant term is the zero index.

```cpp
double c = p.get(Eigen::Array2i::Zero());
p.set(Eigen::Array2i({2, 0}), 5.0);
```

If you want to read a term that may be absent, use `try_get`.

```cpp
auto maybe_xy = p.try_get(Eigen::Array2i({1, 1}));
if (maybe_xy.has_value()) {
double coeff = *maybe_xy;
}
```

`set(index, 0.0)` removes the term, and zero-coefficient terms are normalized away automatically.

The class provides basic polynomial arithmetic.

```cpp
auto q = MP2({
{{0, 0}, 4.0},
{{1, 0}, 1.0},
});
auto sum = p + q;
auto sub = p - q;
auto mul = p * q;
auto div = p / 2.0;
auto pw = p.pow(3);
```

`p /= scalar` and `p / scalar` are supported. Division by zero scalar throws `std::invalid_argument`.

You can evaluate a polynomial at a point.

```cpp
double value = p(Eigen::Vector2d({2.0, 3.0}));
```

You can also substitute a polynomial for one axis.

```cpp
auto x = MP2({
{{0, 0}, 1.0},
{{1, 0}, 2.0},
});
auto composed = p(x, 1);
```

Differentiation and integration are available both as member functions and as free functions.

```cpp
auto dx_member = p.D(0);
auto iy_member = p.Integrate(1);

auto dx = mvPolynomial::D(p, 0);
auto iy = mvPolynomial::Integrate(p, 1);
```

## Polynomial
A class `Polynomial` implements one-variable polynomials.
Its interface is also the same as Boost's flat_map except that it lacks `merge` member function.
So, please see the document of Boost's flat_map.
Negative indices are not supported and cause `std::invalid_argument`.
18 changes: 0 additions & 18 deletions include/mvPolynomial/index_comparer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,6 @@ class IndexComparer final {

static constexpr int dim = D;

/**
* \brief If lhd[i] == rhd[i] for i = 0, ..., N - 1 and lhd[N] < rhd[N],
* return greater: if lhd[N] > rhd[N], return less: otherwise, return equal.
* \param[in] lhd an index
* \param[in] rhd an index
*/
static constexpr std::strong_ordering Compare(const Index& lhd, const Index& rhd) {
for (int i = 0; i < lhd.size(); ++i) {
auto comp = lhd[i] <=> rhd[i];
if (comp < 0) {
return std::strong_ordering::greater;
} else if (comp > 0) {
return std::strong_ordering::less;
}
}
return std::strong_ordering::equal;
}

/**
* \brief If lhd[i] == rhd[i] for i = 0, ..., N - 1 and lhd[N] < rhd[N],
* return true: otherwise, false.
Expand Down
Loading