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; } }
///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; } } }
Matrix quad(Matrix a) { int n = min(a.size(), a[0].size()); for(int i = n; i < a.size(); ++i) a.erase(a.begin() + i); a = transpose(a); for(int i = n; i < a.size(); ++i) a.erase(a.begin() + i); return transpose(a); }
Matrix MatrixMultiply( Matrix MatrixA,Matrix MatrixB ) { Matrix TempMatrix; Matrix::const_iterator iterA1 = MatrixA.begin(); Matrix::const_iterator iterB1 = MatrixB.begin(); for(int row = 0;row != MatrixA.size();++row) { vector<complex<double> > tempresult; tempresult.clear(); for(int column = 0;column != iterB1->size();++column) { complex<double> tempCom(0); tempresult.push_back(tempCom); } TempMatrix.push_back(tempresult); } for(int row = 0;row != MatrixA.size();++row) { for(int columnB = 0;columnB != iterB1->size();++columnB) { for(int columnA = 0;columnA != iterA1->size();++columnA) { TempMatrix[row][columnB] += MatrixA[row][columnA] * MatrixB[columnA][columnB]; } } } return TempMatrix; }
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; }
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; }
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; } }
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; } }
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; } }
///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; } }
///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; } } }
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; } } }
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 GA::nn_addon::_build_surf(ulong net_ind, const Matrix& input, const Matrix& targets) { /* Matrix x(1, 100), y(100, 1); x = abs(_ga.opt_.initRange(1, 0) - _ga.opt_.initRange(0, 0))/(x.size() - 1); x[0] = 0; x <<= !(!x).CumSum(); x += _ga.opt_.initRange(0, 0); y = abs(_ga.opt_.initRange(1, 1) - _ga.opt_.initRange(0, 1))/(y.size() - 1); y[0] = 0; y <<= y.CumSum(); y += _ga.opt_.initRange(0, 1); Matrix yrow(1, 100); Matrix surf; for(ulong i = 0; i < y.row_num(); ++i) { yrow = y[i]; if(opt_.netType == matrix_nn) surf &= _net.Sim(x & yrow); else surf &= _onet[net_ind]->sim(x & yrow); }l DumpMatrix(x, "x.txt"); DumpMatrix(y, "y.txt"); DumpMatrix(surf, "z.txt"); */ Matrix points(ga_.opt_.initRange.col_num(), 10000); generate(points.begin(), points.end(), prg::rand01); const ulong pnum = points.col_num(); //range matrix Matrix range = input.minmax(false); //lower bound Matrix a = range.GetRows(0); //difference between bounds Matrix scale = range.GetRows(1) - a; //start moving points Matrix::r_iterator pos = points.begin(); Matrix::r_iterator p_a = a.begin(); Matrix::r_iterator p_sc = scale.begin(); for(ulong i = 0; i < points.row_num(); ++i, ++p_a, ++p_sc) { //x = x*(b - a) transform(pos, pos + pnum, pos, bind2nd(multiplies<double>(), *p_sc)); pos = transform(pos, pos + pnum, pos, bind2nd(plus<double>(), *p_a)); //pos += points.col_num(); } DumpMatrix(points, "y.txt"); Matrix surf; if(opt_.netType == matrix_nn) surf <<= net_.Sim(points); else surf <<= _onet[net_ind]->sim(points); DumpMatrix(surf, "z.txt"); }
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; }
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; }
///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; } }
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; }
void update(Array in, int reward){ if(reward > 0 && random(5) % 2 == 0){ trainingInputs.push_back(inputs); trainingOutputs.push_back(outputs); updateWeights(outputs, 0.5, 0.3); } if(trainingInputs.size() > 300){ trainingInputs.erase(trainingInputs.begin()); trainingOutputs.erase(trainingOutputs.begin()); } inputs = in; outputs = computeOutputs(inputs); }
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 debugPrint(const Matrix& M) { for (Matrix::const_iterator i = M.begin(); i != M.end(); ++i) { debugPrint(*i); } }
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; } }
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 }
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 }
/** * * @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; }
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(); }
Disposable<Array> qrSolve(const Matrix& a, const Array& b, bool pivot, const Array& d) { const Size m = a.rows(); const Size n = a.columns(); QL_REQUIRE(b.size() == m, "dimensions of A and b don't match"); QL_REQUIRE(d.size() == n || d.empty(), "dimensions of A and d don't match"); Matrix q(m, n), r(n, n); std::vector<Size> lipvt = qrDecomposition(a, q, r, pivot); boost::scoped_array<int> ipvt(new int[n]); std::copy(lipvt.begin(), lipvt.end(), ipvt.get()); Matrix rT = transpose(r); boost::scoped_array<Real> sdiag(new Real[n]); boost::scoped_array<Real> wa(new Real[n]); Array ld(n, 0.0); if (!d.empty()) { std::copy(d.begin(), d.end(), ld.begin()); } Array x(n); Array qtb = transpose(q)*b; MINPACK::qrsolv(n, rT.begin(), n, ipvt.get(), ld.begin(), qtb.begin(), x.begin(), sdiag.get(), wa.get()); return x; }
bool readMatrix(Matrix &m, const short max_matrix_size, const string &file_name) { if (!fileExists(file_name)) return false; string line; ifstream inf; inf.open(file_name); if (!inf) return false; short counter = 0; while (getline(inf, line)) { if (counter > max_matrix_size) break; istringstream is( line ); m.push_back( vector<unsigned short>(istream_iterator<unsigned short>(is), istream_iterator<unsigned short>() ) ); counter++; } m.erase(m.begin()); for (auto &n : m) { for (;n.size() > max_matrix_size;) { n.pop_back(); } } return true; }
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()); }