Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pycodegen
pystencils
Commits
ef41f461
Commit
ef41f461
authored
11 months ago
by
Frederik Hennig
Browse files
Options
Downloads
Patches
Plain Diff
add typed_sympy tests
parent
d85f682c
No related branches found
No related tags found
1 merge request
!400
Extensions and fixes to the type system
Pipeline
#67440
passed
11 months ago
Stage: Code Quality
Stage: Unit Tests
Stage: legacy_test
Stage: docs
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/pystencils/sympyextensions/typed_sympy.py
+7
-7
7 additions, 7 deletions
src/pystencils/sympyextensions/typed_sympy.py
src/pystencils/types/types.py
+2
-2
2 additions, 2 deletions
src/pystencils/types/types.py
tests/symbolics/test_typed_sympy.py
+57
-0
57 additions, 0 deletions
tests/symbolics/test_typed_sympy.py
with
66 additions
and
9 deletions
src/pystencils/sympyextensions/typed_sympy.py
+
7
−
7
View file @
ef41f461
...
...
@@ -41,8 +41,8 @@ class DynamicType(Enum):
INDEX_TYPE
=
auto
()
class
Ps
TypeAtom
(
sp
.
Atom
):
"""
Wrapper around a
PsT
ype to disguise it as a SymPy atom.
"""
class
TypeAtom
(
sp
.
Atom
):
"""
Wrapper around a
t
ype to disguise it as a SymPy atom.
"""
def
__new__
(
cls
,
*
args
,
**
kwargs
):
return
sp
.
Basic
.
__new__
(
cls
)
...
...
@@ -74,7 +74,7 @@ class TypedSymbol(sp.Symbol):
assumptions
.
update
(
kwargs
)
obj
=
super
(
TypedSymbol
,
cls
).
__xnew__
(
cls
,
name
,
**
assumptions
)
obj
.
_dtype
=
create_type
(
dtype
)
obj
.
_dtype
=
dtype
return
obj
...
...
@@ -235,11 +235,11 @@ class CastFunc(sp.Function):
if
expr
.
__class__
==
CastFunc
:
expr
=
expr
.
args
[
0
]
if
not
isinstance
(
dtype
,
(
Ps
TypeAtom
)):
if
not
isinstance
(
dtype
,
(
TypeAtom
)):
if
isinstance
(
dtype
,
DynamicType
):
dtype
=
Ps
TypeAtom
(
dtype
)
dtype
=
TypeAtom
(
dtype
)
else
:
dtype
=
Ps
TypeAtom
(
create_type
(
dtype
))
dtype
=
TypeAtom
(
create_type
(
dtype
))
# to work in conditions of sp.Piecewise cast_func has to be of type Boolean as well
# however, a cast_function should only be a boolean if its argument is a boolean, otherwise this leads
...
...
@@ -269,7 +269,7 @@ class CastFunc(sp.Function):
@property
def
dtype
(
self
)
->
PsType
|
DynamicType
:
assert
isinstance
(
self
.
args
[
1
],
Ps
TypeAtom
)
assert
isinstance
(
self
.
args
[
1
],
TypeAtom
)
return
self
.
args
[
1
].
get
()
@property
...
...
This diff is collapsed.
Click to expand it.
src/pystencils/types/types.py
+
2
−
2
View file @
ef41f461
...
...
@@ -72,7 +72,7 @@ class PsPointerType(PsDereferencableType):
__match_args__
=
(
"
base_type
"
,)
def
__init__
(
self
,
base_type
:
PsType
,
restrict
:
bool
=
Tru
e
,
const
:
bool
=
False
):
def
__init__
(
self
,
base_type
:
PsType
,
restrict
:
bool
=
Fals
e
,
const
:
bool
=
False
):
super
().
__init__
(
base_type
,
const
)
self
.
_restrict
=
restrict
...
...
@@ -94,7 +94,7 @@ class PsPointerType(PsDereferencableType):
return
f
"
{
base_str
}
*
{
restrict_str
}
{
self
.
_const_string
()
}
"
def
__repr__
(
self
)
->
str
:
return
f
"
PsPointerType(
{
repr
(
self
.
base_type
)
}
, const=
{
self
.
const
}
)
"
return
f
"
PsPointerType(
{
repr
(
self
.
base_type
)
}
, const=
{
self
.
const
}
, restrict=
{
self
.
restrict
}
)
"
class
PsArrayType
(
PsDereferencableType
):
...
...
This diff is collapsed.
Click to expand it.
tests/symbolics/test_typed_sympy.py
0 → 100644
+
57
−
0
View file @
ef41f461
import
numpy
as
np
from
pystencils.sympyextensions.typed_sympy
import
(
TypedSymbol
,
CastFunc
,
TypeAtom
,
DynamicType
,
)
from
pystencils.types
import
create_type
from
pystencils.types.quick
import
UInt
,
Ptr
def
test_type_atoms
():
atom1
=
TypeAtom
(
create_type
(
"
int32
"
))
atom2
=
TypeAtom
(
create_type
(
"
int32
"
))
assert
atom1
==
atom2
atom3
=
TypeAtom
(
create_type
(
"
const int32
"
))
assert
atom1
!=
atom3
atom4
=
TypeAtom
(
DynamicType
.
INDEX_TYPE
)
atom5
=
TypeAtom
(
DynamicType
.
NUMERIC_TYPE
)
assert
atom3
!=
atom4
assert
atom4
!=
atom5
def
test_typed_symbol
():
x
=
TypedSymbol
(
"
x
"
,
"
uint32
"
)
x2
=
TypedSymbol
(
"
x
"
,
"
uint64 *
"
)
z
=
TypedSymbol
(
"
z
"
,
"
float32
"
)
assert
x
==
TypedSymbol
(
"
x
"
,
np
.
uint32
)
assert
x
!=
x2
assert
x
.
dtype
==
UInt
(
32
)
assert
x2
.
dtype
==
Ptr
(
UInt
(
64
))
assert
x
.
is_integer
assert
x
.
is_nonnegative
assert
not
x2
.
is_integer
assert
z
.
is_real
assert
not
z
.
is_nonnegative
def
test_cast_func
():
assert
(
CastFunc
(
TypedSymbol
(
"
s
"
,
np
.
uint
),
np
.
int64
).
canonical
==
TypedSymbol
(
"
s
"
,
np
.
uint
).
canonical
)
a
=
CastFunc
(
5
,
np
.
uint
)
assert
a
.
is_negative
is
False
assert
a
.
is_nonnegative
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