/*! Returns maximum norm if p == 0 */ template<class Grid_T> double get_diff_lp_norm( const std::vector<uint64_t>& cells, const Grid_T& grid, const double p, const double cell_volume, const size_t dimension ) { double local_norm = 0, global_norm = 0; for (const auto& cell: cells) { const auto* const cell_data = grid[cell]; if (cell_data == NULL) { std::cerr << __FILE__ << ":" << __LINE__ << ": No data for cell " << cell << std::endl; abort(); } const auto center = grid.geometry.get_center(cell); if (p == 0) { local_norm = std::max( local_norm, std::fabs( (*cell_data)[Gradient()][dimension] - grad_of_function(center[dimension]) ) ); } else { local_norm += std::pow( std::fabs( (*cell_data)[Gradient()][dimension] - grad_of_function(center[dimension]) ), p ); } } local_norm *= cell_volume; if (p == 0) { MPI_Comm comm = grid.get_communicator(); MPI_Allreduce(&local_norm, &global_norm, 1, MPI_DOUBLE, MPI_MAX, comm); MPI_Comm_free(&comm); return global_norm; } else { MPI_Comm comm = grid.get_communicator(); MPI_Allreduce(&local_norm, &global_norm, 1, MPI_DOUBLE, MPI_SUM, comm); MPI_Comm_free(&comm); return std::pow(global_norm, 1.0 / p); } }
float Noise3D::get(float x, float y, float z) const { Vec3f origins[8]; Vec3f grads[8]; get_gradients(origins, grads, x, y, z); float vals[] = { Gradient(origins[0], grads[0], {x, y, z}), Gradient(origins[1], grads[1], {x, y, z}), Gradient(origins[2], grads[2], {x, y, z}), Gradient(origins[3], grads[3], {x, y, z}), Gradient(origins[4], grads[4], {x, y, z}), Gradient(origins[5], grads[5], {x, y, z}), Gradient(origins[6], grads[6], {x, y, z}), Gradient(origins[7], grads[7], {x, y, z}), }; float fz = Smooth(z - origins[0].z); float vz0 = lerp(vals[0], vals[1], fz); float vz1 = lerp(vals[2], vals[3], fz); float vz2 = lerp(vals[4], vals[5], fz); float vz3 = lerp(vals[6], vals[7], fz); float fy = Smooth(y - origins[0].y); float vy0 = lerp(vz0, vz1, fy); float vy1 = lerp(vz2, vz3, fy); float fx = Smooth(x - origins[0].x); return lerp(vy0, vy1, fx); }
CurveGradient::CurveGradient(): Layer_Composite(1.0,Color::BLEND_COMPOSITE), param_origin(ValueBase(Point(0,0))), param_width(ValueBase(Real(0.25))), param_bline(ValueBase(std::vector<synfig::BLinePoint>())), param_gradient(Gradient(Color::black(), Color::white())), param_loop(ValueBase(false)), param_zigzag(ValueBase(false)), param_perpendicular(ValueBase(false)), param_fast(ValueBase(true)) { std::vector<synfig::BLinePoint> bline; bline.push_back(BLinePoint()); bline.push_back(BLinePoint()); bline.push_back(BLinePoint()); bline[0].set_vertex(Point(0,1)); bline[1].set_vertex(Point(0,-1)); bline[2].set_vertex(Point(1,0)); bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f); bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f); bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f); bline[0].set_width(1.0f); bline[1].set_width(1.0f); bline[2].set_width(1.0f); bline_loop=true; param_bline.set(bline); sync(); SET_INTERPOLATION_DEFAULTS(); SET_STATIC_DEFAULTS(); }
Point SmoothCircleBaseIC::computeCircleGradient(const Point & p, const Point & center, const Real & radius) { Point l_center = center; Point l_p = p; if (!_3D_spheres) //Create 3D cylinders instead of spheres { l_p(2) = 0.0; l_center(2) = 0.0; } //Compute the distance between the current point and the center Real dist = _mesh.minPeriodicDistance(_var.number(), l_p, l_center); Real DvalueDr = 0.0; if (dist < radius + _int_width/2.0 && dist > radius - _int_width/2.0) { Real int_pos = (dist - radius + _int_width / 2.0) / _int_width; Real Dint_posDr = 1.0 / _int_width; DvalueDr = Dint_posDr * (_invalue - _outvalue) * (-std::sin(int_pos * libMesh::pi) * libMesh::pi) / 2.0; } //Set gradient over the smooth interface if (dist != 0.0) return _mesh.minPeriodicVector(_var.number(), center, p) * (DvalueDr / dist); else return Gradient(0.0, 0.0, 0.0); }
ChromoConditions::ChromoConditions(double iColumnLength, double iColumnDiameter, double iColumnPoreSize, Gradient iGradient, double iSecondSolventConcentrationA, double iSecondSolventConcentrationB, double iDelayTime, double iFlowRate, double iDV, double iColumnRelativeStrength, double iColumnVpToVtot, double iColumnPorosity, double iTemperature) throw(ChromoConditionsException) { // Set an empty gradient to prevent recalculation of SSConcentrations. mGradient = Gradient(); setMixingCorrection(false); setColumnLength(iColumnLength); setColumnDiameter(iColumnDiameter); setColumnPoreSize(iColumnPoreSize); setColumnVpToVtot(iColumnVpToVtot); setColumnPorosity(iColumnPorosity); setTemperature(iTemperature); setColumnRelativeStrength(iColumnRelativeStrength); setFlowRate(iFlowRate); setDV(iDV); setDelayTime(iDelayTime); setSecondSolventConcentrationA(iSecondSolventConcentrationA); setSecondSolventConcentrationB(iSecondSolventConcentrationB); setGradient(iGradient); }
void Function::gradient(const IntervalVector& x, IntervalVector& g) const { assert(g.size()==nb_var()); assert(x.size()==nb_var()); Gradient().gradient(*this,x,g); // if (!df) ((Function*) this)->df=new Function(*this,DIFF); // g=df->eval_vector(x); }
void UpdateTheta( std::vector< std::list< Node<T1,T2> > > const& X, std::vector< double >& theta, std::vector<int> const& Y, const double alpha_start, const double alpha_stop ){ double alpha = alpha_start, logLikelihood, newLogLikelihood; std::vector<double> gradient = Gradient( X, theta, Y ); std::vector<double> new_theta = theta; logLikelihood = 99999999; newLogLikelihood = -99999999999; while( logLikelihood > newLogLikelihood && alpha > alpha_stop ){ for( unsigned int k = 0; k < new_theta.size(); ++k ){ new_theta[k] = theta[k] + alpha * gradient[k]; } logLikelihood = LogLikelihood( X, theta, Y ); newLogLikelihood = LogLikelihood( X, new_theta, Y ); alpha = alpha/2; } theta = new_theta; }
Gradient Biharmonic::JR::InitialGradientZero(const Point&, const Parameters&, const std::string&, const std::string&) { return Gradient(0.0,0.0,0.0); }
static void Draw() { float xy[2], d, grad[2]; // convert mouse position from screen to identity xy[0] = (float)mousepos[0] / (float)renderwidth; xy[1] = 1.0f - ((float)mousepos[1] / (float)renderheight); // convert from identity to screen pos xy[0] = -2 + xy[0] * 4; xy[1] = -2 + xy[1] * 4; fprintf(stdout, "x, y: %2.2f, %2.2f\n", xy[0], xy[1]); d = Distance(xy, 1.0f); //fprintf(stdout, "distance %f\n", d); Gradient(grad, xy, 1.0f); Vec2_Normalize(grad); //fprintf(stdout, "gradient %f, %f\n", grad[0], grad[1]); if(d > 0) glColor3f(1, 0, 0); else glColor3f(0, 0, 1); glBegin(GL_LINES); { glVertex2f(xy[0], xy[1]); glVertex2f(xy[0] + d * -grad[0], xy[1] + d * -grad[1]); } glEnd(); }
int TLogRegFit::MLENewton(const double& ChangeEps, const int& MaxStep, const TStr PlotNm) { TExeTm ExeTm; TFltV GradV(Theta.Len()), DeltaLV(Theta.Len()); TFltVV HVV(Theta.Len(), Theta.Len()); int iter = 0; double MinVal = -1e10, MaxVal = 1e10; for(iter = 0; iter < MaxStep; iter++) { Gradient(GradV); Hessian(HVV); GetNewtonStep(HVV, GradV, DeltaLV); double Increment = TLinAlg::DotProduct(GradV, DeltaLV); if (Increment <= ChangeEps) { break; } double LearnRate = GetStepSizeByLineSearch(DeltaLV, GradV, 0.15, 0.5);//InitLearnRate/double(0.01*(double)iter + 1); for(int i = 0; i < Theta.Len(); i++) { double Change = LearnRate * DeltaLV[i]; Theta[i] += Change; if(Theta[i] < MinVal) { Theta[i] = MinVal; } if(Theta[i] > MaxVal) { Theta[i] = MaxVal; } } } if (! PlotNm.Empty()) { printf("MLE with Newton method completed with %d iterations(%s)\n",iter,ExeTm.GetTmStr()); } return iter; }
inline void LinearGradient::fill_params(Params ¶ms)const { params.p1=param_p1.get(Point()); params.p2=param_p2.get(Point()); params.loop=param_loop.get(bool()); params.zigzag=param_zigzag.get(bool()); params.gradient.set(param_gradient.get(Gradient()), params.loop, params.zigzag); params.calc_diff(); }
/*virtual*/ void ScatterPackedNode<ElemType>::BackpropToNonLooping(size_t inputIndex) /*override*/ { if (inputIndex == SOURCEDATA) { let& index = Input(INDEXDATA)->Value(); // column indices to copy from auto& sourceGradient = Input(SOURCEDATA)->Gradient(); // source to propagate the gradient input auto& outputGradient = Gradient(); // output gradient to propagate sourceGradient.DoGatherColumnsOf(/*beta=*/1, index, outputGradient, /*alpha=*/1); } }
float Noise2D::get(float x, float y) const { Vec2f origins[4]; Vec2f grads[4]; get_gradients(origins, grads, x, y); float vals[] = { Gradient(origins[0], grads[0], {x, y}), Gradient(origins[1], grads[1], {x, y}), Gradient(origins[2], grads[2], {x, y}), Gradient(origins[3], grads[3], {x, y}), }; float fx = Smooth(x - origins[0].x); float vx0 = lerp(vals[0], vals[1], fx); float vx1 = lerp(vals[2], vals[3], fx); float fy = Smooth(y - origins[0].y); return lerp(vx0, vx1, fy); }
SnakeClass::SnakeClass(HDIB m_hDIB) { int n,nBitCount; LPSTR lpDIB; BYTE *lpSrc; hDIB = m_hDIB; //锁定DIB,返回DIB内存首地址 lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB); //图像宽度 lWidth = ::DIBWidth(lpDIB); //图像高度 lHeight = ::DIBHeight(lpDIB); //找到DIB像素起始位置 lpDIBBits = ::FindDIBBits(lpDIB); //获得DIB文件头 lpbmi = (LPBITMAPINFO)lpDIB; //每像素所占位数 nBitCount = lpbmi->bmiHeader.biBitCount; //计算图像每行的字节数 lLineBytes = (lWidth*nBitCount/8+3)/4*4; //开辟新空间存储DIB ImageData = new double[lWidth*lHeight]; for(int i = 0; i<lHeight; i++) { for(int j = 0; j<lWidth; j++) { lpSrc = (unsigned char*)lpDIBBits + lLineBytes*(lHeight-1-i)+j*nBitCount/8; n=i*lWidth+j; if(nBitCount==8) ImageData[n]=*lpSrc; if(nBitCount==24) //24位图中计算亮度值时,RGB三分量权值分别为0.3,0.59,0.11 ImageData[n]=(*(lpSrc+1)*0.59f+*(lpSrc+2)*0.11f+*(lpSrc+0)*0.3f); } } DrawContour=false; p_NumPos=0; pp=0; p=NULL; top=NULL; Gauss(); Gradient(); }
LinearGradient::LinearGradient(): Layer_Composite(1.0,Color::BLEND_COMPOSITE), param_p1(ValueBase(Point(1,1))), param_p2(ValueBase(Point(-1,-1))), param_gradient(ValueBase(Gradient(Color::black(), Color::white()))), param_loop(ValueBase(false)), param_zigzag(ValueBase(false)) { SET_INTERPOLATION_DEFAULTS(); SET_STATIC_DEFAULTS(); }
void LEDItem::paint( QPainter *pPainter, const QStyleOptionGraphicsItem *pOption, QWidget *pWidget ) { QRadialGradient Gradient( 10, 10, 22 ); Gradient.setColorAt( 0.0, Qt::white ); Gradient.setColorAt( 0.1, mColour.darker( 100 + ( 1.0 - ( mBrightness * 1.00 ) ) * 150.0 ) ); Gradient.setColorAt( 1.0, mColour.darker( 100 + ( 1.0 - ( mBrightness * 0.75 ) ) * 200.0 ) ); mEllipse->setBrush( Gradient ); mEllipse->paint( pPainter, pOption, pWidget ); }
int TLogRegFit::MLEGradient(const double& ChangeEps, const int& MaxStep, const TStr PlotNm) { TExeTm ExeTm; TFltV GradV(Theta.Len()); int iter = 0; TIntFltPrV IterLV, IterGradNormV; double MinVal = -1e10, MaxVal = 1e10; double GradCutOff = 100000; for(iter = 0; iter < MaxStep; iter++) { Gradient(GradV); //if gradient is going out of the boundary, cut off for(int i = 0; i < Theta.Len(); i++) { if (GradV[i] < -GradCutOff) { GradV[i] = -GradCutOff; } if (GradV[i] > GradCutOff) { GradV[i] = GradCutOff; } if (Theta[i] <= MinVal && GradV[i] < 0) { GradV[i] = 0.0; } if (Theta[i] >= MaxVal && GradV[i] > 0) { GradV[i] = 0.0; } } double Alpha = 0.15, Beta = 0.9; //double LearnRate = 0.1 / (0.1 * iter + 1); //GetStepSizeByLineSearch(GradV, GradV, Alpha, Beta); double LearnRate = GetStepSizeByLineSearch(GradV, GradV, Alpha, Beta); if (TLinAlg::Norm(GradV) < ChangeEps) { break; } for(int i = 0; i < Theta.Len(); i++) { double Change = LearnRate * GradV[i]; Theta[i] += Change; if(Theta[i] < MinVal) { Theta[i] = MinVal; } if(Theta[i] > MaxVal) { Theta[i] = MaxVal; } } if (! PlotNm.Empty()) { double L = Likelihood(); IterLV.Add(TIntFltPr(iter, L)); IterGradNormV.Add(TIntFltPr(iter, TLinAlg::Norm(GradV))); } } if (! PlotNm.Empty()) { TGnuPlot::PlotValV(IterLV, PlotNm + ".likelihood_Q"); TGnuPlot::PlotValV(IterGradNormV, PlotNm + ".gradnorm_Q"); printf("MLE for Lambda completed with %d iterations(%s)\n",iter,ExeTm.GetTmStr()); } return iter; }
void RoundedRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { painter->save(); painter->setRenderHints(QPainter::Antialiasing); QPen pen(painter->pen()); pen.setWidth(m_penWidth); pen.setColor(m_penColor); painter->setPen(pen); if(m_useGradient) { if(m_GradientDirection == Vertical) { QLinearGradient Gradient(this->rect().width()/2,0,this->rect().width()/2,this->rect().height()); Gradient.setColorAt(0,m_color1); Gradient.setColorAt(1,m_color2); painter->setBrush(QBrush(Gradient)); } else { QLinearGradient Gradient(0,this->rect().height()/2,this->rect().width(),this->rect().height()/2); Gradient.setColorAt(0,m_color1); Gradient.setColorAt(1,m_color2); painter->setBrush(QBrush(Gradient)); } } else { painter->setBrush(QBrush(m_color1)); } QRectF r(m_penWidth/2.0, m_penWidth/2.0, this->boundingRect().width()-m_penWidth, this->boundingRect().height()-m_penWidth); painter->drawRoundedRect(r,m_RadiousX,m_RadiousY); painter->restore(); Container::paint(painter,option,widget); }
mat Wavefunction::Gradient_Radio(const mat &r, double alpha, int number_particles, int at_nr) { //Slater up biten Spin_Up_Slater_det = slater_reduced_det(r, 0, alpha, number_particles, at_nr); Spin_Up_Slater_det_Inv = inv(Spin_Up_Slater_det); //Slater ned biten Spin_Down_Slater_det = slater_reduced_det(r, 1, alpha, number_particles, at_nr); Spin_Down_Slater_det_Inv = inv(Spin_Down_Slater_det); mat gradientradio = zeros(number_particles, dimension); for (int i = 0; i < number_particles/2; i++) { for (int j=0; j<number_particles/2; j++) { gradientradio.row(i) += Gradient(r, i, j, alpha, at_nr) * Spin_Up_Slater_det_Inv(j,i); gradientradio.row(i+number_particles/2) += Gradient(r, i + number_particles/2, j, alpha, at_nr) * Spin_Down_Slater_det_Inv(j,i); } } return gradientradio; }
RealGradient CrossIC::gradient(const Point & p) { Point pxminus = p, pxplus = p, pyminus = p, pyplus = p; pxminus(0) -= TOLERANCE; pyminus(1) -= TOLERANCE; pxplus(0) += TOLERANCE; pyplus(1) += TOLERANCE; Number uxminus = value(pxminus), uxplus = value(pxplus), uyminus = value(pyminus), uyplus = value(pyplus); return Gradient((uxplus - uxminus) / 2.0 / TOLERANCE, (uyplus - uyminus) / 2.0 / TOLERANCE); }
void MixMod::vem (void) { int i, mycounter; double step,xs,ymax; int imin,imax,icount; for(icount=0;icount<maxstep;icount++) { Gradient ( ); imax=maxderiv(ymax); imin=minderiv(); for(i=0;i<n;i++) { ht[i]=(xf[i][imax]-xf[i][imin]); ht[i]=ht[i]*p[imin]; } step=stepsize (); xs=step*p[imin]; p[imin]=p[imin]-xs; p[imax]=p[imax]+xs; VEMStepsDone = VEMStepsDone + 1; if (((ymax-1.0 )< acc) || (icount == (maxstep-1))) { //stopping criterion is fulfilled! vem_details[0] = k+0.0; //save results of VEM-algorithm in vem_details vem_details[1] = ymax-1.0; //final accurancy of vem... mycounter=0; for (i=2; i<(k+2); i++){ vem_details[i] = p[mycounter]; mycounter++; } mycounter=0; for (i=(2+k); i<((2*k)+2); i++){ vem_details[i] = t[mycounter]; mycounter++; } mycounter=0; for (i=(2+2*k); i<((3*k)+2); i++){ vem_details[i] = grad[mycounter]; mycounter++; } // grad break; } } }
inline Color LinearGradient::color_func(const Point &point, float supersample)const { Point p1=param_p1.get(Point()); Gradient gradient=param_gradient.get(Gradient()); bool loop=param_loop.get(bool()); bool zigzag=param_zigzag.get(bool()); Real dist(point*diff-p1*diff); if(loop) dist-=floor(dist); if(zigzag) { dist*=2.0; supersample*=2.0; if(dist>1)dist=2.0-dist; } if(loop) { if(dist+supersample*0.5>1.0) { float left(supersample*0.5-(dist-1.0)); float right(supersample*0.5+(dist-1.0)); Color pool(gradient(1.0-(left*0.5),left).premult_alpha()*left/supersample); if (zigzag) pool+=gradient(1.0-right*0.5,right).premult_alpha()*right/supersample; else pool+=gradient(right*0.5,right).premult_alpha()*right/supersample; return pool.demult_alpha(); } if(dist-supersample*0.5<0.0) { float left(supersample*0.5-dist); float right(supersample*0.5+dist); Color pool(gradient(right*0.5,right).premult_alpha()*right/supersample); if (zigzag) pool+=gradient(left*0.5,left).premult_alpha()*left/supersample; else pool+=gradient(1.0-left*0.5,left).premult_alpha()*left/supersample; return pool.demult_alpha(); } } return gradient(dist,supersample); }
RealGradient SmoothCircleBaseIC::gradient(const Point & p) { Point gradient = Gradient(0.0, 0.0, 0.0); Real value = _outvalue; Real val2 = 0.0; for (unsigned int circ = 0; circ < _centers.size(); ++circ) { val2 = computeCircleValue(p, _centers[circ], _radii[circ]); if ( (val2 > value && _invalue > _outvalue) || (val2 < value && _outvalue > _invalue) ) { value = val2; gradient = computeCircleGradient(p, _centers[circ], _radii[circ]); } } return gradient; }
/*! Returns maximum norm */ template<class Grid_T> std::array<double, 3> get_max_norm( const std::vector<uint64_t>& cells, const Grid_T& grid ) { std::array<double, 3> local_norm{{0, 0, 0}}, global_norm{{0, 0, 0}}; for (const auto& cell: cells) { const auto* const cell_data = grid[cell]; if (cell_data == NULL) { std::cerr << __FILE__ << ":" << __LINE__ << ": No data for cell " << cell << std::endl; abort(); } const auto center = grid.geometry.get_center(cell); const auto grad_of = grad_of_function(center); for (size_t i = 0; i < 3; i++) { local_norm[i] = std::max( local_norm[i], std::fabs((*cell_data)[Gradient()][i] - grad_of[i]) ); } } MPI_Comm comm = grid.get_communicator(); MPI_Allreduce( local_norm.data(), global_norm.data(), 3, MPI_DOUBLE, MPI_MAX, comm ); MPI_Comm_free(&comm); return global_norm; }
RealGradient MultiSmoothCircleIC::gradient(const Point & p) { RealGradient grad = Gradient(0.0,0.0,0.0); RealGradient grad2; for(unsigned int i=0; i<_numbub; i++) { _radius = _bubradi[i]; _center = _bubcent[i]; if((p-_center).size() < _radius + _int_width/2.0) { grad2 = SmoothCircleIC::gradient(p)/_numbub; //if (grad.size() < grad2.size()) grad = grad2; } } return grad; }
static void Thing_Frame() { static float pos[2]; static bool spawned; if(!spawned) { pos[0] = 2; pos[1] = 2; spawned = true; } // distance, normal and tangent float d, n[2], t[2]; d = Distance(pos, 1.0f); Gradient(n, pos, 1.0f); Vec2_Normalize(n); t[0] = -n[1]; t[1] = n[0]; // do a distance correction pos[0] += (d - 0.01f) * -n[0]; pos[1] += (d - 0.01f) * -n[1]; // do a move in the tangent direction // 2 * pi radians / 60 hz / 10 seconds = 10 seconds to go around the circle pos[0] += 0.01f * -t[0]; pos[1] += 0.01f * -t[1]; glColor3f(0, 0, 1); glPointSize(4.0f); glBegin(GL_POINTS); { glVertex2f(pos[0], pos[1]); } glEnd(); }
bool LinearGradient::accelerated_cairorender(Context context, cairo_t *cr, int quality, const RendDesc &renddesc, ProgressCallback *cb)const { bool loop=param_loop.get(bool()); Point p1=param_p1.get(Point()); Point p2=param_p2.get(Point()); Gradient gradient=param_gradient.get(Gradient()); cairo_save(cr); cairo_pattern_t* pattern=cairo_pattern_create_linear(p1[0], p1[1], p2[0], p2[1]); bool cpoints_all_opaque=compile_gradient(pattern, gradient); if(loop) cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT); if(quality>8) cairo_pattern_set_filter(pattern, CAIRO_FILTER_FAST); else if(quality>=4) cairo_pattern_set_filter(pattern, CAIRO_FILTER_GOOD); else cairo_pattern_set_filter(pattern, CAIRO_FILTER_BEST); if( ! (is_solid_color() || (cpoints_all_opaque && get_blend_method()==Color::BLEND_COMPOSITE && get_amount()==1.f)) ) { // Initially render what's behind us if(!context.accelerated_cairorender(cr,quality,renddesc,cb)) { if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Cairo Renderer Failure",__LINE__)); return false; } } cairo_set_source(cr, pattern); cairo_paint_with_alpha_operator(cr, get_amount(), get_blend_method()); cairo_pattern_destroy(pattern); // Not needed more cairo_restore(cr); return true; }
void NLCG (Matrix<cxfl>& x, const Matrix<cxfl>& data, const CSParam& cgp) { float t0 = 1.0, t = 1.0, z = 0.0; float xn = TypeTraits<cxfl>::Real(norm(x)); float rmse, bk, f0, f1; Matrix<cxfl> g0, g1, dx, ffdbx, ffdbg, ttdbx, ttdbg, wx, wdx; DWT<cxfl>& dwt = *cgp.dwt; FT<float>& ft = *cgp.ft; TVOP& tvt = *cgp.tvt; wx = dwt->*x; g0 = Gradient (x, wx, data, cgp); dx = -g0; wdx = dwt->*dx; for (size_t k = 0; k < (size_t)cgp.cgiter; k++) { t = t0; ffdbx = ft * wx; ffdbg = ft * wdx; if (cgp.tvw) { ttdbx = tvt * wx; ttdbg = tvt * wdx; } f0 = Objective (ffdbx, ffdbg, ttdbx, ttdbg, x, dx, data, z, rmse, cgp); int i = 0; while (i < cgp.lsiter) { t *= cgp.lsb; f1 = Objective(ffdbx, ffdbg, ttdbx, ttdbg, x, dx, data, t, rmse, cgp); if (f1 <= f0 - (cgp.lsa * t * abs(g0.dotc(dx)))) break; i++; } printf (" %02zu - nrms: %1.7f, l-search: %i, ", k, rmse, i); fflush (stdout); if (i == cgp.lsiter) { printf ("Reached max line search, exiting... \n"); return; } if (i > 2) t0 *= cgp.lsb; else if (i < 1) t0 /= cgp.lsb; // Update image x += (dx * t); wx = dwt->*x; // CG computation g1 = Gradient (x, wx, data, cgp); bk = real(g1.dotc(g1)) / real(g0.dotc(g0)); g0 = g1; dx = -g1 + dx * bk; wdx = dwt->*dx; float dxn = norm(dx)/xn; printf ("dxnrm: %0.4f\n", dxn); if (dxn < cgp.cgconv) break; } }
int main() { double PixelWidth = (CxMax-CxMin)/ImageWidth; double PixelHeight = (CyMax-CyMin)/ImageHeight; const int MaxColorComponentValue = 255; pixel_t *pixels = malloc(sizeof(pixel_t) * ImageHeight * ImageWidth); FILE *fp; /* */ { int iY; for (iY = 0; iY < ImageHeight; iY++) { double Cy = CyMin + iY * PixelHeight; if (fabs(Cy) < PixelHeight / 2) { Cy = 0.0; /* Main antenna */ } int iX; for (iX = 0; iX < ImageWidth; iX++) { double Cx = CxMin + iX * PixelWidth; double Zx = 0.0; double Zy = 0.0; double Zx2 = Zx*Zx; double Zy2 = Zy*Zy; /* */ { int it; const int itMax = 50; const double Bailout = 2; /* bail-out value */ const double CircleRadius = Bailout * Bailout; /* circle radius */ for (it = 0; it < itMax && ((Zx2+Zy2) < CircleRadius); it++) { Zy = 2*Zx*Zy + Cy; Zx = Zx2-Zy2 + Cx; Zx2 = Zx*Zx; Zy2 = Zy*Zy; } if (it == itMax) { pixels[iY *ImageWidth + iX][0] = 0; pixels[iY *ImageWidth + iX][1] = 0; pixels[iY *ImageWidth + iX][2] = 0; } else { Gradient((double)((it-log2(log2(sqrt(Zx2+Zy2)))))/itMax, pixels[iY*ImageWidth + iX]); } } } } } fp = fopen("MandelbrotSetNEW.ppm", "wb"); fprintf(fp, "P6\n %s\n %d\n %d\n %d\n", "# no comment", ImageWidth, ImageHeight, MaxColorComponentValue); fwrite(pixels, sizeof(pixel_t), ImageWidth * ImageHeight, fp); fclose(fp); free(pixels); return EXIT_SUCCESS; }
void SweepHelper(Mesh& mesh) { float r_i = 0.2; float r_o = 0.7; glm::vec3 startSweep(0.0f, 0.0f, 1.0f); glm::vec3 endSweep(0.0f, 0.0f, 3.5f); // the sweep curve. auto sweepCurve = [=](float t) { return (1.0f-t)*startSweep + t * endSweep // glm::vec3(0.0f, -0.9f * t*t, 0.0f ); // glm::vec3(0.0f,0.5f,0.0f) * (float)sin(0.8f*2.0f*M_PI * t); ; }; const float STEP_LENGTH = 0.01; for(float t = 0.0f; t <= 1.0; t+=STEP_LENGTH) { float t2 = t+STEP_LENGTH; float t1 = t; glm::vec3 c = sweepCurve(t1); glm::vec3 v = sweepCurve(t2) - sweepCurve(t1); float deltaLength = glm::length(v); v = glm::normalize(v); glm::vec3 u; glm::vec3 w; FindBasis(v, u, w); auto e = [&](glm::vec3 x) { return glm::dot(u, x-c ); }; auto f = [&](glm::vec3 x) { return glm::dot(w, x-c ); }; auto b = [=](float rx) { float a = (rx - r_i) / (r_o - r_i); return 3*a*a*a*a - 4*a*a*a + 1; }; auto p = [&](const glm::vec3& x) { float rx = glm::length(x - c); if(rx < r_i) { return e(x); }else if(rx >= r_i && rx <= r_o) { return e(x) * b(rx); } else { return 0.0f; } }; auto q = [&](const glm::vec3& x) { float rx = glm::length(x - c); if(rx < r_i) { return f(x); }else if(rx >= r_i && rx <= r_o) { return f(x) * b(rx); } else { return 0.0f; } }; for(glm::vec3& x : mesh.vertices) { glm::vec3 grad_p = Gradient(p, x ); glm::vec3 grad_q = Gradient(q, x ); /* float rx = glm::length(x - c); float s = 1.0f; if(rx < r_i) { s *= deltaLength; } else if(rx >= r_i && rx <= r_o) { s *= b(rx); } else { s *= 0.0f; } // deformation field. //glm::vec3 D = (glm::cross(div_e, div_f)); glm::vec3 D = s * v; */ glm::vec3 D = deltaLength * glm::cross(grad_q, grad_p ); x += D; } } }