Skip to content

Commit

Permalink
Push src/Math changes.
Browse files Browse the repository at this point in the history
+ Math\GSeq (Geometric sequence generator)
+ Math\Point (ascended from Misc/Coordinate3D)
  • Loading branch information
AnrDaemon committed Jun 1, 2018
1 parent 2c02dd0 commit 75c3cd2
Show file tree
Hide file tree
Showing 3 changed files with 436 additions and 76 deletions.
96 changes: 20 additions & 76 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,98 +1,42 @@
------------------------------------------------------------------------
r788 | anrdaemon | 2018-04-03 17:58:56 +0300 (Tue, 03 Apr 2018) | 2 lines
r799 | anrdaemon | 2018-04-15 20:07:18 +0300 (Sun, 15 Apr 2018) | 3 lines

+ Net\Browser initial release.
+ Math:GSeq Use common validity check for $n.
+ Plan for tests.

------------------------------------------------------------------------
r778 | anrdaemon | 2018-03-30 15:17:41 +0300 (Fri, 30 Mar 2018) | 3 lines
r796 | anrdaemon | 2018-04-08 23:51:15 +0300 (Sun, 08 Apr 2018) | 2 lines

+ Added author.
+ Put notes/todo under version control.
+ GSeq initial (ok, not) implementation.

------------------------------------------------------------------------
r767 | anrdaemon | 2018-03-19 23:49:28 +0300 (Mon, 19 Mar 2018) | 2 lines
r795 | anrdaemon | 2018-04-05 22:12:30 +0300 (Thu, 05 Apr 2018) | 2 lines

* Only confirm skipped tests for known mangled characters.
- Reduce the number of "new static" calls to absolutely required.

------------------------------------------------------------------------
r765 | anrdaemon | 2018-03-14 01:25:26 +0300 (Wed, 14 Mar 2018) | 2 lines
r794 | anrdaemon | 2018-04-05 18:22:37 +0300 (Thu, 05 Apr 2018) | 2 lines

* Implement mangled characters test.
* Optimize zero distance directional movements.

------------------------------------------------------------------------
r764 | anrdaemon | 2018-03-14 01:24:28 +0300 (Wed, 14 Mar 2018) | 2 lines
r793 | anrdaemon | 2018-04-04 21:58:30 +0300 (Wed, 04 Apr 2018) | 4 lines

* Use more common host names.
* Unify property access pattern across the class.
Only directly access property storage, when you actually mean it.
In all other cases, use magic to retrieve value of the property.

------------------------------------------------------------------------
r763 | anrdaemon | 2018-03-14 00:50:47 +0300 (Wed, 14 Mar 2018) | 2 lines
r792 | anrdaemon | 2018-04-04 19:41:30 +0300 (Wed, 04 Apr 2018) | 2 lines

+ Use dedicated normalization of parts array.
+ Refactored Coordinate3D into a generic Point class.

------------------------------------------------------------------------
r762 | anrdaemon | 2018-03-14 00:19:16 +0300 (Wed, 14 Mar 2018) | 4 lines
r791 | anrdaemon | 2018-04-04 02:09:41 +0300 (Wed, 04 Apr 2018) | 5 lines

* Split autoload.
* Tweak tests semantics.
* Put a note about parameter names mangling for Net\Url.

------------------------------------------------------------------------
r741 | anrdaemon | 2018-03-07 02:43:15 +0300 (Wed, 07 Mar 2018) | 2 lines

* Tweak CodeStyle.

------------------------------------------------------------------------
r740 | anrdaemon | 2018-03-04 02:50:24 +0300 (Sun, 04 Mar 2018) | 3 lines

* Hard bind classloader function calls to root namespace.
- Remove unnecessary stuff from classloader.

------------------------------------------------------------------------
r738 | anrdaemon | 2018-03-03 22:03:36 +0300 (Sat, 03 Mar 2018) | 3 lines

= Move code to subdirectory.
+ Add some functional tests.

------------------------------------------------------------------------
r737 | anrdaemon | 2018-03-03 21:50:26 +0300 (Sat, 03 Mar 2018) | 3 lines

+ Explicitly create empty object from empty string URL.
* Lighten up creation process overall.

------------------------------------------------------------------------
r736 | anrdaemon | 2018-03-03 21:14:52 +0300 (Sat, 03 Mar 2018) | 2 lines

* Lighten up Url::setParts() logic.

------------------------------------------------------------------------
r735 | anrdaemon | 2018-03-03 21:01:46 +0300 (Sat, 03 Mar 2018) | 2 lines

* Wrap parse_str and parse_url into usable methods.

------------------------------------------------------------------------
r734 | anrdaemon | 2018-03-03 20:30:12 +0300 (Sat, 03 Mar 2018) | 3 lines

- Remove port type conversion from Url::parse()
Url::setParts() already handles it.

------------------------------------------------------------------------
r733 | anrdaemon | 2018-03-03 20:25:24 +0300 (Sat, 03 Mar 2018) | 2 lines

* Normalize internal wrappers naming.

------------------------------------------------------------------------
r727 | anrdaemon | 2018-02-26 17:17:00 +0300 (Mon, 26 Feb 2018) | 2 lines

* Actually universally stackable classloader.

------------------------------------------------------------------------
r709 | anrdaemon | 2017-11-28 22:10:42 +0300 (Tue, 28 Nov 2017) | 2 lines

+ Cache Url::__toString() representation.

------------------------------------------------------------------------
r708 | anrdaemon | 2017-11-28 22:09:53 +0300 (Tue, 28 Nov 2017) | 2 lines

* Explicitly declare methods visibility.
* Set the groundwork for further development of Point class.
* Drop redundant capabilities.
* Make class immutable.
* Add degrees variant of fromPolar() constructor.

------------------------------------------------------------------------
113 changes: 113 additions & 0 deletions src/Math/GSeq.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace AnrDaemon\Math;

/** Geometric sequence class
*
* General usage idea is that you create a new sequence using either base and
* multiplier, or multiplier, number of elements and a known sum of them.
*
* Then you can seek/iterate them as needed, or retrieve specific element via
* simple function call.
*/
class GSeq
implements \SeekableIterator
{
protected $b, $q, $n;

protected static function ensureValid($n, $message = null)
{
if($n < 1 || !is_int($n))
throw new \OutOfBoundsException($message ?: 'Sequence number should be integer starting with 1.');
}

/** Create new geometric sequence...
*
* [1] create($b, $q)
* - from base and multiplier;
* [2] create($sum, $q, $n)
* - from sum, multiplier and number of elements.
*/
public static function create($b, $q, $n = 1)
{
if($n == 1)
return new static($b, $q);

static::ensureValid($n, "Amount of elements must be an integer number bigger than zero.");

return new static($b * (1 - $q) / (1 - pow($q, $n)), $q);
}

/** Calculate sum of N consequent characters.
*
* Either N first, or N after first M.
*
* @param int $n number of elements to sum up
* @param ?int $m number of elements to skip
* @return float the sum of elements
*/
public function sum($n, $m = 0)
{
if($m > 0)
return $this->sum($n+$m) - $this->sum($m);

static::ensureValid($n);

return $this->b * (1 - pow($this->q, $n)) / (1 - $this->q);
}

// Magic!

private function __construct($b, $q)
{
$this->b = $b;
$this->q = $q;
$this->n = 1;
}

public function __invoke($n)
{
if($n == 1)
return $this->b;

static::ensureValid($n);

return $this->b * pow($this->q, (int)$n - 1);
}

// SeekableIterator

public function seek($n)
{
static::ensureValid($n);

$this->n = (int)$n;
}

// Iterator

public function valid()
{
return $this->n > 0;
}

public function current()
{
return $this->__invoke($this->n);
}

public function key()
{
return $this->n;
}

public function next()
{
$this->n++;
}

public function rewind()
{
$this->seek(1);
}
}
Loading

0 comments on commit 75c3cd2

Please sign in to comment.