replicate

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !

replicate

Postby Roger|Dyalog on Mon May 03, 2021 9:05 pm

In the continuing series on computing a "squiggle" function without using the squiggle, the game here is to compute ⍺⌿⍵ without using ⌿. A solution should satisfy the following:

      x  ← 0 0 2 ¯3 4 1 ¯1 0
y ← 'abcdefgh'
y1 ← '0x0'⍪111 1 1⍪(⊂2 4⍴0)⍪33 3 3⍪(⊂4 5⍴0)⍪5⍪'six'⍪1 3⍴7

(x⌿y ) ≡ x f y
(x⌿y1) ≡ x f y1

To focus on essentials, we ignore 0-length left arguments and unit extension cases (3⌿4 5 or 2 3⌿4).

Dfn

      
rd←{
y←(⊂⊃0⍴⍵)@(⍸0>⍺)⊢⍵
⍺ {⍬≡⍺:⍵ ⋄ (((|⊃⍺),1↓⍴⍵)⍴1↑⍵) ⍪ (1↓⍺)∇(1↓⍵)} y
}

Reshape

      
rr←{
y←(⊂⊃0⍴⍵)@(⍸0>⍺)⊢⍵
x←(|⍺),⍤0 1⊢1↓⍴⍵
⊃ ⍪⌿ x (⊂⍴)⍤¯1 ⊢y
}

The speed of this solution depends crucially on the speed of ⍪⌿, which appears to be linear:

      
⎕cr 'time'
z←time n;p;q
p←¯5+?n⍴20
q←?(n,2)⍴100000
z←⍎cmpx'p rr q'

⊢ t ← time¨ n←10*1+⍳6
0.00001 0.0000339 0.000261 0.00252 0.026 0.26

rsq
rsq←{ ⍝ r-squared statistic of ⍺ (y) fitted by ⍵ (x)
b←⍺⌹1,⍪⍵ ⍝ regression coefficients
yhat←(1,⍪⍵)+.×b ⍝ predicted values
ybar←(+⌿÷≢)⍺ ⍝ mean
rss←+⌿×⍨⍺-yhat ⍝ residual sum of squares
tss←+⌿×⍨⍺-ybar ⍝ total sum of squares
1-rss÷tss
}
t rsq n
1
¯9 ⍕ t rsq n
9.99999895E¯1

Indexing

      
ri←{
y←(⊂⊃0⍴⍵)@(⍸0>⍺)⊢⍵
x←(-0=⊃⍺)++⍀(⍳+⌿|⍺)∊+⍀|⍺
(⊂x)⌷(⊂⍸0≠⍺)⌷y
}

The algorithm appear in Some Uses of { and }, 1987, §3.1, updated to allow negative items in ⍺ and the availability of ⍸⍵.

In all three solutions here, a new right argument y is constructed with cells of ⍵ replaced by the fill element of ⍵ when the corresponding cell of ⍺ is negative. For y so contructed, ⍺ f ⍵ ←→ (|⍺) f y.
Roger|Dyalog
 
Posts: 236
Joined: Thu Jul 28, 2011 10:53 am

Return to APL Chat

Who is online

Users browsing this forum: No registered users and 1 guest