Exemple #1
0
/* {{{ void php_output_clean_all(TSRMLS_D)
 * Cleans all output handler buffers, without regard whether the handler is cleanable */
PHPAPI void php_output_clean_all(TSRMLS_D)
{
	php_output_context context;

	if (OG(active)) {
		php_output_context_init(&context, PHP_OUTPUT_HANDLER_CLEAN TSRMLS_CC);
		zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_clean, &context);
	}
}
Exemple #2
0
/* {{{ SUCCESS|FAILURE php_output_clean(TSRMLS_D)
 * Cleans the most recent output handlers buffer if the handler is cleanable */
PHPAPI int php_output_clean(TSRMLS_D)
{
	php_output_context context;

	if (OG(active) && (OG(active)->flags & PHP_OUTPUT_HANDLER_CLEANABLE)) {
		OG(active)->buffer.used = 0;
		php_output_context_init(&context, PHP_OUTPUT_HANDLER_CLEAN TSRMLS_CC);
		php_output_handler_op(OG(active), &context);
		php_output_context_dtor(&context);
		return SUCCESS;
	}
	return FAILURE;
}
Exemple #3
0
static inline void _http_flush(void *nothing, const char *data, size_t data_len TSRMLS_DC)
{
	PHPWRITE(data, data_len);
	/*	we really only need to flush when throttling is enabled,
		because we push the data as fast as possible anyway if not */
	if (HTTP_G->send.throttle_delay >= HTTP_DIFFSEC) {
		if (OG(ob_nesting_level)) {
			php_end_ob_buffer(1, 1 TSRMLS_CC);
		}
		if (!OG(implicit_flush)) {
			sapi_flush(TSRMLS_C);
		}
		http_sleep(HTTP_G->send.throttle_delay);
	}
}
Exemple #4
0
/* {{{ int php_output_write_unbuffered(const char *str, size_t len TSRMLS_DC)
 * Unbuffered write */
PHPAPI int php_output_write_unbuffered(const char *str, size_t len TSRMLS_DC)
{
	if (OG(flags) & PHP_OUTPUT_DISABLED) {
		return 0;
	}
	return sapi_module.ub_write(str, len TSRMLS_CC);
}
Exemple #5
0
/* {{{ int php_output_write(const char *str, size_t len TSRMLS_DC)
 * Buffered write */
PHPAPI int php_output_write(const char *str, size_t len TSRMLS_DC)
{
	if (OG(flags) & PHP_OUTPUT_DISABLED) {
		return 0;
	}
	php_output_op(PHP_OUTPUT_HANDLER_WRITE, str, len TSRMLS_CC);
	return (int) len;
}
Exemple #6
0
/* {{{ SUCCESS|FAILURE php_output_activate(TSRMLS_D)
 * Reset output globals and setup the output handler stack */
PHPAPI int php_output_activate(TSRMLS_D)
{
#ifdef ZTS
	memset((*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(output_globals_id)], 0, sizeof(zend_output_globals));
#else
	memset(&output_globals, 0, sizeof(zend_output_globals));
#endif

	zend_stack_init(&OG(handlers));

	return SUCCESS;
}
Exemple #7
0
/* {{{ void php_output_deactivate(TSRMLS_D)
 * Destroy the output handler stack */
PHPAPI void php_output_deactivate(TSRMLS_D)
{
	php_output_handler **handler = NULL;

	OG(active) = NULL;
	OG(running) = NULL;

	/* release all output handlers */
	if (OG(handlers).elements) {
		while (SUCCESS == zend_stack_top(&OG(handlers), (void *) &handler)) {
			php_output_handler_free(handler TSRMLS_CC);
			zend_stack_del_top(&OG(handlers));
		}
		zend_stack_destroy(&OG(handlers));
	}
}
Exemple #8
0
/* {{{ SUCCESS|FAILURE php_output_flush(TSRMLS_D)
 * Flush the most recent output handlers buffer */
PHPAPI int php_output_flush(TSRMLS_D)
{
	php_output_context context;

	if (OG(active) && (OG(active)->flags & PHP_OUTPUT_HANDLER_FLUSHABLE)) {
		php_output_context_init(&context, PHP_OUTPUT_HANDLER_FLUSH TSRMLS_CC);
		php_output_handler_op(OG(active), &context);
		if (context.out.data && context.out.used) {
			zend_stack_del_top(&OG(handlers));
			php_output_write(context.out.data, context.out.used TSRMLS_CC);
			zend_stack_push(&OG(handlers), &OG(active), sizeof(php_output_handler *));
		}
		php_output_context_dtor(&context);
		return SUCCESS;
	}
	return FAILURE;
}
Exemple #9
0
/* {{{ int php_output_get_level(TSRMLS_D)
 * Get output buffering level, ie. how many output handlers the stack contains */
PHPAPI int php_output_get_level(TSRMLS_D)
{
	return OG(active) ? zend_stack_count(&OG(handlers)) : 0;
}
Exemple #10
0
/* {{{ void php_output_discard_all(TSRMLS_D)
 * Discard all output handlers and buffers without regard whether a handler is removable */
PHPAPI void php_output_discard_all(TSRMLS_D)
{
	while (OG(active)) {
		php_output_stack_pop(PHP_OUTPUT_POP_DISCARD|PHP_OUTPUT_POP_FORCE TSRMLS_CC);
	}
}
Exemple #11
0
/* {{{ void php_output_end_all(TSRMLS_D)
 * Finalizes all output handlers and ends output buffering without regard whether a handler is removable */
PHPAPI void php_output_end_all(TSRMLS_D)
{
	while (OG(active) && php_output_stack_pop(PHP_OUTPUT_POP_FORCE TSRMLS_CC));
}
Exemple #12
0
/* {{{ void php_output_flush_all(TSRMLS_C)
 * Flush all output buffers subsequently */
PHPAPI void php_output_flush_all(TSRMLS_D)
{
	if (OG(active)) {
		php_output_op(PHP_OUTPUT_HANDLER_FLUSH, NULL, 0 TSRMLS_CC);
	}
}
Exemple #13
0
/* {{{ int php_output_get_status(TSRMLS_C)
 * Get output control status */
PHPAPI int php_output_get_status(TSRMLS_D)
{
	return OG(flags)
			| (OG(active) ? PHP_OUTPUT_ACTIVE : 0)
			| (OG(running)? PHP_OUTPUT_LOCKED : 0);
}
Exemple #14
0
/* {{{ void php_output_set_status(int status TSRMLS_DC)
 * Used by SAPIs to disable output */
PHPAPI void php_output_set_status(int status TSRMLS_DC)
{
	OG(flags) = status & 0xf;
}
Exemple #15
0
PHP_METHOD(imageObj, saveImage)
{
    zval *zobj = getThis();
    zval *zmap = NULL;
    char *filename = NULL;
    long filename_len = 0;
    php_image_object *php_image;
    php_map_object *php_map;
    int status = MS_SUCCESS;
    /* stdout specific vars */
    int size=0;
    void *iptr=NULL;

    PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sO",
                              &filename, &filename_len,
                              &zmap, mapscript_ce_map) == FAILURE) {
        PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
        return;
    }
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);

    php_image = (php_image_object *) zend_object_store_get_object(zobj TSRMLS_CC);
    if (zmap)
        php_map = (php_map_object *) zend_object_store_get_object(zmap TSRMLS_CC);

    if(filename_len > 0)
    {
        if ((status = msSaveImage((zmap ? php_map->map:NULL), php_image->image, filename) != MS_SUCCESS))
        {
            mapscript_throw_mapserver_exception("Failed writing image to %s" TSRMLS_CC, filename);
            return;
        }

        RETURN_LONG(status);
    }

    /* no filename - read stdout */

    /* if there is no output buffer active, set the header */
    //handle changes in PHP 5.4.x
    #if PHP_VERSION_ID < 50399
      if (OG(ob_nesting_level)<=0)
      {
          php_header(TSRMLS_C);
      }
    #else
      if (php_output_get_level(TSRMLS_C)<=0)
      {
          php_header(TSRMLS_C);
      }
    #endif

    if (MS_RENDERER_PLUGIN(php_image->image->format))
    {
        iptr = (void *)msSaveImageBuffer(php_image->image, &size, php_image->image->format);
    }
    else if (php_image->image->format->name && (strcasecmp(php_image->image->format->name, "imagemap")==0))
    {
        iptr = php_image->image->img.imagemap;
        size = strlen(php_image->image->img.imagemap);
    }

    if (size == 0) {
        mapscript_throw_mapserver_exception("Failed writing image to stdout" TSRMLS_CC);
        return;
    }
    else
    {
        php_write(iptr, size TSRMLS_CC);
        status = MS_SUCCESS;
        /* status = size;  why should we return the size ?? */
        msFree(iptr);
    }

    RETURN_LONG(status);
}
void DriverElasticCurvesRO(double *C1, double *C2, integer d, integer n, double w, bool rotated, bool isclosed,
	bool onlyDP, integer skipm, std::string solverstr, integer autoselectC, ProductElement *Xopt, bool &swap, double *fopts, double *comtime, integer &Nsout, integer &numinitialx)
{ // The first and last point of C1 and C2 should be the same if they are viewed as closed curves, i.e., isclosed = true.
	double threshold = M_PI / 2;
	integer minSkip = skipm;
	integer randshift = 0;
	bool computeCD1 = false;

	Solvers *solver = nullptr;
	// Let C2 be the complex one
	double TAC1 = ComputeTotalAngle(C1, d, n);
	double TAC2 = ComputeTotalAngle(C2, d, n);
	double *temppt, TACtemp;
    swap = false;

	// autoselectC: 0: keep the order, 1 and 2: C2 is the simple one
	if (autoselectC != 0)
	{
		//if (autoselectC == 1)
		//{
		//	if (TAC1 > TAC2)
		//	{
		//		temppt = C1;
		//		C1 = C2;
		//		C2 = temppt;
		//		TACtemp = TAC1;
		//		TAC1 = TAC2;
		//		TAC2 = TACtemp;
		//		swap = true;
		//	}
		//}
		//else
		//{
			if (TAC1 < TAC2)
			{
				temppt = C1;
				C1 = C2;
				C2 = temppt;
				TACtemp = TAC1;
				TAC1 = TAC2;
				TAC2 = TACtemp;
				swap = true;
			}
		//}
	}

	// find initial breaks and Ns
	integer *ms = new integer[n];
	integer lms = 0, ns = n;
	if (isclosed)
	{
		if (onlyDP)
		{
			skipm = (skipm < 1) ? 1 : skipm;
			for (integer i = 0; i < n - 1; i += skipm)
			{
				ms[lms] = i;
				lms++;
			}
		}
		else
		{
			if (autoselectC != 1)
			{
				if (TAC2 > TAC1)
				{
					FindInitialBreaksAndNs(C2, d, n, minSkip, threshold, randshift, ms, lms, ns);
				}
				else
				{
					FindInitialBreaksAndNs(C1, d, n, minSkip, threshold, randshift, ms, lms, ns);
					for (integer i = 1; i < lms; i++)
					{
						ms[i] = n - ms[i];
					}
				}
			}
			else
			{
				if (TAC2 < TAC1)
				{
					FindInitialBreaksAndNs(C2, d, n, minSkip, threshold, randshift, ms, lms, ns);
					ns = static_cast<int> (static_cast<double> (n) / 3);
					ns = (ns > 30) ? 30 : ns;
					ns += static_cast<int> (TAC1 / M_PI * 2.0);
				}
				else
				{
					FindInitialBreaksAndNs(C1, d, n, minSkip, threshold, randshift, ms, lms, ns);
					for (integer i = 1; i < lms; i++)
					{
						ms[i] = n - ms[i];
					}
					ns = static_cast<int> (static_cast<double> (n) / 3);
					ns = (ns > 30) ? 30 : ns;
					ns += static_cast<int> (TAC2 / M_PI * 2.0);
				}
			}
		}
	}
	else
	{
		ms[0] = 0;
		lms = 1;
		if (!onlyDP)
		{
			ns = static_cast<int> (static_cast<double> (n) / 3);
			ns = (ns > 30) ? 30 : ns;
			ns += static_cast<int> (TAC2 / M_PI * 2.0);
		}
	}
	Nsout = ns;
	numinitialx = lms;

	// create manifold and initial iterate objects.
	integer numofmanis = 3;
	integer numofmani1 = 1;
	integer numofmani2 = 1;
	integer numofmani3 = 1;
	L2Sphere TNS(n);
	OrthGroup OG(d);
	Euclidean Euc(1);
	ProductManifold *Domain = nullptr;
	Domain = new ProductManifold(numofmanis, &TNS, numofmani1, &OG, numofmani2, &Euc, numofmani3);

// 	Domain->SetIsIntrApproach(false);

	L2SphereVariable TNSV(n);
	OrthGroupVariable OGV(d);
	EucVariable EucV(1);
	ProductElement *InitialX = nullptr;
	InitialX = new ProductElement(numofmanis, &TNSV, numofmani1, &OGV, numofmani2, &EucV, numofmani3);
	double *Xptr = InitialX->ObtainWriteEntireData();

	// initialize rotation and shift:
	Xptr[n + d * d] = 0;
	for (integer j = 0; j < d; j++)
	{
		Xptr[n + j + j * d] = 1;
		for (integer k = j + 1; k < d; k++)
		{
			Xptr[n + k + j * d] = 0;
			Xptr[n + j + k * d] = 0;
		}
	}


	// find initialX for each break and run the solver
	ElasticCurvesRO *ECRO = nullptr;
	double *C2shift = new double[5 * d * n + d * d + n + lms + 2 * d * d];
	double *q2shift = C2shift + d * n;
	double *q1 = q2shift + d * n;
	double *O = q1 + d * n;
	double *Rotq2shift = O + d * d;
	double *RotC2shift = Rotq2shift + d * n;
	double *DPgam = RotC2shift + d * n;
	double *msV = DPgam + n;
	double *O2 = msV + lms;
	double *O3 = O2 + d * d; // d * d

	double *C1s = nullptr, *C2s = nullptr, *q1s = nullptr, *q2s = nullptr, *DPgams = nullptr;
	if (!onlyDP)
	{
		C1s = new double[4 * d * ns + ns];
		C2s = C1s + d * ns;
		q1s = C2s + d * ns;
		q2s = q1s + d * ns;
		DPgams = q2s + d * ns; // ns
	}
	double *C2_coefs = nullptr, *q2 = nullptr;
	if (onlyDP)
	{
		C2_coefs = new double[4 * d * (n - 1) + n * d];
		q2 = C2_coefs + 4 * d * (n - 1);
	}

	CurveToQ(C1, d, n, q1, isclosed);

	char *transn = const_cast<char *> ("n"), *transt = const_cast<char *> ("t");
	double one = 1, zero = 0;
	integer dd = d * d, inc = 1;
	unsigned long starttime = getTickCount();
	double minmsV = 10000;

	if (!onlyDP)
	{
		GetCurveSmall(C1, C1s, d, n, ns, isclosed);
		CurveToQ(C1s, d, ns, q1s, isclosed);
	}
	//Rcpp::Rcout << "lms:" << lms << ", ns:" << ns << std::endl;//----
	//for (integer i = 0; i < lms; i++) //---
	//{
	//	Rcpp::Rcout << ms[i] << std::endl;
	//}

	double *Xoptptr = Xopt->ObtainWriteEntireData();
	Xoptptr[n + d * d] = 0;

    for(integer i = 0; i < 5; i++)
    {
        fopts[i] = 1000;
        comtime[i] = static_cast<double>(getTickCount() - starttime) / CLK_PS;
    }

	for (integer i = 0; i < lms; i++) //lms
	{
		//Rcpp::Rcout << ms[i] << ", ";
        starttime = getTickCount();
		// obtain initial reparameterization
		ShiftC(C2, d, n, C2shift, ms[i]);
		CurveToQ(C2shift, d, n, q2shift, isclosed);

		if (rotated)
		{
			FindBestRotation(q1, q2shift, d, n, O);
// 			//ForDebug::Print("O best rot:", O, d, d);//---
//             for(integer j = 0; j < d; j++)//--------
//             {
//                 O[j + j * d] = 1;
//                 for(integer k = j + 1; k < d; k++)
//                 {
//                     O[j + k * d] = 0;
//                     O[k + j * d] = 0;
//                 }
//             }//-------------
			dgemm_(transn, transt, &n, &d, &d, &one, q2shift, &n, O, &d, &zero, Rotq2shift, &n);
			dgemm_(transn, transt, &n, &d, &d, &one, C2shift, &n, O, &d, &zero, RotC2shift, &n);
		}
		else
		{
			integer nd = n * d, inc = 1;
			dcopy_(&nd, q2shift, &inc, Rotq2shift, &inc);
			dcopy_(&nd, C2shift, &inc, RotC2shift, &inc);
		}

		if (!onlyDP)
		{
			GetCurveSmall(RotC2shift, C2s, d, n, ns, isclosed);
			CurveToQ(C2s, d, ns, q2s, isclosed);
 			DynamicProgramming(q2s, q1s, d, ns, DPgams, isclosed);
			//for (integer j = 0; j < ns; j++) //---
			//{
			//	DPgams[j] = static_cast<double> (j) / (ns - 1);///----
			//}//---
//         ForDebug::Print("DPgams:", DPgams, 1, ns);//----
			ReSampleGamma(DPgams, ns, DPgam, n);
		}
		else
		{
			DynamicProgramming(Rotq2shift, q1, d, n, DPgam, isclosed);

			if (rotated)
			{
				if (computeCD1)
				{
					if (isclosed)
						GradientPeriod(DPgam, n, 1.0 / (n - 1), Xptr);
					else
						Gradient(DPgam, n, 1.0 / (n - 1), Xptr);
					for (integer j = 0; j < n; j++)
					{
						Xptr[j] = sqrt(Xptr[j]);
					}
					ECRO = new ElasticCurvesRO(q1, Rotq2shift, d, n, w, rotated, isclosed);
					ECRO->SetDomain(Domain);
					//Rcpp::Rcout << "CD1 func:" << ECRO->f(InitialX) << std::endl;
				}

				if (isclosed)
				{
					for (integer j = 0; j < d; j++)
					{
						Spline::SplineUniformPeriodic(RotC2shift + j * n, n, 1.0 / (n - 1), C2_coefs + j * 4 * (n - 1));
					}
				}
				else
				{
					for (integer j = 0; j < d; j++)
					{
						Spline::SplineUniformSlopes(RotC2shift + j * n, n, 1.0 / (n - 1), C2_coefs + j * 4 * (n - 1));
					}
				}
				for (integer j = 0; j < n; j++)
				{
					for (integer k = 0; k < d; k++)
					{
						RotC2shift[j + k * n] = Spline::ValSplineUniform(C2_coefs + k * 4 * (n - 1), n, 1.0 / (n - 1), DPgam[j]);
					}
				}
				CurveToQ(RotC2shift, d, n, q2, isclosed);
				FindBestRotation(q1, q2, d, n, O2);
//                 ForDebug::Print("O:", O, d, d);//---
//                 ForDebug::Print("O2:", O2, d, d);//---
				dgemm_(transn, transn, &d, &d, &d, &one, O, &d, O2, &d, &zero, O3, &d);
//                 ForDebug::Print("O3:", O3, d, d);//---
				dcopy_(&dd, O3, &inc, O, &inc);
				dcopy_(&dd, O2, &inc, Xptr + n, &inc); // used to evaluate the cost function
			}
		}

		if (isclosed)
			GradientPeriod(DPgam, n, 1.0 / (n - 1), Xptr);
		else
			Gradient(DPgam, n, 1.0 / (n - 1), Xptr);
//         ForDebug::Print("DPgam:", DPgam, 1, n);//----
//         ForDebug::Print("Xptr:", Xptr, 1, n);//----

		for (integer j = 0; j < n; j++)
		{
			Xptr[j] = sqrt(Xptr[j]);
		}

//         ForDebug::Print("Xptr:", Xptr, 1, n);//----
		//ForDebug::Print("q1:", q1, n, d);//---
		//ForDebug::Print("Rotq2shift:", Rotq2shift, n, d);//---

		// Compute reparameterization for q1 and rotated and shifted q2;
		ECRO = new ElasticCurvesRO(q1, Rotq2shift, d, n, w, rotated, isclosed);
		ECRO->SetDomain(Domain);
		//Domain->SetHasHHR(true);//--
		//ECRO->CheckGradHessian(InitialX);//--
		if (onlyDP)
		{
			ECRO->w = 0;
			msV[i] = ECRO->f(InitialX);
			//Rcpp::Rcout << "CD1H func:" << msV[i] << std::endl;
		}
		if (!onlyDP)
		{
			//if (solverstr == "RNewton")
			//	solver = new RNewton(ECRO, InitialX);
			//else
			if (solverstr == "RBFGS")
			{
				solver = new RBFGS(ECRO, InitialX);
				dynamic_cast<SolversLS *> (solver)->Initstepsize = 0.001;
			}
			else
			if (solverstr == "LRBFGS")
			{
				solver = new LRBFGS(ECRO, InitialX);
				dynamic_cast<SolversLS *> (solver)->Initstepsize = 0.001;
			}
			else
			if (solverstr == "RCG")
			{
				solver = new RCG(ECRO, InitialX);
				dynamic_cast<SolversLS *> (solver)->Initstepsize = 0.001;
			}
			else
			if (solverstr == "RSD")
			{
				solver = new RSD(ECRO, InitialX);
				dynamic_cast<SolversLS *> (solver)->Initstepsize = 0.001;
			}
			else
			//if (solverstr == "RTRNewton")
			//	solver = new RTRNewton(ECRO, InitialX);
			//else
			if (solverstr == "RTRSR1")
			{
				solver = new RTRSR1(ECRO, InitialX);
				dynamic_cast<SolversTR *> (solver)->kappa = 0.1;
				dynamic_cast<SolversTR *> (solver)->theta = 1.0;
			}
			else
			if (solverstr == "LRTRSR1")
			{
				solver = new LRTRSR1(ECRO, InitialX);
				dynamic_cast<SolversTR *> (solver)->kappa = 0.1;
				dynamic_cast<SolversTR *> (solver)->theta = 1.0;
			}
			else
			if (solverstr == "RTRSD")
			{
				solver = new RTRSD(ECRO, InitialX);
			}
			else
			{
				Rcpp::Rcout << "This solver is not used in this problem!" << std::endl;
				delete ECRO;
				delete solver;
				delete[] C2shift;
				if (C2_coefs != nullptr)
				{
					delete[] C2_coefs;
				}
				if (C1s != nullptr)
				{
					delete[] C1s;
				}
				delete[] ms;
				delete Domain;
				delete InitialX;
				return;
			}

			//Domain->CheckIntrExtr(InitialX);//--------

			//solver->OutputGap = 100;
			solver->Max_Iteration = 500;
			solver->Min_Iteration = 10;
			solver->DEBUG = NOOUTPUT; //--FINALRESULT;//--NOOUTPUT; //ITERRESULT
			solver->Stop_Criterion = FUN_REL;
			solver->Tolerance = 1e-3;
			solver->Run();
			ECRO->w = 0;
			//--Xopt->RemoveAllFromTempData();
			msV[i] = ECRO->f(const_cast<Element *> (solver->GetXopt()));
			//Rcpp::Rcout << solverstr << "func:" << msV[i] << ", num of iter:" << solver->GetIter() << std::endl;//---
		}
		//ECRO->CheckGradHessian(solver->GetXopt());//--
		delete ECRO;

		if (msV[i] < minmsV)
		{
			minmsV = msV[i];
			if (onlyDP)
			{
				for (integer j = 0; j < n; j++)
				{
					Xoptptr[j] = DPgam[j];
				}
            //ForDebug::Print("O:", O, d, d);//-----
				dcopy_(&dd, O, &inc, Xoptptr + n, &inc);
				Xoptptr[n + d * d] = static_cast<double> (ms[i]) / (n - 1);
			}
			else
			{
				solver->GetXopt()->CopyTo(Xopt);
//                 solver->GetXopt()->Print("XOPT:");//---
				Xoptptr = Xopt->ObtainWritePartialData();
				for (integer j = 0; j < n; j++)
				{
					Xoptptr[j] *= Xoptptr[j];
				}
				double tmp1 = Xoptptr[0], tmp2 = 0;
                Xoptptr[0] = 0;
				for (integer j = 1; j < n; j++)
				{
					tmp2 = Xoptptr[j];
					Xoptptr[j] = Xoptptr[j - 1] + (tmp1 + tmp2) / 2 / (n - 1);
					tmp1 = tmp2;
				}
				//ForDebug::Print("XoptO1:", Xoptptr + n, d, d);//----
				dgemm_(transn, transt, &d, &d, &d, &one, O, &d, Xoptptr + n, &d, &zero, O2, &d);\
				dcopy_(&dd, O2, &inc, Xoptptr + n, &inc);
				//ForDebug::Print("XoptO2:", Xoptptr + n, d, d);//----

//                 Rcpp::Rcout << "ms[i]:" << ms[i] << ",:" << static_cast<double> (ms[i]) / (n - 1) << std::endl;//---
//                 Rcpp::Rcout << "Xoptptr[n + d * d]:" << Xoptptr[n + d * d] << std::endl;//---
				Xoptptr[n + d * d] = Xoptptr[n + d * d] + static_cast<double> (ms[i]) / (n - 1);
			}
		}
		if (!onlyDP)
			delete solver;

        comtime[0] += (double) (getTickCount() - starttime) / CLK_PS;
        if(msV[i] < fopts[0])
            fopts[0] = msV[i];
        if(i % 2 == 0)
        {
            comtime[1] += (double) (getTickCount() - starttime) / CLK_PS;
            if(msV[i] < fopts[1])
                fopts[1] = msV[i];
        }
        if(i % 4 == 0)
        {
            comtime[2] += (double) (getTickCount() - starttime) / CLK_PS;
            if(msV[i] < fopts[2])
                fopts[2] = msV[i];
        }
        if(i % 8 == 0)
        {
            comtime[3] += (double) (getTickCount() - starttime) / CLK_PS;
            if(msV[i] < fopts[3])
                fopts[3] = msV[i];
        }
        if(i % 16 == 0)
        {
            comtime[4] += (double) (getTickCount() - starttime) / CLK_PS;
            if(msV[i] < fopts[4])
                fopts[4] = msV[i];
        }
	}

	//Rcpp::Rcout << "min f:" << minmsV << std::endl;
	//Rcpp::Rcout << "time:" << comtime[0] << std::endl;
	delete[] C2shift;
	if (C2_coefs != nullptr)
	{
		delete[] C2_coefs;
	}
	if (C1s != nullptr)
	{
		delete[] C1s;
	}
	delete[] ms;
	delete InitialX;
	delete Domain;
};