Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Jul 25, 2019
0 parents commit d1bd30e
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Math smart statistics
=====================

Smart statistic algorithms.

> Please help improve this documentation by sending a Pull request.
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "mathematicator-core/statistic",
"description": "Smart statistic algorithms.",
"homepage": "https://github.com/mathematicator-core/statistic",
"authors": [
{
"name": "Jan Barášek",
"homepage": "http://baraja.cz"
}
],
"require": {
"php": ">=7.1.0"
},
"autoload": {
"classmap": [
"src/"
]
},
"minimum-stability": "stable"
}
2 changes: 2 additions & 0 deletions config.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
services:
- Model\Math\Statistics\StatisticsManager
116 changes: 116 additions & 0 deletions src/Entity/Sequence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Mathematicator\Statistics\Entity;


use Doctrine\ORM\Mapping as ORM;
use Nette\SmartObject;
use Nette\Utils\Strings;
use ShopUp\Database\UUID\UuidIdentifier;

/**
* @ORM\Entity()
*/
class Sequence
{

use UuidIdentifier;
use SmartObject;

private const FORMAT_PATTERN = '/^\%(?<type>[a-zA-Z0-9]+)\s+(A\d+)\s?(?<content>.*?)\s*$/';

/**
* @var string
* @ORM\Column(type="string", unique=true)
*/
private $aId;

/**
* @var string|null
* @ORM\Column(type="text", nullable=true)
*/
private $sequence;

/**
* @var string|null
* @ORM\Column(type="text", nullable=true)
*/
private $data;

/**
* @return string
*/
public function getAId(): string
{
return $this->aId;
}

/**
* @return int[]
*/
public function getSequence(): array
{
if ($this->sequence === null) {
return [];
}

$return = [];
foreach (explode(',', $this->sequence) as $item) {
$return[] = (int) trim($item);
}

return $return;
}

/**
* @return null|string
*/
public function getData(): ?string
{
return $this->data;
}

/**
* @param string $type
* @return string|null
*/
public function getDataType(string $type): ?string
{
if ($this->getData() === null) {
$this->updateData();
}

$return = [];

foreach (explode("\n", $this->getData()) as $line) {
if (preg_match(self::FORMAT_PATTERN, $line, $parser) && $parser['type'] === $type) {
$return[] = $parser['content'];
}
}

if ($return === []) {
return null;
}

$return = implode("\n", $return);

return $type === 'A' ? str_replace('_', '', $return) : $return;
}

public function updateData(): void
{
if ($this->data === null) {
$this->data = Strings::normalize(
Strings::fixEncoding(
file_get_contents('https://oeis.org/search?q=id:' . $this->getAId() . '&fmt=text')
)
);
}
}

public function getFormula(): ?string
{
return $this->getDataType('F');
}

}
152 changes: 152 additions & 0 deletions src/StatisticsManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace Model\Math\Statistics;


use Mathematicator\Statistics\Entity\Sequence;
use Nette\Utils\Strings;
use Nette\Utils\Validators;
use ShopUp\Database\EntityManager;
use ShopUp\Database\EntityManagerException;

class StatisticsManager
{

/**
* @var EntityManager
*/
private $entityManager;

/**
* @param EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}

/**
* @param string $query
* @return int[]|float[]
*/
public function getNumbers(string $query): array
{
$numbers = [];

$query = preg_replace('/[^0-9-.\/]/', ';', $query);
$query = (string) preg_replace('/\;+/', ';', $query);

foreach (explode(';', $query) as $number) {
if (Validators::isNumeric($number)) {
$numbers[] = $number;
}
}

return $numbers;
}

/**
* @param string $data
* @return int[][]|float[][]
*/
public function getData(string $data): array
{
$return = [];

foreach (explode("\n", Strings::normalize($data)) as $line) {
$return[] = $this->getNumbers($line);
}

return $return;
}

/**
* @param int[]|float[] $array
* @return float|int|mixed
*/
public function getMedian(array $array)
{
if ($array) {
$count = \count($array);
sort($array);
$mid = (int) floor(($count - 1) / 2);

return ($array[$mid] + $array[$mid + 1 - $count % 2]) / 2;
}

return 0;
}

/**
* @param int[]|float[] $array
* @return float|int
*/
public function getAverage(array $array)
{
if ($array) {
$sum = 0;
$count = 0;
foreach ($array as $item) {
$count++;
$sum += $item;
}

return $sum / ($count === 0 ? 1 : $count);
}

return 0;
}

/**
* @param \int[] $sequence
* @param int $limit
* @return Sequence[]
*/
public function getSequences(array $sequence, int $limit = 6): array
{
/** @var Sequence[] $return */
$return = $this->entityManager->getRepository(Sequence::class)
->createQueryBuilder('sequence')
->where('sequence.sequence LIKE :sequence')
->setParameter('sequence', implode(',', $sequence) . ',%')
->setMaxResults($limit)
->getQuery()
->getResult();

foreach ($return as $item) {
if ($item->getData() === null) {
$item->updateData();
try {
$this->entityManager->flush($item);
} catch (EntityManagerException $e) {
}
}
}

return $return;
}

/**
* @param string $aId
* @return Sequence
*/
public function getSequence(string $aId): Sequence
{
/** @var Sequence $return */
$return = $this->entityManager->getRepository(Sequence::class)
->createQueryBuilder('sequence')
->where('sequence.aId = :aId')
->setParameter('aId', $aId)
->setMaxResults(1)
->getQuery()
->getSingleResult();

if ($return->getData() === null) {
$return->updateData();
$this->entityManager->flush($return);
}

return $return;
}

}

0 comments on commit d1bd30e

Please sign in to comment.