Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pymbolic
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
Alexandru Fikl
pymbolic
Commits
971949fc
Commit
971949fc
authored
19 years ago
by
Andreas Tester
Browse files
Options
Downloads
Patches
Plain Diff
[pymbolic @ Arch-1:inform@tiker.net--iam-2005%pymbolic--mainline--1.0--patch-9]
Arch Inventory in mapper/ subdir.
parent
46a976d0
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/polynomial.py
+63
-0
63 additions, 0 deletions
src/polynomial.py
with
63 additions
and
0 deletions
src/polynomial.py
0 → 100644
+
63
−
0
View file @
971949fc
class
Polynomial
(
Expression
):
def
__init__
(
self
,
base
,
coeff
):
self
.
Base
=
base
# list of (exponent, coefficient tuples)
# sorted in increasing order
# one entry per degree
self
.
Data
=
children
# Remember the Zen, Luke: Sparse is better than dense.
def
__neg__
(
self
):
return
Polynomial
(
self
.
Base
,
[(
exp
,
-
coeff
)
for
(
exp
,
coeff
)
in
self
.
Data
])
def
__add__
(
self
,
other
):
if
not
isinstance
(
other
,
Polynomial
)
or
other
.
Base
!=
self
.
Base
:
return
Expression
.
__add__
(
self
,
other
)
iself
=
0
iother
=
0
result
=
[]
while
iself
<
len
(
self
.
Data
)
and
iother
<
len
(
other
.
Data
):
exp_self
=
self
.
Data
[
iself
][
0
]
exp_other
=
other
.
Data
[
iother
][
0
]
if
exp_self
==
exp_other
:
coeff
=
self
.
Data
[
iself
][
1
]
+
other
.
Data
[
iother
][
1
]
if
coeff
!=
Constant
(
0
):
result
.
append
((
exp_self
,
coeff
))
iself
+=
1
iother
+=
1
elif
exp_self
>
exp_other
:
result
.
append
((
exp_other
,
other
.
Data
[
iother
][
1
]))
iother
+=
1
elif
exp_self
<
exp_other
:
result
.
append
((
exp_self
,
self
.
Data
[
iself
][
1
]))
iself
+=
1
# we have exhausted at least one list, exhaust the other
while
iself
<
len
(
self
.
Data
):
exp_self
=
self
.
Data
[
iself
][
0
]
result
.
append
((
exp_self
,
self
.
Data
[
iself
][
1
]))
iself
+=
1
while
iother
<
len
(
other
.
Data
):
exp_other
=
other
.
Data
[
iother
][
0
]
result
.
append
((
exp_other
,
other
.
Data
[
iother
][
1
]))
iother
+=
1
return
Polynomial
(
self
.
Base
,
result
)
def
__mul__
(
self
,
other
):
if
not
isinstance
(
other
,
Polynomial
)
or
other
.
Base
!=
self
.
Base
:
return
Expression
.
__mul__
(
self
,
other
)
raise
NotImplementedError
def
__hash__
(
self
):
return
hash
(
self
.
Base
)
^
hash
(
self
.
Children
)
def
invoke_mapper
(
self
,
mapper
):
return
mapper
.
map_polynomial
(
self
)
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