Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
eigenpy
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
Container Registry
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
Stack Of Tasks
eigenpy
Commits
dda4980a
Verified
Commit
dda4980a
authored
4 years ago
by
Justin Carpentier
Browse files
Options
Downloads
Patches
Plain Diff
core: move NumpyType definition to .cpp
parent
91c9bab8
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CMakeLists.txt
+1
-0
1 addition, 0 deletions
CMakeLists.txt
include/eigenpy/numpy-type.hpp
+15
-95
15 additions, 95 deletions
include/eigenpy/numpy-type.hpp
src/numpy-type.cpp
+121
-0
121 additions, 0 deletions
src/numpy-type.cpp
with
137 additions
and
95 deletions
CMakeLists.txt
+
1
−
0
View file @
dda4980a
...
...
@@ -150,6 +150,7 @@ SET(${PROJECT_NAME}_SOURCES
src/exception.cpp
src/eigenpy.cpp
src/numpy.cpp
src/numpy-type.cpp
src/matrix-float.cpp
src/matrix-complex-float.cpp
src/matrix-complex-double.cpp
...
...
This diff is collapsed.
Click to expand it.
include/eigenpy/numpy-type.hpp
+
15
−
95
View file @
dda4980a
...
...
@@ -8,7 +8,6 @@
#include
"eigenpy/fwd.hpp"
#include
"eigenpy/scalar-conversion.hpp"
#include
<patchlevel.h>
// For PY_MAJOR_VERSION
#include
<stdexcept>
#include
<typeinfo>
#include
<sstream>
...
...
@@ -76,118 +75,39 @@ namespace eigenpy
struct
NumpyType
{
static
NumpyType
&
getInstance
()
{
static
NumpyType
instance
;
return
instance
;
}
static
NumpyType
&
getInstance
();
operator
bp
::
object
()
{
return
getInstance
().
CurrentNumpyType
;
}
static
bp
::
object
make
(
PyArrayObject
*
pyArray
,
bool
copy
=
false
)
{
return
make
((
PyObject
*
)
pyArray
,
copy
);
}
static
bp
::
object
make
(
PyArrayObject
*
pyArray
,
bool
copy
=
false
);
static
bp
::
object
make
(
PyObject
*
pyObj
,
bool
copy
=
false
)
{
bp
::
object
m
;
if
(
isMatrix
())
m
=
getInstance
().
NumpyMatrixObject
(
bp
::
object
(
bp
::
handle
<>
(
pyObj
)),
bp
::
object
(),
copy
);
// m = NumpyAsMatrixObject(bp::object(bp::handle<>(pyObj)));
else
if
(
isArray
())
m
=
bp
::
object
(
bp
::
handle
<>
(
pyObj
));
// nothing to do here
Py_INCREF
(
m
.
ptr
());
return
m
;
}
static
bp
::
object
make
(
PyObject
*
pyObj
,
bool
copy
=
false
);
static
void
setNumpyType
(
bp
::
object
&
obj
)
{
PyTypeObject
*
obj_type
=
PyType_Check
(
obj
.
ptr
())
?
reinterpret_cast
<
PyTypeObject
*>
(
obj
.
ptr
())
:
obj
.
ptr
()
->
ob_type
;
if
(
PyType_IsSubtype
(
obj_type
,
getInstance
().
NumpyMatrixType
))
switchToNumpyMatrix
();
else
if
(
PyType_IsSubtype
(
obj_type
,
getInstance
().
NumpyArrayType
))
switchToNumpyArray
();
}
static
void
setNumpyType
(
bp
::
object
&
obj
);
static
void
sharedMemory
(
const
bool
value
)
{
getInstance
().
shared_memory
=
value
;
}
static
void
sharedMemory
(
const
bool
value
);
static
bool
sharedMemory
()
{
return
getInstance
().
shared_memory
;
}
static
bool
sharedMemory
();
static
void
switchToNumpyArray
()
{
getInstance
().
CurrentNumpyType
=
getInstance
().
NumpyArrayObject
;
getInstance
().
getType
()
=
ARRAY_TYPE
;
}
static
void
switchToNumpyArray
();
static
void
switchToNumpyMatrix
()
{
getInstance
().
CurrentNumpyType
=
getInstance
().
NumpyMatrixObject
;
getInstance
().
getType
()
=
MATRIX_TYPE
;
}
static
void
switchToNumpyMatrix
();
static
NP_TYPE
&
getType
()
{
return
getInstance
().
np_type
;
}
static
NP_TYPE
&
getType
();
static
bp
::
object
getNumpyType
()
{
return
getInstance
().
CurrentNumpyType
;
}
static
bp
::
object
getNumpyType
();
static
const
PyTypeObject
*
getNumpyMatrixType
()
{
return
getInstance
().
NumpyMatrixType
;
}
static
const
PyTypeObject
*
getNumpyMatrixType
();
static
const
PyTypeObject
*
getNumpyArrayType
()
{
return
getInstance
().
NumpyArrayType
;
}
static
const
PyTypeObject
*
getNumpyArrayType
();
static
bool
isMatrix
()
{
return
PyType_IsSubtype
(
reinterpret_cast
<
PyTypeObject
*>
(
getInstance
().
CurrentNumpyType
.
ptr
()),
getInstance
().
NumpyMatrixType
);
}
static
bool
isMatrix
();
static
bool
isArray
()
{
if
(
getInstance
().
isMatrix
())
return
false
;
return
PyType_IsSubtype
(
reinterpret_cast
<
PyTypeObject
*>
(
getInstance
().
CurrentNumpyType
.
ptr
()),
getInstance
().
NumpyArrayType
);
}
static
bool
isArray
();
protected
:
NumpyType
()
{
pyModule
=
bp
::
import
(
"numpy"
);
#if PY_MAJOR_VERSION >= 3
// TODO I don't know why this Py_INCREF is necessary.
// Without it, the destructor of NumpyType SEGV sometimes.
Py_INCREF
(
pyModule
.
ptr
());
#endif
NumpyMatrixObject
=
pyModule
.
attr
(
"matrix"
);
NumpyMatrixType
=
reinterpret_cast
<
PyTypeObject
*>
(
NumpyMatrixObject
.
ptr
());
NumpyArrayObject
=
pyModule
.
attr
(
"ndarray"
);
NumpyArrayType
=
reinterpret_cast
<
PyTypeObject
*>
(
NumpyArrayObject
.
ptr
());
//NumpyAsMatrixObject = pyModule.attr("asmatrix");
//NumpyAsMatrixType = reinterpret_cast<PyTypeObject*>(NumpyAsMatrixObject.ptr());
CurrentNumpyType
=
NumpyArrayObject
;
// default conversion
np_type
=
ARRAY_TYPE
;
shared_memory
=
true
;
}
NumpyType
();
bp
::
object
CurrentNumpyType
;
bp
::
object
pyModule
;
...
...
This diff is collapsed.
Click to expand it.
src/numpy-type.cpp
0 → 100644
+
121
−
0
View file @
dda4980a
/*
* Copyright 2018-2020 INRIA
*/
#include
"eigenpy/numpy-type.hpp"
#include
<patchlevel.h>
// For PY_MAJOR_VERSION
namespace
eigenpy
{
namespace
bp
=
boost
::
python
;
NumpyType
&
NumpyType
::
getInstance
()
{
static
NumpyType
instance
;
return
instance
;
}
bp
::
object
NumpyType
::
make
(
PyArrayObject
*
pyArray
,
bool
copy
)
{
return
make
((
PyObject
*
)
pyArray
,
copy
);
}
bp
::
object
NumpyType
::
make
(
PyObject
*
pyObj
,
bool
copy
)
{
bp
::
object
m
;
if
(
isMatrix
())
m
=
getInstance
().
NumpyMatrixObject
(
bp
::
object
(
bp
::
handle
<>
(
pyObj
)),
bp
::
object
(),
copy
);
// m = NumpyAsMatrixObject(bp::object(bp::handle<>(pyObj)));
else
if
(
isArray
())
m
=
bp
::
object
(
bp
::
handle
<>
(
pyObj
));
// nothing to do here
Py_INCREF
(
m
.
ptr
());
return
m
;
}
void
NumpyType
::
setNumpyType
(
bp
::
object
&
obj
)
{
PyTypeObject
*
obj_type
=
PyType_Check
(
obj
.
ptr
())
?
reinterpret_cast
<
PyTypeObject
*>
(
obj
.
ptr
())
:
obj
.
ptr
()
->
ob_type
;
if
(
PyType_IsSubtype
(
obj_type
,
getInstance
().
NumpyMatrixType
))
switchToNumpyMatrix
();
else
if
(
PyType_IsSubtype
(
obj_type
,
getInstance
().
NumpyArrayType
))
switchToNumpyArray
();
}
void
NumpyType
::
sharedMemory
(
const
bool
value
)
{
getInstance
().
shared_memory
=
value
;
}
bool
NumpyType
::
sharedMemory
()
{
return
getInstance
().
shared_memory
;
}
void
NumpyType
::
switchToNumpyArray
()
{
getInstance
().
CurrentNumpyType
=
getInstance
().
NumpyArrayObject
;
getInstance
().
getType
()
=
ARRAY_TYPE
;
}
void
NumpyType
::
switchToNumpyMatrix
()
{
getInstance
().
CurrentNumpyType
=
getInstance
().
NumpyMatrixObject
;
getInstance
().
getType
()
=
MATRIX_TYPE
;
}
NP_TYPE
&
NumpyType
::
getType
()
{
return
getInstance
().
np_type
;
}
bp
::
object
NumpyType
::
getNumpyType
()
{
return
getInstance
().
CurrentNumpyType
;
}
const
PyTypeObject
*
NumpyType
::
getNumpyMatrixType
()
{
return
getInstance
().
NumpyMatrixType
;
}
const
PyTypeObject
*
NumpyType
::
getNumpyArrayType
()
{
return
getInstance
().
NumpyArrayType
;
}
bool
NumpyType
::
isMatrix
()
{
return
PyType_IsSubtype
(
reinterpret_cast
<
PyTypeObject
*>
(
getInstance
().
CurrentNumpyType
.
ptr
()),
getInstance
().
NumpyMatrixType
);
}
bool
NumpyType
::
isArray
()
{
if
(
getInstance
().
isMatrix
())
return
false
;
return
PyType_IsSubtype
(
reinterpret_cast
<
PyTypeObject
*>
(
getInstance
().
CurrentNumpyType
.
ptr
()),
getInstance
().
NumpyArrayType
);
}
NumpyType
::
NumpyType
()
{
pyModule
=
bp
::
import
(
"numpy"
);
#if PY_MAJOR_VERSION >= 3
// TODO I don't know why this Py_INCREF is necessary.
// Without it, the destructor of NumpyType SEGV sometimes.
Py_INCREF
(
pyModule
.
ptr
());
#endif
NumpyMatrixObject
=
pyModule
.
attr
(
"matrix"
);
NumpyMatrixType
=
reinterpret_cast
<
PyTypeObject
*>
(
NumpyMatrixObject
.
ptr
());
NumpyArrayObject
=
pyModule
.
attr
(
"ndarray"
);
NumpyArrayType
=
reinterpret_cast
<
PyTypeObject
*>
(
NumpyArrayObject
.
ptr
());
//NumpyAsMatrixObject = pyModule.attr("asmatrix");
//NumpyAsMatrixType = reinterpret_cast<PyTypeObject*>(NumpyAsMatrixObject.ptr());
CurrentNumpyType
=
NumpyArrayObject
;
// default conversion
np_type
=
ARRAY_TYPE
;
shared_memory
=
true
;
}
}
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