/** * Description not yet available. * \param */ dmatrix_position::dmatrix_position(const dmatrix& m) : lb(m.rowmin(),m.rowmax()), ub(m.rowmin(),m.rowmax()), ptr(m.rowmin(),m.rowmax()) { row_min=m.rowmin(); row_max=m.rowmax(); for (int i=row_min;i<=row_max;i++) { lb(i)=m(i).indexmin(); ub(i)=m(i).indexmax(); ptr(i)=m(i).get_v(); } }
/** * @brief Return molt increment matrix based on empirical data * @details Fit's a cubic spline to the empirical data. * Note that the spline function strictly requires increasing * values for each of the knots. * * @param data [description] * @return dmatrix of molt increments by sex for each size bin */ dmatrix get_empirical_molt_increment(const dvector& bin, const dmatrix& data) { cout<<"In get_empirical_molt_increment"<<endl; int n = bin.size(); ivector sex = ivector(column(data,2)); int nsex = count_factor(sex); dmatrix mi(1,nsex,1,n); ivector nh(1,nsex); nh.initialize(); // Count number of observations in each sex. for (int i = 1; i <= data.rowmax(); ++i) { int h = sex(i); nh(h) ++; } // get male and famale arrays dmatrix x(1,nsex,1,nh); dmatrix y(1,nsex,1,nh); int bb=1; int gg=1; for (int i = 1; i <= data.rowmax(); ++i) { int h = sex(i); int j = h==1 ? bb++ : gg++ ; x(h,j) = data(i,1); y(h,j) = data(i,3); } // rescale size to 0-1 over bin width for (int h = 1; h <= nsex; ++h) { dvector knts = (x(h) - min(x(h))) / (max(x(h)) - min(x(h))); dvector pnts = (bin - min(bin)) / (max(bin) - min(bin)); COUT(knts); COUT(y(h)); cubic_spline_function cSmooth(knts,y(h)); dvector test = cSmooth(pnts); COUT(cSmooth(0.5)); COUT(test); } cout<<"leaving get_empirical_molt_increment"<<endl; return mi; }
/* Returns dmatrix with pow of each element in m raised to the exponent e. \param m dmatrix \param e floating point exponent */ dmatrix pow(const dmatrix& m, const double e) { ivector cmin(m.rowmin(), m.rowmax()); ivector cmax(m.rowmin(), m.rowmax()); for (int i = m.rowmin(); i <= m.rowmax(); ++i) { cmin(i) = m(i).indexmin(); cmax(i) = m(i).indexmax(); } dmatrix tmp(m.rowmin(), m.rowmax(), cmin, cmax); for (int i = m.rowmin(); i <= m.rowmax(); ++i) { tmp(i) = pow(m(i), e); } return tmp; }
/** Returns dmatrix with each of element in m is squared. \param m dmatrix */ dmatrix sqr(const dmatrix& m) { ivector cmin(m.rowmin(), m.rowmax()); ivector cmax(m.rowmin(), m.rowmax()); for (int i = m.rowmin(); i <= m.rowmax(); ++i) { cmin(i) = m(i).indexmin(); cmax(i) = m(i).indexmax(); } dmatrix tmp(m.rowmin(), m.rowmax(), cmin, cmax); for (int i = m.rowmin(); i <= m.rowmax(); ++i) { tmp(i) = sqr(m(i)); } return tmp; }
/** * Description not yet available. * \param */ dmatrix log(const dmatrix& m) { ivector cmin(m.rowmin(),m.rowmax()); ivector cmax(m.rowmin(),m.rowmax()); int i; for (i=m.rowmin();i<=m.rowmax();i++) { cmin(i)=m(i).indexmin(); cmax(i)=m(i).indexmax(); } dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax); for (i=m.rowmin();i<=m.rowmax();i++) { tmp(i)=log(m(i)); } return tmp; }
/** * Description not yet available. * \param */ dmatrix elem_div(const dmatrix& m, const dmatrix& m2) { ivector cmin(m.rowmin(),m.rowmax()); ivector cmax(m.rowmin(),m.rowmax()); int i; for (i=m.rowmin();i<=m.rowmax();i++) { cmin(i)=m(i).indexmin(); cmax(i)=m(i).indexmax(); } dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax); for (i=m.rowmin();i<=m.rowmax();i++) { tmp(i)=elem_div(m(i),m2(i)); } return tmp; }
/** Return total sum of all elements in matrix. \param matrix dmatrix */ double sum(const dmatrix& matrix) { double total = 0.0; for (int i = matrix.rowmin(); i <= matrix.rowmax(); ++i) { total += sum(matrix.elem(i)); } return total; }
/** * Description not yet available. * \param */ double max(const dmatrix& m) { double tmp=max(m(m.rowmin())); for (int i=m.rowmin()+1;i<=m.rowmax();i++) { double tmp1=max(m(i)); if (tmp<tmp1) tmp=tmp1; } return tmp; }
/** Returns dvector where each element contains the sum total of each row in matrix. \param matrix dmatrix */ dvector rowsum(const dmatrix& matrix) { int min = matrix.rowmin(); int max = matrix.rowmax(); dvector sums(min, max); for (int i = min; i <= max; ++i) { sums(i) = sum(matrix(i)); } return sums; }
/** * Description not yet available. * \param */ dmatrix symmetrize(const dmatrix& m) { if (m.rowmin() != m.colmin() || m.rowmax() != m.colmax() ) { cerr << " Non square matrix passed to dmatrix symmetrize\n"; ad_exit(1); } int rmin=m.rowmin(); int rmax=m.rowmax(); dmatrix s(rmin,rmax,rmin,rmax); for (int i=rmin; i<=rmax; i++) { s(i,i)=m(i,i); for (int j=rmin; j<i; j++) { s(i,j)=(m(i,j)+m(j,i))/2.; s(j,i)=s(i,j); } } return s; }
/** Return the sum of the diagonal of a square matrix mat. \param mat is a square scalar matrix */ double trace(const dmatrix& mat) { if (mat.colmin() != mat.rowmin() || mat.colmax() != mat.rowmax() ) { cerr << "Matrix not square in trace\n"; ad_exit(1); } double sum = 0.0; for (int i = mat.colmin(); i <= mat.colmax(); ++i) { sum += mat.elem(i, i); } return sum; }
/** Return computed column(col) sum for dmatrix m. */ double colsum(const dmatrix& m, int col) { int mmin = m.rowmin(); int mmax = m.rowmax(); if (col < mmin || col > mmax) { cerr << "Row out of bounds in function" " colsum(const imatrix& m,int col)" << endl; ad_exit(1); } double sum = 0; for (int i = mmin; i <= mmax; ++i) { sum += m(i, col); } return sum; }
/** Returns dvector where each element contains the sum total of each column in matrix. \param matrix dmatrix */ dvector colsum(const dmatrix& matrix) { int cmin = matrix.colmin(); int cmax = matrix.colmax(); int rmin = matrix.rowmin(); int rmax = matrix.rowmax(); dvector sums(cmin, cmax); sums.initialize(); for (int j=cmin; j<=cmax; ++j) { for (int i=rmin; i<=rmax; ++i) { sums(j) += matrix(i, j); } } return sums; }
/** * \ingroup eigen * Eigenvectors * \param m \f$m\f$ * \return a variable matrix with the * eigenvectors of \f$m\f$ stored in its columns. */ dmatrix eigenvectors(const dmatrix &m) { if (m.rowsize() != m.colsize()) { cerr << "error -- non square matrix passed to dmatrix eigenvectors(const dmatrix& m)\n"; ad_exit(1); } int rmin = m.rowmin(); int rmax = m.rowmax(); dmatrix evecs(rmin, rmax, rmin, rmax); dvector evals(rmin, rmax); eigens(m, evecs, evals); return evecs; }
dvariable mult_likelihood(const dmatrix &o, const dvar_matrix &p, dvar_matrix &nu, const dvariable &log_vn) { // kludge to ensure observed and predicted matrixes are the same size if(o.colsize()!=p.colsize() || o.rowsize()!=p.rowsize()) { cerr<<"Error in multivariate_t_likelihood, observed and predicted matrixes" " are not the same size\n"; ad_exit(1); } dvariable vn = mfexp(log_vn); dvariable ff = 0.0; int r1 = o.rowmin(); int r2 = o.rowmax(); int c1 = o.colmin(); int c2 = o.colmax(); for(int i = r1; i <= r2; i++ ) { dvar_vector sobs = vn * o(i)/sum(o(i)); //scale observed numbers by effective sample size. ff -= gammln(vn); for(int j = c1; j <= c2; j++ ) { if( value(sobs(j)) > 0.0 ) ff += gammln(sobs(j)); } ff -= sobs * log(TINY + p(i)); dvar_vector o1=o(i)/sum(o(i)); dvar_vector p1=p(i)/sum(p(i)); nu(i) = elem_div(o1-p1,sqrt(elem_prod(p1,1.-p1)/vn)); } // exit(1); return ff; }
/** * Description not yet available. * \param */ void dmatrix::allocate(const dmatrix& dm) { int nrl=dm.rowmin(); int nrh=dm.rowmax(); index_min=nrl; index_max=nrh; if ( (m = new dvector [rowsize()]) == 0) { cerr << " Error allocating memory in dmatrix contructor" << endl; ad_exit(21); } if ( (shape = new mat_shapex(m))== 0) { cerr << " Error allocating memory in dmatrix contructor" << endl; ad_exit(21); } m -= rowmin(); for (int i=rowmin(); i<=rowmax(); i++) { m[i].allocate(dm(i)); } }
/** * Description not yet available. * \param */ dvar_matrix operator*(const dvar_matrix& m1, const dmatrix& cm2) { if (m1.colmin() != cm2.rowmin() || m1.colmax() != cm2.rowmax()) { cerr << " Incompatible array bounds in " "dmatrix operator*(const dvar_matrix& x, const dmatrix& m)\n"; ad_exit(21); } dmatrix cm1=value(m1); //dmatrix cm2=value(m2); dmatrix tmp(m1.rowmin(),m1.rowmax(), cm2.colmin(), cm2.colmax()); #ifdef OPT_LIB const size_t rowsize = (size_t)cm2.rowsize(); #else const int _rowsize = cm2.rowsize(); assert(_rowsize > 0); const size_t rowsize = (size_t)_rowsize; #endif try { double* temp_col = new double[rowsize]; temp_col-=cm2.rowmin(); for (int j=cm2.colmin(); j<=cm2.colmax(); j++) { for (int k=cm2.rowmin(); k<=cm2.rowmax(); k++) { temp_col[k] = cm2.elem(k,j); } for (int i=cm1.rowmin(); i<=cm1.rowmax(); i++) { double sum=0.0; dvector& temp_row = cm1(i); for (int k=cm1.colmin(); k<=cm1.colmax(); k++) { sum+=temp_row(k) * (temp_col[k]); // sum+=temp_row(k) * cm2(k,j); } tmp(i,j)=sum; } } temp_col+=cm2.rowmin(); delete [] temp_col; temp_col = 0; } catch (std::bad_alloc& e) { cerr << "Error[" << __FILE__ << ':' << __LINE__ << "]: Unable to allocate array.\n"; //ad_exit(21); throw e; } dvar_matrix vtmp=nograd_assign(tmp); save_identifier_string("TEST1"); //m1.save_dvar_matrix_value(); m1.save_dvar_matrix_position(); cm2.save_dmatrix_value(); cm2.save_dmatrix_position(); vtmp.save_dvar_matrix_position(); save_identifier_string("TEST6"); gradient_structure::GRAD_STACK1-> set_gradient_stack(dmcm_prod); return vtmp; }