4,577
edits
No edit summary |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
(One intermediate revision by the same user not shown) | |||
Line 3: | Line 3: | ||
Polivka observed that since the pupils were writing [[wikipedia:binary code|binary code]] with a handful of [[wikipedia:Hardware register|register]]s and only 1 Kb of memory, this was quite a stiff problem to encounter on your first course. | Polivka observed that since the pupils were writing [[wikipedia:binary code|binary code]] with a handful of [[wikipedia:Hardware register|register]]s and only 1 Kb of memory, this was quite a stiff problem to encounter on your first course. | ||
The webinar participants discussed this for quite a while at the end of which Curtis Jones said [[Adám Brudzewsky]]'s approach was a good example of APL as a [https://www.jsoftware.com/papers/tot.htm tool of thought]. Ellis Morgan commented that the problem is effectively a [[sum]] of a [[wikipedia:geometric series|geometric series]] here. The function < | The webinar participants discussed this for quite a while at the end of which Curtis Jones said [[Adám Brudzewsky]]'s approach was a good example of APL as a [https://www.jsoftware.com/papers/tot.htm tool of thought]. Ellis Morgan commented that the problem is effectively a [[sum]] of a [[wikipedia:geometric series|geometric series]] here. The function <syntaxhighlight lang=apl inline>Shirley</syntaxhighlight> is a [[traditional function]] which one might have [[APL\360|written in 1970]] (except for the [[Mix]] in the definition of <syntaxhighlight lang=apl inline>wt</syntaxhighlight>): | ||
< | <syntaxhighlight lang=apl> | ||
wt←xb Shirley w;a;b;c;k;m;n;r | wt←xb Shirley w;a;b;c;k;m;n;r | ||
⍝ weight of Shirley in ounces after "w" weeks ("w" may be a vector or whatever) | ⍝ weight of Shirley in ounces after "w" weeks ("w" may be a vector or whatever) | ||
Line 17: | Line 17: | ||
⍝ wt=sum of (birth, first 20 weeks growth, the next complete months, the last month part) | ⍝ wt=sum of (birth, first 20 weeks growth, the next complete months, the last month part) | ||
wt←+⌿a⍪↑(k×5⌊m)(k×b×(n×b=1)+(1-r)÷(b=1)+1-b)(b×r×(m>5)×k×m-c) | wt←+⌿a⍪↑(k×5⌊m)(k×b×(n×b=1)+(1-r)÷(b=1)+1-b)(b×r×(m>5)×k×m-c) | ||
</ | </syntaxhighlight> | ||
Starting with a birth weight of 100 ounces (six pounds four ounces), using a decay factor of 0.9 (meaning that growth in this month is 0.9 times the growth in last month) and using UK Imperial tons<ref>In UK Imperial units, an ounce is 16 drams, a pound is 16 ounces, a stone is 14 pounds, a hundredweight is 8 stone, and a ton is 20 hundredweights. A UK ton is the "long ton" of 2240 pounds (2240=20×8×14). A US "ton" is a "short ton" of 2000 pounds. The metric "ton" (or "tonne") has 2204.6226 pounds, it being 1000 kilograms.</ref> we get this answer: | Starting with a birth weight of 100 ounces (six pounds four ounces), using a decay factor of 0.9 (meaning that growth in this month is 0.9 times the growth in last month) and using UK Imperial tons<ref>In UK Imperial units, an ounce is 16 drams, a pound is 16 ounces, a stone is 14 pounds, a hundredweight is 8 stone, and a ton is 20 hundredweights. A UK ton is the "long ton" of 2240 pounds (2240=20×8×14). A US "ton" is a "short ton" of 2000 pounds. The metric "ton" (or "tonne") has 2204.6226 pounds, it being 1000 kilograms.</ref> we get this answer: | ||
[https://tio.run/##bVJBbqswEN1zilFXdhMqoKXSF@q/xD@BTYaAgg0yTmku0FZVKrWrrHqBXqDLLrgJF2nHQNMk@gjJBj@/92bmibr0FxtRVsuvr/7xobX9/cudhH95YUrcQJuIRCZpskpUohPj9c9v0GKxzC1U2R5VaKjWOsUGRGbRwFl7RihcNcDcVokNSAQBt5jaygC9bS4s3qLhHv2WpMnC624XRVcBD2nt71/JhHucoCyMzX9k91pzUJW2ebmBpalaAiwwJaFMOA2idaT99im4iGeOW/DuI7yG6XG0tgKNwmBjYWGE8kDRlbb7uAI4hI2FELhca2FG0caDlMBEr@AYnVaqLtHiHqcJF/Tbx9SPD3F6rSR1ipp4cmNqYRSMwh4YIpDn@lhmKnks1pkTdV0OgygF1XPM6cGKOC4j@C9HjVNRB2PMHGmOkBWG2H69DP2wN81aOedsGMz8BDXxzgcCjXenbprxZPA56tbCWArCkL1Zv/0U/fM7JYCtul3sWszdTnY7pmm5CfmMhb6hcbLhI/QlZ3RqCKD@xrzbEVr5KfcozxTqMQKMhUHgEjClDIKLP3wfYApGHH0D Try it online!] | [https://tio.run/##bVJBbqswEN1zilFXdhMqoKXSF@q/xD@BTYaAgg0yTmku0FZVKrWrrHqBXqDLLrgJF2nHQNMk@gjJBj@/92bmibr0FxtRVsuvr/7xobX9/cudhH95YUrcQJuIRCZpskpUohPj9c9v0GKxzC1U2R5VaKjWOsUGRGbRwFl7RihcNcDcVokNSAQBt5jaygC9bS4s3qLhHv2WpMnC624XRVcBD2nt71/JhHucoCyMzX9k91pzUJW2ebmBpalaAiwwJaFMOA2idaT99im4iGeOW/DuI7yG6XG0tgKNwmBjYWGE8kDRlbb7uAI4hI2FELhca2FG0caDlMBEr@AYnVaqLtHiHqcJF/Tbx9SPD3F6rSR1ipp4cmNqYRSMwh4YIpDn@lhmKnks1pkTdV0OgygF1XPM6cGKOC4j@C9HjVNRB2PMHGmOkBWG2H69DP2wN81aOedsGMz8BDXxzgcCjXenbprxZPA56tbCWArCkL1Zv/0U/fM7JYCtul3sWszdTnY7pmm5CfmMhb6hcbLhI/QlZ3RqCKD@xrzbEVr5KfcozxTqMQKMhUHgEjClDIKLP3wfYApGHH0D Try it online!] | ||
< | <syntaxhighlight lang=apl> | ||
⌊0.5+((100÷16×2240) 0.9) Shirley 16 52 ⍝ calculate Shirley's weight in ounces | ⌊0.5+((100÷16×2240) 0.9) Shirley 16 52 ⍝ calculate Shirley's weight in ounces | ||
228 424 | 228 424 | ||
</ | </syntaxhighlight> | ||
For the < | For the <syntaxhighlight lang=apl inline>n</syntaxhighlight> complete months after month 5 the total weight increase is <syntaxhighlight lang=apl inline>k×+/b*⍳n</syntaxhighlight> which we know is <syntaxhighlight lang=apl inline>k×b×(1-b*n)÷1-b</syntaxhighlight> ([[wikipedia:Geometric_series#Sum|sum of a geometric series]]). | ||
Even if Shirley lives to be 100, applying < | Even if Shirley lives to be 100, applying <syntaxhighlight lang=apl inline>+/</syntaxhighlight> over all the weeks is unlikely to cause any problematic performance. | ||
[https://tio.run/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hxqIRBu/N3P4Pd@@/0QFk0x8S32LqT6I/EI3bA6MzwkKD4yTDhYWCEnw1CFCPCe0PiFZhRgP08/HIogjYAnD0bOnefp05ghB886aYjBfd3t/RT0tHPiKJ/wI Try it online!] | ||
< | <syntaxhighlight lang=apl> | ||
100 14 16 16⊤⌊0.5+16×((100÷16×2240)0.93)Shirley 0 16 52 | 100 14 16 16⊤⌊0.5+16×((100÷16×2240)0.93)Shirley 0 16 52 | ||
0 1 1 | 0 1 1 | ||
Line 38: | Line 38: | ||
4 4 15 | 4 4 15 | ||
0 0 4 | 0 0 4 | ||
</ | </syntaxhighlight> | ||
So we see, with these assumptions, Shirley grows from 6 pounds and 4 ounces at birth to 1 stone and 4 ounces at 16 weeks and to 1 stone, 13 pounds, 15 ounces and 4 drams when a year old. | So we see, with these assumptions, Shirley grows from 6 pounds and 4 ounces at birth to 1 stone and 4 ounces at 16 weeks and to 1 stone, 13 pounds, 15 ounces and 4 drams when a year old. | ||
=== Notes === | === Notes === | ||
<references/> | <references/> | ||
[[Category:Examples]][[Category:Dyalog APL examples]] | [[Category:Examples]][[Category:Dyalog APL examples]] |