Calculate the minimum number of dimensions for n total samples with a
given maximum distortion using the Johnson-Lindenstrauss lemma.

@param int $n
@param float $maxDistortion
@return int
7196
public static function minDimensions(int $n, float $maxDistortion = 0.1) : int
    {
        return (int) round(4. * log($n)
            / ($maxDistortion ** 2 / 2. - $maxDistortion ** 3 / 3.));
    }
82641
public static function rand($min = 0, $max = 9999, &$tbCheck = [])
    {
        $val = rand($min, $max);
        
        $iter = 0;
        $maxiter = $max - $min;
        while (in_array($val, $tbCheck)) {
            if ($iter == $maxiter) {
                throw new \Exception("Nb rand max deja atteint", 5);
                
                return false;
            }
            $val = rand($min, $max);
            $iter++;
        }
        $tbCheck[] = $val;
        
        return $val;
    }
186025
public function length($min = null, $max = null)
	{
		$lastSegmentKey = $this->getLastSegmentKey();

		if (!is_null($min) && !is_null($max) && $max > $min)
		{
			$lengthPattern = sprintf('{%d,%d}', (int) $min, (int) $max);
		}
		elseif (!is_null($min) && !$max)
		{
			$lengthPattern = sprintf('{%d}', (int) $min);
		}
		else
		{
			$lengthPattern = '{1}';
		}

		return $this->replaceQuantifierByKey($lastSegmentKey, $lengthPattern);
	}
853064
public static function maximum(Vector $a, Vector $b) : self
    {
        if ($a->n() !== $b->n()) {
            throw new DimensionalityMismatchException('Vector A requires'
                . " {$a->n()} elements but Vector B has{$b->n()}.");
        }

        $c = array_map('max', $a->asArray(), $b->asArray());

        return static::quick($c);
    }
707378
public function &random($m = null, $n = null, $a = RAND_MIN, $b = RAND_MAX)
    {
        if (is_int($m) && is_int($n) && is_numeric($a) && is_numeric($b)) {
            $R = new Matrix($m, $n);
 
            for ($i = 0; $i < $m; $i++) {
                for ($j = 0; $j < $n; $j++) {
                    $R->set($i, $j, mt_rand($a, $b));
                }
            }
 
            return $R;
        } else {
            trigger_error(ArgumentTypeException, ERROR);
        }
    }
19329
public static function gaussian(int $n) : self
    {
        if ($n < 1) {
            throw new InvalidArgumentException('The number of elements'
                . " must be greater than 0, $n given.");
        }

        $a = [];

        for ($i = 0; $i < $n; $i += 2) {
            $r1 = rand(0, PHP_INT_MAX) / PHP_INT_MAX;
            $r2 = rand(0, PHP_INT_MAX) / PHP_INT_MAX;
            
            $r = sqrt(-2. * log($r1));

            $a[] = $r * sin($r2 * self::TWO_PI);
            $a[] = $r * cos($r2 * self::TWO_PI);
        }

        if (count($a) > $n) {
            $a = array_slice($a, 0, $n);
        }

        return static::quick($a);
    }
1164908
public static function numberLimit($val, $min, $max)
    {
        return ($val > $min ? ($val < $max ? $val : $max) : $min);
    }
250901
private double[] calculateMaximumSlope( BitMatrix analizedMatrix, RandomIter pitRandomIter, WritableRandomIter tcaRandomIter,
            int col, int row ) {
        double[] maxSlopeData = new double[10];
        int n = 1, m = 1;

        double dirmax = 0f, e1min = -9999f, e2min = -9999f;

        analizedMatrix.mark(col, row);
        tcaRandomIter.setSample(col, row, 0, 1);
        double pendmax = 0f;
        maxSlopeData[3] = pitRandomIter.getSampleDouble(col, row, 0);
        /*
         * per ogni triangolo calcolo la pendenza massima e la direzione di
         * deflusso reale.
         */
        for( int j = 0; j <= 7; j++ ) {
            n = tri[j][0];
            m = tri[j][1];

            maxSlopeData[4] = pitRandomIter.getSampleDouble(col + order[n][1], row + order[n][0], 0);
            maxSlopeData[5] = pitRandomIter.getSampleDouble(col + order[m][1], row + order[m][0], 0);
            /*
             * verifico che i punti attorno al pixel considerato non siano
             * novalue. In questo caso trascuro il triangolo.
             */
            if (!isNovalue(maxSlopeData[4]) && !isNovalue(maxSlopeData[5])) {
                calculateMaxSlopeAndDirection4Triangles(maxSlopeData);
                if (maxSlopeData[1] > pendmax) {
                    dirmax = maxSlopeData[2];
                    pendmax = maxSlopeData[1];
                    /* - direzione cardinale */
                    maxSlopeData[7] = tri[j][0];
                    /* - direzione diagonale */
                    maxSlopeData[8] = tri[j][1];
                    /* - segno del triangolo */
                    maxSlopeData[9] = tri[j][2];
                    /*
                     * - quote del triangolo avente pendenza
                     * maggiore
                     */
                    e1min = maxSlopeData[4];
                    /*
                     * non necessariamente sono le quote
                     * minime.
                     */
                    e2min = maxSlopeData[5];
                }
            }
        }
        maxSlopeData[1] = pendmax;
        maxSlopeData[2] = dirmax;
        maxSlopeData[4] = e1min;
        maxSlopeData[5] = e2min;

        return maxSlopeData;
    }
1141464
public function gexp(int $n):int{

		if($n < 0){
			$n += 255;
		}
		elseif($n >= 256){
			$n -= 255;
		}

		return Polynomial::table[$n][0];
	}
656988
private static function calculateMean($min, $max)
    {
        if ($min === null || $max === null) {
            return null;
        }

        return ($min + $max) / 2;
    }
76478
public final double sampleSigma(int n)
  {
    synchronized (_lock) {
      long count = _count.get();
      long lastCount = _lastStdCount;
      _lastStdCount = count;

      double sum = _sum.get();
      double lastSum = _lastStdSum;
      _lastStdSum = sum;

      double sumSquare = _sumSquare;
      _sumSquare = 0;

      if (count == lastCount)
        return 0;

      double avg = (sum - lastSum) / (count - lastCount);
      double part = (count - lastCount) * sumSquare - sum * sum;

      if (part < 0)
        part = 0;

      double std = Math.sqrt(part) / (count - lastCount);

      return _scale * (avg + n * std);
    }
  }
1215231
public function deleteByValue($min, $max): int
    {
        return $this->handle->zRemRangeByScore($this->name, $min, $max);
    }
1070509
public function skip(int $n) : int
    {
        if ($n <= 0) {
            return 0;
        }

        $skipped = 0;
        $skipLimit = min(self::MAX_SKIP_BUFFER_SIZE, $n);
        while ($skipped < $n) {
            $buf = $this->read(min($skipLimit, $n - $skipped));
            if ($buf === null) {
                break;
            }
            $skipped += strlen($buf);
        }
        return $skipped;
    }
1186190
public function getInt($min = 0, $max = 2147483647)
    {
        /** @noinspection TypeUnsafeComparisonInspection */
        if ($min == $max) {
            return $min;
        } else {
            $ar = unpack('l', $this->getByte(4));
            return $min + abs($ar[1]) % ($max - $min + 1);
        }
    }
172037
function(n) {
      // Fitting to the curve is too costly, we compute a larger bounding box
      // using the control points:
      var cp = sigma.utils.getSelfLoopControlPoints(n.x, n.y, n.size);

      // Bounding box of the point and the two control points:
      var minX = Math.min(n.x, cp.x1, cp.x2),
          maxX = Math.max(n.x, cp.x1, cp.x2),
          minY = Math.min(n.y, cp.y1, cp.y2),
          maxY = Math.max(n.y, cp.y1, cp.y2);

      return {
        x1: minX - n.size,
        y1: minY - n.size,
        x2: maxX + n.size,
        y2: minY - n.size,
        height: maxY - minY + n.size * 2
      };
    }
1068804
public AddBrownNtoN_F64 setDistortion( /**/double[] radial, /**/double t1, /**/double t2) {
		params = new RadialTangential_F64(radial,t1,t2);
		return this;
	}
1198033
public static final BigDecimal sqrt(BigDecimal n) {
		if (n.compareTo(BigDecimal.ZERO) <= 0) {
			throw new IllegalArgumentException();
		}

		final BigDecimal TWO = new BigDecimal("2");
		BigDecimal error;
		BigDecimal lastGuess = BigDecimal.ZERO;
		BigDecimal guess = BigDecimal.ONE.movePointRight(n.toBigInteger().toString().length() / 2);
		int maxIterations = 50;
		int iterations = 0;
		boolean more = true;
		while (more) {
			lastGuess = guess;
			guess = n.divide(guess, UJMPSettings.getInstance().getMathContext());
			guess = guess.add(lastGuess);
			guess = guess.divide(TWO, UJMPSettings.getInstance().getMathContext());
			error = n.subtract(guess.multiply(guess));
			if (++iterations >= maxIterations) {
				more = false;
			} else if (lastGuess.equals(guess)) {
				more = error.abs().compareTo(BigDecimal.ONE) >= 0;
			}
		}
		return guess;
	}