Function performs multi objective downhill simplex
486983
Object[] mosim(double S[][], double SF[][], int SR[], int n, double minn[], double maxn[]) {
        int lenS = S[0].length;
        int lenSF = SF[0].length;

//        int e = S.length;
        int r = lenS + lenSF + 1;
        // Define Simplex .. Simplex = [S SF SR];
        double Simplex[][] = new double[S.length][lenS + lenSF + 1];

        for (int i = 0; i < S.length; i++) {
            for (int j = 0; j < lenS; j++) {
                Simplex[i][j] = S[i][j];
            }
            for (int j = 0; j < lenSF; j++) {
                Simplex[i][lenS + j] = SF[i][j];
            }
            Simplex[i][lenS + lenSF] = (double) SR[i];
        }
        // Sort Simplex
        sort(Simplex, r - 1);
        //Assing function values for worst point in Simplex
        double Fw[] = new double[lenSF];
        for (int i = 0; i < lenSF; i++) {
            Fw[i] = Simplex[Simplex.length - 1][n + i];
        }
        // Assing parameter values worst ranked point in Simplex
        double Sw[] = new double[n];
        for (int i = 0; i < n; i++) {
            Sw[i] = Simplex[Simplex.length - 1][i];
        }
        double Sg[] = new double[n];
        // Compute centroid of Simplex after excluding the worst ranked point
        for (int i = 0; i < n; i++) {
            Sg[i] = 0;
            for (int j = 0; j < Simplex.length - 1; j++) {
                Sg[i] += Simplex[j][i];
            }
            Sg[i] /= (double) (Simplex.length - 1);
        }
        // Compute Reflection step
        double Sref[] = new double[n];
        for (int i = 0; i < n; i++) {
            Sref[i] = 2 * Sg[i] - Sw[i];
        }
        boolean accept = isSampleValid(Sref);
        double Snew[], Fnew[];
        if (!accept) {
            // Compute contraction step
            double Scon[] = new double[n];
            for (int i = 0; i < n; i++) {
                Scon[i] = 0.5 * Sg[i] + 0.5 * Sw[i];
            }
            accept = isSampleValid(Scon);
            //if it fails again, try a mutation
            while (!accept) {
                double newpar[][] = lhsu(this.lowBound, this.upBound, 1);
                Scon = newpar[0];
                accept = isSampleValid(Scon);
            }
            double Fcon[] = compob(Scon);
            Snew = Scon;
            Fnew = Fcon;
        // Update number of function evaluations
        } else {
            // Compute corresponding objective function values
            double Fref[] = compob(Sref);
            double SimplexTmp[][] = new double[Simplex.length][lenSF];
            for (int j = 0; j < lenSF; j++) {
                for (int i = 0; i < Simplex.length - 1; i++) {
                    SimplexTmp[i][j] = Simplex[i][n + j];
                }
                SimplexTmp[Simplex.length - 1][j] = Fref[j];
            }
            // Test for non dominance
            Object ret[] = parrank(SimplexTmp, 2); // 2 ist possible wrong
            int Rref[] = (int[]) ret[0];

            int Rrefmax = Integer.MIN_VALUE;
            for (int i = 0; i < Rref.length - 1; i++) {
                if (Rref[i] > Rrefmax) {
                    Rrefmax = Rref[i];
                }
            }
            if (Rref[Rref.length - 1] <= Rrefmax) {
                Snew = Sref;
                Fnew = Fref;
            } else {
                // Compute contraction step
                double Scon[] = new double[n];
                for (int i = 0; i < n; i++) {
                    Scon[i] = 0.5 * Sg[i] + 0.5 * Sw[i];
                }
                accept = isSampleValid(Scon);
                //if it fails again, try a mutation
                while (!accept) {
                    double newpar[][] = lhsu(this.lowBound, this.upBound, 1);
                    Scon = newpar[0];
                    accept = isSampleValid(Scon);
                }
                // Compute corresponding objective function values
                double Fcon[] = compob(Scon);
                Snew = Scon;
                Fnew = Fcon;
            }
        }
        for (int i = 0; i < n; i++) {
            S[S.length - 1][i] = Snew[i];
        }
        for (int i = 0; i < SF[0].length; i++) {
            SF[SF.length - 1][i] = Fnew[i];
        }
        return new Object[]{S, SF};
    }
1118344
def _get_numdiff_hessian_element_func(objective_func, nmr_steps, step_ratio):
    """"""
    return SimpleCLFunction.from_string('''
        /**
         * Compute the Hessian using (possibly) multiple steps with various interpolations. 
         */ 
        void _numdiff_hessian_element(
                void* data, local mot_float_type* x_tmp, mot_float_type f_x_input,
                uint px, uint py, global float* initial_step, global double* derivative, 
                global double* error, local double* scratch){

            const uint nmr_steps = ''' + str(nmr_steps) + ''';
            uint nmr_steps_remaining = nmr_steps;

            local double* scratch_ind = scratch;
            local double* steps = scratch_ind;      scratch_ind += nmr_steps; 
            local double* errors = scratch_ind;     scratch_ind += nmr_steps - 1;
            local double* steps_tmp = scratch_ind;  scratch_ind += nmr_steps;
            
            if(get_local_id(0) == 0){
                for(int i = 0; i < nmr_steps - 1; i++){
                    errors[i] = 0;
                }
            }
            barrier(CLK_LOCAL_MEM_FENCE);
            
            _numdiff_hessian_steps(data, x_tmp, f_x_input, px, py, steps, initial_step);

            if(nmr_steps_remaining > 1){
                nmr_steps_remaining = _numdiff_hessian_richardson_extrapolation(steps); 
                barrier(CLK_LOCAL_MEM_FENCE);
            }
            
            if(nmr_steps_remaining >= 3){
                nmr_steps_remaining = _numdiff_wynn_extrapolation(steps, errors, nmr_steps_remaining);
                barrier(CLK_LOCAL_MEM_FENCE);                
            }
            
            if(nmr_steps_remaining > 1){
                _numdiff_find_best_step(steps, errors, steps_tmp, nmr_steps_remaining);
                barrier(CLK_LOCAL_MEM_FENCE);
            }
            
            if(get_local_id(0) == 0){
                *derivative = steps[0];
                *error = errors[0];
            }
        }
    ''', dependencies=[
        _get_numdiff_hessian_steps_func(objective_func, nmr_steps, step_ratio),
        _get_numdiff_hessian_richardson_extrapolation_func(nmr_steps, step_ratio),
        _get_numdiff_wynn_extrapolation_func(),
        _get_numdiff_find_best_step_func()
    ])
697645
function constrainedMDSGradient(x, fxprime, distances, constraints) {
        var loss = 0, i;
        for (i = 0; i < fxprime.length; ++i) {
            fxprime[i] = 0;
        }

        for (i = 0; i < distances.length; ++i) {
            var xi = x[2 * i], yi = x[2 * i + 1];
            for (var j = i + 1; j < distances.length; ++j) {
                var xj = x[2 * j], yj = x[2 * j + 1],
                    dij = distances[i][j],
                    constraint = constraints[i][j];

                var squaredDistance = (xj - xi) * (xj - xi) + (yj - yi) * (yj - yi),
                    distance$$1 = Math.sqrt(squaredDistance),
                    delta = squaredDistance - dij * dij;

                if (((constraint > 0) && (distance$$1 <= dij)) ||
                    ((constraint < 0) && (distance$$1 >= dij))) {
                    continue;
                }

                loss += 2 * delta * delta;

                fxprime[2*i]     += 4 * delta * (xi - xj);
                fxprime[2*i + 1] += 4 * delta * (yi - yj);

                fxprime[2*j]     += 4 * delta * (xj - xi);
                fxprime[2*j + 1] += 4 * delta * (yj - yi);
            }
        }
        return loss;
    }
659510
def _create_w_objective(m, X, R):
    
    genes, clusters = m.shape
    cells = X.shape[1]
    R1 = R.reshape((genes, 1)).dot(np.ones((1, cells)))
    def objective(w):
        # convert w into a matrix first... because it's a vector for
        # optimization purposes
        w = w.reshape((m.shape[1], X.shape[1]))
        d = m.dot(w)+eps
        return np.sum((X + R1)*np.log(d + R1) - X*np.log(d))/genes
    def deriv(w):
        # derivative of objective wrt all elements of w
        # for w_{ij}, the derivative is... m_j1+...+m_jn sum over genes minus 
        # x_ij
        w2 = w.reshape((m.shape[1], X.shape[1]))
        d = m.dot(w2)+eps
        temp = X/d
        temp2 = (X+R1)/(d+R1)
        m1 = m.T.dot(temp2)
        m2 = m.T.dot(temp)
        deriv = m1 - m2
        return deriv.flatten()/genes
    return objective, deriv
633365
def create_objective(dist, abscissas):
    """"""
    abscissas_ = numpy.array(abscissas[1:-1])
    def obj(absisa):
        """Local objective function."""
        out = -numpy.sqrt(dist.pdf(absisa))
        out *= numpy.prod(numpy.abs(abscissas_ - absisa))
        return out
    return obj
259749
def _create_m_objective(w, X):
    
    clusters, cells = w.shape
    genes = X.shape[0]
    w_sum = w.sum(1)
    def objective(m):
        m = m.reshape((X.shape[0], w.shape[0]))
        d = m.dot(w)+eps
        temp = X/d
        w2 = w.dot(temp.T)
        deriv = w_sum - w2.T
        return np.sum(d - X*np.log(d))/genes, deriv.flatten()/genes
    return objective
1267211
def xstep(self):
        
        """

        b = self.AHSf + self.rho*np.sum(
            np.conj(self.Gf)*sl.rfftn(self.Y-self.U, axes=self.axes),
            axis=self.Y.ndim-1)
        self.Xf = b / (self.AHAf + self.rho*self.GHGf)
        self.X = sl.irfftn(self.Xf, self.axsz, axes=self.axes)

        if self.opt['LinSolveCheck']:
            ax = (self.AHAf + self.rho*self.GHGf)*self.Xf
            self.xrrs = sl.rrs(ax, b)
        else:
            self.xrrs = None
784119
def acquisition_function_withGradients(self, x):
        
        aqu_x      = self.acquisition_function(x)
        aqu_x_grad = self.d_acquisition_function(x)
        return aqu_x, aqu_x_grad
154684
def _rank_optimize(self):
        
        assert self.dag
        for l in reversed(self.layers):
            for v in l:
                gv = self.grx[v]
                for x in v.N(-1):
                   if all((self.grx[y].rank>=gv.rank for y in x.N(+1))):
                        gx = self.grx[x]
                        self.layers[gx.rank].remove(x)
                        gx.rank = gv.rank-1
                        self.layers[gv.rank-1].append(x)
653355
def getLinearityFunction(expTimes, imgs, mxIntensity=65535, min_ascent=0.001,
                         ):
    '''
    
    '''
    # TODO: calculate [min_ascent] from noise function
    # instead of having it as variable

    ascent, offset, error = linRegressUsingMasked2dArrays(
        expTimes, imgs, imgs > mxIntensity)

    ascent[np.isnan(ascent)] = 0
    # remove low frequent noise:
    if min_ascent > 0:
        i = ascent < min_ascent
        offset[i] += (0.5 * (np.min(expTimes) + np.max(expTimes))) * ascent[i]
        ascent[i] = 0

    return offset, ascent, error
856584
def main(dimension, iterations):
    
    objective_function = minimize(functions.sphere)
    stopping_condition = max_iterations(iterations)
    (solution, metrics) = optimize(objective_function=objective_function,
                                   domain=Domain(-5.12, 5.12, dimension),
                                   stopping_condition=stopping_condition,
                                   parameters={'seed': 3758117674,
                                               'rho': 1.0, 'e_s': 15, 'e_f': 5},
                                   velocity_update=gc_velocity_update,
                                   parameter_update=update_rho,
                                   measurements=[fitness_measurement])
    return solution
95213
public Object[] mocom(int n, int s, double minn[], double maxn[], int m, int MaxIter) {
        // Start with generating the initial population
        double D[][] = lhsu(minn, maxn, s);
        // Compute the objective function value for each point
        double F[][] = compf(D, m);
        // Now save some important variables
        int nobj = F[0].length;
        // Now do Pareto ranking
        Object ret[] = parrank(F, nobj);
        int R[] = (int[]) ret[0];
        int Rmax = ((Integer) ret[1]).intValue();
        // Now start optimization loop
        int loopcounter = 1;
        while (Rmax > 1) {
            // Assign selection probability P(i) to each of the members
            double P[] = asswght(R, Rmax, s);

            // Construct A to be the points having largest ranks
            ret = worst(D, n, R, Rmax);
            double A[][] = (double[][]) ret[0]; //A <- worst Points
            int L[] = (int[]) ret[1];
            int nA = ((Integer) ret[2]).intValue();

            // Select n points from D to generate Simplex
            ret = choose(P, D, F, R, n);
            double S1[][] = (double[][]) ret[0];
            double F1[][] = (double[][]) ret[1];
            int R1[] = (int[]) ret[2];

            double FA[][] = new double[nA][F[0].length];

            for (int j = 0; j < nA; j++) {
                //addsim
                if (S1.length != F1.length || F1.length != R1.length) {
                    throw new RuntimeException("Component " + this + ": internal error.");
                }
                //build S,SF,SR
                double S[][] = new double[S1.length + 1][S1[0].length];
                double SF[][] = new double[F1.length + 1][F1[0].length];
                int SR[] = new int[R1.length + 1];

                for (int c1 = 0; c1 < S1.length; c1++) {
                    for (int c2 = 0; c2 < S1[0].length; c2++) {
                        S[c1][c2] = S1[c1][c2];
                    }
                    for (int c2 = 0; c2 < F1[0].length; c2++) {
                        SF[c1][c2] = F1[c1][c2];
                    }
                    SR[c1] = R1[c1];
                }
                for (int c1 = 0; c1 < S1[0].length; c1++) {
                    S[S1.length][c1] = A[j][c1];
                }

                for (int c1 = 0; c1 < F1[0].length; c1++) {
                    SF[F1.length][c1] = F[L[j]][c1];
                }
                SR[R1.length] = R[L[j]];

                //mosim
                Object res[] = mosim(S, SF, SR, n, minn, maxn);

                S = (double[][]) res[0];
                SF = (double[][]) res[1];

                for (int c1 = 0; c1 < n; c1++) {
                    A[j][c1] = S[S.length - 1][c1];
                }
                for (int c1 = 0; c1 < FA[j].length; c1++) {
                    FA[j][c1] = SF[SF.length - 1][c1];
                }
            }
            ret = update(D, L, A, n, nA, F, FA);
            D = (double[][]) ret[0];
            F = (double[][]) ret[1];

            // Compute paretorank for each of the parameter sets according to Goldberg, 1989
            ret = parrank(F, nobj);
            R = (int[]) ret[0];
            Rmax = ((Integer) ret[1]).intValue();
            //System.out.println("Evolution Loop:" + loopcounter + " - Trial - " + icall);

            double currentResult[][] = new double[s][N + M + 1];
            for (int i = 0; i < s; i++) {
                for (int j = 0; j < N; j++) {
                    currentResult[i][j] = D[i][j];
                }
                for (int j = 0; j < M; j++) {
                    currentResult[i][N + j] = F[i][j];
                }
                currentResult[i][N + M] = R[i];
            }
            sort(currentResult, N + M);

            int c = 0;
            if (continousOutput) {
                while (c < s && loopcounter % 10 == 0) {
                    //writer.write("Parameter: ");
                    writer.write("" + currentResult[c][N + M] + "\t");
                    for (int i = 0; i < N; i++) {
                        writer.write("" + currentResult[c][i] + "\t");
                    }
                    //System.out.print("Function - Values:");
                    for (int i = 0; i < M; i++) {
                        writer.write("" + currentResult[c][N + i] + "\t");
                    }
                    //System.out.print("Rank:");
                    //System.out.println("");
                    c++;
                    writer.println("");
                    writer.flush();
                }
            }
            loopcounter++;
            if (MaxIter < loopcounter) {
                System.out.println("********************************************");
                System.out.println("---------->OPTIMIZATION STOP<---------------");
                System.out.println("--->MAXIMUM NUMBER OF LOOPS HAS REACHED<----");
                System.out.println("********************************************");
                c = 0;
                System.out.print("Rank\t");
                for (int i = 0; i < N; i++) {
                    System.out.print(this.parameterNames[i] + "\t");
                }
                for (int i = 0; i < M; i++) {
                    System.out.print(this.effNames[i] + "\t");
                }
                System.out.println("");
                while (c < s) {
                    System.out.print("" + currentResult[c][N + M] + "\t");
                    for (int i = 0; i < N; i++) {
                        System.out.print("" + currentResult[c][i] + "\t");
                    }
                    for (int i = 0; i < M; i++) {
                        System.out.print("" + currentResult[c][N + i] + "\t");
                    }
                    System.out.println("");
                    c++;
                }
                c = 0;
                writer.println("Number of model runs: " + icall);
                writer.write("Rank\t");
                for (int i = 0; i < N; i++) {
                    writer.write(this.parameterNames[i] + "\t");
                }
                for (int i = 0; i < M; i++) {
                    writer.write(this.effNames[i] + "\t");
                }
                writer.write("\n");
                while (c < s) {
                    writer.write("" + currentResult[c][N + M] + "\t");
                    for (int i = 0; i < N; i++) {
                        writer.write("" + currentResult[c][i] + "\t");
                    }
                    for (int i = 0; i < M; i++) {
                        writer.write("" + currentResult[c][N + i] + "\t");
                    }
                    c++;
                    writer.println("");
                    writer.flush();
                }
                writer.flush();
                break;
            }
            if (Rmax <= 1) {
                System.out.println("********************************************");
                System.out.println("---------->OPTIMIZATION STOP<---------------");
                System.out.println("-------------->SUCCESSFUL<------------------");
                System.out.println("********************************************");
                c = 0;
                System.out.print("Rank\t");
                for (int i = 0; i < N; i++) {
                    System.out.print(this.parameterNames[i] + "\t");
                }
                for (int i = 0; i < M; i++) {
                    System.out.print(this.effNames[i] + "\t");
                }
                System.out.println("");

                while (c < s) {
                    System.out.print("" + currentResult[c][N + M] + "\t");
                    for (int i = 0; i < N; i++) {
                        System.out.print("" + currentResult[c][i] + "\t");
                    }
                    for (int i = 0; i < M; i++) {
                        System.out.print("" + currentResult[c][N + i] + "\t");
                    }
                    System.out.println("");
                    c++;
                }
                c = 0;
                writer.println("Number of model runs: " + this.icall);
                writer.write("Rank\t");
                for (int i = 0; i < N; i++) {
                    writer.write(this.parameterNames[i] + "\t");
                }
                for (int i = 0; i < M; i++) {
                    writer.write(this.effNames[i] + "\t");
                }
                writer.println();
                while (c < s) {
                    writer.write("" + currentResult[c][N + M] + "\t");
                    for (int i = 0; i < N; i++) {
                        writer.write("" + currentResult[c][i] + "\t");
                    }
                    for (int i = 0; i < M; i++) {
                        writer.write("" + currentResult[c][N + i] + "\t");
                    }
                    c++;
                    writer.println();
                    writer.flush();
                }
                writer.flush();
            }
        }
        return new Object[]{D, F, R, new Integer(Rmax)};
    }
123141
def constant_cost_withGradients(x):
    
    return np.ones(x.shape[0])[:,None], np.zeros(x.shape)
424955
def optimize(self, angles0, target):
        """"""
        def new_objective(angles):
            return self.f(angles, target)

        return scipy.optimize.minimize(
            new_objective,
            angles0,
            **self.optimizer_opt).x
734300
function Gradient($x, $y, $w, $h, $type, $stops=array(), $colorspace='RGB', $coords='', $extend='', $return=false, $is_mask=false) {
	if (strtoupper(substr($type,0,1)) == 'L') { $type = 2; }	// linear
	else if (strtoupper(substr($type,0,1)) == 'R') { $type = 3; }	// radial
	if ($colorspace != 'CMYK' && $colorspace != 'Gray') {
		$colorspace = 'RGB';
	}
	$bboxw = $w;
	$bboxh = $h;
	$usex = $x;
	$usey = $y;
	$usew = $bboxw;
	$useh = $bboxh;

	if ($type < 1) { $type = 2; }
	if ($coords[0]!==false && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$coords[0],$m)) { 
		$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
		if ($tmp) { $coords[0] = $tmp/$w; }
	}
	if ($coords[1]!==false && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$coords[1],$m)) { 
		$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
		if ($tmp) { $coords[1] = 1-($tmp/$h); }
	}
	// LINEAR
	if ($type == 2) { 
		$angle = (isset($coords[4]) ? $coords[4] : false);
		$repeat = (isset($coords[5]) ? $coords[5] : false);
		// ALL POINTS SET (default for custom mPDF linear gradient) - no -moz
		if ($coords[0]!==false && $coords[1]!==false && $coords[2]!==false && $coords[3]!==false) {
			// do nothing - coords used as they are
		}

		// If both a <point> and <angle> are defined, the gradient axis starts from the point and runs along the angle. The end point is 
		// defined as before - in this case start points may not be in corners, and axis may not correctly fall in the right quadrant.
		// NO end points (Angle defined & Start points)
		else if ($angle!==false && $coords[0]!==false && $coords[1]!==false && $coords[2]===false && $coords[3]===false) {
		  if ($angle==0 || $angle==360) { $coords[3]=$coords[1]; if ($coords[0]==1) $coords[2]=2; else $coords[2]=1; }
		  else if ($angle==90) { $coords[2]=$coords[0]; $coords[3]=1; if ($coords[1]==1) $coords[3]=2; else $coords[3]=1; }
		  else if ($angle==180) { if ($coords[4]==0) $coords[2]=-1; else $coords[2]=0; $coords[3]=$coords[1]; }
		  else if ($angle==270) { $coords[2]=$coords[0]; if ($coords[1]==0) $coords[3]=-1; else $coords[3]=0; }
		  else {
			$endx=1; $endy=1; 
			if ($angle <=90) { 
				if ($angle <=45) { $endy=tan(deg2rad($angle)); }
				else { $endx=tan(deg2rad(90-$angle)); }
				$b = atan2(($endy*$bboxh), ($endx*$bboxw));
				$ny = 1 - $coords[1] - (tan($b) * (1-$coords[0]));
				$tx = sin($b) * cos($b) * $ny;
				$ty = cos($b) * cos($b) * $ny;
				$coords[2] = 1+$tx; $coords[3] = 1-$ty; 
			}
			else if ($angle <=180) { 
				if ($angle <=135) { $endx=tan(deg2rad($angle-90)); }
				else { $endy=tan(deg2rad(180-$angle)); }
				$b = atan2(($endy*$bboxh), ($endx*$bboxw));
				$ny = 1 - $coords[1] - (tan($b) * ($coords[0]));
				$tx = sin($b) * cos($b) * $ny;
				$ty = cos($b) * cos($b) * $ny;
				$coords[2] =  -$tx; $coords[3] = 1-$ty;
			}
			else if ($angle <=270) { 
				if ($angle <=225) { $endy=tan(deg2rad($angle-180)); }
				else { $endx=tan(deg2rad(270-$angle)); }
				$b = atan2(($endy*$bboxh), ($endx*$bboxw));
				$ny = $coords[1] - (tan($b) * ($coords[0]));
				$tx = sin($b) * cos($b) * $ny;
				$ty = cos($b) * cos($b) * $ny;
				$coords[2] = -$tx; $coords[3] = $ty; 
			}
			else { 
				if ($angle <=315) { $endx=tan(deg2rad($angle-270)); }
				else { $endy=tan(deg2rad(360-$angle));  }
				$b = atan2(($endy*$bboxh), ($endx*$bboxw));
				$ny = $coords[1] - (tan($b) * (1-$coords[0]));
				$tx = sin($b) * cos($b) * $ny;
				$ty = cos($b) * cos($b) * $ny;
				$coords[2] = 1+$tx; $coords[3] = $ty; 

			}
		  }
		}

		// -moz If the first parameter is only an <angle>, the gradient axis starts from the box's corner that would ensure the 
		// axis goes through the box. The axis runs along the specified angle. The end point of the axis is defined such that the 
		// farthest corner of the box from the starting point is perpendicular to the gradient axis at that point.
		// NO end points or Start points (Angle defined)
		else if ($angle!==false && $coords[0]===false && $coords[1]===false) {
		  if ($angle==0 || $angle==360) { $coords[0]=0; $coords[1]=0; $coords[2]=1; $coords[3]=0; }
		  else if ($angle==90) { $coords[0]=0; $coords[1]=0; $coords[2]=0; $coords[3]=1; }
		  else if ($angle==180) { $coords[0]=1; $coords[1]=0; $coords[2]=0; $coords[3]=0; }
		  else if ($angle==270) { $coords[0]=0; $coords[1]=1; $coords[2]=0; $coords[3]=0; }
		  else {
			if ($angle <=90) { 
				$coords[0]=0; $coords[1]=0; 
				if ($angle <=45) { $endx=1; $endy=tan(deg2rad($angle)); }
				else { $endx=tan(deg2rad(90-$angle)); $endy=1; }
			}
			else if ($angle <=180) { 
				$coords[0]=1; $coords[1]=0; 
				if ($angle <=135) { $endx=tan(deg2rad($angle-90)); $endy=1; }
				else { $endx=1; $endy=tan(deg2rad(180-$angle)); }
			}
			else if ($angle <=270) { 
				$coords[0]=1; $coords[1]=1; 
				if ($angle <=225) { $endx=1; $endy=tan(deg2rad($angle-180)); }
				else { $endx=tan(deg2rad(270-$angle)); $endy=1; }
			}
			else { 
				$coords[0]=0; $coords[1]=1; 
				if ($angle <=315) { $endx=tan(deg2rad($angle-270)); $endy=1; }
				else { $endx=1; $endy=tan(deg2rad(360-$angle));  }
			}
			$b = atan2(($endy*$bboxh), ($endx*$bboxw));
			$h2 = $bboxh - ($bboxh * tan($b));
			$px = $bboxh + ($h2 * sin($b) * cos($b));
			$py = ($bboxh * tan($b)) + ($h2 * sin($b) * sin($b));
			$x1 = $px / $bboxh;
			$y1 = $py / $bboxh;
			if ($angle <=90) { $coords[2] = $x1; $coords[3] = $y1; }
			else if ($angle <=180) { $coords[2] = 1-$x1; $coords[3] = $y1; }
			else if ($angle <=270) { $coords[2] = 1-$x1; $coords[3] = 1-$y1; }
			else { $coords[2] = $x1; $coords[3] = 1-$y1; }
		  }
		}
		// -moz If the first parameter to the gradient function is only a <point>, the gradient axis starts from the specified point, 
		// and ends at the point you would get if you rotated the starting point by 180 degrees about the center of the box that the 
		// gradient is to be applied to.
		// NO angle and NO end points (Start points defined)
		else if ((!isset($angle) || $angle===false) && $coords[0]!==false && $coords[1]!==false) { 	// should have start and end defined
		  $coords[2] = 1-$coords[0]; $coords[3] = 1-$coords[1];
		  $angle = rad2deg(atan2($coords[3]-$coords[1],$coords[2]-$coords[0]));
		  if ($angle < 0) { $angle += 360; }
		  else if ($angle > 360) { $angle -= 360; }
		  if ($angle!=0 && $angle!=360 && $angle!=90 && $angle!=180 && $angle!=270) { 
		    if ($w >= $h) {
			$coords[1] *= $h/$w ;
			$coords[3] *= $h/$w ;
			$usew = $useh = $bboxw;
			$usey -= ($w-$h);
		    }
		    else {
			$coords[0] *= $w/$h ;
			$coords[2] *= $w/$h ;
			$usew = $useh = $bboxh;
		    }
		  }
		}

		// -moz If neither a <point> or <angle> is specified, i.e. the entire function consists of only <stop> values, the gradient 
		// axis starts from the top of the box and runs vertically downwards, ending at the bottom of the box.
		else {	// default values T2B
			// All values are set in parseMozGradient - so won't appear here
			$coords = array(0,0,1,0);	// default for original linear gradient (L2R)
		}
		$s = ' q';
		$s .= sprintf(' %.3F %.3F %.3F %.3F re W n', $x*_MPDFK, ($this->mpdf->h-$y)*_MPDFK, $w*_MPDFK, -$h*_MPDFK)."\n";
		$s .= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm', $usew*_MPDFK, $useh*_MPDFK, $usex*_MPDFK, ($this->mpdf->h-($usey+$useh))*_MPDFK)."\n";
	}

	// RADIAL
	else if ($type == 3) { 
		$radius = (isset($coords[4]) ? $coords[4] : false);
		$angle = (isset($coords[5]) ? $coords[5] : false);	// ?? no effect
		$shape = (isset($coords[6]) ? $coords[6] : false);
		$size = (isset($coords[7]) ? $coords[7] : false);
		$repeat = (isset($coords[8]) ? $coords[8] : false);
		// ALL POINTS AND RADIUS SET (default for custom mPDF radial gradient) - no -moz
		if ($coords[0]!==false && $coords[1]!==false && $coords[2]!==false && $coords[3]!==false && $coords[4]!==false) {
			// do nothing - coords used as they are
		}
		// If a <point> is defined
		else if ($shape!==false && $size!==false) {
		   if ($coords[2]==false) { $coords[2] = $coords[0]; }
		   if ($coords[3]==false) { $coords[3] = $coords[1]; }
		   // ELLIPSE
		   if ($shape=='ellipse') {
			$corner1 = sqrt(pow($coords[0],2) + pow($coords[1],2));
			$corner2 = sqrt(pow($coords[0],2) + pow((1-$coords[1]),2));
			$corner3 = sqrt(pow((1-$coords[0]),2) + pow($coords[1],2));
			$corner4 = sqrt(pow((1-$coords[0]),2) + pow((1-$coords[1]),2));
			if ($size=='closest-side') { $radius = min($coords[0], $coords[1], (1-$coords[0]), (1-$coords[1])); }
			else if ($size=='closest-corner') { $radius = min($corner1, $corner2, $corner3, $corner4); }
			else if ($size=='farthest-side') { $radius = max($coords[0], $coords[1], (1-$coords[0]), (1-$coords[1])); }
			else { $radius = max($corner1, $corner2, $corner3, $corner4); }	// farthest corner (default)
		   }
		   // CIRCLE
		   else if ($shape=='circle') {
		    if ($w >= $h) {
			$coords[1] = $coords[3] = ($coords[1] * $h/$w) ;
			$corner1 = sqrt(pow($coords[0],2) + pow($coords[1],2));
			$corner2 = sqrt(pow($coords[0],2) + pow((($h/$w)-$coords[1]),2));
			$corner3 = sqrt(pow((1-$coords[0]),2) + pow($coords[1],2));
			$corner4 = sqrt(pow((1-$coords[0]),2) + pow((($h/$w)-$coords[1]),2));
			if ($size=='closest-side') { $radius = min($coords[0], $coords[1], (1-$coords[0]), (($h/$w)-$coords[1])); }
			else if ($size=='closest-corner') { $radius = min($corner1, $corner2, $corner3, $corner4); }
			else if ($size=='farthest-side') { $radius = max($coords[0], $coords[1], (1-$coords[0]), (($h/$w)-$coords[1])); }
			else if ($size=='farthest-corner') { $radius = max($corner1, $corner2, $corner3, $corner4); }	// farthest corner (default)
			$usew = $useh = $bboxw;
			$usey -= ($w-$h);
		    }
		    else {
			$coords[0] = $coords[2] = ($coords[0] * $w/$h) ;
			$corner1 = sqrt(pow($coords[0],2) + pow($coords[1],2));
			$corner2 = sqrt(pow($coords[0],2) + pow((1-$coords[1]),2));
			$corner3 = sqrt(pow((($w/$h)-$coords[0]),2) + pow($coords[1],2));
			$corner4 = sqrt(pow((($w/$h)-$coords[0]),2) + pow((1-$coords[1]),2));
			if ($size=='closest-side') { $radius = min($coords[0], $coords[1], (($w/$h)-$coords[0]), (1-$coords[1])); }
			else if ($size=='closest-corner') { $radius = min($corner1, $corner2, $corner3, $corner4); }
			else if ($size=='farthest-side') { $radius = max($coords[0], $coords[1], (($w/$h)-$coords[0]), (1-$coords[1])); }
			else if ($size=='farthest-corner') { $radius = max($corner1, $corner2, $corner3, $corner4); }	// farthest corner (default)
			$usew = $useh = $bboxh;
		    }
		   }
		   if ($radius==0) { $radius=0.001; }	// to prevent error
		   $coords[4] = $radius; 
		}

		// -moz If entire function consists of only <stop> values
		else {	// default values 
			// All values are set in parseMozGradient - so won't appear here
			$coords = array(0.5,0.5,0.5,0.5);	// default for radial gradient (centred)
		}
		$s = ' q';
		$s .= sprintf(' %.3F %.3F %.3F %.3F re W n', $x*_MPDFK, ($this->mpdf->h-$y)*_MPDFK, $w*_MPDFK, -$h*_MPDFK)."\n";
		$s .= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm', $usew*_MPDFK, $useh*_MPDFK, $usex*_MPDFK, ($this->mpdf->h-($usey+$useh))*_MPDFK)."\n";
	}

	$n = count($this->mpdf->gradients) + 1;
	$this->mpdf->gradients[$n]['type'] = $type;
	$this->mpdf->gradients[$n]['colorspace'] = $colorspace;
	$trans = false;
	$this->mpdf->gradients[$n]['is_mask'] = $is_mask;
	if ($is_mask) { $trans = true; }
	if (count($stops) == 1) { $stops[1] = $stops[0]; }
	if (!isset($stops[0]['offset'])) { $stops[0]['offset'] = 0; }
	if (!isset($stops[(count($stops)-1)]['offset'])) { $stops[(count($stops)-1)]['offset'] = 1; }

	// Fix stop-offsets set as absolute lengths
	if ($type==2) {
		$axisx = ($coords[2]-$coords[0])*$usew;
		$axisy = ($coords[3]-$coords[1])*$useh;
		$axis_length = sqrt(pow($axisx,2) + pow($axisy,2));
	}
	else { $axis_length = $coords[4]*$usew; }	// Absolute lengths are meaningless for an ellipse - Firefox uses Width as reference

	for($i=0;$i<count($stops);$i++) {
	  if (isset($stops[$i]['offset']) && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$stops[$i]['offset'],$m)) { 
		$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
		$stops[$i]['offset'] = $tmp/$axis_length;
	  }
	}


	if (isset($stops[0]['offset']) && $stops[0]['offset']>0) { 
		$firststop = $stops[0]; 
		$firststop['offset'] = 0;
		array_unshift($stops, $firststop); 
	}
	if (!$repeat && isset($stops[(count($stops)-1)]['offset']) && $stops[(count($stops)-1)]['offset']<1) {
		$endstop = $stops[(count($stops)-1)]; 
		$endstop['offset'] = 1;
		$stops[] = $endstop; 
	}
	if ($stops[0]['offset'] > $stops[(count($stops)-1)]['offset']) { 
		$stops[0]['offset'] = 0;
		$stops[(count($stops)-1)]['offset'] = 1;
	}

	for($i=0;$i<count($stops);$i++) {
		// mPDF 5.3.74
		if ($colorspace == 'CMYK') {
			$this->mpdf->gradients[$n]['stops'][$i]['col'] = sprintf('%.3F %.3F %.3F %.3F', (ord($stops[$i]['col']{1})/100), (ord($stops[$i]['col']{2})/100), (ord($stops[$i]['col']{3})/100), (ord($stops[$i]['col']{4})/100));
		}
		else if ($colorspace == 'Gray') {
			$this->mpdf->gradients[$n]['stops'][$i]['col'] = sprintf('%.3F', (ord($stops[$i]['col']{1})/255));
		}
		else {
			$this->mpdf->gradients[$n]['stops'][$i]['col'] = sprintf('%.3F %.3F %.3F', (ord($stops[$i]['col']{1})/255), (ord($stops[$i]['col']{2})/255), (ord($stops[$i]['col']{3})/255));
		}
		if (!isset($stops[$i]['opacity'])) { $stops[$i]['opacity'] = 1; } 
		else if ($stops[$i]['opacity'] > 1 || $stops[$i]['opacity'] < 0) { $stops[$i]['opacity'] = 1; } 
		else if ($stops[$i]['opacity'] < 1) { 
			$trans = true; 
		}
		$this->mpdf->gradients[$n]['stops'][$i]['opacity'] = $stops[$i]['opacity'];
		// OFFSET
		if ($i>0 && $i<(count($stops)-1)) {
		  if (!isset($stops[$i]['offset']) || (isset($stops[$i+1]['offset']) && $stops[$i]['offset']>$stops[$i+1]['offset']) || $stops[$i]['offset']<$stops[$i-1]['offset']) { 
			if (isset($stops[$i-1]['offset']) && isset($stops[$i+1]['offset'])) { 
				$stops[$i]['offset'] = ($stops[$i-1]['offset']+$stops[$i+1]['offset'])/2; 
			}
			else {
				for($j=($i+1);$j<count($stops);$j++) {
					if(isset($stops[$j]['offset'])) { break; }
				}
				$int = ($stops[$j]['offset'] - $stops[($i-1)]['offset'])/($j-$i+1);
				for($f=0;$f<($j-$i-1);$f++) {
					$stops[($i+$f)]['offset'] = $stops[($i+$f-1)]['offset'] + ($int);
				}
			}
		  }
		}
		$this->mpdf->gradients[$n]['stops'][$i]['offset'] = $stops[$i]['offset'];
		$this->mpdf->gradients[$n]['stops'][$i]['offset'] = $stops[$i]['offset'];
	}

	if ($repeat) {
		$ns = count($this->mpdf->gradients[$n]['stops']);
		$offs = array();
		for($i=0;$i<$ns;$i++) {
			$offs[$i] = $this->mpdf->gradients[$n]['stops'][$i]['offset'];
		}
		$gp = 0;
		$inside=true;
		while($inside) {
		   $gp++;
		   for($i=0;$i<$ns;$i++) {
			$this->mpdf->gradients[$n]['stops'][(($ns*$gp)+$i)] = $this->mpdf->gradients[$n]['stops'][(($ns*($gp-1))+$i)];
			$tmp = $this->mpdf->gradients[$n]['stops'][(($ns*($gp-1))+($ns-1))]['offset']+$offs[$i] ;
			if ($tmp < 1) { $this->mpdf->gradients[$n]['stops'][(($ns*$gp)+$i)]['offset'] = $tmp; }
			else {
				$this->mpdf->gradients[$n]['stops'][(($ns*$gp)+$i)]['offset'] = 1;
				$inside = false;
				break(2);
			}
		   }
		}
	}

	if ($trans) { 
		$this->mpdf->gradients[$n]['trans'] = true;	
		$s .= ' /TGS'.$n.' gs ';
	}
	if (!is_array($extend) || count($extend) <1) { 
		$extend=array('true', 'true');	// These are supposed to be quoted - appear in PDF file as text
	}
	$this->mpdf->gradients[$n]['coords'] = $coords;
	$this->mpdf->gradients[$n]['extend'] = $extend;
	//paint the gradient
	$s .= '/Sh'.$n.' sh '."\n";
	//restore previous Graphic State
	$s .= ' Q '."\n";
	if ($return) { return $s; }
	else { $this->mpdf->_out($s); }
}
631772
def minimize_l1(self, weights={}):
        

        if self._z is None:
            self._add_minimization_vars()

        objective = self._z.expr(
            (reaction_id, -weights.get(reaction_id, 1))
            for reaction_id in self._model.reactions)
        self._prob.set_objective(objective)

        self._solve()
1292473
def gradient_descent(alpha=0.01, num_iters=1500)
      m = @xs.length
      alpha_m = (alpha / m)

      (num_iters).times do

        @thetas = 
          @xs.map do |x_row|
            x_row.zip(@thetas).map do |x, theta|
             x * theta
            end.inject(:+)
          end.zip(@ys).map do |(predict_val, y)|  
            predict_val - y 
          end.zip(@xs).map do |(y, x_row)|
            x_row.map { |x| x * y }
          end.reduce([0.0] * 2) do |sum, val|
            sum.zip(val).map { |(a, b)| a + b}
          end.map do |value|
            alpha_m * value
          end.zip( @thetas ).map do |(a, b)|
            b - a
          end
      end

      @thetas
    end