Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
a9fd939c
Commit
a9fd939c
authored
Aug 09, 2013
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added another matrix constructor. Now we can construct from python lists.
parent
bbf9863b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
2 deletions
+35
-2
matrix.cpp
tools/python/src/matrix.cpp
+35
-2
No files found.
tools/python/src/matrix.cpp
View file @
a9fd939c
...
...
@@ -7,7 +7,7 @@
#include <dlib/string.h>
#include "serialize_pickle.h"
#include <boost/python/args.hpp>
#include "pyassert.h"
using
namespace
dlib
;
using
namespace
boost
::
python
;
...
...
@@ -73,12 +73,44 @@ boost::shared_ptr<matrix<double> > from_object(object obj)
return
temp
;
}
boost
::
shared_ptr
<
matrix
<
double
>
>
from_list
(
list
l
)
{
const
long
nr
=
len
(
l
);
if
(
extract
<
list
>
(
l
[
0
]).
check
())
{
const
long
nc
=
len
(
l
[
0
]);
// make sure all the other rows have the same length
for
(
long
r
=
1
;
r
<
nr
;
++
r
)
pyassert
(
len
(
l
[
r
])
==
nc
,
"All rows of a matrix must have the same number of columns."
);
boost
::
shared_ptr
<
matrix
<
double
>
>
temp
(
new
matrix
<
double
>
(
nr
,
nc
));
for
(
long
r
=
0
;
r
<
nr
;
++
r
)
{
for
(
long
c
=
0
;
c
<
nc
;
++
c
)
{
(
*
temp
)(
r
,
c
)
=
extract
<
double
>
(
l
[
r
][
c
]);
}
}
return
temp
;
}
else
{
// In this case we treat it like a column vector
boost
::
shared_ptr
<
matrix
<
double
>
>
temp
(
new
matrix
<
double
>
(
nr
,
1
));
for
(
long
r
=
0
;
r
<
nr
;
++
r
)
{
(
*
temp
)(
r
)
=
extract
<
double
>
(
l
[
r
]);
}
return
temp
;
}
}
long
matrix_double__len__
(
matrix
<
double
>&
c
)
{
return
c
.
nr
();
}
struct
mat_row
{
mat_row
()
:
data
(
0
),
size
(
0
)
{}
...
...
@@ -165,6 +197,7 @@ void bind_matrix()
.
def
(
"__init__"
,
make_constructor
(
&
make_matrix_from_size
))
.
def
(
"set_size"
,
&
matrix_set_size
,
(
arg
(
"rows"
),
arg
(
"cols"
)),
"Set the size of the matrix to the given number of rows and columns."
)
.
def
(
"__init__"
,
make_constructor
(
&
from_object
))
.
def
(
"__init__"
,
make_constructor
(
&
from_list
))
.
def
(
"__repr__"
,
&
matrix_double__repr__
)
.
def
(
"__str__"
,
&
matrix_double__str__
)
.
def
(
"nr"
,
&
matrix
<
double
>::
nr
,
"Return the number of rows in the matrix."
)
...
...
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