Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
6bb0ee11
Commit
6bb0ee11
authored
Jul 24, 2016
by
Ivan Smirnov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add all possible ctors for py::array
parent
d77bc8c3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
17 deletions
+45
-17
include/pybind11/numpy.h
+45
-17
No files found.
include/pybind11/numpy.h
View file @
6bb0ee11
...
...
@@ -211,12 +211,16 @@ public:
forcecast
=
detail
::
npy_api
::
NPY_ARRAY_FORCECAST_
};
template
<
typename
Type
>
array
(
size_t
size
,
const
Type
*
ptr
)
{
array
(
const
pybind11
::
dtype
&
dt
,
const
std
::
vector
<
size_t
>&
shape
,
void
*
ptr
,
const
std
::
vector
<
size_t
>&
strides
)
{
auto
&
api
=
detail
::
npy_api
::
get
();
auto
descr
=
pybind11
::
dtype
::
of
<
Type
>
().
release
().
ptr
();
Py_intptr_t
shape
=
(
Py_intptr_t
)
size
;
object
tmp
=
object
(
api
.
PyArray_NewFromDescr_
(
api
.
PyArray_Type_
,
descr
,
1
,
&
shape
,
nullptr
,
(
void
*
)
ptr
,
0
,
nullptr
),
false
);
auto
ndim
=
shape
.
size
();
if
(
shape
.
size
()
!=
strides
.
size
())
pybind11_fail
(
"NumPy: shape ndim doesn't match strides ndim"
);
auto
descr
=
dt
;
object
tmp
(
api
.
PyArray_NewFromDescr_
(
api
.
PyArray_Type_
,
descr
.
release
().
ptr
(),
(
int
)
ndim
,
(
Py_intptr_t
*
)
shape
.
data
(),
(
Py_intptr_t
*
)
strides
.
data
(),
ptr
,
0
,
nullptr
),
false
);
if
(
!
tmp
)
pybind11_fail
(
"NumPy: unable to create array!"
);
if
(
ptr
)
...
...
@@ -224,18 +228,30 @@ public:
m_ptr
=
tmp
.
release
().
ptr
();
}
array
(
const
buffer_info
&
info
)
{
auto
&
api
=
detail
::
npy_api
::
get
();
auto
descr
=
pybind11
::
dtype
(
info
).
release
().
ptr
();
object
tmp
(
api
.
PyArray_NewFromDescr_
(
api
.
PyArray_Type_
,
descr
,
(
int
)
info
.
ndim
,
(
Py_intptr_t
*
)
&
info
.
shape
[
0
],
(
Py_intptr_t
*
)
&
info
.
strides
[
0
],
info
.
ptr
,
0
,
nullptr
),
false
);
if
(
!
tmp
)
pybind11_fail
(
"NumPy: unable to create array!"
);
if
(
info
.
ptr
)
tmp
=
object
(
api
.
PyArray_NewCopy_
(
tmp
.
ptr
(),
-
1
/* any order */
),
false
);
m_ptr
=
tmp
.
release
().
ptr
();
}
array
(
const
pybind11
::
dtype
&
dt
,
const
std
::
vector
<
size_t
>&
shape
,
void
*
ptr
)
:
array
(
dt
,
shape
,
ptr
,
default_strides
(
shape
,
dt
.
itemsize
()))
{
}
array
(
const
pybind11
::
dtype
&
dt
,
size_t
size
,
void
*
ptr
)
:
array
(
dt
,
std
::
vector
<
size_t
>
{
size
},
ptr
)
{
}
template
<
typename
T
>
array
(
const
std
::
vector
<
size_t
>&
shape
,
T
*
ptr
,
const
std
::
vector
<
size_t
>&
strides
)
:
array
(
pybind11
::
dtype
::
of
<
T
>
(),
shape
,
(
void
*
)
ptr
,
strides
)
{
}
template
<
typename
T
>
array
(
const
std
::
vector
<
size_t
>&
shape
,
T
*
ptr
)
:
array
(
shape
,
ptr
,
default_strides
(
shape
,
sizeof
(
T
)))
{
}
template
<
typename
T
>
array
(
size_t
size
,
T
*
ptr
)
:
array
(
std
::
vector
<
size_t
>
{
size
},
ptr
)
{
}
array
(
const
buffer_info
&
info
)
:
array
(
pybind11
::
dtype
(
info
),
info
.
shape
,
info
.
ptr
,
info
.
strides
)
{
}
pybind11
::
dtype
dtype
()
{
return
attr
(
"dtype"
).
cast
<
pybind11
::
dtype
>
();
...
...
@@ -243,6 +259,18 @@ public:
protected
:
template
<
typename
T
,
typename
SFINAE
>
friend
struct
detail
::
npy_format_descriptor
;
static
std
::
vector
<
size_t
>
default_strides
(
const
std
::
vector
<
size_t
>&
shape
,
size_t
itemsize
)
{
auto
ndim
=
shape
.
size
();
std
::
vector
<
size_t
>
strides
(
ndim
);
if
(
ndim
)
{
std
::
fill
(
strides
.
begin
(),
strides
.
end
(),
itemsize
);
for
(
size_t
i
=
0
;
i
<
ndim
-
1
;
i
++
)
for
(
size_t
j
=
0
;
j
<
ndim
-
1
-
i
;
j
++
)
strides
[
j
]
*=
shape
[
ndim
-
1
-
i
];
}
return
strides
;
}
};
template
<
typename
T
,
int
ExtraFlags
=
array
::
forcecast
>
class
array_t
:
public
array
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment