void function_minimizer::sd_routine(void) { int nvar=initial_params::nvarcalc(); // get the number of active parameters dvector x(1,nvar); initial_params::xinit(x); // get the number of active parameters initial_params::restore_start_phase(); int nvar1=initial_params::nvarcalc(); // get the number of active parameters int num_sdrep_types=stddev_params::num_stddev_params + initial_params::num_active_calc(); param_labels.allocate(1,num_sdrep_types); param_size.allocate(1,num_sdrep_types); int ii=1; size_t max_name_length = 0; for (int i=0;i<initial_params::num_initial_params;i++) { //if ((initial_params::varsptr[i])->phase_start // <= initial_params::current_phase) if (withinbound(0,(initial_params::varsptr[i])->phase_start, initial_params::current_phase)) { param_labels[ii]= (initial_params::varsptr[i])->label(); param_size[ii]= (initial_params::varsptr[i])->size_count(); if (max_name_length<param_labels[ii].size()) { max_name_length=param_labels[ii].size(); } ii++; } } int start_stdlabels=ii; for (int i=0;i< stddev_params::num_stddev_params;i++) { param_labels[ii]= stddev_params::stddevptr[i]->label(); param_size[ii]= stddev_params::stddevptr[i]->size_count(); if (max_name_length<param_labels[ii].size()) { max_name_length=param_labels[ii].size(); } ii++; } int end_stdlabels=ii-1; int ndvar=stddev_params::num_stddev_calc(); dvector scale(1,nvar1); // need to get scale from somewhere dvector v(1,nvar); // need to read in v from model.rep dmatrix S(1,nvar,1,nvar); { uistream cif("admodel.cov"); int tmp_nvar = 0; cif >> tmp_nvar; if (nvar !=tmp_nvar) { cerr << "Incorrect number of independent variables in file" " model.cov" << endl; exit(1); } cif >> S; if (!cif) { cerr << "error reading covariance matrix from model.cov" << endl; exit(1); } } int sgn; initial_params::stddev_scale(scale,x); double lndet=-ln_det(S,sgn)-2.0*sum(log(scale)); initial_params::set_active_random_effects(); //int nvar1=initial_params::nvarcalc(); dvector diag(1,nvar1+ndvar); dvector tmp(1,nvar1+ndvar); { ofstream ofs("admodel.tmp"); #if defined(__GNU__) || defined(DOS386) || defined(__GNUDOS__) // ******************************************************* // ******************************************************* { if (nvar==nvar1) // no random effects { for (int i=1;i<=nvar;i++) { for (int j=1;j<=i;j++) { tmp(j)=S(i,j)*scale(i)*scale(j); ofs << tmp(j) << " "; } ofs << endl; diag(i)=tmp(i); } dmatrix tv(1,ndvar,1,nvar1); adstring tmpstring="admodel.dep"; if (ad_comm::wd_flag) tmpstring = ad_comm::adprogram_name + ".dep"; cifstream cif((char*)tmpstring); int tmp_nvar = 0, tmp_ndvar = 0; cif >> tmp_nvar >> tmp_ndvar; if (tmp_nvar!=nvar1) { cerr << " tmp_nvar != nvar1 in file " << tmpstring << endl; ad_exit(1); } if (ndvar>0) { cif >> tv; dvector tmpsub(1,nvar); for (int i=1;i<=ndvar;i++) { for (int j=1;j<=nvar;j++) { tmpsub(j)=(tv(i)*S(j))*scale(j); } ofs << tmpsub << " "; tmpsub=tv(i)*S; for (int j=1;j<=i;j++) { tmp(nvar+j)=tmpsub*tv(j); ofs << tmp(nvar+j) << " "; } diag(i+nvar)=tmp(i+nvar); if (diag(i+nvar)<=0.0) { cerr << "Estimated covariance matrix may not" " be positive definite" << endl; cerr << sort(eigenvalues(S)) << endl; } ofs << endl; } } } else // have random effects { dmatrix tv(1,ndvar,1,nvar1); adstring tmpstring="admodel.dep"; if (ad_comm::wd_flag) tmpstring = ad_comm::adprogram_name + ".dep"; cifstream cif((char*)tmpstring); int tmp_nvar = 0, tmp_ndvar = 0; cif >> tmp_nvar >> tmp_ndvar; if (tmp_nvar!=nvar1) { cerr << " tmp_nvar != nvar1 in file " << tmpstring << endl; ad_exit(1); } dmatrix BS(1,nvar1,1,nvar1); BS.initialize(); get_bigS(ndvar,nvar1,nvar,S,BS,scale); { tmpstring = ad_comm::adprogram_name + ".bgs"; uostream uos((char*)(tmpstring)); if (!uos) { cerr << "error opening file " << tmpstring << endl; ad_exit(1); } uos << nvar1; uos << BS; if (!uos) { cerr << "error writing to file " << tmpstring << endl; ad_exit(1); } } for (int i=1;i<=nvar1;i++) { for (int j=1;j<=i;j++) { tmp(j)=BS(i,j)*scale(i)*scale(j); ofs << tmp(j) << " "; } ofs << endl; diag(i)=tmp(i); } if (ndvar>0) { cif >> tv; dvector tmpsub(1,nvar1); for (int i=1;i<=ndvar;i++) { for (int j=1;j<=nvar1;j++) { tmpsub(j)=(tv(i)*BS(j))*scale(j); } ofs << tmpsub << " "; tmpsub=tv(i)*BS; for (int j=1;j<=i;j++) { tmp(nvar1+j)=tmpsub*tv(j); ofs << tmp(nvar1+j) << " "; } diag(i+nvar1)=tmp(i+nvar1); if (diag(i+nvar1)<=0.0) { if (norm(tv(i))>1.e-100) { cerr << "Estimated covariance matrix may not" " be positive definite" << endl; cerr << sort(eigenvalues(BS)) << endl; } } ofs << endl; } } }
// paint hwnd into bmout, where bmout points at bmout_xpos,bmout_ypos in window coordinates void SWELL_internalLICEpaint(HWND hwnd, LICE_IBitmap *bmout, int bmout_xpos, int bmout_ypos, bool forceref) { // todo: check to see if any children completely intersect clip region, if so then we don't need to render ourselves // todo: opaque flag for windows? (to disable above behavior) // todo: implement/use per-window backing store. // we would want to only really use them for top level windows, and on those, only update windows (and children of windows) who had backingstore invalidated. if (hwnd->m_invalidated) forceref=true; if (forceref || hwnd->m_child_invalidated) { swell_gdpLocalContext ctx={0,}; // todo set up gdi context defaults? ctx.ctx.surface = bmout; ctx.ctx.surface_offs.x = -bmout_xpos; ctx.ctx.surface_offs.y = -bmout_ypos; ctx.clipr.left = bmout_xpos; ctx.clipr.top = bmout_ypos; ctx.clipr.right = ctx.clipr.left + bmout->getWidth(); ctx.clipr.bottom = ctx.clipr.top + bmout->getHeight(); void *oldpaintctx = hwnd->m_paintctx; if (forceref) hwnd->m_paintctx = &ctx; LICE_SubBitmap tmpsub(NULL,0,0,0,0); if (hwnd->m_wndproc) // this happens after m_paintctx is set -- that way GetWindowDC()/GetDC()/ReleaseDC() know to not actually update the screen { RECT r; GetWindowContentViewRect(hwnd,&r); r.right-=r.left; r.bottom-=r.top; r.left=r.top=0; NCCALCSIZE_PARAMS p={{r,},}; hwnd->m_wndproc(hwnd,WM_NCCALCSIZE,FALSE,(LPARAM)&p); RECT r2=r; r = p.rgrc[0]; if (forceref) hwnd->m_wndproc(hwnd,WM_NCPAINT,(WPARAM)1,0); if (r.left!=r2.left) {} // todo: adjust drawing offsets, clip rects, accordingly if (r.top!=r2.top) {} // todo: adjust drawing offsets, clip rects, accordingly int dx = r.left-r2.left,dy=r.top-r2.top; // dx,dy is offset from the window's root of bmout_xpos -= dx; bmout_ypos -= dy; if (dx||dy) { tmpsub.m_parent = ctx.ctx.surface; tmpsub.m_x = dx; tmpsub.m_y = dy; tmpsub.m_w = ctx.ctx.surface->getWidth()-dx; tmpsub.m_h = ctx.ctx.surface->getHeight()-dy; if (tmpsub.m_w<0) tmpsub.m_w=0; if (tmpsub.m_h<0) tmpsub.m_h=0; ctx.ctx.surface_offs.x += dx; ctx.ctx.surface_offs.y += dy; ctx.ctx.surface = &tmpsub; ctx.clipr.left-=dx; ctx.clipr.top-=dy; ctx.clipr.right-=dx; ctx.clipr.bottom-=dy; } // adjust clip rects for right/bottom extents int newr = r.right-r.left; if (ctx.clipr.right > newr) ctx.clipr.right=newr; int newb = r.bottom-r.top; if (ctx.clipr.bottom > newb) ctx.clipr.bottom=newb; } // paint if (forceref) { if (hwnd->m_wndproc && ctx.clipr.right > ctx.clipr.left && ctx.clipr.bottom > ctx.clipr.top) { hwnd->m_wndproc(hwnd,WM_PAINT,(WPARAM)&ctx,0); } hwnd->m_paintctx = oldpaintctx; // it might be good to blit here on some OSes, rather than from the top level caller... hwnd->m_invalidated=false; } } if (forceref || hwnd->m_child_invalidated) { HWND h = hwnd->m_children; while (h && h->m_next) h=h->m_next; while (h) // go through list backwards (first in list = top of Z order) { if (h->m_visible && (forceref || h->m_invalidated||h->m_child_invalidated)) { int width = h->m_position.right - h->m_position.left, height = h->m_position.bottom - h->m_position.top; // max width possible for this window int xp = h->m_position.left - bmout_xpos, yp = h->m_position.top - bmout_ypos; // if xp/yp < 0, that means that the clip region starts inside the window, so we need to pass a positive render offset, and decrease the potential draw width // if xp/yp > 0, then the clip region starts before the window, so we use the subbitmap and pass 0 for the offset parm int subx = 0, suby=0; if (xp<0) width+=xp; else { subx=xp; xp=0; } if (yp<0) height+=yp; else { suby=yp; yp=0; } LICE_SubBitmap subbm(bmout,subx,suby,width,height); // the right/bottom will automatically be clipped to the clip rect etc if (subbm.getWidth()>0 && subbm.getHeight()>0) SWELL_internalLICEpaint(h,&subbm,-xp,-yp,forceref); } h = h->m_prev; } } hwnd->m_child_invalidated=false; }