コード例 #1
0
ファイル: PolynomialContext.php プロジェクト: JordanRL/Fermat
 public function getY($x)
 {
     $value = 0;
     foreach ($this->equationDefinition as $exponent => $coefficient) {
         $value = BCProvider::add($value, BCProvider::multiply($coefficient, BCProvider::exp($x, $exponent)));
     }
     return Numbers::make($this->numberType, $value);
 }
コード例 #2
0
ファイル: UniformContext.php プロジェクト: JordanRL/Fermat
 /**
  * Generates a random number in the provided range, where all possible values are equally likely. (Even distribution.)
  *
  * NOTE: If $max is more than PHP_INT_MAX or $min is less than PHP_INT_MIN, no additional entropy will be gained for
  * the random number, and the distribution will become less evenly distributed across all possible values due to
  * rounding.
  *
  * @param $min
  * @param $max
  * @return NumberInterface
  */
 public function random($min = 0, $max = PHP_INT_MAX)
 {
     $min = Numbers::makeOrDont(Numbers::IMMUTABLE, $min);
     $max = Numbers::makeOrDont(Numbers::IMMUTABLE, $max);
     $difference = new ImmutableNumber(BCProvider::add($max->absValue(), $min->absValue()));
     $randFactory = new Factory();
     if ($max->compare(PHP_INT_MAX) != 1 && $min->compare(PHP_INT_MIN) != -1 && $difference->compare(PHP_INT_MAX) != 1) {
         $x = $randFactory->getMediumStrengthGenerator()->generateInt($min, $max);
         return Numbers::makeFromBase10($this->numberType, $x, null, $this->contextBase);
     } else {
         $x = $randFactory->getMediumStrengthGenerator()->generateInt();
         $fraction = BCProvider::divide($x, PHP_INT_MAX);
         $addedValue = BCProvider::multiply($fraction, $difference->getValue());
         $randVal = Numbers::makeFromBase10($this->numberType, BCProvider::add($min->getValue(), $addedValue), null, $this->contextBase);
         return $randVal->round();
     }
 }
コード例 #3
0
ファイル: GaussianContext.php プロジェクト: JordanRL/Fermat
 public function random()
 {
     $rand = GaussianProvider::random($this->getMean()->getValue(), $this->getSD()->getValue());
     return Numbers::make($this->numberType, $rand);
 }
コード例 #4
0
ファイル: BaseValue.php プロジェクト: JordanRL/Fermat
 /**
  * @param NumberInterface|int|float|string $value
  * @return int
  */
 public function compare($value)
 {
     $value = Numbers::makeOrDont($this, $value, $this->getPrecision());
     if ($this->getBase() != 10) {
         $thisValue = $this->convertToBase(10)->getValue();
     } else {
         $thisValue = $this->getValue();
     }
     if ($value->getBase() != 10) {
         $thatValue = $value->convertToBase(10)->getValue();
     } else {
         $thatValue = $value->getValue();
     }
     $scale = $this->getPrecision() < $value->getPrecision() ? $this->getPrecision() : $value->getPrecision();
     return BCProvider::compare($thisValue, $thatValue, $scale);
 }