Skip to content

Commit 36bc352

Browse files
committed
Implemented basic logic for table integrals + automated tests + service definition
1 parent 36b4862 commit 36bc352

File tree

6 files changed

+457
-9
lines changed

6 files changed

+457
-9
lines changed

common.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
services:
2-
integralSolver: Mathematicator\Integral\IntegralSolver
2+
integralSolver: Mathematicator\Integral\IntegralSolver
3+
- Mathematicator\Integral\Solver\Solver
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mathematicator\Integral\Exception;
6+
7+
8+
use Mathematicator\Engine\MathematicatorException;
9+
use Mathematicator\Tokenizer\Token\IToken;
10+
use Mathematicator\Tokenizer\Token\SubToken;
11+
12+
class CanNotSolveException extends MathematicatorException
13+
{
14+
15+
/**
16+
* @param IToken[] $tokens
17+
* @param int $level
18+
* @throws CanNotSolveException
19+
*/
20+
public static function canNotSolve(array $tokens, int $level = 0): void
21+
{
22+
static $buffer;
23+
if ($level === 0) {
24+
$buffer = '';
25+
}
26+
27+
foreach ($tokens as $token) {
28+
$buffer .= $token->getToken();
29+
if ($token instanceof SubToken) {
30+
self::canNotSolve($token->getTokens(), $level + 1);
31+
$buffer .= ')';
32+
}
33+
}
34+
35+
if ($level > 0) {
36+
return;
37+
}
38+
39+
throw new self('Can not solve "' . $buffer . '".');
40+
}
41+
42+
/**
43+
* @param string $function
44+
* @throws CanNotSolveException
45+
*/
46+
public static function notImplemented(string $function): void
47+
{
48+
throw new self('This function does not implemented "' . $function . '".');
49+
}
50+
51+
}

src/IntegralResult.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mathematicator\Integral;
6+
7+
8+
use Mathematicator\Calculator\Step;
9+
use Mathematicator\Tokenizer\Token\IToken;
10+
11+
final class IntegralResult
12+
{
13+
14+
/**
15+
* @var IToken[]
16+
*/
17+
private $queryTokens;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $queryLaTeX;
23+
24+
/**
25+
* @var string|null
26+
*/
27+
private $result;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $differential;
33+
34+
/**
35+
* @var bool
36+
*/
37+
private $singleToken;
38+
39+
/**
40+
* @var Step[]
41+
*/
42+
private $steps = [];
43+
44+
/**
45+
* @param IToken[] $queryTokens
46+
* @param string $queryLaTeX
47+
* @param string $differential
48+
* @param bool $singleToken
49+
*/
50+
public function __construct(array $queryTokens, string $queryLaTeX, string $differential, bool $singleToken)
51+
{
52+
$this->queryTokens = $queryTokens;
53+
$this->queryLaTeX = $queryLaTeX;
54+
$this->differential = $differential;
55+
$this->singleToken = $singleToken;
56+
}
57+
58+
/**
59+
* @return bool
60+
*/
61+
public function isFinalResult(): bool
62+
{
63+
return strpos($this->getResult(), '?') === false;
64+
}
65+
66+
/**
67+
* @return string
68+
*/
69+
public function getResult(): string
70+
{
71+
return $this->result ?? '';
72+
}
73+
74+
/**
75+
* @internal
76+
* @param string $result
77+
*/
78+
public function setResult(string $result): void
79+
{
80+
$this->result = $result;
81+
}
82+
83+
/**
84+
* @return string
85+
*/
86+
public function getDifferential(): string
87+
{
88+
return $this->differential;
89+
}
90+
91+
/**
92+
* @return bool
93+
*/
94+
public function isSingleToken(): bool
95+
{
96+
return $this->singleToken;
97+
}
98+
99+
/**
100+
* @return IToken[]
101+
*/
102+
public function getQueryTokens(): array
103+
{
104+
return $this->queryTokens;
105+
}
106+
107+
/**
108+
* @return string
109+
*/
110+
public function getQueryLaTeX(): string
111+
{
112+
return '\int '
113+
. ($this->isSingleToken() ? $this->queryLaTeX : '\left(' . $this->queryLaTeX . '\right)')
114+
. ' \ d' . $this->getDifferential();
115+
}
116+
117+
/**
118+
* @return Step[]
119+
*/
120+
public function getSteps(): array
121+
{
122+
return $this->steps;
123+
}
124+
125+
/**
126+
* @param Step $step
127+
* @return IntegralResult
128+
*/
129+
public function addStep(Step $step): self
130+
{
131+
$this->steps[] = $step;
132+
133+
return $this;
134+
}
135+
136+
}

0 commit comments

Comments
 (0)