math-php icon indicating copy to clipboard operation
math-php copied to clipboard

fetch one particular value from a Sequence

Open Beakerboy opened this issue 6 years ago • 3 comments

I like the idea of having generating functions for popular sequences. However, I worry about the overhead of always returning an array of all values up to the value of interest. Is there a way you would like to abstract this to return either a value from the sequence or the entire sequence? If someone wanted the 617th Harmonic number, would we want to return all 617 and expect them to do:

$harmonic = Sequence\NonInteger::Harmonnic(617);
$important_value = end($harmonic);

Should we have a library of Closures which generate the n value given n-1? The Sequence namespace could create an entire array while another namespace could return just the single value? Or there could be an optional function argument to tell if the array is necessary.

Beakerboy avatar Apr 15 '19 12:04 Beakerboy

Saying you want the n-th harmonic number seems like a different use case than saying you want the first n numbers in the harmonic series.

Thinking about use cases, and what format you'd want:

  • All the n numbers in a sequence: array
  • Some manipulation of all the n numbers in a sequence: array to send to array_map
  • A subset of all the n numbers in a sequence: array to send to array_filter
  • Some reduction of the entire sequence (sum, etc.): array to send to array_reduce
  • Specific number in a sequence: scalar number
  • Multiple specific numbers in a sequence: multiple calls to get scalar number, or array where you can index as needed
  • Iterating over the sequence in a complex way that a loop is preferred over a map: Generator
  • Iterating over the sequence up to a large n: Generator

All of these use cases can be reduced to two basic use cases:

  • I want all the numbers
  • I want a specific number

Array covers both of these, but is inefficient and clunky for the specific number, and possibly blows up if n is large. A generator would cover all the numbers, but would limit usage to iteration with a for loop. You can't send a generator to array_map unfortunately.

It seems like that you probably want a new interface of classes/namespace to get specific numbers, as this is not a sequence that is being provided. That's fine.

The question is what to do about the Sequences. Array works and is the most flexible. But is it a concern that at some point it will run out of memory.

To test, I was able to get a sequence of one million harmonic numbers in 100 ms which included all the overhead of loading PHPUnit. However, ten million caused a PHP Fatal error due to memory being exhausted.

It seems like you have to pick what is more important: convenience of usage vs large size sequences.

markrogoyski avatar Apr 15 '19 16:04 markrogoyski

Another option is to use a more OOP approach and have each sequence be an instantiable class that are traversable by implementing the PHP Iterator interface but uses memory-efficient generators in their implementations. And they could also provide an interface to get specific numbers in the sequence without needing to traverse the sequence.

markrogoyski avatar Apr 15 '19 16:04 markrogoyski

I’m probably prematurely worried about optimization. The Zipf’s law distribution requires specific values of generalized Harmonic Numbers so I was thinking about this. I’ll just make a call to end() and i’m Sure everything will be fine.

Beakerboy avatar Apr 15 '19 20:04 Beakerboy