Skip to content

Commit 1040815

Browse files
committed
Updated contexts
1 parent 1cf2e79 commit 1040815

File tree

6 files changed

+137
-7
lines changed

6 files changed

+137
-7
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2-
vendor/
2+
vendor/
3+
composer.lock

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "samsara/cantor",
33
"require": {
4+
"ircmaxell/random-lib": "1.1.*",
45
"ext-stats": "*"
56
},
67
"require-dev": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Samsara\Cantor\Context\Base;
4+
5+
use Samsara\Cantor\Values\Base\NumberInterface;
6+
7+
abstract class BaseContext
8+
{
9+
10+
protected $contextBase;
11+
12+
/**
13+
* @var string|NumberInterface
14+
*/
15+
protected $numberType;
16+
17+
public function setBase($base)
18+
{
19+
$this->contextBase = $base;
20+
21+
return $this;
22+
}
23+
24+
public function changeBase($base)
25+
{
26+
return $this->transformBaseContext($base)->setBase($base);
27+
}
28+
29+
/**
30+
* Must be implemented by each context. This method transforms the base of ALL NumberInterface objects which are
31+
* stored as class properties within the context so that the actual math is preserved no matter what base it's in.
32+
*
33+
* @param int $base
34+
* @return $this
35+
*/
36+
abstract protected function transformBaseContext($base);
37+
38+
}

src/Samsara/Cantor/Bounds/GaussianBound.php src/Samsara/Cantor/Context/GaussianContext.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
<?php
22

3-
namespace Samsara\Cantor\Bounds;
3+
namespace Samsara\Cantor\Context;
44

55
use Samsara\Cantor\Numbers;
66
use Samsara\Cantor\Provider\BCProvider;
77
use Samsara\Cantor\Provider\GaussianProvider;
88
use Samsara\Cantor\Values\Base\NumberInterface;
99

10-
class GaussianBound
10+
/**
11+
* Used for operating on numbers within the context of a Gaussian (or normal) distribution.
12+
*/
13+
class GaussianContext
1114
{
1215

1316
/**
@@ -20,8 +23,6 @@ class GaussianBound
2023
*/
2124
protected $standardDev;
2225

23-
protected $numberType;
24-
2526
/**
2627
* @var NumberInterface
2728
*/
@@ -123,9 +124,9 @@ public function PDFByValue($value)
123124

124125
public function random()
125126
{
126-
$rand = GaussianProvider::random($this->getMean(), $this->getSD());
127+
$rand = GaussianProvider::random($this->getMean()->getValue(), $this->getSD()->getValue());
127128

128-
return $rand;
129+
return Numbers::make($this->numberType, $rand);
129130
}
130131

131132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Samsara\Cantor\Context;
4+
5+
use RandomLib\Factory;
6+
use RandomLib\Source\Random;
7+
use Samsara\Cantor\Context\Base\BaseContext;
8+
use Samsara\Cantor\Numbers;
9+
use Samsara\Cantor\Provider\BCProvider;
10+
use Samsara\Cantor\Values\Base\NumberInterface;
11+
12+
/**
13+
* Used for operating on numbers withing a polynomial context.
14+
*/
15+
class PolynomialContext extends BaseContext
16+
{
17+
18+
/**
19+
* The exponents and coefficients present in this polynomial. This is ALWAYS represented in base10 regardless of
20+
* the context base, in order to take advantage of the fact that PHP and most computer programming languages are
21+
* base10 by default.
22+
*
23+
* The format for this array is:
24+
*
25+
* $equationDefinition[$exponent] => $coefficient
26+
*
27+
* Meaning that this equation:
28+
*
29+
* 2x^4 + x^2 - 7x + 3
30+
*
31+
* Would be represented by:
32+
*
33+
* [
34+
* 0 => 3,
35+
* 1 => -7,
36+
* 2 => 1,
37+
* 3 => 0, // This could also be omitted
38+
* 4 => 2
39+
* ]
40+
*
41+
* @var array $definition
42+
* @var string|NumberInterface $type
43+
*/
44+
protected $equationDefinition = [];
45+
46+
public function __construct(array $definition, $type)
47+
{
48+
$this->numberType = $type;
49+
50+
$this->equationDefinition = $definition;
51+
}
52+
53+
public function random()
54+
{
55+
$randFactory = new Factory();
56+
$x = $randFactory->getMediumStrengthGenerator()->generateInt();
57+
58+
return $this->getY($x);
59+
}
60+
61+
public function getY($x)
62+
{
63+
$value = 0;
64+
65+
foreach ($this->equationDefinition as $exponent => $coefficient) {
66+
$value = BCProvider::add(
67+
$value,
68+
BCProvider::multiply(
69+
$coefficient,
70+
BCProvider::exp(
71+
$x,
72+
$exponent
73+
)
74+
)
75+
);
76+
}
77+
78+
return Numbers::make(
79+
$this->numberType,
80+
$value
81+
);
82+
}
83+
84+
protected function transformBaseContext($base)
85+
{
86+
return $this;
87+
}
88+
89+
}

0 commit comments

Comments
 (0)