Introduction
This blog post is for the two other people in the world that might be remotely interested in rasters/arrays and dimensionality as it can be applied to reshaping for subsequent analysis. The topic was introduced in my previous blog about block statistics. I am gently warming the readers (both of you) to moving/sliding window functions and how they work on a mechanical level.
The code below is for the record. This is one incarnation of what is out there on the web. Most of the comments and posts deal with the 'speed' and efficacy of determining what you will see. In my opinion, those discussions don't matter... I have only needed primes and combinations and permutations once outside of an academic environment. I will rely on this code should the need arise again. So here it is with an output example.
Nerd Note:
As of worlds largest prime as of this date, , the worlds largest prime is ... 274207281-1 .... or 22,338,618 digits the number is called M74207281
which is the 49th Mersenne Prime discovered. http://www.mersenne.org/primes/?press=M74207281
In the next post, I will use this foundation to provide more background on working with block and moving window operations.
<SPAN class="string token">"""
Script: primes_combos_demo.py
Author:
Dan.Patterson@carleton.ca
Purpose: Long forgotten, but it is done.
"""</SPAN>
<SPAN class="keyword token">import</SPAN> numpy <SPAN class="keyword token">as</SPAN> np
<SPAN class="keyword token">import</SPAN> itertools
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">primes</SPAN><SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">''</SPAN>'Returns a array of primes<SPAN class="punctuation token">,</SPAN> p <SPAN class="operator token"><</SPAN> n
http<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">//</SPAN>stackoverflow<SPAN class="punctuation token">.</SPAN>com<SPAN class="operator token">/</SPAN>questions<SPAN class="operator token">/</SPAN><SPAN class="number token">2068372</SPAN><SPAN class="operator token">/</SPAN>fastest<SPAN class="operator token">-</SPAN>way<SPAN class="operator token">-</SPAN>to<SPAN class="operator token">-</SPAN>list<SPAN class="operator token">-</SPAN>
all<SPAN class="operator token">-</SPAN>primes<SPAN class="operator token">-</SPAN>below<SPAN class="operator token">-</SPAN>n<SPAN class="operator token">-</SPAN><SPAN class="keyword token">in</SPAN><SPAN class="operator token">-</SPAN>python<SPAN class="operator token">/</SPAN><SPAN class="number token">3035188</SPAN><SPAN class="comment token">#3035188</SPAN>
<SPAN class="string token">''</SPAN>'
<SPAN class="keyword token">assert</SPAN> n<SPAN class="operator token">>=</SPAN><SPAN class="number token">2</SPAN>
sieve <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>ones<SPAN class="punctuation token">(</SPAN>n<SPAN class="operator token">/</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> dtype<SPAN class="operator token">=</SPAN>np<SPAN class="punctuation token">.</SPAN>bool<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN>int<SPAN class="punctuation token">(</SPAN>n<SPAN class="operator token">**</SPAN><SPAN class="number token">0.5</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> sieve<SPAN class="punctuation token">[</SPAN>i<SPAN class="operator token">/</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN>
sieve<SPAN class="punctuation token">[</SPAN>i<SPAN class="operator token">*</SPAN>i<SPAN class="operator token">/</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">:</SPAN>i<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN>
p_num <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>r_<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="operator token">*</SPAN>np<SPAN class="punctuation token">.</SPAN>nonzero<SPAN class="punctuation token">(</SPAN>sieve<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">+</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">return</SPAN> p_num
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">divisor</SPAN><SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">,</SPAN> prn<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Determine the divisors, products, cumulative products and
divisors given a set of primes < n
"""</SPAN>
p_vals <SPAN class="operator token">=</SPAN> primes<SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">)</SPAN>
divs <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>p <SPAN class="keyword token">for</SPAN> p <SPAN class="keyword token">in</SPAN> p_vals <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>n <SPAN class="operator token">%</SPAN> p <SPAN class="operator token">==</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN>
cump <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>cumproduct<SPAN class="punctuation token">(</SPAN>divs<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>tolist<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
divs <SPAN class="operator token">=</SPAN> divs <SPAN class="operator token">+</SPAN> cump
prods <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>set<SPAN class="punctuation token">(</SPAN>i<SPAN class="operator token">*</SPAN>j <SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> divs <SPAN class="keyword token">for</SPAN> j <SPAN class="keyword token">in</SPAN> divs<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
all_div <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>set<SPAN class="punctuation token">(</SPAN>divs<SPAN class="operator token">+</SPAN>prods<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
all_div<SPAN class="punctuation token">.</SPAN>sort<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> prn<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"all"</SPAN><SPAN class="punctuation token">,</SPAN>all_div<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> all_div
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">perm_sums</SPAN><SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">,</SPAN>max_dim<SPAN class="operator token">=</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Determine the permutations that sum to n for an array of given size.
A verbose workflow follows... """</SPAN>
<SPAN class="comment token">##divs, subs, prods = divisor(n)</SPAN>
perms <SPAN class="operator token">=</SPAN> divisor<SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">)</SPAN>
p <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>max_dim<SPAN class="operator token">+</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
p<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>list<SPAN class="punctuation token">(</SPAN>itertools<SPAN class="punctuation token">.</SPAN>product<SPAN class="punctuation token">(</SPAN>perms<SPAN class="punctuation token">,</SPAN> repeat<SPAN class="operator token">=</SPAN>i<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
combos <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>p<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
vals <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>v <SPAN class="keyword token">for</SPAN> v <SPAN class="keyword token">in</SPAN> p<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">if</SPAN> np<SPAN class="punctuation token">.</SPAN>product<SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">==</SPAN>n <SPAN class="punctuation token">]</SPAN>
combos<SPAN class="punctuation token">.</SPAN>extend<SPAN class="punctuation token">(</SPAN>vals<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#.append(vals)</SPAN>
perms <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN>perms<SPAN class="punctuation token">)</SPAN>
ppp <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>product<SPAN class="punctuation token">(</SPAN>perms<SPAN class="punctuation token">,</SPAN>axis<SPAN class="operator token">=</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
wh <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>where<SPAN class="punctuation token">(</SPAN>ppp <SPAN class="operator token">==</SPAN> n<SPAN class="punctuation token">)</SPAN>
perm_vals <SPAN class="operator token">=</SPAN> perms<SPAN class="punctuation token">[</SPAN>wh<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">return</SPAN> combos
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">demo</SPAN><SPAN class="punctuation token">(</SPAN>n<SPAN class="operator token">=</SPAN><SPAN class="number token">36</SPAN><SPAN class="punctuation token">,</SPAN> prn<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">""" demo perm_sums"""</SPAN>
p <SPAN class="operator token">=</SPAN> primes<SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\nPrimes:...{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>p<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
all_div <SPAN class="operator token">=</SPAN> divisor<SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Number:...{}\nAll:...{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">,</SPAN> all_div<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
ndim <SPAN class="operator token">=</SPAN> <SPAN class="number token">4</SPAN>
combos <SPAN class="operator token">=</SPAN> perm_sums<SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">,</SPAN> max_dim<SPAN class="operator token">=</SPAN>ndim<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> prn<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\nProducts for... {} - max_dim = {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">,</SPAN> ndim<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> combos<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> combos
<SPAN class="keyword token">if</SPAN> __name__<SPAN class="operator token">==</SPAN><SPAN class="string token">"__main__"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""primes demo"""</SPAN>
n <SPAN class="operator token">=</SPAN> <SPAN class="number token">36</SPAN>
combos <SPAN class="operator token">=</SPAN> demo<SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">,</SPAN> prn<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>For the example in my last post for a 6x6 array for 36 unique numbers, these list the possible ways that that data can be reconfigure.
Primes:...[ 2 3 5 7 11 13 17 19 23 29 31] Number:...36 All:...[2, 3, 4, 6, 9, 12, 18, 36] Products for... 36 - max_dim = 4 |
|---|
| 2D | 3D | 4D |
|---|
(36,) (2, 18) (3, 12) (4, 9) (6, 6) (9, 4) (12, 3) (18, 2) | (2, 2, 9) (2, 3, 6) (2, 6, 3) (2, 9, 2) (3, 2, 6) (3, 3, 4) (3, 4, 3) (3, 6, 2) (4, 3, 3) (6, 2, 3) (6, 3, 2) (9, 2, 2) | (2, 2, 3, 3) (2, 3, 2, 3) (2, 3, 3, 2) (3, 2, 2, 3) (3, 2, 3, 2) (3, 3, 2, 2) |
If you specify a minimum window size you can get possible combinations.
Minimum window...[3, 3]
[(3, 12), (4, 9), (6, 6), (9, 4), (12, 3), (2, 3, 6), (2, 6, 3), (3, 3, 4), (3, 4, 3), (4, 3, 3), (2, 2, 3, 3)]
That's all for now. The cases of being able to reshape a raster in 1, 2, 3 or 4 dimensions are given above.