Matrix nn_addon::GetNormalizedData(Matrix& data) { Matrix res; //Matrix ldata; switch(opt_.normType) { default: case UnchangedData: res = data; break; case LinearNorm: res = (data - state_.norm_min)*(opt_.maxtar - opt_.mintar)/(state_.norm_max - state_.norm_min) + opt_.mintar; break; case LogNorm: res = data; transform(res.begin(), res.end(), res.begin(), ptr_fun<double, double>(std::log)); res = (res - state_.lnorm_min)*(opt_.maxtar - opt_.mintar)/(state_.lnorm_max - state_.lnorm_min) + opt_.mintar; break; case LogsigNorm: res.NewMatrix(data.row_num(), data.col_num()); Matrix::r_iterator p_data(data.begin()); for(Matrix::r_iterator p_res = res.begin(); p_res != res.end(); ++p_res) { *p_res = 1.0/(1.0 + exp(-state_.a * (*p_data))); //*p_res = (*p_res - _state.lsnorm_min)*(opt_.maxtar - opt_.mintar)/ // (_state.lsnorm_max - _state.lsnorm_min) + opt_.mintar; ++p_data; } } return res; }
///Elementwise multiplication void dot(Matrix a, Matrix b, Matrix out) { ASSERT(a.size == out.size); ASSERT(a.size >= b.size); ASSERT(a.size % b.size == 0); ASSERT(a.state == NORMAL && b.state == NORMAL && out.state == NORMAL); if (a.size == b.size && a.stride == 0 && b.stride == 0 && out.stride == 0) { int n = static_cast<int>(a.size); cblas_dgbmv(CblasColMajor, CblasNoTrans, n, n, 0, 0, 1.0, a.get_data(), 1, b.get_data(), 1, 0.0, out.get_data(), 1); } else { fill(out.begin(), out.end(), 0.0); auto a_end = a.end(); auto b_end = b.end(); for (auto ita=a.begin(), itb=b.begin(), ito=out.begin(); ita != a_end; ++ita, ++itb, ++ito) { if (itb == b_end) itb = b.begin(); *ito += *ita * *itb; } } }
void squash_add(Matrix a, Matrix out) { auto ito_end = out.end(); for (auto ita = a.begin(), ito = out.begin(); ita != a.end(); ++ita, ++ito) { if (ito == ito_end) ito = out.begin(); *ito += *ita; } }
void squash(Matrix a, Matrix out, d_type scale) { out.set_all_elements_to(0.0); auto ito_end = out.end(); for (auto ita = a.begin(), ito = out.begin(); ita != a.end(); ++ita, ++ito) { if (ito == ito_end) ito = out.begin(); *ito += (*ita) * scale; } }
Matrix MatrixOps::augment(const Matrix& A, const Matrix& B, size_t r0, size_t c0, bool symmetric) { std::cout << "Augmenting matrix of dimension " << A.N() << "x" << A.M() << " with matrix of dimension " << B.N() << "x" << B.M() << std::endl; size_t nrow = A.N(); size_t ncol = A.M(); if (r0+B.N() > nrow) nrow = r0+B.N(); if (symmetric && r0+B.N() > ncol) ncol = r0+B.N(); if (c0+B.M() > ncol) ncol = c0+B.M(); if (symmetric && c0+B.M() > nrow) nrow = c0+B.M(); std::cout << "Resulting size: " << nrow << "x" << ncol << std::endl; AdjacencyPattern adj; adj.resize(nrow); for (Matrix::ConstIterator it = A.begin(); it != A.end();++it) { for (Matrix::ConstColIterator it2 = it->begin(); it2 != it->end();++it2) { adj[it.index()].insert(it2.index()); } } for (Matrix::ConstIterator it = B.begin(); it != B.end();++it) { for (Matrix::ConstColIterator it2 = it->begin(); it2 != it->end();++it2) { adj[it.index()+r0].insert(it2.index()+c0); if (symmetric) adj[it2.index()+c0].insert(it.index()+r0); } } if (symmetric) { // always establish diagonal elements or superLU crashes for (size_t i=0;i<nrow;++i) adj[i].insert(i); } Matrix result; fromAdjacency(result,adj,nrow,ncol); for (Matrix::ConstIterator it = A.begin(); it != A.end();++it) { for (Matrix::ConstColIterator it2 = it->begin(); it2 != it->end();++it2) { result[it.index()][it2.index()] = *it2; } } for (Matrix::ConstIterator it = B.begin(); it != B.end();++it) { for (Matrix::ConstColIterator it2 = it->begin(); it2 != it->end();++it2) { result[it.index()+r0][it2.index()+c0] = *it2; if (symmetric) result[it2.index()+c0][it.index()+r0] = *it2; } } return result; }
void add_vector_into(Matrix vec, Matrix mat) { auto it_v_end = vec.end(); for (auto it = mat.begin(), it_v = vec.begin(); it != mat.end(); ++it, ++it_v) { if (it_v == it_v_end) { it_v = vec.begin(); } *it += *it_v; } }
///Elementwise multiplication, with squash to size of out (out is smaller than a and b) void dot_squash(Matrix a, Matrix b, Matrix out, d_type scale) { ASSERT(a.size == b.size); ASSERT(a.size % out.size == 0); ASSERT(a.state == b.state && b.state == out.state); out.set_all_elements_to(0.0); auto ito_end = out.end(); for (auto ita = a.begin(), itb = b.begin(), ito = out.begin(); ita != a.end(); ++ita, ++itb, ++ito) { if (ito == ito_end) { ito = out.begin(); } *ito += (*ita) * (*itb) * scale; } }
SEXP ToRMatrix(const Matrix &m, const std::vector<std::string> &rownames, const std::vector<std::string> &colnames){ if (!rownames.empty() && rownames.size() != m.nrow()) { report_error("In ToRMatrix: Vector of row names does not match " "the number of rows in m."); } else if (!colnames.empty() && colnames.size() != m.ncol()) { report_error("In ToRMatrix: Vector of column names does not match " "the number of columns in m."); } SEXP ans; PROTECT(ans = Rf_allocMatrix(REALSXP, m.nrow(), m.ncol())); double *data = REAL(ans); std::copy(m.begin(), m.end(), data); SEXP r_dimnames; PROTECT(r_dimnames = Rf_allocVector(VECSXP, 2)); SET_VECTOR_ELT( r_dimnames, 0, rownames.empty() ? R_NilValue : CharacterVector(rownames)); SET_VECTOR_ELT( r_dimnames, 1, colnames.empty() ? R_NilValue : CharacterVector(colnames)); Rf_dimnamesgets(ans, r_dimnames); UNPROTECT(2); return ans; }
void dot_into_b(Matrix a, Matrix b) { ASSERT(a.size == b.size); auto enda = a.end(); for (auto ita = a.begin(), itb = b.begin(); ita != enda; ++ita, ++itb) { *itb *= *ita; } }
/** * * @param matrix * Largest product in row using rolling multiplication * @param n number of factors * @param v factors of largest product * @return largest product */ int largestProductInRow( Matrix& matrix, int n, vector< int>& v) { if ( n > matrix.size()) return -1; int res = 0; int N = matrix.size() - n + 1; // number of products in row (or column) /* search in rows */ outIt it = matrix.begin(); while (it != matrix.end()) { inIt it2 = (*it).begin(); int currN = N; int product = 1; while (currN) { // rolling product calculation inIt it3 = it2; int currn = n; if (currN == N) { // compute full product first time while (currn) { product *= (*it3++); --currn; } } else { // rolling computation product *= (*(it3 + n - 1)) / (*(it3 - 1)); it3 += n; } if (product > res) { res = product; copy(it3 - n, it3, v.begin()); } --currN; ++it2; } ++it; } return res; }
Disposable<Matrix> inverse(const Matrix& m) { #if !defined(QL_NO_UBLAS_SUPPORT) QL_REQUIRE(m.rows() == m.columns(), "matrix is not square"); boost::numeric::ublas::matrix<Real> a(m.rows(), m.columns()); std::copy(m.begin(), m.end(), a.data().begin()); boost::numeric::ublas::permutation_matrix<Size> pert(m.rows()); // lu decomposition const Size singular = lu_factorize(a, pert); QL_REQUIRE(singular == 0, "singular matrix given"); boost::numeric::ublas::matrix<Real> inverse = boost::numeric::ublas::identity_matrix<Real>(m.rows()); // backsubstitution boost::numeric::ublas::lu_substitute(a, pert, inverse); Matrix retVal(m.rows(), m.columns()); std::copy(inverse.data().begin(), inverse.data().end(), retVal.begin()); return retVal; #else QL_FAIL("this version of gcc does not support " "the Boost uBLAS library"); #endif }
Real determinant(const Matrix& m) { #if !defined(QL_NO_UBLAS_SUPPORT) QL_REQUIRE(m.rows() == m.columns(), "matrix is not square"); boost::numeric::ublas::matrix<Real> a(m.rows(), m.columns()); std::copy(m.begin(), m.end(), a.data().begin()); // lu decomposition boost::numeric::ublas::permutation_matrix<Size> pert(m.rows()); /* const Size singular = */ lu_factorize(a, pert); Real retVal = 1.0; for (Size i=0; i < m.rows(); ++i) { if (pert[i] != i) retVal *= -a(i,i); else retVal *= a(i,i); } return retVal; #else QL_FAIL("this version of gcc does not support " "the Boost uBLAS library"); #endif }
void MatrixOps::saveAsc(const Matrix& A, const std::string& file) { std::ofstream f; f.open(file.c_str()); f << "% " << A.N() << " " << A.M() << std::endl; int prevrow=-1; for (Matrix::ConstIterator it = A.begin(); it != A.end(); ++it) { for (int i=0;i<int(it.index())-prevrow-1;++i) { for (size_t j=0;j<A.M();++j) f << "0 "; f << std::endl; } int prevcol=-1; for (Matrix::ConstColIterator it2 = it->begin(); it2 != it->end();++it2) { for (int j=0;j<int(it2.index())-prevcol-1;++j) f << "0 "; double val = *it2; f << val << " "; prevcol = it2.index(); } for (int j=0;j<int(A.M())-prevcol-1;++j) f << "0 "; prevrow = it.index(); f << std::endl; } f.close(); }
void slack_matrix::init_geqz_constraints(Matrix& m) { geqz_constraints = new bool[m.num_vars()]; memset(geqz_constraints, 0, m.num_vars()*sizeof(bool)); Matrix::iterator it = m.begin(); for(; it!= m.end(); it++) { bignum constant = it->second; if(constant > 0) continue; int index = -1; Equation* eq = it->first; for(int c=0; c<eq->num_entries(); c++) { bignum e = eq->get(c); if(e > 0){ index = -1; break; } else if(e<0 && index != -1) { index = -1; break; } else if(e<0) index = c; } // Mark entry as 1 if var has non-negativity constraint if(index != -1) geqz_constraints[index] = true; } }
void debugPrint(const Matrix& M) { for (Matrix::const_iterator i = M.begin(); i != M.end(); ++i) { debugPrint(*i); } }
Matrix nn_addon::GetRealData(const Matrix& ndata) { Matrix res; //Matrix ldata; switch(opt_.normType) { default: case UnchangedData: res = ndata; break; case LinearNorm: res = (ndata - opt_.mintar)*(state_.norm_max - state_.norm_min)/(opt_.maxtar - opt_.mintar) + state_.norm_min; break; case LogNorm: res = (ndata - opt_.mintar)*(state_.lnorm_max - state_.lnorm_min)/(opt_.maxtar - opt_.mintar) + state_.lnorm_min; for(ulong i=0; i<res.size(); ++i) res[i] = pow(10.0, res[i]); //for_each(res.begin(), res.end(), bind1st(pow, 10.0)); break; case LogsigNorm: //er = -log(1/x - 1)/a; res.NewMatrix(ndata.row_num(), ndata.col_num()); Matrix::cr_iterator p_ndata(ndata.begin()); for(Matrix::r_iterator p_res = res.begin(); p_res != res.end(); ++p_res) { //*p_res = (*p_ndata - opt_.mintar)*(_state.lsnorm_max - _state.lsnorm_min)/ // (opt_.maxtar - opt_.mintar) + _state.lsnorm_min; *p_res = -log(1/(*p_res) - 1.)/state_.a; ++p_ndata; } break; } return res; }
SEXP ToRMatrix(const Matrix &m){ SEXP ans; PROTECT(ans = Rf_allocMatrix(REALSXP, m.nrow(), m.ncol())); double *data = REAL(ans); std::copy(m.begin(), m.end(), data); UNPROTECT(1); return ans; }
inline Matrix::Matrix(const Matrix& from) : data_(!from.empty() ? new Real[from.rows_*from.columns_] : (Real*)(0)), rows_(from.rows_), columns_(from.columns_) { #if defined(QL_PATCH_MSVC) && defined(QL_DEBUG) if (!from.empty()) #endif std::copy(from.begin(),from.end(),begin()); }
void add_scalar(Matrix a, d_type b) { if (a.stride == 0) { cblas_daxpy(static_cast<int>(a.size), 1.0, &b, 0, a.get_data(), 1); } else { for (auto it = a.begin(); it != a.end(); ++it) { *it += b; } } }
void clip_elements(Matrix a, const d_type min, const d_type max) { for (auto it = a.begin(); it != a.end(); ++it) { if (*it < min) { *it = min; } else if (*it > max) { *it = max; } } }
///Copy the data of one matrix into another void copy(Matrix a, Matrix b) { ASSERT(a.size <= b.size); ASSERT(!(a.overlaps_with(b))); if (a.stride == 0 && b.stride == 0 && a.state == b.state) { cblas_dcopy(static_cast<int>(a.size), a.get_data(), 1, b.get_data(), 1); } else { for (auto ita = a.begin(), itb = b.begin(); ita != a.end(); ++ita, ++itb) { *itb = *ita; } } }
///Elementwise subtract: out = a - b void subtract(Matrix a, Matrix b, Matrix out) { ASSERT(a.size == b.size); ASSERT(a.size == out.size); Matrix::iterator ita = a.begin(); Matrix::iterator ita_end = a.end(); Matrix::iterator itb = b.begin(); Matrix::iterator itout = out.begin(); for (; ita != ita_end; ++ita, ++itb, ++itout) { *itout = *ita - *itb; } }
bool equals(Matrix a, Matrix b) { if (a.n_rows != b.n_rows || a.n_columns != b.n_columns || a.n_slices != b.n_slices) { return false; } for (auto ita = a.begin(), itb = b.begin(); ita != a.end(); ++ita, ++itb) { if (*ita != *itb) { return false; } } return true; }
void printMatrix( Matrix& matrix) { outIt it = matrix.begin(); while ( it != matrix.end()) { inIt it2 = (*it).begin(); while ( it2 != (*it).end()) { printf( "%d", *it2); ++it2; } printf( "\n"); ++it; } }
void add_into_b(Matrix a, Matrix b) { if (a.state == b.state && a.stride == 0 && b.stride == 0) { cblas_daxpy(static_cast<int>(a.size), 1.0, a.get_data(), 1, b.get_data(), 1); } else { Matrix::iterator ita = a.begin(); Matrix::iterator ita_end = a.end(); Matrix::iterator itb = b.begin(); for (; ita != ita_end; ++ita, ++itb) { *itb += *ita; } } }
inline const Disposable<Matrix> operator-(const Matrix& m1, const Matrix& m2) { QL_REQUIRE(m1.rows() == m2.rows() && m1.columns() == m2.columns(), "matrices with different sizes (" << m1.rows() << "x" << m1.columns() << ", " << m2.rows() << "x" << m2.columns() << ") cannot be " "subtracted"); Matrix temp(m1.rows(),m1.columns()); std::transform(m1.begin(),m1.end(),m2.begin(),temp.begin(), std::minus<Real>()); return temp; }
void MatrixOps::print(const Matrix& A) { for (Matrix::ConstIterator it = A.begin(); it != A.end(); ++it) { for (Matrix::ConstColIterator it2 = it->begin(); it2 != it->end();++it2) { double val = *it2; if (fabs(val) < 1.e-14) continue; std::cout << it.index() << " " << it2.index() << " : " << val << std::endl; } } }
bool inverse( Matrix &_matrix ) { typedef typename scalar_type< typename scalar_type< Matrix >::type >::type MatrixElement; int max_row_index = 0; int max_col_index = 0; int element_count; { typename indexed_iterator< Matrix >::type row_iter( _matrix.begin() ); std::advance( row_iter, _matrix.size() - 1 ); element_count = row_iter.getIndex() + 1; } { typename indexed_iterator< Matrix >::type row_iter( _matrix.begin() ); typename indexed_iterator< Matrix >::type row_end( _matrix.end() ); for( ; row_iter != row_end; ++row_iter ) { typename indexed_iterator< typename scalar_type< Matrix >::type >::type col_iter( row_iter->begin() ); std::advance( col_iter, row_iter->size() - 1 ); element_count = std::max( element_count, col_iter.getIndex() + 1 ); } } Vector< std::vector< int > > log( element_count ); { indexed_iterator< std::vector< int > >::type log_iter( log.begin() ); indexed_iterator< std::vector< int > >::type log_end( log.end() ); for( ; log_iter != log_end; ++log_iter ) *log_iter = log_iter.getIndex(); } Matrix lu_matrix = _matrix; if( !lu( lu_matrix, log ) ) return false; for( int index = 0; index != element_count; ++index ) { { for( int row = 0; row != element_count; ++row ) { int pivot = log.getConstValue( row ); MatrixElement sum = ( ( pivot == index ) ? 1 : 0 ); for( int col = 0; col != row; ++col ) sum -= lu_matrix.getConstValue( row ).getConstValue( col ) * _matrix.getConstValue( col ).getConstValue( index ); _matrix.getValue( row ).getValue( index ) = sum; } } { for( int row = element_count - 1; row != -1; row-- ) { MatrixElement sum = _matrix.getConstValue( row ).getConstValue( index ); for( int col = row + 1; col != element_count; col++ ) sum -= lu_matrix.getConstValue( row ).getConstValue( col ) * _matrix.getConstValue( col ).getConstValue( index ); _matrix.getValue( row ).getValue( index ) = sum / lu_matrix.getConstValue( row ).getConstValue( row ); } } } return true; }
Matrix MatrixOps::Axpy(const Matrix& A, const Matrix& B, double alpha) { assert(A.M() == B.M() && A.N() == B.N()); // establish union adjacency pattern AdjacencyPattern adj; adj.resize(A.N()); for (Matrix::ConstIterator it = A.begin(); it != A.end(); ++it) { for (Matrix::ConstColIterator it2 = it->begin(); it2 != it->end();++it2) adj[it.index()].insert(it2.index()); } for (Matrix::ConstIterator it = B.begin(); it != B.end(); ++it) { for (Matrix::ConstColIterator it2 = it->begin(); it2 != it->end();++it2) adj[it.index()].insert(it2.index()); } Matrix result; fromAdjacency(result,adj,A.N(),A.M()); // now insert elements from A for (Matrix::ConstIterator it = A.begin(); it != A.end(); ++it) { for (Matrix::ConstColIterator it2 = it->begin(); it2 != it->end();++it2) result[it.index()][it2.index()] = *it2; } // and subtract elements from B for (Matrix::ConstIterator it = B.begin(); it != B.end(); ++it) { for (Matrix::ConstColIterator it2 = it->begin(); it2 != it->end();++it2) result[it.index()][it2.index()] += alpha*(*it2); } return result; }
bool silvester(Matrix a) { if (a.size() != a[0].size()) return false; for (int i = 0; i < a.size(); ++i) { for (int j = i + 1; j < a.size(); ++j) { if (a[i][j] != a[j][i]) return false; } } while (a.size() > 0) { if (det(a) < 0) return false; a.erase(a.begin() + a.size() - 1); for (auto it = a.begin(); it != a.end(); ++it) {it->erase(it->begin() + it->size() - 1);} } return true; }