Skip to content

Commit

Permalink
cleaned up Matrix<>::__setitem__
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeandersson committed May 24, 2011
1 parent ecc07b6 commit f18e0cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 61 deletions.
23 changes: 15 additions & 8 deletions casadi/matrix/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,16 @@ class Matrix : public PrintableObject{
const Matrix<T> __getitem__(const Slice& i, const Slice &j) const;

/// Python: set a non-zero entry
void setitem(int k, const T& el);
void __setitem__(int k, const T& el);

/// Python: set several non-zero entries
void __setitem__(const Slice& k, const Matrix<T>& m);

/// Python: set a matrix entry
void setitem(const std::pair<int,int> &ij, const T& el);
void __setitem__(const std::pair<int,int> &ij, const T& el);

/// Python: set a submatrix
void setitem(const std::vector< std::vector<int> > &II, const Matrix<T>& m);
void __setitem__(const Slice& i, const Slice &j, const Matrix<T>& m);

/// Set all elements to zero
void setZero();
Expand Down Expand Up @@ -1063,7 +1066,7 @@ const Matrix<T> Matrix<T>::__getitem__(const Slice& kk) const{
}

template<class T>
void Matrix<T>::setitem(int k, const T& el){
void Matrix<T>::__setitem__(int k, const T& el){
data().at(k) = el;
}

Expand All @@ -1078,14 +1081,18 @@ void Matrix<T>::setAll(const T& val){
}

template<class T>
void Matrix<T>::setitem(const std::pair<int,int> &ij, const T& el){
void Matrix<T>::__setitem__(const Slice& k, const Matrix<T>& m){
(*this)[k.getAll(size())] = m;
}

template<class T>
void Matrix<T>::__setitem__(const std::pair<int,int> &ij, const T& el){
getElementRef(ij.first,ij.second) = el;
}

template<class T>
void Matrix<T>::setitem(const std::vector< std::vector<int> > &II, const Matrix<T>& m){
casadi_assert_message(II.size()==2,"Index vector must be two-dimensional");
setSub(II[0],II[1],m);
void Matrix<T>::__setitem__(const Slice& i, const Slice &j, const Matrix<T>& m){
setSub(i.getAll(size1()),j.getAll(size2()),m);
}

template<class T>
Expand Down
54 changes: 1 addition & 53 deletions swig/sx.i
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,7 @@ except:
%}

namespace CasADi {
%extend Matrix<double> {

%pythoncode %{
def __setitem__(self,s,val):
if isinstance(s,int):
self.setitem(s,val)
return
elif isinstance(s,tuple):
if len(s)!=2:
raise Exception("get/setitem can only do 1D or 2D indexing")
if isinstance(s[0],int) and isinstance(s[1],int):
self.setitem(s,val)
return
else:
s = list(s)
for k in range(2):
if isinstance(s[k],slice):
J = s[k].indices(self.shape[k])
s[k] = range(J[0],J[1],J[2])
elif isinstance(s[k],int):
s[k] = [s[k]]
else:
raise Exception("get/setitem expecting a tuple or int. Got %s of type %s" % (str(s),str(type(s))))
self.setitem(s,val)
%}

};


%extend SX {
%pythoncode %{
def __lt__(self,other):
Expand All @@ -94,7 +67,6 @@ namespace CasADi {
// The constructor has to be added since SX::operator Matrix<SX does not work
// Matrix<SX>(const SX&){ *$self


%pythoncode %{
def __lt__(self,other):
return _casadi.__lt__(self,other)
Expand Down Expand Up @@ -185,30 +157,6 @@ namespace CasADi {
import numpy as n
return n.matrix(self.toArray())
%}

%pythoncode %{
def __setitem__(self,s,val):
if isinstance(s,int):
self.setitem(s,val)
return
elif isinstance(s,tuple):
if len(s)!=2:
raise Exception("get/setitem can only do 1D or 2D indexing")
if isinstance(s[0],int) and isinstance(s[1],int):
self.setitem(s,val)
return
else:
s = list(s)
for k in range(2):
if isinstance(s[k],slice):
J = s[k].indices(self.shape[k])
s[k] = range(J[0],J[1],J[2])
elif isinstance(s[k],int):
s[k] = [s[k]]
else:
raise Exception("get/setitem expecting a tuple or int. Got %s of type %s" % (str(s),str(type(s))))
self.setitem(s,val)
%}
};
} // namespace CasADi

Expand Down

0 comments on commit f18e0cb

Please sign in to comment.