Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
a9fd939c
Commit
a9fd939c
authored
Aug 09, 2013
by
Davis King
Browse files
Added another matrix constructor. Now we can construct from python lists.
parent
bbf9863b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
2 deletions
+35
-2
tools/python/src/matrix.cpp
tools/python/src/matrix.cpp
+35
-2
No files found.
tools/python/src/matrix.cpp
View file @
a9fd939c
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
#include <dlib/string.h>
#include <dlib/string.h>
#include "serialize_pickle.h"
#include "serialize_pickle.h"
#include <boost/python/args.hpp>
#include <boost/python/args.hpp>
#include "pyassert.h"
using
namespace
dlib
;
using
namespace
dlib
;
using
namespace
boost
::
python
;
using
namespace
boost
::
python
;
...
@@ -73,12 +73,44 @@ boost::shared_ptr<matrix<double> > from_object(object obj)
...
@@ -73,12 +73,44 @@ boost::shared_ptr<matrix<double> > from_object(object obj)
return
temp
;
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
)
long
matrix_double__len__
(
matrix
<
double
>&
c
)
{
{
return
c
.
nr
();
return
c
.
nr
();
}
}
struct
mat_row
struct
mat_row
{
{
mat_row
()
:
data
(
0
),
size
(
0
)
{}
mat_row
()
:
data
(
0
),
size
(
0
)
{}
...
@@ -165,6 +197,7 @@ void bind_matrix()
...
@@ -165,6 +197,7 @@ void bind_matrix()
.
def
(
"__init__"
,
make_constructor
(
&
make_matrix_from_size
))
.
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
(
"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_object
))
.
def
(
"__init__"
,
make_constructor
(
&
from_list
))
.
def
(
"__repr__"
,
&
matrix_double__repr__
)
.
def
(
"__repr__"
,
&
matrix_double__repr__
)
.
def
(
"__str__"
,
&
matrix_double__str__
)
.
def
(
"__str__"
,
&
matrix_double__str__
)
.
def
(
"nr"
,
&
matrix
<
double
>::
nr
,
"Return the number of rows in the matrix."
)
.
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