Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prodigy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tobias Winkler
prodigy
Commits
f70f5e88
Commit
f70f5e88
authored
2 years ago
by
Tobias Winkler
Browse files
Options
Downloads
Patches
Plain Diff
implemented CoefficientIterator with bindings
parent
ceb9ea46
No related branches found
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
python/bindings.cpp
+5
-0
5 additions, 0 deletions
python/bindings.cpp
python/test.py
+4
-0
4 additions, 0 deletions
python/test.py
src/Dist.cpp
+13
-0
13 additions, 0 deletions
src/Dist.cpp
src/Dist.h
+39
-3
39 additions, 3 deletions
src/Dist.h
test/TestDist.cpp
+7
-0
7 additions, 0 deletions
test/TestDist.cpp
with
68 additions
and
3 deletions
python/bindings.cpp
+
5
−
0
View file @
f70f5e88
...
...
@@ -50,6 +50,7 @@ PYBIND11_MODULE(pygin, m) {
.
def
(
"filterLeq"
,
&
Dist
::
filterLeq
)
.
def
(
"filterGreater"
,
&
Dist
::
filterGreater
)
.
def
(
"filterGeq"
,
&
Dist
::
filterGeq
)
.
def
(
"coefficient_iterator"
,
&
Dist
::
coefficientIterator
)
.
def
(
py
::
self
==
py
::
self
)
.
def
(
py
::
self
!=
py
::
self
)
//.def(string() * py::self)
...
...
@@ -71,6 +72,10 @@ PYBIND11_MODULE(pygin, m) {
.
value
(
"true"
,
troolean
::
TRUE
)
.
value
(
"unknown"
,
troolean
::
UNKNOWN
);
py
::
class_
<
Dist
::
CoefficientIterator
>
(
m
,
"CoefficientIterator"
)
.
def
(
"next"
,
&
Dist
::
CoefficientIterator
::
next
)
.
def
(
"rest"
,
&
Dist
::
CoefficientIterator
::
rest
);
#ifdef VERSION_INFO
m
.
attr
(
"__version__"
)
=
MACRO_STRINGIFY
(
VERSION_INFO
);
...
...
This diff is collapsed.
Click to expand it.
python/test.py
+
4
−
0
View file @
f70f5e88
...
...
@@ -25,3 +25,7 @@ if __name__ == "__main__":
check_if_zero
(
dist
)
check_if_zero
(
pg
.
Dist
(
"
0
"
))
it
=
dist
.
coefficient_iterator
(
var
)
print
(
f
"
first terms of dist are
{
it
.
next
()
}
,
{
it
.
next
()
}
, and
{
it
.
next
()
}
"
)
print
(
f
"
remaining terms not consumed by iterator:
{
it
.
rest
()
}
"
)
This diff is collapsed.
Click to expand it.
src/Dist.cpp
+
13
−
0
View file @
f70f5e88
...
...
@@ -370,6 +370,7 @@ namespace prodigy {
EXPECTS
(
eExp
.
info
(
info_flags
::
nonnegint
),
"given expression must be non-neg int"
);
int
order
=
ex_to
<
numeric
>
(
eExp
).
to_int
();
ex
xExp
;
// todo why not use parseWithKnownSymbols?
if
(
!
isKnownAsVar
(
x
))
{
xExp
=
symbol
{
x
};
Dist
::
registerAsVar
(
ex_to
<
symbol
>
(
xExp
));
...
...
@@ -380,6 +381,18 @@ namespace prodigy {
return
Dist
{
series_to_poly
(
series
)};
}
Dist
Dist
::
CoefficientIterator
::
next
()
{
ex
resGf
=
_rest
.
subs
(
_var
==
0
);
_rest
=
(
_rest
-
resGf
)
*
pow
(
_var
,
-
1
);
_rest
=
_rest
.
normal
();
// for canceling common factors
return
Dist
(
resGf
);
}
Dist
::
CoefficientIterator
Dist
::
coefficientIterator
(
const
std
::
string
&
var
)
const
{
EXPECTS
(
isKnownAsVar
(
var
),
"can only expand series in known variables"
);
return
CoefficientIterator
(
*
this
,
ex_to
<
symbol
>
(
_vars
.
at
(
var
)));
}
Dist
Dist
::
substituteParam
(
const
std
::
string
&
p
,
const
std
::
string
&
val
)
const
{
EXPECTS
(
!
isKnownAsVar
(
p
),
"given symbol is considered a variable"
);
if
(
!
isKnownAsParam
(
p
))
{
...
...
This diff is collapsed.
Click to expand it.
src/Dist.h
+
39
−
3
View file @
f70f5e88
...
...
@@ -132,11 +132,25 @@ namespace prodigy {
const
ex
&
gf
()
const
;
/*
* returns the specified raw moment (possibly non-numeric due to presence of parameters)
* if *this is a distribution, then the raw moment is the expected value of the given monomial
* this function does not modify the global state (//todo why?)
*/
ex
moment
(
const
std
::
map
<
std
::
string
,
int
>&
monomial
)
const
;
/*
* returns total probability mass, possibly non-numeric due to params
* // todo implement in terms of moment
*/
ex
mass
()
const
;
/*
* returns expected value, possibly non-numeric due to params
* // todo implement in terms of moment
*/
ex
E
(
const
std
::
string
&
var
)
const
;
/*
* approximate size of underlying gf representation
*/
...
...
@@ -180,10 +194,9 @@ namespace prodigy {
troolean
areIndependent
(
const
std
::
string
&
x
,
const
std
::
string
&
y
)
const
;
/*
* returns expected value, possibly non-numeric due to params
* does not modify global state
* attempts to prove that *this is finite-support, i.e., just finitely many coefficients are non-zero
*/
ex
E
(
const
std
::
string
&
var
)
const
;
troolean
isFiniteSupport
(
)
const
;
/*
* update distribution according to assignment x := e
...
...
@@ -217,6 +230,29 @@ namespace prodigy {
Dist
filterEq
(
const
std
::
string
&
x
,
const
std
::
string
&
e
)
const
{
return
*
this
-
filterLess
(
x
,
e
)
-
filterGreater
(
x
,
e
);
}
Dist
filterNeq
(
const
std
::
string
&
x
,
const
std
::
string
&
e
)
const
{
return
*
this
-
filterEq
(
x
,
e
);
}
/*
* this iterator regards a k-dimensional distribution as 1-dimensional in the given variable with coefficients
* that are k-1 dimensional, and it iterates of these coefficients
*
* in other words, the iterator ranges over the coefficients in the series expansion wrt. the given of the gf
* underlying the given distribution
*/
class
CoefficientIterator
{
private:
ex
_rest
;
symbol
_var
;
public:
CoefficientIterator
(
Dist
dist
,
symbol
var
)
:
_rest
(
std
::
move
(
dist
.
_gf
)),
_var
(
std
::
move
(
var
))
{}
Dist
next
();
Dist
rest
()
const
{
return
Dist
(
_rest
);
}
};
/*
* returns a coefficient iterator for the dimension specified by var
* throws an exception if var is not registered as a variable
*/
CoefficientIterator
coefficientIterator
(
const
std
::
string
&
var
)
const
;
/*
* replaces given parameter with given value or parametric expression
*/
...
...
This diff is collapsed.
Click to expand it.
test/TestDist.cpp
+
7
−
0
View file @
f70f5e88
...
...
@@ -69,3 +69,10 @@ TEST(Dist, gfSize) {
ASSERT_EQ
(
Dist
(
"x"
).
gfSize
(),
1
);
ASSERT_EQ
(
Dist
(
"x+y"
).
gfSize
(),
3
);
}
TEST
(
Dist
,
coefficientIterator
)
{
Dist
::
resetSymbolCache
();
Dist
dist
=
Fac
::
geometric
(
"x"
,
"q"
);
Dist
::
CoefficientIterator
it
=
dist
.
coefficientIterator
(
"x"
);
ASSERT_EQ
(
it
.
next
()
+
it
.
next
()
+
it
.
next
(),
Dist
(
"1-q - q^2 + q + q^2 - q^3"
,
"q"
));
}
\ No newline at end of file
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